-
Notifications
You must be signed in to change notification settings - Fork 117
/
kustomize.go
92 lines (86 loc) · 2.49 KB
/
kustomize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package promotion
import (
"fmt"
"path/filepath"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/controller/git"
"github.com/akuity/kargo/internal/credentials"
"github.com/akuity/kargo/internal/kustomize"
)
// newKustomizeMechanism returns a gitMechanism that only only selects and
// performs updates that involve Kustomize.
func newKustomizeMechanism(
credentialsDB credentials.Database,
) Mechanism {
return newGitMechanism(
"Kustomize promotion mechanism",
credentialsDB,
selectKustomizeUpdates,
(&kustomizer{
setImageFn: kustomize.SetImage,
}).apply,
)
}
// selectKustomizeUpdates returns a subset of the given updates that involve
// Kustomize.
func selectKustomizeUpdates(updates []kargoapi.GitRepoUpdate) []kargoapi.GitRepoUpdate {
selectedUpdates := make([]kargoapi.GitRepoUpdate, 0, len(updates))
for _, update := range updates {
if update.Kustomize != nil {
selectedUpdates = append(selectedUpdates, update)
}
}
return selectedUpdates
}
// kustomizer is a helper struct whose sole purpose is to close over several
// other functions that are used in the implementation of the apply() function.
type kustomizer struct {
setImageFn func(dir, fqImageRef string) error
}
// apply uses Kustomize to carry out the provided update in the specified
// working directory.
func (k *kustomizer) apply(
update kargoapi.GitRepoUpdate,
newFreight kargoapi.FreightReference,
_ string, // TODO: sourceCommit would be a nice addition to the commit message
_ string,
workingDir string,
_ git.RepoCredentials,
) ([]string, error) {
changeSummary := make([]string, 0, len(update.Kustomize.Images))
for _, imgUpdate := range update.Kustomize.Images {
var fqImageRef string // Fully-qualified image reference
for _, img := range newFreight.Images {
if img.RepoURL == imgUpdate.Image {
if imgUpdate.UseDigest {
fqImageRef = fmt.Sprintf("%s@%s", img.RepoURL, img.Digest)
} else {
fqImageRef = fmt.Sprintf("%s:%s", img.RepoURL, img.Tag)
}
break
}
}
if fqImageRef == "" {
// TODO: Warn?
continue
}
dir := filepath.Join(workingDir, imgUpdate.Path)
if err := k.setImageFn(dir, fqImageRef); err != nil {
return nil, fmt.Errorf(
"error updating image %q to %q using Kustomize: %w",
imgUpdate.Image,
fqImageRef,
err,
)
}
changeSummary = append(
changeSummary,
fmt.Sprintf(
"updated %s/kustomization.yaml to use image %s",
imgUpdate.Path,
fqImageRef,
),
)
}
return changeSummary, nil
}