Skip to content

Commit 4d22f43

Browse files
committed
update references to images in the autoupdate code
1 parent 14f3341 commit 4d22f43

File tree

9 files changed

+156
-155
lines changed

9 files changed

+156
-155
lines changed

tools/internal/autoupdate/config.go

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ type AutoUpdateOptions struct {
3838
GithubClient *github.Client
3939
}
4040

41-
// AutoupdateImageRef is used to map a given update image to an entry in config.yaml.
42-
// There may be multiple entries that have the same SourceImage, but different
43-
// TargetImageNames, so we need to choose which one receives the update image.
44-
type AutoupdateImageRef struct {
45-
SourceImage string
46-
TargetImageName string `json:",omitempty"`
41+
// AutoupdateArtifactRef is used to map a given update artifact to an entry in config.yaml.
42+
// There may be multiple entries that have the same SourceArtifact, but different
43+
// TargetArtifactName, so we need to choose which one receives the update artifact.
44+
type AutoupdateArtifactRef struct {
45+
SourceArtifact string
46+
TargetArtifactName string `json:",omitempty"`
4747
}
4848

4949
func Parse(filePath string) ([]ConfigEntry, error) {
@@ -135,53 +135,53 @@ func (entry ConfigEntry) Validate() error {
135135
return nil
136136
}
137137

138-
// GetUpdateImages returns a slice of Artifacts that depends on the
138+
// GetUpdateArtifacts returns a slice of Artifacts that depends on the
139139
// configured update strategy. The returned Artifacts may be from
140140
// any source, and they may be gathered in any way. The intention
141141
// is that they are new Artifacts (or new tags of existing Artifacts) that
142142
// we want to mirror.
143-
func (entry ConfigEntry) GetUpdateImages() ([]*config.Artifact, error) {
143+
func (entry ConfigEntry) GetUpdateArtifacts() ([]*config.Artifact, error) {
144144
switch {
145145
case entry.GithubRelease != nil:
146-
return entry.GithubRelease.GetUpdateImages()
146+
return entry.GithubRelease.GetUpdateArtifacts()
147147
case entry.HelmLatest != nil:
148-
return entry.HelmLatest.GetUpdateImages()
148+
return entry.HelmLatest.GetUpdateArtifacts()
149149
case entry.Registry != nil:
150-
return entry.Registry.GetUpdateImages()
150+
return entry.Registry.GetUpdateArtifacts()
151151
default:
152152
return nil, errors.New("did not find update strategy")
153153
}
154154
}
155155

156156
func (entry ConfigEntry) Run(ctx context.Context, opts AutoUpdateOptions) error {
157-
newImages, err := entry.GetUpdateImages()
157+
newArtifacts, err := entry.GetUpdateArtifacts()
158158
if err != nil {
159-
return fmt.Errorf("failed to get latest images for %s: %w", entry.Name, err)
159+
return fmt.Errorf("failed to get latest artifacts for %s: %w", entry.Name, err)
160160
}
161161

162162
accumulator := config.NewArtifactAccumulator()
163163
accumulator.AddArtifacts(opts.ConfigYaml.Artifacts...)
164164

165-
imagesToUpdate := make([]*config.Artifact, 0, len(newImages))
166-
for _, latestImage := range newImages {
167-
imageToUpdate, err := accumulator.TagDifference(latestImage)
165+
artifactsToUpdate := make([]*config.Artifact, 0, len(newArtifacts))
166+
for _, latestArtifact := range newArtifacts {
167+
artifactToUpdate, err := accumulator.TagDifference(latestArtifact)
168168
if err != nil {
169-
return fmt.Errorf("failed to get tag difference for image %s: %w", latestImage.SourceArtifact, err)
169+
return fmt.Errorf("failed to get tag difference for artifact %s: %w", latestArtifact.SourceArtifact, err)
170170
}
171-
if imageToUpdate != nil {
172-
imagesToUpdate = append(imagesToUpdate, imageToUpdate)
171+
if artifactToUpdate != nil {
172+
artifactsToUpdate = append(artifactsToUpdate, artifactToUpdate)
173173
}
174174
}
175-
if len(imagesToUpdate) == 0 {
175+
if len(artifactsToUpdate) == 0 {
176176
fmt.Printf("%s: no updates found\n", entry.Name)
177177
return nil
178178
}
179179

180-
imageSetHash, err := hashImageSet(imagesToUpdate)
180+
artifactSetHash, err := hashArtifactSet(artifactsToUpdate)
181181
if err != nil {
182-
return fmt.Errorf("failed to hash set of images that need updates: %w", err)
182+
return fmt.Errorf("failed to hash set of artifacts that need updates: %w", err)
183183
}
184-
branchName := fmt.Sprintf("autoupdate/%s/%s", entry.Name, imageSetHash)
184+
branchName := fmt.Sprintf("autoupdate/%s/%s", entry.Name, artifactSetHash)
185185

186186
// When filtering pull requests by head branch, the github API
187187
// requires that the head branch is in the format <owner>:<branch>.
@@ -213,62 +213,62 @@ func (entry ConfigEntry) Run(ctx context.Context, opts AutoUpdateOptions) error
213213

214214
if opts.DryRun {
215215
msg := fmt.Sprintf("%s: would make PR under branch %s that adds:\n", entry.Name, branchName)
216-
for _, imageToUpdate := range imagesToUpdate {
217-
for _, fullImage := range imageToUpdate.CombineSourceArtifactAndTags() {
218-
msg = msg + " - " + fullImage + "\n"
216+
for _, artifactToUpdate := range artifactsToUpdate {
217+
for _, fullArtifact := range artifactToUpdate.CombineSourceArtifactAndTags() {
218+
msg = msg + " - " + fullArtifact + "\n"
219219
}
220220
}
221221
fmt.Print(msg)
222222
return nil
223223
}
224224

225-
return entry.CreateImageUpdatePullRequest(ctx, opts, branchName, imagesToUpdate)
225+
return entry.CreateArtifactUpdatePullRequest(ctx, opts, branchName, artifactsToUpdate)
226226
}
227227

228-
func (entry ConfigEntry) CreateImageUpdatePullRequest(ctx context.Context, opts AutoUpdateOptions, branchName string, imagesToUpdate []*config.Artifact) error {
228+
func (entry ConfigEntry) CreateArtifactUpdatePullRequest(ctx context.Context, opts AutoUpdateOptions, branchName string, artifactsToUpdate []*config.Artifact) error {
229229
accumulator := config.NewArtifactAccumulator()
230230
accumulator.AddArtifacts(opts.ConfigYaml.Artifacts...)
231231

232232
if err := git.CreateAndCheckoutBranch(opts.BaseBranch, branchName); err != nil {
233233
return fmt.Errorf("failed to create and checkout branch %s: %w", branchName, err)
234234
}
235-
for _, imageToUpdate := range imagesToUpdate {
235+
for _, artifactToUpdate := range artifactsToUpdate {
236236
// We can reuse the accumulator here because we are making a sequence
237-
// of commits, each of which makes an addition from imagesToUpdate.
237+
// of commits, each of which makes an addition from artifactsToUpdate.
238238
configYaml := opts.ConfigYaml
239-
accumulator.AddArtifacts(imageToUpdate)
239+
accumulator.AddArtifacts(artifactToUpdate)
240240
configYaml.Artifacts = accumulator.Artifacts()
241241
if err := config.Write(paths.ConfigYaml, configYaml); err != nil {
242242
return fmt.Errorf("failed to write %s: %w", paths.ConfigYaml, err)
243243
}
244244

245245
regsyncYaml, err := configYaml.ToRegsyncConfig()
246246
if err != nil {
247-
return fmt.Errorf("failed to generate regsync config for commit for image %s: %w", imageToUpdate.SourceArtifact, err)
247+
return fmt.Errorf("failed to generate regsync config for commit for artifact %s: %w", artifactToUpdate.SourceArtifact, err)
248248
}
249249
if err := regsync.WriteConfig(paths.RegsyncYaml, regsyncYaml); err != nil {
250-
return fmt.Errorf("failed to write regsync config for commit for image %s: %w", imageToUpdate.SourceArtifact, err)
250+
return fmt.Errorf("failed to write regsync config for commit for artifact %s: %w", artifactToUpdate.SourceArtifact, err)
251251
}
252252

253-
tagString := strings.Join(imageToUpdate.Tags, ", ")
254-
msg := fmt.Sprintf("Add tag(s) %s for image %s", tagString, imageToUpdate.SourceArtifact)
253+
tagString := strings.Join(artifactToUpdate.Tags, ", ")
254+
msg := fmt.Sprintf("Add tag(s) %s for artifact %s", tagString, artifactToUpdate.SourceArtifact)
255255
if err := git.Commit(msg); err != nil {
256-
return fmt.Errorf("failed to commit changes for image %s: %w", imageToUpdate.SourceArtifact, err)
256+
return fmt.Errorf("failed to commit changes for artifact %s: %w", artifactToUpdate.SourceArtifact, err)
257257
}
258258
}
259259
if err := git.PushBranch(branchName, "origin"); err != nil {
260260
return fmt.Errorf("failed to push branch %s: %w", branchName, err)
261261
}
262262

263263
tagCount := 0
264-
for _, imageToUpdate := range imagesToUpdate {
265-
tagCount = tagCount + len(imageToUpdate.Tags)
264+
for _, artifactToUpdate := range artifactsToUpdate {
265+
tagCount = tagCount + len(artifactToUpdate.Tags)
266266
}
267267
title := fmt.Sprintf("[autoupdate] Add %d tag(s) for `%s`", tagCount, entry.Name)
268-
body := "This PR was created by the autoupdate workflow.\n\nIt adds the following image tags:"
269-
for _, imageToUpdate := range imagesToUpdate {
270-
for _, fullImage := range imageToUpdate.CombineSourceArtifactAndTags() {
271-
body = body + "\n- `" + fullImage + "`"
268+
body := "This PR was created by the autoupdate workflow.\n\nIt adds the following artifact tags:"
269+
for _, artifactToUpdate := range artifactsToUpdate {
270+
for _, fullArtifact := range artifactToUpdate.CombineSourceArtifactAndTags() {
271+
body = body + "\n- `" + fullArtifact + "`"
272272
}
273273
}
274274
maintainerCanModify := true
@@ -292,21 +292,21 @@ func (entry ConfigEntry) CreateImageUpdatePullRequest(ctx context.Context, opts
292292
return nil
293293
}
294294

295-
// hashImageSet computes a human-readable hash from a passed
295+
// hashArtifactSet computes a human-readable hash from a passed
296296
// set of Artifacts. Immune to different order of Artifacts, and
297-
// immune to the order of of the tags in those Artifacts.
298-
func hashImageSet(images []*config.Artifact) (string, error) {
299-
for _, image := range images {
300-
image.Sort()
297+
// immune to the order of the tags in those Artifacts.
298+
func hashArtifactSet(artifacts []*config.Artifact) (string, error) {
299+
for _, artifact := range artifacts {
300+
artifact.Sort()
301301
}
302-
slices.SortStableFunc(images, config.CompareArtifacts)
302+
slices.SortStableFunc(artifacts, config.CompareArtifacts)
303303

304304
hasher := sha256.New()
305-
for _, image := range images {
306-
for _, fullImage := range image.CombineSourceArtifactAndTags() {
307-
_, err := io.WriteString(hasher, fullImage)
305+
for _, artifact := range artifacts {
306+
for _, fullArtifact := range artifact.CombineSourceArtifactAndTags() {
307+
_, err := io.WriteString(hasher, fullArtifact)
308308
if err != nil {
309-
return "", fmt.Errorf("failed to write full image %q: %w", fullImage, err)
309+
return "", fmt.Errorf("failed to write full artifact %q: %w", fullArtifact, err)
310310
}
311311
}
312312
}

tools/internal/autoupdate/config_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestConfigEntry(t *testing.T) {
2323
GithubRelease: &GithubRelease{
2424
Owner: "test-owner",
2525
Repository: "test-repo",
26-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
26+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
2727
},
2828
Reviewers: []string{"user", "org/team"},
2929
},
@@ -50,7 +50,7 @@ func TestConfigEntry(t *testing.T) {
5050
ConfigEntry: ConfigEntry{
5151
Name: "test-entry",
5252
Registry: &Registry{
53-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
53+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
5454
Latest: false,
5555
VersionFilter: "^v1\\.([3-9][0-9])\\.[0-9]+$",
5656
},
@@ -65,7 +65,7 @@ func TestConfigEntry(t *testing.T) {
6565
GithubRelease: &GithubRelease{
6666
Owner: "test-owner",
6767
Repository: "test-repo",
68-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
68+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
6969
},
7070
},
7171
ExpectedError: "must specify Name",
@@ -84,8 +84,8 @@ func TestConfigEntry(t *testing.T) {
8484
GithubRelease: &GithubRelease{
8585
Owner: "test-owner",
8686
Repository: "test-repo",
87-
Images: []AutoupdateImageRef{{
88-
SourceImage: "rancher/rancher",
87+
Artifacts: []AutoupdateArtifactRef{{
88+
SourceArtifact: "rancher/rancher",
8989
}},
9090
},
9191
HelmLatest: &HelmLatest{
@@ -106,7 +106,7 @@ func TestConfigEntry(t *testing.T) {
106106
GithubRelease: &GithubRelease{
107107
Owner: "test-owner",
108108
Repository: "test-repo",
109-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
109+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
110110
},
111111
Reviewers: []string{"user", "org/team"},
112112
},
@@ -119,7 +119,7 @@ func TestConfigEntry(t *testing.T) {
119119
GithubRelease: &GithubRelease{
120120
Owner: "test-owner",
121121
Repository: "test-repo",
122-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
122+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
123123
},
124124
Reviewers: []string{"org/team/foo"},
125125
},
@@ -132,7 +132,7 @@ func TestConfigEntry(t *testing.T) {
132132
GithubRelease: &GithubRelease{
133133
Owner: "test-owner",
134134
Repository: "test-repo",
135-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
135+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
136136
},
137137
Reviewers: []string{"org/"},
138138
},
@@ -145,7 +145,7 @@ func TestConfigEntry(t *testing.T) {
145145
GithubRelease: &GithubRelease{
146146
Owner: "test-owner",
147147
Repository: "test-repo",
148-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
148+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
149149
},
150150
Reviewers: []string{"/team"},
151151
},
@@ -158,7 +158,7 @@ func TestConfigEntry(t *testing.T) {
158158
GithubRelease: &GithubRelease{
159159
Owner: "test-owner",
160160
Repository: "test-repo",
161-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
161+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
162162
},
163163
Reviewers: []string{},
164164
},
@@ -171,7 +171,7 @@ func TestConfigEntry(t *testing.T) {
171171
GithubRelease: &GithubRelease{
172172
Owner: "test-owner",
173173
Repository: "test-repo",
174-
Images: []AutoupdateImageRef{{SourceImage: "rancher/rancher"}},
174+
Artifacts: []AutoupdateArtifactRef{{SourceArtifact: "rancher/rancher"}},
175175
},
176176
Reviewers: nil,
177177
},
@@ -199,11 +199,11 @@ func TestGetBranchHash(t *testing.T) {
199199
assert.Nil(t, err)
200200

201201
imageSet1 := []*config.Artifact{image1, image2}
202-
hash1, err := hashImageSet(imageSet1)
202+
hash1, err := hashArtifactSet(imageSet1)
203203
assert.Nil(t, err)
204204

205205
imageSet2 := []*config.Artifact{image2, image1}
206-
hash2, err := hashImageSet(imageSet2)
206+
hash2, err := hashArtifactSet(imageSet2)
207207
assert.Nil(t, err)
208208

209209
assert.Equal(t, hash1, hash2)
@@ -213,13 +213,13 @@ func TestGetBranchHash(t *testing.T) {
213213
image1, err := config.NewArtifact("test-org/image", []string{"asdf", "qwer"}, "", nil, nil)
214214
assert.Nil(t, err)
215215
images1 := []*config.Artifact{image1}
216-
hash1, err := hashImageSet(images1)
216+
hash1, err := hashArtifactSet(images1)
217217
assert.Nil(t, err)
218218

219219
image2, err := config.NewArtifact("test-org/image", []string{"qwer", "asdf"}, "", nil, nil)
220220
assert.Nil(t, err)
221221
images2 := []*config.Artifact{image2}
222-
hash2, err := hashImageSet(images2)
222+
hash2, err := hashArtifactSet(images2)
223223
assert.Nil(t, err)
224224

225225
assert.Equal(t, hash1, hash2)
@@ -232,11 +232,11 @@ func TestGetBranchHash(t *testing.T) {
232232
assert.Nil(t, err)
233233

234234
imageSet1 := []*config.Artifact{image1, image2}
235-
hash1, err := hashImageSet(imageSet1)
235+
hash1, err := hashArtifactSet(imageSet1)
236236
assert.Nil(t, err)
237237

238238
imageSet2 := []*config.Artifact{image1, image2}
239-
hash2, err := hashImageSet(imageSet2)
239+
hash2, err := hashArtifactSet(imageSet2)
240240
assert.Nil(t, err)
241241

242242
assert.Equal(t, hash1, hash2)
@@ -246,13 +246,13 @@ func TestGetBranchHash(t *testing.T) {
246246
image1, err := config.NewArtifact("test-org/image", []string{"asdf", "qwer"}, "", nil, nil)
247247
assert.Nil(t, err)
248248
images1 := []*config.Artifact{image1}
249-
hash1, err := hashImageSet(images1)
249+
hash1, err := hashArtifactSet(images1)
250250
assert.Nil(t, err)
251251

252252
image2, err := config.NewArtifact("test-org/image", []string{"asdf", "qwer", "zxcv"}, "", nil, nil)
253253
assert.Nil(t, err)
254254
images2 := []*config.Artifact{image2}
255-
hash2, err := hashImageSet(images2)
255+
hash2, err := hashArtifactSet(images2)
256256
assert.Nil(t, err)
257257

258258
assert.NotEqual(t, hash1, hash2)

0 commit comments

Comments
 (0)