-
Notifications
You must be signed in to change notification settings - Fork 99
/
values.go
141 lines (108 loc) · 3.06 KB
/
values.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
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package template
import (
"context"
"fmt"
"io/ioutil"
"sort"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/memdir"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type Values struct {
ValuesFrom []v1alpha1.AppTemplateValuesSource
genericOpts GenericOpts
coreClient kubernetes.Interface
}
func (t Values) AsPaths(dirPath string) ([]string, func(), error) {
var valuesDirs []*memdir.TmpDir
cleanUpFunc := func() {
for _, valDir := range valuesDirs {
_ = valDir.Remove()
}
}
var allPaths []string
for _, source := range t.ValuesFrom {
var paths []string
var err error
valuesDir := memdir.NewTmpDir("template-values")
err = valuesDir.Create()
if err != nil {
cleanUpFunc()
return nil, nil, err
}
valuesDirs = append(valuesDirs, valuesDir)
switch {
case source.SecretRef != nil:
paths, err = t.writeFromSecret(valuesDir.Path(), *source.SecretRef)
case source.ConfigMapRef != nil:
paths, err = t.writeFromConfigMap(valuesDir.Path(), *source.ConfigMapRef)
case len(source.Path) > 0:
if source.Path == stdinPath {
paths = append(paths, stdinPath)
} else {
checkedPath, err := memdir.ScopedPath(dirPath, source.Path)
if err == nil {
paths = append(paths, checkedPath)
}
}
default:
err = fmt.Errorf("Expected either secretRef, configMapRef or path as a source")
}
if err != nil {
cleanUpFunc()
return nil, nil, fmt.Errorf("Writing paths: %s", err)
}
allPaths = append(allPaths, paths...)
}
return allPaths, cleanUpFunc, nil
}
func (t Values) writeFromSecret(dstPath string,
secretRef v1alpha1.AppTemplateValuesSourceRef) ([]string, error) {
secret, err := t.coreClient.CoreV1().Secrets(t.genericOpts.Namespace).Get(
context.Background(), secretRef.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
var result []string
for name, val := range secret.Data {
path, err := t.writeFile(dstPath, name, val)
if err != nil {
return nil, err
}
result = append(result, path)
}
sort.Strings(result)
return result, nil
}
func (t Values) writeFromConfigMap(dstPath string,
configMapRef v1alpha1.AppTemplateValuesSourceRef) ([]string, error) {
configMap, err := t.coreClient.CoreV1().ConfigMaps(t.genericOpts.Namespace).Get(
context.Background(), configMapRef.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
var result []string
for name, val := range configMap.Data {
path, err := t.writeFile(dstPath, name, []byte(val))
if err != nil {
return nil, err
}
result = append(result, path)
}
sort.Strings(result)
return result, nil
}
func (t Values) writeFile(dstPath, subPath string, content []byte) (string, error) {
newPath, err := memdir.ScopedPath(dstPath, subPath)
if err != nil {
return "", err
}
err = ioutil.WriteFile(newPath, content, 0600)
if err != nil {
return "", fmt.Errorf("Writing file '%s': %s", newPath, err)
}
return newPath, nil
}