/
service_values.go
186 lines (151 loc) · 5.12 KB
/
service_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
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
package helpers
import (
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sigs.k8s.io/yaml"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/werf"
)
func NewChartExtenderServiceValuesData() *ChartExtenderServiceValuesData {
return &ChartExtenderServiceValuesData{ServiceValues: make(map[string]interface{})}
}
type ChartExtenderServiceValuesData struct {
ServiceValues map[string]interface{}
}
func (d *ChartExtenderServiceValuesData) GetServiceValues() map[string]interface{} {
return d.ServiceValues
}
func (d *ChartExtenderServiceValuesData) SetServiceValues(vals map[string]interface{}) {
d.ServiceValues = vals
}
type ServiceValuesOptions struct {
Namespace string
Env string
IsStub bool
StubImagesNames []string
CustomTagFunc func(string) string
SetDockerConfigJsonValue bool
DockerConfigPath string
}
func GetServiceValues(ctx context.Context, projectName string, repo string, imageInfoGetters []*image.InfoGetter, opts ServiceValuesOptions) (map[string]interface{}, error) {
globalInfo := map[string]interface{}{
"werf": map[string]interface{}{
"name": projectName,
"version": werf.Version,
},
}
werfInfo := map[string]interface{}{
"name": projectName,
"version": werf.Version,
"repo": repo,
"image": map[string]interface{}{},
"tag": map[string]interface{}{},
}
if opts.Env != "" {
globalInfo["env"] = opts.Env
werfInfo["env"] = opts.Env
} else if opts.IsStub {
globalInfo["env"] = ""
werfInfo["env"] = ""
}
if opts.Namespace != "" {
werfInfo["namespace"] = opts.Namespace
}
if opts.IsStub {
stubTag := "TAG"
stubImage := fmt.Sprintf("%s:%s", repo, stubTag)
werfInfo["is_stub"] = true
werfInfo["stub_image"] = stubImage
for _, name := range opts.StubImagesNames {
werfInfo["image"].(map[string]interface{})[name] = stubImage
werfInfo["tag"].(map[string]interface{})[name] = stubTag
}
}
for _, imageInfoGetter := range imageInfoGetters {
var tag string
var image string
if opts.CustomTagFunc != nil {
tag = opts.CustomTagFunc(imageInfoGetter.GetWerfImageName())
image = strings.Join([]string{repo, tag}, ":")
} else {
tag = imageInfoGetter.GetTag()
image = imageInfoGetter.GetName()
}
if imageInfoGetter.IsNameless() {
werfInfo["is_nameless_image"] = true
werfInfo["nameless_image"] = image
} else {
werfInfo["image"].(map[string]interface{})[imageInfoGetter.GetWerfImageName()] = image
werfInfo["tag"].(map[string]interface{})[imageInfoGetter.GetWerfImageName()] = tag
}
}
res := map[string]interface{}{
"werf": werfInfo,
"global": globalInfo,
}
if opts.SetDockerConfigJsonValue {
if err := writeDockerConfigJsonValue(ctx, res, opts.DockerConfigPath); err != nil {
return nil, fmt.Errorf("error writing docker config value: %s", err)
}
}
data, err := yaml.Marshal(res)
logboek.Context(ctx).Debug().LogF("GetServiceValues result (err=%s):\n%s\n", err, data)
return res, nil
}
func GetBundleServiceValues(ctx context.Context, opts ServiceValuesOptions) (map[string]interface{}, error) {
globalInfo := map[string]interface{}{
"werf": map[string]interface{}{
"version": werf.Version,
},
}
werfInfo := map[string]interface{}{
"version": werf.Version,
}
if opts.Env != "" {
globalInfo["env"] = opts.Env
werfInfo["env"] = opts.Env
}
if opts.Namespace != "" {
werfInfo["namespace"] = opts.Namespace
}
res := map[string]interface{}{
"werf": werfInfo,
"global": globalInfo,
}
if opts.SetDockerConfigJsonValue {
if err := writeDockerConfigJsonValue(ctx, res, opts.DockerConfigPath); err != nil {
return nil, fmt.Errorf("error writing docker config value: %s", err)
}
}
data, err := yaml.Marshal(res)
logboek.Context(ctx).Debug().LogF("GetBundleServiceValues result (err=%s):\n%s\n", err, data)
return res, nil
}
func writeDockerConfigJsonValue(ctx context.Context, values map[string]interface{}, dockerConfigPath string) error {
if dockerConfigPath == "" {
dockerConfigPath = filepath.Join(os.Getenv("HOME"), ".docker")
}
configJsonPath := filepath.Join(dockerConfigPath, "config.json")
if _, err := os.Stat(configJsonPath); os.IsNotExist(err) {
return nil
} else if err != nil {
return fmt.Errorf("error accessing %q: %s", configJsonPath, err)
}
if data, err := ioutil.ReadFile(configJsonPath); err != nil {
return fmt.Errorf("error reading %q: %s", configJsonPath, err)
} else {
values["dockerconfigjson"] = base64.StdEncoding.EncodeToString(data)
}
logboek.Context(ctx).Default().LogF("NOTE: ### --set-docker-config-json-value option has been specified ###\n")
logboek.Context(ctx).Default().LogF("NOTE:\n")
logboek.Context(ctx).Default().LogF("NOTE: Werf sets .Values.dockerconfigjson with the current docker config content %q with --set-docker-config-json-value option.\n", configJsonPath)
logboek.Context(ctx).Default().LogF("NOTE: This docker config may contain temporal login credentials created using temporal short-lived token (CI_JOB_TOKEN for example),\n")
logboek.Context(ctx).Default().LogF("NOTE: and in such case should not be used as imagePullSecrets.\n")
return nil
}