forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kustomize.go
241 lines (206 loc) · 5.91 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package kustomize
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
argoexec "github.com/argoproj/pkg/exec"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/util/kube"
)
type ImageTag struct {
Name string
Value string
}
func newImageTag(image Image) ImageTag {
parts := strings.Split(image, ":")
if len(parts) > 0 {
return ImageTag{Name: parts[0], Value: parts[1]}
} else {
return ImageTag{Name: parts[0], Value: "latest"}
}
}
// represents a Docker image in the format NAME[:TAG].
type Image = string
// Kustomize provides wrapper functionality around the `kustomize` command.
type Kustomize interface {
// Build returns a list of unstructured objects from a `kustomize build` command and extract supported parameters
Build(opts *v1alpha1.ApplicationSourceKustomize) ([]*unstructured.Unstructured, []ImageTag, []Image, error)
}
type GitCredentials struct {
Username string
Password string
}
// NewKustomizeApp create a new wrapper to run commands on the `kustomize` command-line tool.
func NewKustomizeApp(path string, creds *GitCredentials) Kustomize {
return &kustomize{
path: path,
creds: creds,
}
}
type kustomize struct {
path string
creds *GitCredentials
}
func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize) ([]*unstructured.Unstructured, []ImageTag, []Image, error) {
version, err := k.getKustomizationVersion()
if err != nil {
return nil, nil, nil, err
}
commandName := GetCommandName(version)
if opts != nil {
if opts.NamePrefix != "" {
cmd := exec.Command(commandName, "edit", "set", "nameprefix", opts.NamePrefix)
cmd.Dir = k.path
_, err := argoexec.RunCommandExt(cmd)
if err != nil {
return nil, nil, nil, err
}
}
if len(opts.ImageTags) > 0 {
if version != 1 {
log.Warn("ignoring image tags as kustomize is not version 1")
} else {
for _, override := range opts.ImageTags {
cmd := exec.Command(commandName, "edit", "set", "imagetag", fmt.Sprintf("%s:%s", override.Name, override.Value))
cmd.Dir = k.path
_, err := argoexec.RunCommandExt(cmd)
if err != nil {
return nil, nil, nil, err
}
}
}
}
if len(opts.Images) > 0 {
if version != 2 {
log.Warn("ignoring images as kustomize is not version 2")
} else {
// set image postgres=eu.gcr.io/my-project/postgres:latest my-app=my-registry/my-app@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
// set image node:8.15.0 mysql=mariadb alpine@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3
args := []string{"edit", "set", "image"}
args = append(args, opts.Images...)
cmd := exec.Command(commandName, args...)
cmd.Dir = k.path
_, err := argoexec.RunCommandExt(cmd)
if err != nil {
return nil, nil, nil, err
}
}
}
}
cmd := exec.Command(commandName, "build", k.path)
cmd.Env = os.Environ()
if k.creds != nil {
cmd.Env = append(cmd.Env, "GIT_ASKPASS=git-ask-pass.sh")
cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_USERNAME=%s", k.creds.Username))
cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_PASSWORD=%s", k.creds.Password))
}
out, err := argoexec.RunCommandExt(cmd)
if err != nil {
return nil, nil, nil, err
}
objs, err := kube.SplitYAML(out)
if err != nil {
return nil, nil, nil, err
}
if version == 1 {
return objs, getImageTagParameters(objs), nil, nil
}
return objs, nil, getImageParameters(objs), nil
}
func GetCommandName(version int) string {
if version == 1 {
return "kustomize1"
}
return "kustomize"
}
var KustomizationNames = []string{"kustomization.yaml", "kustomization.yml", "Kustomization"}
// kustomization is a file that describes a configuration consumable by kustomize.
func (k *kustomize) findKustomization() (string, error) {
for _, file := range KustomizationNames {
kustomization := filepath.Join(k.path, file)
if _, err := os.Stat(kustomization); err == nil {
return kustomization, nil
}
}
return "", errors.New("did not find kustomization in " + k.path)
}
func IsKustomization(path string) bool {
for _, kustomization := range KustomizationNames {
if path == kustomization {
return true
}
}
return false
}
type kustomization struct {
Kind string
}
func (k *kustomize) getKustomizationVersion() (int, error) {
kustomizationFile, err := k.findKustomization()
if err != nil {
return 0, err
}
dat, err := ioutil.ReadFile(kustomizationFile)
if err != nil {
return 0, err
}
var obj kustomization
err = yaml.Unmarshal(dat, &obj)
if err != nil {
return 0, err
}
if obj.Kind == "Kustomization" {
return 2, nil
}
return 1, nil
}
func getImageTagParameters(objs []*unstructured.Unstructured) []ImageTag {
var images []ImageTag
for _, image := range getImageParameters(objs) {
images = append(images, newImageTag(image))
}
return images
}
func getImageParameters(objs []*unstructured.Unstructured) []Image {
var images []Image
for _, obj := range objs {
images = append(images, getImages(obj.Object)...)
}
sort.Slice(images, func(i, j int) bool {
return j < j
})
return images
}
func getImages(object map[string]interface{}) []Image {
var images []Image
for k, v := range object {
if array, ok := v.([]interface{}); ok {
if k == "containers" || k == "initContainers" {
for _, obj := range array {
if mapObj, isMapObj := obj.(map[string]interface{}); isMapObj {
if image, hasImage := mapObj["image"]; hasImage {
images = append(images, fmt.Sprintf("%s", image))
}
}
}
} else {
for i := range array {
if mapObj, isMapObj := array[i].(map[string]interface{}); isMapObj {
images = append(images, getImages(mapObj)...)
}
}
}
} else if objMap, ok := v.(map[string]interface{}); ok {
images = append(images, getImages(objMap)...)
}
}
return images
}