Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Add events for container release
Browse files Browse the repository at this point in the history
For ReleaseEventMetadata use spec with both types: ReleaseImageSpec and ReleaseContainersSpec

Need this to fix weaveworks/service#2277
  • Loading branch information
lelenanam committed Oct 25, 2018
1 parent ee8b90c commit bcfb292
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 34 deletions.
6 changes: 3 additions & 3 deletions cmd/fluxctl/release_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (opts *controllerReleaseOpts) RunE(cmd *cobra.Command, args []string) error
}

ctx := context.Background()
spec := update.ReleaseSpec{
spec := update.ReleaseImageSpec{
ServiceSpecs: controllers,
ImageSpec: image,
Kind: kind,
Expand Down Expand Up @@ -182,10 +182,10 @@ func (opts *controllerReleaseOpts) RunE(cmd *cobra.Command, args []string) error
return await(ctx, cmd.OutOrStdout(), cmd.OutOrStderr(), opts.API, jobID, !opts.dryRun, opts.verbosity)
}

func promptSpec(out io.Writer, result job.Result, verbosity int) (update.ContainerSpecs, error) {
func promptSpec(out io.Writer, result job.Result, verbosity int) (update.ReleaseContainersSpec, error) {
menu := update.NewMenu(out, result.Result, verbosity)
containerSpecs, err := menu.Run()
return update.ContainerSpecs{
return update.ReleaseContainersSpec{
Kind: update.ReleaseKindExecute,
ContainerSpecs: containerSpecs,
SkipMismatches: false,
Expand Down
29 changes: 27 additions & 2 deletions daemon/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,30 @@ func (d *Daemon) doSync(logger log.Logger) (retErr error) {

// Interpret some notes as events to send to the upstream
switch n.Spec.Type {
case update.Containers:
spec := n.Spec.Spec.(update.ReleaseContainersSpec)
noteEvents = append(noteEvents, event.Event{
ServiceIDs: n.Result.AffectedResources(),
Type: event.EventRelease,
StartedAt: started,
EndedAt: time.Now().UTC(),
LogLevel: event.LogLevelInfo,
Metadata: &event.ReleaseEventMetadata{
ReleaseEventCommon: event.ReleaseEventCommon{
Revision: commits[i].Revision,
Result: n.Result,
Error: n.Result.Error(),
},
Spec: event.ReleaseSpec{
Type: event.ReleaseContainerSpecType,
ReleaseContainerSpec: &spec,
},
Cause: n.Spec.Cause,
},
})
includes[event.EventRelease] = true
case update.Images:
spec := n.Spec.Spec.(update.ReleaseSpec)
spec := n.Spec.Spec.(update.ReleaseImageSpec)
noteEvents = append(noteEvents, event.Event{
ServiceIDs: n.Result.AffectedResources(),
Type: event.EventRelease,
Expand All @@ -320,7 +342,10 @@ func (d *Daemon) doSync(logger log.Logger) (retErr error) {
Result: n.Result,
Error: n.Result.Error(),
},
Spec: spec,
Spec: event.ReleaseSpec{
Type: event.ReleaseImageSpecType,
ReleaseImageSpec: &spec,
},
Cause: n.Spec.Cause,
},
})
Expand Down
30 changes: 24 additions & 6 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ func (e Event) String() string {
if len(strImageIDs) == 0 {
strImageIDs = []string{"no image changes"}
}
for _, spec := range metadata.Spec.ServiceSpecs {
if spec == update.ResourceSpecAll {
strServiceIDs = []string{"all services"}
break
if metadata.Spec.Type == ReleaseImageSpecType {
for _, spec := range metadata.Spec.ReleaseImageSpec.ServiceSpecs {
if spec == update.ResourceSpecAll {
strServiceIDs = []string{"all services"}
break
}
}
}
if len(strServiceIDs) == 0 {
Expand Down Expand Up @@ -239,11 +241,27 @@ type ReleaseEventCommon struct {
Error string `json:"error,omitempty"`
}

const (
// ReleaseImageSpecType is a type of release spec when there are update.Images
ReleaseImageSpecType = "releaseImageSpecType"
// ReleaseContainerSpecType is a type of release spec when there are update.Containers
ReleaseContainerSpecType = "releaseContainerSpecType"
)

// ReleaseSpec is a spec for images and containers release
type ReleaseSpec struct {
// Type is ReleaseImagesSpecType or ReleaseContainersSpecType
// if empty (for previous version), then use ReleaseImagesSpecType
Type string
ReleaseImageSpec *update.ReleaseImageSpec
ReleaseContainerSpec *update.ReleaseContainersSpec
}

// ReleaseEventMetadata is the metadata for when service(s) are released
type ReleaseEventMetadata struct {
ReleaseEventCommon
Spec update.ReleaseSpec `json:"spec"`
Cause update.Cause `json:"cause"`
Spec ReleaseSpec `json:"spec"`
Cause update.Cause `json:"cause"`
}

// AutoReleaseEventMetadata is for when service(s) are released
Expand Down
2 changes: 1 addition & 1 deletion http/daemon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (s HTTPServer) UpdateImages(w http.ResponseWriter, r *http.Request) {
excludes = append(excludes, s)
}

spec := update.ReleaseSpec{
spec := update.ReleaseImageSpec{
ServiceSpecs: serviceSpecs,
ImageSpec: imageSpec,
Kind: releaseKind,
Expand Down
2 changes: 1 addition & 1 deletion remote/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func ServerTestBattery(t *testing.T, wrap func(mock api.UpstreamServer) api.Upst

updateSpec := update.Spec{
Type: update.Images,
Spec: update.ReleaseSpec{
Spec: update.ReleaseImageSpec{
ServiceSpecs: []update.ResourceSpec{
update.ResourceSpecAll,
},
Expand Down
2 changes: 1 addition & 1 deletion remote/rpc/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func requireSpecKinds(s update.Spec, kinds []string) error {
return fmt.Errorf("Unsupported resource kind: %s", kind)
}
}
case update.ReleaseSpec:
case update.ReleaseImageSpec:
for _, ss := range s.ServiceSpecs {
if err := requireServiceSpecKinds(ss, kinds); err != nil {
return err
Expand Down
20 changes: 11 additions & 9 deletions update/containers.go → update/release_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

var zeroImageRef = image.Ref{}

// ContainerSpecs defines the spec for a `containers` manifest update.
type ContainerSpecs struct {
// ReleaseContainersSpec defines the spec for a `containers` manifest update.
type ReleaseContainersSpec struct {
Kind ReleaseKind
ContainerSpecs map[flux.ResourceID][]ContainerUpdate
SkipMismatches bool
Expand All @@ -25,7 +25,7 @@ type ContainerSpecs struct {

// CalculateRelease computes required controller updates to satisfy this specification.
// It returns an error if any spec calculation fails unless `SkipMismatches` is true.
func (s ContainerSpecs) CalculateRelease(rc ReleaseContext, logger log.Logger) ([]*ControllerUpdate, Result, error) {
func (s ReleaseContainersSpec) CalculateRelease(rc ReleaseContext, logger log.Logger) ([]*ControllerUpdate, Result, error) {
results := Result{}
prefilter, postfilter := s.filters()
all, err := rc.SelectServices(results, prefilter, postfilter)
Expand All @@ -36,7 +36,7 @@ func (s ContainerSpecs) CalculateRelease(rc ReleaseContext, logger log.Logger) (
return updates, results, s.resultsError(results)
}

func (s ContainerSpecs) resultsError(results Result) error {
func (s ReleaseContainersSpec) resultsError(results Result) error {
failures := 0
successes := 0
for _, res := range results {
Expand All @@ -56,11 +56,13 @@ func (s ContainerSpecs) resultsError(results Result) error {
return nil
}

func (s ContainerSpecs) filters() ([]ControllerFilter, []ControllerFilter) {
func (s ReleaseContainersSpec) filters() ([]ControllerFilter, []ControllerFilter) {
var rids []flux.ResourceID
for rid := range s.ContainerSpecs {
rids = append(rids, rid)
}
fmt.Printf(" ReleaseContainersSpec: %#v\n", s)
fmt.Printf(" rids: %#v\n", rids)
pre := []ControllerFilter{&IncludeFilter{IDs: rids}}

if !s.Force {
Expand All @@ -69,7 +71,7 @@ func (s ContainerSpecs) filters() ([]ControllerFilter, []ControllerFilter) {
return pre, []ControllerFilter{}
}

func (s ContainerSpecs) controllerUpdates(results Result, all []*ControllerUpdate) []*ControllerUpdate {
func (s ReleaseContainersSpec) controllerUpdates(results Result, all []*ControllerUpdate) []*ControllerUpdate {
var updates []*ControllerUpdate
for _, u := range all {
cs, err := u.Controller.ContainersOrError()
Expand Down Expand Up @@ -157,15 +159,15 @@ func (s ContainerSpecs) controllerUpdates(results Result, all []*ControllerUpdat
return updates
}

func (s ContainerSpecs) ReleaseKind() ReleaseKind {
func (s ReleaseContainersSpec) ReleaseKind() ReleaseKind {
return s.Kind
}

func (s ContainerSpecs) ReleaseType() ReleaseType {
func (s ReleaseContainersSpec) ReleaseType() ReleaseType {
return "containers"
}

func (s ContainerSpecs) CommitMessage(result Result) string {
func (s ReleaseContainersSpec) CommitMessage(result Result) string {
buf := &bytes.Buffer{}
fmt.Fprintln(buf, "Release containers")
for _, res := range result.AffectedResources() {
Expand Down
18 changes: 9 additions & 9 deletions update/release.go → update/release_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ReleaseContext interface {

// NB: these get sent from fluxctl, so we have to maintain the json format of
// this. Eugh.
type ReleaseSpec struct {
type ReleaseImageSpec struct {
ServiceSpecs []ResourceSpec
ImageSpec ImageSpec
Kind ReleaseKind
Expand All @@ -61,7 +61,7 @@ type ReleaseSpec struct {

// ReleaseType gives a one-word description of the release, mainly
// useful for labelling metrics or log messages.
func (s ReleaseSpec) ReleaseType() ReleaseType {
func (s ReleaseImageSpec) ReleaseType() ReleaseType {
switch {
case s.ImageSpec == ImageSpecLatest:
return "latest_images"
Expand All @@ -70,7 +70,7 @@ func (s ReleaseSpec) ReleaseType() ReleaseType {
}
}

func (s ReleaseSpec) CalculateRelease(rc ReleaseContext, logger log.Logger) ([]*ControllerUpdate, Result, error) {
func (s ReleaseImageSpec) CalculateRelease(rc ReleaseContext, logger log.Logger) ([]*ControllerUpdate, Result, error) {
results := Result{}
timer := NewStageTimer("select_services")
updates, err := s.selectServices(rc, results)
Expand All @@ -89,11 +89,11 @@ func (s ReleaseSpec) CalculateRelease(rc ReleaseContext, logger log.Logger) ([]*
return updates, results, nil
}

func (s ReleaseSpec) ReleaseKind() ReleaseKind {
func (s ReleaseImageSpec) ReleaseKind() ReleaseKind {
return s.Kind
}

func (s ReleaseSpec) CommitMessage(result Result) string {
func (s ReleaseImageSpec) CommitMessage(result Result) string {
image := strings.Trim(s.ImageSpec.String(), "<>")
var services []string
for _, spec := range s.ServiceSpecs {
Expand All @@ -105,7 +105,7 @@ func (s ReleaseSpec) CommitMessage(result Result) string {
// Take the spec given in the job, and figure out which services are
// in question based on the running services and those defined in the
// repo. Fill in the release results along the way.
func (s ReleaseSpec) selectServices(rc ReleaseContext, results Result) ([]*ControllerUpdate, error) {
func (s ReleaseImageSpec) selectServices(rc ReleaseContext, results Result) ([]*ControllerUpdate, error) {
// Build list of filters
prefilters, postfilters, err := s.filters(rc)
if err != nil {
Expand All @@ -115,7 +115,7 @@ func (s ReleaseSpec) selectServices(rc ReleaseContext, results Result) ([]*Contr
return rc.SelectServices(results, prefilters, postfilters)
}

func (s ReleaseSpec) filters(rc ReleaseContext) ([]ControllerFilter, []ControllerFilter, error) {
func (s ReleaseImageSpec) filters(rc ReleaseContext) ([]ControllerFilter, []ControllerFilter, error) {
var prefilters, postfilters []ControllerFilter

ids := []flux.ResourceID{}
Expand Down Expand Up @@ -157,7 +157,7 @@ func (s ReleaseSpec) filters(rc ReleaseContext) ([]ControllerFilter, []Controlle
return prefilters, postfilters, nil
}

func (s ReleaseSpec) markSkipped(results Result) {
func (s ReleaseImageSpec) markSkipped(results Result) {
for _, v := range s.ServiceSpecs {
if v == ResourceSpecAll {
continue
Expand All @@ -181,7 +181,7 @@ func (s ReleaseSpec) markSkipped(results Result) {
// however we do want to see if we *can* do the replacements, because
// if not, it indicates there's likely some problem with the running
// system vs the definitions given in the repo.)
func (s ReleaseSpec) calculateImageUpdates(rc ReleaseContext, candidates []*ControllerUpdate, results Result, logger log.Logger) ([]*ControllerUpdate, error) {
func (s ReleaseImageSpec) calculateImageUpdates(rc ReleaseContext, candidates []*ControllerUpdate, results Result, logger log.Logger) ([]*ControllerUpdate, error) {
// Compile an `ImageRepos` of all relevant images
var imageRepos ImageRepos
var singleRepo image.CanonicalName
Expand Down
4 changes: 2 additions & 2 deletions update/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (spec *Spec) UnmarshalJSON(in []byte) error {
}
spec.Spec = update
case Images:
var update ReleaseSpec
var update ReleaseImageSpec
if err := json.Unmarshal(wire.SpecBytes, &update); err != nil {
return err
}
Expand All @@ -67,7 +67,7 @@ func (spec *Spec) UnmarshalJSON(in []byte) error {
}
spec.Spec = update
case Containers:
var update ContainerSpecs
var update ReleaseContainersSpec
if err := json.Unmarshal(wire.SpecBytes, &update); err != nil {
return err
}
Expand Down

0 comments on commit bcfb292

Please sign in to comment.