Browse Source

Omit changes that aren't, in auto-releases

Part of the problem being addressed here is that releases decide what
to do based on the images used in the cluster, and that does not
always correspond to changes in the repo. This commit removes the
possibility for automated releases, by

 - skipping any containers that _already_ use the supposedly new image

 - building the commit message by looking only at the images that were
   successfully changed.
Michael Bridgen 1 year ago
parent
commit
bd748c70d1
6 changed files with 39 additions and 34 deletions
  1. 1
    1
      daemon/daemon.go
  2. 2
    2
      event/event.go
  3. 1
    1
      release/releaser.go
  4. 30
    28
      update/automated.go
  5. 1
    1
      update/release.go
  6. 4
    1
      update/result.go

+ 1
- 1
daemon/daemon.go View File

@@ -348,7 +348,7 @@ func (d *Daemon) release(spec update.Spec, c release.Changes) daemonJobFunc {
348 348
 		if c.ReleaseKind() == update.ReleaseKindExecute {
349 349
 			commitMsg := spec.Cause.Message
350 350
 			if commitMsg == "" {
351
-				commitMsg = c.CommitMessage()
351
+				commitMsg = c.CommitMessage(result)
352 352
 			}
353 353
 			commitAuthor := ""
354 354
 			if d.GitConfig.SetAuthor {

+ 2
- 2
event/event.go View File

@@ -91,7 +91,7 @@ func (e Event) String() string {
91 91
 	switch e.Type {
92 92
 	case EventRelease:
93 93
 		metadata := e.Metadata.(*ReleaseEventMetadata)
94
-		strImageIDs := metadata.Result.ImageIDs()
94
+		strImageIDs := metadata.Result.ChangedImages()
95 95
 		if len(strImageIDs) == 0 {
96 96
 			strImageIDs = []string{"no image changes"}
97 97
 		}
@@ -121,7 +121,7 @@ func (e Event) String() string {
121 121
 		)
122 122
 	case EventAutoRelease:
123 123
 		metadata := e.Metadata.(*AutoReleaseEventMetadata)
124
-		strImageIDs := metadata.Result.ImageIDs()
124
+		strImageIDs := metadata.Result.ChangedImages()
125 125
 		if len(strImageIDs) == 0 {
126 126
 			strImageIDs = []string{"no image changes"}
127 127
 		}

+ 1
- 1
release/releaser.go View File

@@ -12,7 +12,7 @@ type Changes interface {
12 12
 	CalculateRelease(update.ReleaseContext, log.Logger) ([]*update.ControllerUpdate, update.Result, error)
13 13
 	ReleaseKind() update.ReleaseKind
14 14
 	ReleaseType() update.ReleaseType
15
-	CommitMessage() string
15
+	CommitMessage(update.Result) string
16 16
 }
17 17
 
18 18
 func Release(rc *ReleaseContext, changes Changes, logger log.Logger) (results update.Result, err error) {

+ 30
- 28
update/automated.go View File

@@ -1,8 +1,8 @@
1 1
 package update
2 2
 
3 3
 import (
4
+	"bytes"
4 5
 	"fmt"
5
-	"strings"
6 6
 
7 7
 	"github.com/go-kit/kit/log"
8 8
 	"github.com/weaveworks/flux"
@@ -52,24 +52,24 @@ func (a *Automated) ReleaseKind() ReleaseKind {
52 52
 	return ReleaseKindExecute
53 53
 }
54 54
 
55
-func (a *Automated) CommitMessage() string {
56
-	var images []string
57
-	for _, image := range a.Images() {
58
-		images = append(images, image.String())
55
+func (a *Automated) CommitMessage(result Result) string {
56
+	images := result.ChangedImages()
57
+	buf := &bytes.Buffer{}
58
+	prefix := ""
59
+	switch len(images) {
60
+	case 0: // FIXME(michael): can we get here?
61
+		fmt.Fprintln(buf, "Auto-release (no images)")
62
+	case 1:
63
+		fmt.Fprint(buf, "Auto-release ")
64
+	default:
65
+		fmt.Fprintln(buf, "Auto-release multiple images")
66
+		fmt.Fprintln(buf)
67
+		prefix = " - "
59 68
 	}
60
-	return fmt.Sprintf("Release %s to automated", strings.Join(images, ", "))
61
-}
62
-
63
-func (a *Automated) Images() []image.Ref {
64
-	imageMap := map[image.Ref]struct{}{}
65
-	for _, change := range a.Changes {
66
-		imageMap[change.ImageID] = struct{}{}
69
+	for _, im := range images {
70
+		fmt.Fprintf(buf, "%s%s\n", prefix, im)
67 71
 	}
68
-	var images []image.Ref
69
-	for image, _ := range imageMap {
70
-		images = append(images, image)
71
-	}
72
-	return images
72
+	return buf.String()
73 73
 }
74 74
 
75 75
 func (a *Automated) markSkipped(results Result) {
@@ -88,15 +88,7 @@ func (a *Automated) calculateImageUpdates(rc ReleaseContext, candidates []*Contr
88 88
 
89 89
 	serviceMap := a.serviceMap()
90 90
 	for _, u := range candidates {
91
-		containers, err := u.Controller.ContainersOrError()
92
-		if err != nil {
93
-			result[u.ResourceID] = ControllerResult{
94
-				Status: ReleaseStatusFailed,
95
-				Error:  err.Error(),
96
-			}
97
-			continue
98
-		}
99
-
91
+		containers := u.Resource.Containers()
100 92
 		changes := serviceMap[u.ResourceID]
101 93
 		containerUpdates := []ContainerUpdate{}
102 94
 		for _, container := range containers {
@@ -106,7 +98,16 @@ func (a *Automated) calculateImageUpdates(rc ReleaseContext, candidates []*Contr
106 98
 					continue
107 99
 				}
108 100
 
101
+				// It turns out this isn't a change after all; skip this container
102
+				if change.ImageID.CanonicalRef() == container.Image.CanonicalRef() {
103
+					continue
104
+				}
105
+
106
+				// We transplant the tag here, to make sure we keep
107
+				// the format of the image name as it is in the
108
+				// resource (e.g., to avoid canonicalising it)
109 109
 				newImageID := currentImageID.WithNewTag(change.ImageID.Tag)
110
+				var err error
110 111
 				u.ManifestBytes, err = rc.Manifests().UpdateDefinition(u.ManifestBytes, container.Name, newImageID)
111 112
 				if err != nil {
112 113
 					return nil, err
@@ -129,8 +130,8 @@ func (a *Automated) calculateImageUpdates(rc ReleaseContext, candidates []*Contr
129 130
 			}
130 131
 		} else {
131 132
 			result[u.ResourceID] = ControllerResult{
132
-				Status: ReleaseStatusIgnored,
133
-				Error:  DoesNotUseImage,
133
+				Status: ReleaseStatusSkipped,
134
+				Error:  ImageUpToDate,
134 135
 			}
135 136
 		}
136 137
 	}
@@ -138,6 +139,7 @@ func (a *Automated) calculateImageUpdates(rc ReleaseContext, candidates []*Contr
138 139
 	return updates, nil
139 140
 }
140 141
 
142
+// serviceMap transposes the changes so they can be looked up by ID
141 143
 func (a *Automated) serviceMap() map[flux.ResourceID][]Change {
142 144
 	set := map[flux.ResourceID][]Change{}
143 145
 	for _, change := range a.Changes {

+ 1
- 1
update/release.go View File

@@ -96,7 +96,7 @@ func (s ReleaseSpec) ReleaseKind() ReleaseKind {
96 96
 	return s.Kind
97 97
 }
98 98
 
99
-func (s ReleaseSpec) CommitMessage() string {
99
+func (s ReleaseSpec) CommitMessage(result Result) string {
100 100
 	image := strings.Trim(s.ImageSpec.String(), "<>")
101 101
 	var services []string
102 102
 	for _, spec := range s.ServiceSpecs {

+ 4
- 1
update/result.go View File

@@ -30,9 +30,12 @@ func (r Result) ServiceIDs() []string {
30 30
 	return result
31 31
 }
32 32
 
33
-func (r Result) ImageIDs() []string {
33
+func (r Result) ChangedImages() []string {
34 34
 	images := map[image.Ref]struct{}{}
35 35
 	for _, serviceResult := range r {
36
+		if serviceResult.Status != ReleaseStatusSuccess {
37
+			continue
38
+		}
36 39
 		for _, containerResult := range serviceResult.PerContainer {
37 40
 			images[containerResult.Target] = struct{}{}
38 41
 		}

Loading…
Cancel
Save