-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathvendir.go
423 lines (356 loc) · 12.6 KB
/
vendir.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package fetch
import (
"bytes"
"context"
"fmt"
"os"
goexec "os/exec"
"path/filepath"
"strings"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
// we run vendir by shelling out to it, but we create the vendir configs with help from a vendored copy of vendir.
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
vendirconf "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
kyaml "sigs.k8s.io/yaml"
)
const (
vendirEntireDirPath = "."
)
type Vendir struct {
nsName string
coreClient kubernetes.Interface
config vendirconf.Config
opts VendirOpts
cmdRunner exec.CmdRunner
}
// VendirOpts allows to customize vendir configuration given to vendir.
type VendirOpts struct {
// ConfigHook provides an opportunity to make changes to vendir configuration
// before it's given to vendir for execution. If not provided it will default
// to the identity function.
ConfigHook func(vendirconf.Config) vendirconf.Config
SkipTLSConfig SkipTLSConfig
BaseCacheFolder string
}
// NewVendir returns vendir.
func NewVendir(nsName string, coreClient kubernetes.Interface,
opts VendirOpts, cmdRunner exec.CmdRunner) *Vendir {
if opts.ConfigHook == nil {
opts.ConfigHook = func(conf vendirconf.Config) vendirconf.Config { return conf }
}
return &Vendir{
nsName: nsName,
coreClient: coreClient,
opts: opts,
config: vendirconf.Config{
APIVersion: "vendir.k14s.io/v1alpha1", // TODO: use constant from vendir package
Kind: "Config", // TODO: use constant from vendir package
},
cmdRunner: cmdRunner,
}
}
// AddDir adds a directory to vendir's config for each fetcher that the app spec declares.
// vendir fetches resources into your filesystem, so the destination directory is a core part of vendir config.
func (v *Vendir) AddDir(fetch v1alpha1.AppFetch, dirPath string) error {
if fetch.Path != "" {
dirPath = fetch.Path
}
switch {
case fetch.Inline != nil:
v.config.Directories = append(v.config.Directories, v.dir(v.inlineConf(*fetch.Inline), dirPath))
case fetch.Image != nil:
v.config.Directories = append(v.config.Directories, v.dir(v.imageConf(*fetch.Image), dirPath))
case fetch.HTTP != nil:
v.config.Directories = append(v.config.Directories, v.dir(v.httpConf(*fetch.HTTP), dirPath))
case fetch.Git != nil:
v.config.Directories = append(v.config.Directories, v.dir(v.gitConf(*fetch.Git), dirPath))
case fetch.HelmChart != nil:
v.config.Directories = append(v.config.Directories, v.dir(v.helmChartConf(*fetch.HelmChart), dirPath))
case fetch.ImgpkgBundle != nil:
v.config.Directories = append(v.config.Directories, v.dir(v.imgpkgBundleConf(*fetch.ImgpkgBundle), dirPath))
default:
return fmt.Errorf("Unsupported way to fetch templates")
}
return nil
}
// Config is just for accessing (a copy of) the internal config for testing; you probably don't want to call this IRL
func (v *Vendir) Config() vendirconf.Config {
return v.config
}
func (v *Vendir) dir(contents vendirconf.DirectoryContents, dirPath string) vendirconf.Directory {
return vendirconf.Directory{
Path: dirPath,
Contents: []vendirconf.DirectoryContents{contents},
}
}
func (v *Vendir) inlineConf(inline v1alpha1.AppFetchInline) vendirconf.DirectoryContents {
var inlineSources []vendirconf.DirectoryContentsInlineSource
for _, source := range inline.PathsFrom {
inlineSources = append(inlineSources, v.inlineSourceConf(source))
}
return vendirconf.DirectoryContents{
Path: vendirEntireDirPath,
Inline: &vendirconf.DirectoryContentsInline{
Paths: inline.Paths,
PathsFrom: inlineSources,
}}
}
func (v *Vendir) imageConf(image v1alpha1.AppFetchImage) vendirconf.DirectoryContents {
return vendirconf.DirectoryContents{
Path: vendirEntireDirPath,
NewRootPath: image.SubPath,
Image: &vendirconf.DirectoryContentsImage{
URL: image.URL,
TagSelection: image.TagSelection,
SecretRef: v.localRefConf(image.SecretRef),
DangerousSkipTLSVerify: v.shouldSkipTLSVerify(image.URL),
},
}
}
func (v *Vendir) imgpkgBundleConf(imgpkgBundle v1alpha1.AppFetchImgpkgBundle) vendirconf.DirectoryContents {
return vendirconf.DirectoryContents{
Path: vendirEntireDirPath,
ImgpkgBundle: &vendirconf.DirectoryContentsImgpkgBundle{
Image: imgpkgBundle.Image,
TagSelection: imgpkgBundle.TagSelection,
SecretRef: v.localRefConf(imgpkgBundle.SecretRef),
DangerousSkipTLSVerify: v.shouldSkipTLSVerify(imgpkgBundle.Image),
},
}
}
func (v *Vendir) httpConf(http v1alpha1.AppFetchHTTP) vendirconf.DirectoryContents {
return vendirconf.DirectoryContents{
Path: vendirEntireDirPath,
HTTP: &vendirconf.DirectoryContentsHTTP{
URL: http.URL,
SHA256: http.SHA256,
SecretRef: v.localRefConf(http.SecretRef),
},
NewRootPath: http.SubPath,
}
}
func (v *Vendir) gitConf(git v1alpha1.AppFetchGit) vendirconf.DirectoryContents {
return vendirconf.DirectoryContents{
Path: vendirEntireDirPath,
NewRootPath: git.SubPath,
Git: &vendirconf.DirectoryContentsGit{
URL: git.URL,
RefSelection: git.RefSelection,
Ref: git.Ref,
SecretRef: v.localRefConf(git.SecretRef),
LFSSkipSmudge: git.LFSSkipSmudge,
},
}
}
func (v *Vendir) helmChartConf(chart v1alpha1.AppFetchHelmChart) vendirconf.DirectoryContents {
return vendirconf.DirectoryContents{
Path: vendirEntireDirPath,
HelmChart: &vendirconf.DirectoryContentsHelmChart{
Name: chart.Name,
Version: chart.Version,
Repository: v.helmRepoConf(chart.Repository),
},
}
}
func (v *Vendir) inlineSourceConf(src v1alpha1.AppFetchInlineSource) vendirconf.DirectoryContentsInlineSource {
return vendirconf.DirectoryContentsInlineSource{
SecretRef: v.inlineSourceRefConf(src.SecretRef),
ConfigMapRef: v.inlineSourceRefConf(src.ConfigMapRef),
}
}
func (v *Vendir) inlineSourceRefConf(ref *v1alpha1.AppFetchInlineSourceRef) *vendirconf.DirectoryContentsInlineSourceRef {
if ref == nil {
return nil
}
return &vendirconf.DirectoryContentsInlineSourceRef{
DirectoryPath: ref.DirectoryPath,
DirectoryContentsLocalRef: vendirconf.DirectoryContentsLocalRef{Name: ref.Name},
}
}
func (v *Vendir) helmRepoConf(repo *v1alpha1.AppFetchHelmChartRepo) *vendirconf.DirectoryContentsHelmChartRepo {
if repo == nil {
return nil
}
return &vendirconf.DirectoryContentsHelmChartRepo{
URL: repo.URL,
SecretRef: v.localRefConf(repo.SecretRef),
}
}
func (v *Vendir) localRefConf(ref *v1alpha1.AppFetchLocalRef) *vendirconf.DirectoryContentsLocalRef {
if ref == nil {
return nil
}
return &vendirconf.DirectoryContentsLocalRef{
Name: ref.Name,
}
}
// ConfigBytes fetches all the referenced Secrets & ConfigMaps and returns the
// multi-document YAML-encoded config that vendir consumes.
// https://github.com/vmware-tanzu/carvel-vendir/blob/develop/examples/secrets/vendir.yml
func (v *Vendir) ConfigBytes() ([]byte, error) {
var resourcesYaml [][]byte
for _, dir := range v.config.Directories {
for _, contents := range dir.Contents {
yamlBytes, err := v.requiredResourcesYaml(contents)
if err != nil {
return nil, err
}
resourcesYaml = append(resourcesYaml, yamlBytes...)
}
}
vendirConfBytes, err := v.opts.ConfigHook(v.config).AsBytes()
if err != nil {
return nil, err
}
finalConfig := bytes.Join(append(resourcesYaml, vendirConfBytes), []byte("---\n"))
return finalConfig, nil
}
func (v *Vendir) requiredResourcesYaml(contents vendirconf.DirectoryContents) ([][]byte, error) {
switch {
case contents.Inline != nil:
return v.inlineResources(*contents.Inline)
case contents.Image != nil:
return v.imageResources(*contents.Image)
case contents.HTTP != nil:
return v.httpResources(*contents.HTTP)
case contents.Git != nil:
return v.gitResources(*contents.Git)
case contents.HelmChart != nil:
return v.helmChartResources(*contents.HelmChart)
case contents.ImgpkgBundle != nil:
return v.imgpkgBundleResources(*contents.ImgpkgBundle)
}
return nil, fmt.Errorf("Unknown fetch type: %v", contents)
}
func (v *Vendir) inlineResources(inline vendirconf.DirectoryContentsInline) ([][]byte, error) {
var resourcesYamlBytes [][]byte
for _, source := range inline.PathsFrom {
switch {
case source.SecretRef != nil:
bytes, err := v.secretBytes(source.SecretRef.DirectoryContentsLocalRef)
if err != nil {
return nil, err
}
resourcesYamlBytes = append(resourcesYamlBytes, bytes)
case source.ConfigMapRef != nil:
bytes, err := v.configMapBytes(source.ConfigMapRef.DirectoryContentsLocalRef)
if err != nil {
return nil, err
}
resourcesYamlBytes = append(resourcesYamlBytes, bytes)
}
}
return resourcesYamlBytes, nil
}
func (v *Vendir) imageResources(image vendirconf.DirectoryContentsImage) ([][]byte, error) {
if image.SecretRef == nil {
return nil, nil
}
resBytes, err := v.secretBytes(*image.SecretRef)
if err != nil {
return nil, err
}
return [][]byte{resBytes}, nil
}
func (v *Vendir) imgpkgBundleResources(imgpkgBundle vendirconf.DirectoryContentsImgpkgBundle) ([][]byte, error) {
if imgpkgBundle.SecretRef == nil {
return nil, nil
}
resBytes, err := v.secretBytes(*imgpkgBundle.SecretRef)
if err != nil {
return nil, err
}
return [][]byte{resBytes}, nil
}
func (v *Vendir) httpResources(http vendirconf.DirectoryContentsHTTP) ([][]byte, error) {
if http.SecretRef == nil {
return nil, nil
}
resBytes, err := v.secretBytes(*http.SecretRef)
if err != nil {
return nil, err
}
return [][]byte{resBytes}, nil
}
func (v *Vendir) gitResources(git vendirconf.DirectoryContentsGit) ([][]byte, error) {
if git.SecretRef == nil {
return nil, nil
}
resBytes, err := v.secretBytes(*git.SecretRef)
if err != nil {
return nil, err
}
return [][]byte{resBytes}, nil
}
func (v *Vendir) helmChartResources(helmChart vendirconf.DirectoryContentsHelmChart) ([][]byte, error) {
if helmChart.Repository == nil || helmChart.Repository.SecretRef == nil {
return nil, nil
}
resBytes, err := v.secretBytes(*helmChart.Repository.SecretRef)
if err != nil {
return nil, err
}
return [][]byte{resBytes}, nil
}
func (v *Vendir) secretBytes(secretRef vendirconf.DirectoryContentsLocalRef) ([]byte, error) {
secret, err := v.coreClient.CoreV1().Secrets(v.nsName).Get(context.Background(), secretRef.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
// typed clients drop GVK or resource (https://github.com/kubernetes/kubernetes/issues/80609)
secret.TypeMeta.Kind = "Secret"
secret.TypeMeta.APIVersion = "v1"
return kyaml.Marshal(secret)
}
func (v *Vendir) configMapBytes(configMapRef vendirconf.DirectoryContentsLocalRef) ([]byte, error) {
configMap, err := v.coreClient.CoreV1().ConfigMaps(v.nsName).Get(context.Background(), configMapRef.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
// typed clients drop GVK or resource (https://github.com/kubernetes/kubernetes/issues/80609)
configMap.TypeMeta.Kind = "ConfigMap"
configMap.TypeMeta.APIVersion = "v1"
return kyaml.Marshal(configMap)
}
// This function only works on image refs. If in the future we decide to
// expand this option to other fetch options, we will need to add hostname
// extraction for those
func (v *Vendir) shouldSkipTLSVerify(url string) bool {
return v.opts.SkipTLSConfig.ShouldSkipTLSForAuthority(ExtractImageRegistry(url))
}
// Run executes vendir command based on given configuration.
func (v *Vendir) Run(conf []byte, workingDir string, cacheID string) exec.CmdRunResult {
var stdoutBs, stderrBs bytes.Buffer
cmd := goexec.Command("vendir", "sync", "-f", "-", "--lock-file", os.DevNull)
cmd.Dir = workingDir
cmd.Stdin = bytes.NewReader(conf)
cmd.Stdout = &stdoutBs
cmd.Stderr = &stderrBs
cmd.Env = append(os.Environ(), "VENDIR_CACHE_DIR="+filepath.Join(v.opts.BaseCacheFolder, cacheID))
err := v.cmdRunner.Run(cmd)
result := exec.CmdRunResult{
Stdout: stdoutBs.String(),
Stderr: stderrBs.String(),
}
result.AttachErrorf("Fetching resources: %s", err)
return result
}
// ClearCache removes all cache entries for the cacheID
func (v *Vendir) ClearCache(cacheID string) error {
return os.RemoveAll(filepath.Join(v.opts.BaseCacheFolder, cacheID))
}
// ExtractImageRegistry returns the registry portion of a Docker image reference
func ExtractImageRegistry(name string) string {
parts := strings.SplitN(name, "/", 2)
var registry string
if len(parts) == 2 && (strings.ContainsRune(parts[0], '.') || strings.ContainsRune(parts[0], ':')) {
registry = parts[0]
} else {
registry = "index.docker.io"
}
return registry
}