Repository navigation
Expand file tree
/
Copy pathgenerate.go
More file actions
296 lines (250 loc) · 10.2 KB
/
Copy pathgenerate.go
File metadata and controls
296 lines (250 loc) · 10.2 KB
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
package deploykf
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/hairyhenderson/gomplate/v3"
"github.com/spf13/cobra"
"github.com/deployKF/cli/internal/generate"
"github.com/deployKF/cli/internal/version"
)
const generateHelp = `This command will generate an output folder containing Kubernetes manifests.
ARGUMENTS:
----------------
You must provide either '--source-version' OR '--source-path' to specify the source of the generator:
- If '--source-version' is provided, the provided version tag will be downloaded from the 'deployKF/deployKF' GitHub.
- If '--source-path' is provided, the source will be read from the provided local directory or '.zip' file.
You may provide one or more '--values' files that contain your configuration values:
- For more information on how to structure your values files, see the 'deployKF/deployKF' GitHub repository.
You must provide '--output-dir' to specify the output directory for the generated manifests:
- If the directory does not exist, it will be created.
- If the directory is non-empty, it will be cleaned before generating the manifests.
However, it must contain a '.deploykf_output' marker file, otherwise the command will fail.
OUTPUT:
----------------
The '.deploykf_output' marker file contains the following information:
- generated_at: the time the generator was run
- source_version: the source version that was used (if '--source-version' was provided)
- source_path: the path of the source artifact that was used
- source_hash: the SHA256 hash of the source artifact that was used
- cli_version: the version of the deployKF CLI that was used
EXAMPLES:
----------------
To generate manifests from a GitHub source version:
$ deploykf generate --source-version v0.1.0 --values ./values.yaml --output-dir ./GENERATOR_OUTPUT
To generate manifests from a local source zip file:
$ deploykf generate --source-path ./deploykf.zip --values ./values.yaml --output-dir ./GENERATOR_OUTPUT
To generate manifests from a local source directory:
$ deploykf generate --source-path ./deploykf --values ./values.yaml --output-dir ./GENERATOR_OUTPUT
`
type generateOptions struct {
sourceVersion string
sourcePath string
values []string
outputDir string
}
func newGenerateCmd(out io.Writer) *cobra.Command {
o := &generateOptions{}
var cmd = &cobra.Command{
Use: "generate",
Short: "Generate Kubernetes manifests from deployKF templates and config values",
Long: generateHelp,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(out)
},
}
// add local flags
cmd.Flags().StringVarP(&o.sourceVersion, "source-version", "V", "", "a version tag from the 'deployKF/deployKF' GitHub repository")
cmd.Flags().StringVar(&o.sourcePath, "source-path", "", "a local path to a directory or '.zip' file containing a generator source")
cmd.Flags().StringSliceVarP(&o.values, "values", "f", []string{}, "a YAML file containing configuration values")
cmd.Flags().StringVarP(&o.outputDir, "output-dir", "O", "", "the output directory in which to generate the manifests")
// mark local flags
cmd.MarkFlagsMutuallyExclusive("source-version", "source-path")
cmd.MarkFlagRequired("output-dir")
return cmd
}
func (o *generateOptions) run(out io.Writer) error {
// TODO: verify the provided `--values`:
// - check the YAML schema against a spec that is defined in the generator source
// - check that all provided file paths exist (before gomplate fails)
// initialise the source helper
// TODO: let users provide their own repo/owner for the source
sourceHelper := generate.NewSourceHelper()
// create a temporary directory to store our generator source,
// and defer a function to clean it up after this function returns
tempSourcePath, err := os.MkdirTemp("", "deploykf-generator-source-*")
if err != nil {
fmt.Printf("Error creating temporary directory: %v\n", err)
return nil
}
defer func() {
err := os.RemoveAll(tempSourcePath)
if err != nil {
fmt.Printf("Error removing temporary directory: %v\n", err)
}
}()
// populate the temporary directory with the generator source
// - CASE 1: if `--source-version` is provided, download that version's `.zip` file and unzip it into the temp folder
// - CASE 2: if `--source-path` points to a `.zip` file, unzip it into the temp folder
// - CASE 3: if `--source-path` points to a folder, copy the contents of that folder into the temp folder
var sourcePath string
if o.sourceVersion != "" {
// CASE 1: download the source from GitHub
sourcePath, err = sourceHelper.DownloadAndUnpackSource(o.sourceVersion, tempSourcePath, out)
if err != nil {
return err
}
} else if o.sourcePath != "" {
sourcePath, err = filepath.EvalSymlinks(o.sourcePath)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("the provided --source-path '%s' does not exist", o.sourcePath)
}
return err
}
sourceIsDir, sourceIsFile, err := generate.PathExists(sourcePath)
if err != nil {
return err
}
if sourceIsFile && strings.HasSuffix(sourcePath, ".zip") {
// CASE 2: source is a .zip file
fmt.Fprintf(out, "Using custom source file: %s\n", o.sourcePath)
err := generate.UnzipFile(sourcePath, tempSourcePath, "generator")
if err != nil {
return err
}
} else if sourceIsDir {
// CASE 3: source is a folder
fmt.Fprintf(out, "Using custom source folder: %s\n", o.sourcePath)
err := generate.CopyFolder(sourcePath, tempSourcePath)
if err != nil {
return err
}
} else {
return fmt.Errorf("the provided --source-path '%s' must be a folder or a .zip file", o.sourcePath)
}
} else {
return fmt.Errorf("at least one of `--source-version` or `--source-path` must be provided")
}
// important paths from the generator source
templatesPath := filepath.Join(tempSourcePath, "templates")
helpersPath := filepath.Join(tempSourcePath, "helpers")
defaultValuesPath := filepath.Join(tempSourcePath, "default_values.yaml")
markerPath := filepath.Join(tempSourcePath, ".deploykf_generator")
// verify the generator source is valid, and is supported by this version of the CLI
err = generate.VerifyGeneratorSource(templatesPath, helpersPath, defaultValuesPath, markerPath)
if err != nil {
return err
}
// write runtime config templates
// - note, we are writing these files into the source folder, not the output folder
runtimePath := filepath.Join(tempSourcePath, "runtime")
err = generate.WriteRuntimeTemplates(runtimePath, templatesPath, o.outputDir)
if err != nil {
return err
}
// GENERATOR PHASE 1: render `.gomplateignore_template` files
// - note, we are rendering the `.gomplateignore` files into the generator source
// templates folder, not the output folder
phase1Config := &gomplate.Config{ //nolint:staticcheck
InputDir: templatesPath,
OutputMap: templatesPath + `/{{< .in | strings.ReplaceAll ".gomplateignore_template" ".gomplateignore" >}}`,
ExcludeGlob: []string{"*", "!*.gomplateignore_template"},
LDelim: "{{<",
RDelim: ">}}",
DataSources: o.gomplateDataSources(),
Contexts: o.gomplateContexts(defaultValuesPath),
Templates: o.gomplateTemplates(helpersPath, runtimePath),
SuppressEmpty: true,
}
err = gomplate.RunTemplates(phase1Config) //nolint:staticcheck
if err != nil {
return err
}
// clean the `--output-dir` if it's safe to do so
err = generate.CleanOutputDirectory(o.outputDir)
if err != nil {
return err
}
// calculate the hash of the generator source
// - if the source was a `.zip` file, we'll use the hash of the file
// - if the source was a folder, we'll use the hash of the folder
// note, we'll ignore the `.gomplateignore` files when calculating the hash
// see `generate.HashPath` for more details
sourceArtifactHash, err := generate.HashPath(sourcePath, []string{".gomplateignore"})
if err != nil {
return err
}
// create marker file in the `--output-dir`
// - will create the directory if it doesn't already exist
// - the marker will contain JSON with information like run time and source version
err = generate.CreateMarkerFile(o.outputDir, o.sourceVersion, sourcePath, sourceArtifactHash, version.GetVersion())
if err != nil {
return err
}
// GENERATOR PHASE 2: render to the output folder
phase2Config := &gomplate.Config{ //nolint:staticcheck
InputDir: templatesPath,
OutputDir: o.outputDir,
LDelim: "{{<",
RDelim: ">}}",
DataSources: o.gomplateDataSources(),
Contexts: o.gomplateContexts(defaultValuesPath),
Templates: o.gomplateTemplates(helpersPath, runtimePath),
SuppressEmpty: true,
}
err = gomplate.RunTemplates(phase2Config) //nolint:staticcheck
if err != nil {
return err
}
// log the output directory
fmt.Fprintf(out, "Generated manifests at: %s\n", o.outputDir)
return nil
}
// build the `DataSources` for our `gomplate.Config`
func (o *generateOptions) gomplateDataSources() []string {
// create DataSource for each `--values` provided by the user
values := o.values
dataSources := make([]string, len(values))
for i, v := range values {
dataSources[i] = "Values_" + strconv.Itoa(i) + "=" + v
}
return dataSources
}
// build the `Contexts` for our `gomplate.Config`
func (o *generateOptions) gomplateContexts(defaultValuesPath string) []string {
var sb strings.Builder
// merge the user-provided `--values` DataSources by placing a "|" between them
// NOTE: merges happen from right to left, so we add the `--values` in reverse
// order so the `--values` that were provided later take precedence
values := o.values
for i := len(values) - 1; i >= 0; i-- {
if sb.Len() > 0 {
sb.WriteString("|")
}
sb.WriteString("Values_")
sb.WriteString(strconv.Itoa(i))
}
// add the `default_values.yaml` to the end of the merge
if sb.Len() > 0 {
sb.WriteString("|")
}
sb.WriteString(defaultValuesPath)
// we only use `merge` if there is more than one DataSource
valuesContext := "Values="
if len(values) > 0 {
valuesContext += "merge:"
}
valuesContext += sb.String()
return []string{valuesContext}
}
// build the `Templates` for our `gomplate.Config`
func (o *generateOptions) gomplateTemplates(helpersPath string, runtimePath string) []string {
return []string{
"helpers=" + helpersPath,
"runtime=" + runtimePath,
}
}