-
Notifications
You must be signed in to change notification settings - Fork 51
/
step_helm_template.go
300 lines (276 loc) · 9.34 KB
/
step_helm_template.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
package helm
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/jenkins-x-plugins/jx-gitops/pkg/cmd/split"
"github.com/jenkins-x-plugins/jx-gitops/pkg/plugins"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/cli"
"github.com/jenkins-x/jx-helpers/v3/pkg/options"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
helmTemplateLong = templates.LongDesc(`
Generate the kubernetes resources from a helm chart
`)
helmTemplateExample = templates.Examples(`
# generates the resources from a helm chart
%s step helm template
`)
)
// HelmTemplateOptions the options for the command
type TemplateOptions struct {
OutDir string
HelmBinary string
ReleaseName string
Namespace string
Chart string
ValuesFiles []string
DefaultDomain string
GitCommitMessage string
Version string
Repository string
BatchMode bool
DoGitCommit bool
NoSplit bool
NoExtSecrets bool
IncludeCRDs bool
CheckExists bool
Gitter gitclient.Interface
CommandRunner cmdrunner.CommandRunner
}
// NewCmdHelmTemplate creates a command object for the command
func NewCmdHelmTemplate() (*cobra.Command, *TemplateOptions) {
o := &TemplateOptions{}
cmd := &cobra.Command{
Use: "template",
Short: "Generate the kubernetes resources from a helm chart",
Long: helmTemplateLong,
Example: fmt.Sprintf(helmTemplateExample, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.OutDir, "output-dir", "o", "", "the output directory to generate the templates to. Defaults to charts/$name/resources")
cmd.Flags().StringVarP(&o.ReleaseName, "name", "n", "", "the name of the helm release to template. Defaults to $APP_NAME if not specified")
cmd.Flags().StringVarP(&o.Namespace, "namespace", "", "", "specifies the namespace to use to generate the templates in")
cmd.Flags().StringVarP(&o.Chart, "chart", "c", "", "the chart name to template. Defaults to 'charts/$name'")
cmd.Flags().StringArrayVarP(&o.ValuesFiles, "values", "f", nil, "the helm values.yaml file used to template values in the generated template")
cmd.Flags().StringVarP(&o.Version, "version", "v", "", "the version of the helm chart to use. If not specified then the latest one is used")
cmd.Flags().StringVarP(&o.Repository, "repository", "r", "", "the helm chart repository to locate the chart")
cmd.Flags().StringVarP(&o.GitCommitMessage, "commit-message", "", "chore: generated kubernetes resources from helm chart", "the git commit message used")
o.AddFlags(cmd)
return cmd, o
}
func (o *TemplateOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.DefaultDomain, "domain", "", "cluster.local", "the default domain name in the generated ingress")
cmd.Flags().BoolVarP(&o.DoGitCommit, "git-commit", "", false, "if set then the template command will git commit any changed files")
cmd.Flags().BoolVarP(&o.NoSplit, "no-split", "", false, "if set then disable splitting of multiple resources into separate files")
cmd.Flags().BoolVarP(&o.NoExtSecrets, "no-external-secrets", "", false, "if set then disable converting Secret resources to ExternalSecrets")
cmd.Flags().BoolVarP(&o.IncludeCRDs, "include-crds", "", true, "if CRDs should be included in the output")
cmd.Flags().BoolVarP(&o.CheckExists, "optional", "", false, "check if there is a charts dir and if not do nothing if it does not exist")
}
// Run implements the command
func (o *TemplateOptions) Run() error {
if o.CommandRunner == nil {
o.CommandRunner = cmdrunner.DefaultCommandRunner
}
var err error
bin := o.HelmBinary
if bin == "" {
bin, err = plugins.GetHelmBinary(plugins.HelmVersion)
if err != nil {
return err
}
}
name := o.ReleaseName
if name == "" {
name = os.Getenv("APP_NAME")
if name == "" {
name = os.Getenv("REPO_NAME")
if name == "" {
return options.MissingOption("name")
}
}
}
chart := o.Chart
if chart == "" {
chart = filepath.Join("charts", name)
}
if o.Repository == "" {
exists, err := files.DirExists(chart)
if err != nil {
return errors.Wrapf(err, "failed to check if dir exists %s", chart)
}
if !exists {
if o.CheckExists {
log.Logger().Infof("no charts dir so doing nothing %s", chart)
return nil
}
return errors.Errorf("there is no chart at %s - you could try supply --chart", chart)
}
}
outDir := o.OutDir
if outDir == "" {
outDir = filepath.Join(chart, "resources")
}
err = os.MkdirAll(outDir, files.DefaultDirWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to ensure output directory exists %s", outDir)
}
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
return errors.Wrap(err, "failed to create temporary directory")
}
tmpChartDir := ""
if o.Repository != "" {
tmpChartDir, err = ioutil.TempDir("", "")
if err != nil {
return errors.Wrap(err, "failed to create temporary chart directory")
}
// lets fetch the chart
args := []string{"fetch", "--untar", "--repo", o.Repository}
if o.Version != "" {
args = append(args, "--version", o.Version)
}
args = append(args, name)
c := &cmdrunner.Command{
Name: bin,
Args: args,
Dir: tmpChartDir,
Out: os.Stdout,
Err: os.Stderr,
}
_, err = o.CommandRunner(c)
if err != nil {
return errors.Wrapf(err, "failed to run %s", c.CLI())
}
}
cmdDir := ""
args := []string{"template", "--output-dir", tmpDir}
for _, valuesFile := range o.ValuesFiles {
args = append(args, "--values", valuesFile)
}
if o.Repository != "" {
args = append(args, "--repo", o.Repository)
cmdDir = tmpChartDir
}
if o.Namespace != "" {
args = append(args, "--namespace", o.Namespace)
}
if o.Version != "" {
args = append(args, "--version", o.Version)
}
if o.IncludeCRDs {
args = append(args, "--include-crds")
}
args = append(args, name, chart)
c := &cmdrunner.Command{
Name: bin,
Args: args,
Dir: cmdDir,
Out: os.Stdout,
Err: os.Stderr,
}
results, err := o.CommandRunner(c)
if err != nil {
return errors.Wrapf(err, "failed to run %s got: %s", c.CLI(), results)
}
// now lets copy the templates from the temp dir to the outDir
crdsDir := filepath.Join(tmpDir, name, "crds")
exists, err := files.DirExists(crdsDir)
if err != nil {
return errors.Wrapf(err, "failed to check if crds dir was generated")
}
if exists {
err = files.CopyDirOverwrite(crdsDir, outDir)
if err != nil {
return errors.Wrapf(err, "failed to copy generated crds at %s to %s", crdsDir, outDir)
}
}
templatesDir := filepath.Join(tmpDir, name, "templates")
exists, err = files.DirExists(templatesDir)
if err != nil {
return errors.Wrapf(err, "failed to check if templates dir was generated")
}
if !exists {
return errors.Errorf("no templates directory was created at %s", templatesDir)
}
err = files.CopyDirOverwrite(templatesDir, outDir)
if err != nil {
return errors.Wrapf(err, "failed to copy generated templates at %s to %s", templatesDir, outDir)
}
// lets copy all dependent chart templates folders
dependentChartsDir := filepath.Join(tmpDir, name, "charts")
exists, err = files.DirExists(dependentChartsDir)
if err != nil {
return errors.Wrapf(err, "failed to check if charts dir was generated")
}
if exists {
depChartDirs, err := ioutil.ReadDir(dependentChartsDir)
if err != nil {
return errors.Wrapf(err, "failed to read sub chart dirs in %s", dependentChartsDir)
}
for _, f := range depChartDirs {
depTemplateDir := filepath.Join(dependentChartsDir, f.Name(), "templates")
exists, err = files.DirExists(depTemplateDir)
if err != nil {
return errors.Wrapf(err, "failed to check if templates dir was generated for %s", depTemplateDir)
}
if exists {
depOutDir := filepath.Join(outDir, f.Name())
err = files.CopyDirOverwrite(depTemplateDir, depOutDir)
if err != nil {
return errors.Wrapf(err, "failed to copy generated templates at %s to %s", depTemplateDir, depOutDir)
}
}
}
}
err = os.RemoveAll(tmpDir)
if err != nil {
return errors.Wrapf(err, "failed to remove tmp dir %s", tmpDir)
}
if !o.NoSplit {
so := &split.Options{
Dir: outDir,
}
err = so.Run()
if err != nil {
return errors.Wrapf(err, "failed to split YAML files at %s", outDir)
}
}
if !o.DoGitCommit {
return nil
}
log.Logger().Infof("performing git commit: %s", o.GitCommitMessage)
return o.GitCommit(outDir, o.GitCommitMessage)
}
func (o *TemplateOptions) GitCommit(outDir string, commitMessage string) error {
gitter := o.Git()
_, err := gitter.Command(outDir, "add", "*")
if err != nil {
return errors.Wrapf(err, "failed to add generated resources to git in dir %s", outDir)
}
err = gitclient.CommitIfChanges(gitter, outDir, commitMessage)
if err != nil {
return errors.Wrapf(err, "failed to commit generated resources to git in dir %s", outDir)
}
return nil
}
// Git returns the gitter - lazily creating one if required
func (o *TemplateOptions) Git() gitclient.Interface {
if o.Gitter == nil {
o.Gitter = cli.NewCLIClient("", o.CommandRunner)
}
return o.Gitter
}