This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
containers
release specs to update-manifests
This adds a new release spec format providing update requests on container level. Sample POST request body: ``` { "type": "containers", "cause": { "Message": "sample request body", "User": "alice" }, "spec": { "ContainerSpecs": { "default:deployment/nginx": [ { "Container": "nginx", "Current": "nginx:1.14.0", "Target": "nginx:1.15.0" } ], "default:deployment/helloworld": [ { "Container": "helloworld", "Current": "quay.io/weaveworks/helloworld:master-07a1b6b", "Target": "quay.io/weaveworks/helloworld:master-a000004" }, { "Container": "sidecar", "Current": "quay.io/weaveworks/sidecar:master-a000001", "Target": "quay.io/weaveworks/sidecar:master-a000002" } ] }, "Kind": "plan", "IgnoreFailedControllers": false } } ``` The request will fail if any of the `Current` container image requirements are not met. To have partial updates go through and ignore the failed requirements, one can pass `true` for `IgnoreFailedControllers`.
- Loading branch information
Showing
5 changed files
with
303 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package update | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/go-kit/kit/log" | ||
|
||
"github.com/weaveworks/flux" | ||
"github.com/weaveworks/flux/resource" | ||
) | ||
|
||
// ContainerSpecs defines the spec for a `containers` manifest update. | ||
type ContainerSpecs struct { | ||
Kind ReleaseKind | ||
ContainerSpecs map[flux.ResourceID][]ContainerUpdate | ||
IgnoreFailedControllers bool | ||
} | ||
|
||
var errCannotSatisfySpecs = errors.New("cannot satisfy specs") | ||
|
||
// CalculateRelease computes required controller updates to satisfy this specification. | ||
// It returns an error if any spec calculation fails unless `IgnoreFailedControllers` is true. | ||
func (s ContainerSpecs) CalculateRelease(rc ReleaseContext, logger log.Logger) ([]*ControllerUpdate, Result, error) { | ||
results := Result{} | ||
|
||
// Collect data from services in spec | ||
var rids []flux.ResourceID | ||
for rid := range s.ContainerSpecs { | ||
rids = append(rids, rid) | ||
} | ||
all, err := rc.SelectServices(results, []ControllerFilter{&IncludeFilter{IDs: rids}}, nil) | ||
if err != nil { | ||
return nil, results, err | ||
} | ||
|
||
// Look at all controllers of services | ||
var updates []*ControllerUpdate | ||
for _, u := range all { | ||
cs, err := u.Controller.ContainersOrError() | ||
if err != nil { | ||
results[u.ResourceID] = ControllerResult{ | ||
Status: ReleaseStatusFailed, | ||
Error: err.Error(), | ||
} | ||
continue | ||
} | ||
|
||
containers := map[string]resource.Container{} | ||
for _, spec := range cs { | ||
containers[spec.Name] = spec | ||
} | ||
|
||
// Go through specs and collect updates | ||
var containerUpdates []ContainerUpdate | ||
for _, spec := range s.ContainerSpecs[u.ResourceID] { | ||
container, ok := containers[spec.Container] | ||
if !ok { | ||
results[u.ResourceID] = ControllerResult{ | ||
Status: ReleaseStatusFailed, | ||
Error: fmt.Sprintf(ContainerNotFound, spec.Container), | ||
} | ||
break // go to next controller | ||
} | ||
|
||
if container.Image != spec.Current { | ||
results[u.ResourceID] = ControllerResult{ | ||
Status: ReleaseStatusFailed, | ||
Error: fmt.Sprintf(ContainerTagMismatch, spec.Container), | ||
} | ||
break // go to next controller | ||
} | ||
containerUpdates = append(containerUpdates, spec) | ||
} | ||
|
||
if res := results[u.ResourceID]; res.Status == "" { | ||
u.Updates = containerUpdates | ||
updates = append(updates, u) | ||
results[u.ResourceID] = ControllerResult{ | ||
Status: ReleaseStatusSuccess, | ||
PerContainer: u.Updates, | ||
} | ||
} | ||
} | ||
|
||
if !s.IgnoreFailedControllers { | ||
for _, res := range results { | ||
if res.Status == ReleaseStatusFailed { | ||
return updates, results, errCannotSatisfySpecs | ||
} | ||
} | ||
} | ||
|
||
return updates, results, nil | ||
} | ||
|
||
func (s ContainerSpecs) ReleaseKind() ReleaseKind { | ||
return s.Kind | ||
} | ||
|
||
func (s ContainerSpecs) ReleaseType() ReleaseType { | ||
return "containers" | ||
} | ||
|
||
func (s ContainerSpecs) CommitMessage(result Result) string { | ||
buf := &bytes.Buffer{} | ||
fmt.Fprintln(buf, "Release containers") | ||
for _, res := range result.AffectedResources() { | ||
fmt.Fprintf(buf, "\n%s", res) | ||
for _, upd := range result[res].PerContainer { | ||
fmt.Fprintf(buf, "\n- %s", upd.Target) | ||
} | ||
fmt.Fprintln(buf) | ||
} | ||
if err := result.Error(); err != "" { | ||
fmt.Fprintf(buf, "\n%s", result.Error()) | ||
} | ||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.