forked from k8sgateway/k8sgateway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.go
189 lines (156 loc) · 5.62 KB
/
generate.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
package main
import (
"flag"
"io/ioutil"
"os"
"github.com/ghodss/yaml"
errors "github.com/rotisserie/eris"
"github.com/solo-io/gloo/install/helm/gloo/generate"
"github.com/solo-io/go-utils/log"
"github.com/solo-io/k8s-utils/installutils/helmchart"
)
var (
valuesTemplate = "install/helm/gloo/values-template.yaml"
valuesOutput = "install/helm/gloo/values.yaml"
docsOutput = "docs/content/reference/values.txt"
chartTemplate = "install/helm/gloo/Chart-template.yaml"
chartOutput = "install/helm/gloo/Chart.yaml"
// Helm docs are generated during builds. Since version changes each build, substitute with descriptive text.
// Provide an example to clarify format (1.2.3, not v1.2.3).
helmDocsVersionText = "<release_version, ex: 1.2.3>"
flagOpts = defaultFlagOptions
)
type flagOptions struct {
version string
// If set, will generate helm docs. Note that some helm values are parameterized within this script to help testing.
// When generating helm values for test purposes you should not set this flag, otherwise you will dirty the repo
// with the test-specific helm values diff.
generateHelmDocs bool
repoPrefixOverride string
globalPullPolicy string
}
const (
versionFlag = "version"
generateHelmDocsFlag = "generate-helm-docs"
repoPrefixOverrideFlag = "repo-prefix-override"
globalPullPolicyFlag = "global-pull-policy-override"
)
var defaultFlagOptions = flagOptions{
version: "",
generateHelmDocs: false,
repoPrefixOverride: "",
globalPullPolicy: "",
}
func ingestFlags() {
flag.StringVar(&flagOpts.version, versionFlag, "", "required, version to use for generated helm files")
flag.BoolVar(&flagOpts.generateHelmDocs, generateHelmDocsFlag, false, "(for release) if set, will generate docs for the helm values")
flag.StringVar(&flagOpts.repoPrefixOverride, repoPrefixOverrideFlag, "", "(for tests) if set, will override container repo")
flag.StringVar(&flagOpts.globalPullPolicy, globalPullPolicyFlag, "", "(for tests) if set, will override all image pull policies")
flag.Parse()
}
func main() {
ingestFlags()
if flagOpts.version == "" {
log.Fatalf("must pass a version with flag: %v", versionFlag)
}
log.Printf("Generating helm files.")
if err := generateValuesYaml(flagOpts.version, flagOpts.repoPrefixOverride, flagOpts.globalPullPolicy); err != nil {
log.Fatalf("generating values.yaml failed!: %v", err)
}
if flagOpts.generateHelmDocs {
log.Printf("Generating helm value docs in file: %v", docsOutput)
if err := generateValueDocs(helmDocsVersionText, flagOpts.repoPrefixOverride, flagOpts.globalPullPolicy); err != nil {
log.Fatalf("generating values.yaml docs failed!: %v", err)
}
} else {
log.Printf("NOT generating helm value docs, set %v to produce helm value docs", generateHelmDocsFlag)
}
if err := generateChartYaml(flagOpts.version); err != nil {
log.Fatalf("generating Chart.yaml failed!: %v", err)
}
}
func generateValuesYaml(version, repositoryPrefix, globalPullPolicy string) error {
cfg, err := generateValuesConfig(version, repositoryPrefix, globalPullPolicy)
if err != nil {
return err
}
return writeYaml(cfg, valuesOutput)
}
func generateValueDocs(version, repositoryPrefix, globalPullPolicy string) error {
cfg, err := generateValuesConfig(version, repositoryPrefix, globalPullPolicy)
if err != nil {
return err
}
// customize config as needed for docs
// (currently only the version text differs, and this is passed as a function argument)
return writeDocs(helmchart.Doc(cfg), docsOutput)
}
func generateChartYaml(version string) error {
var chart generate.Chart
if err := readYaml(chartTemplate, &chart); err != nil {
return err
}
chart.Version = version
return writeYaml(&chart, chartOutput)
}
func readYaml(path string, obj interface{}) error {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrapf(err, "failed reading server config file: %s", path)
}
if err := yaml.Unmarshal(bytes, obj); err != nil {
return errors.Wrap(err, "failed parsing configuration file")
}
return nil
}
func writeYaml(obj interface{}, path string) error {
bytes, err := yaml.Marshal(obj)
if err != nil {
return errors.Wrapf(err, "failed marshaling config struct")
}
err = ioutil.WriteFile(path, bytes, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "failing writing config file")
}
return nil
}
func writeDocs(docs helmchart.HelmValues, path string) error {
err := ioutil.WriteFile(path, []byte(docs.ToMarkdown()), os.ModePerm)
if err != nil {
return errors.Wrapf(err, "failing writing helm values file")
}
return nil
}
func readValuesTemplate() (*generate.HelmConfig, error) {
var config generate.HelmConfig
if err := readYaml(valuesTemplate, &config); err != nil {
return nil, err
}
return &config, nil
}
func generateValuesConfig(version, repositoryPrefix, globalPullPolicy string) (*generate.HelmConfig, error) {
cfg, err := readValuesTemplate()
if err != nil {
return nil, err
}
cfg.Gloo.Deployment.Image.Tag = &version
cfg.Discovery.Deployment.Image.Tag = &version
cfg.Gateway.Deployment.Image.Tag = &version
cfg.Gateway.CertGenJob.Image.Tag = &version
cfg.AccessLogger.Image.Tag = &version
cfg.Ingress.Deployment.Image.Tag = &version
cfg.IngressProxy.Deployment.Image.Tag = &version
cfg.Settings.Integrations.Knative.Proxy.Image.Tag = &version
cfg.Global.GlooMtls.Sds.Image.Tag = &version
cfg.Global.GlooMtls.EnvoySidecar.Image.Tag = &version
for _, v := range cfg.GatewayProxies {
v.PodTemplate.Image.Tag = &version
}
if repositoryPrefix != "" {
cfg.Global.Image.Registry = &repositoryPrefix
}
if globalPullPolicy != "" {
cfg.Global.Image.PullPolicy = &globalPullPolicy
}
return cfg, nil
}