/
cmd.go
339 lines (296 loc) · 10.3 KB
/
cmd.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright 2018 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package new
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/operator-framework/operator-sdk/internal/flags/apiflags"
"github.com/operator-framework/operator-sdk/internal/genutil"
"github.com/operator-framework/operator-sdk/internal/scaffold"
"github.com/operator-framework/operator-sdk/internal/scaffold/ansible"
"github.com/operator-framework/operator-sdk/internal/scaffold/helm"
"github.com/operator-framework/operator-sdk/internal/scaffold/input"
"github.com/operator-framework/operator-sdk/internal/util/projutil"
)
func NewCmd() *cobra.Command { //nolint:golint
/*
The nolint here is used to hide the warning
"func name will be used as new.NewCmd by other packages,
and that stutters; consider calling this Cmd"
which is a false positive.
*/
newCmd := &cobra.Command{
Use: "new <project-name>",
Short: "Creates a new operator application",
Long: `The operator-sdk new command creates a new operator application and
generates a default directory layout based on the input <project-name>.
<project-name> is the project name of the new operator. (e.g app-operator)
`,
Example: ` # Create a new project directory
$ mkdir $HOME/projects/example.com/
$ cd $HOME/projects/example.com/
# Ansible project
$ operator-sdk new app-operator --type=ansible \
--api-version=app.example.com/v1alpha1 \
--kind=AppService
# Helm project
$ operator-sdk new app-operator --type=helm \
--api-version=app.example.com/v1alpha1 \
--kind=AppService
$ operator-sdk new app-operator --type=helm \
--api-version=app.example.com/v1alpha1 \
--kind=AppService \
--helm-chart=myrepo/app
$ operator-sdk new app-operator --type=helm \
--helm-chart=myrepo/app
$ operator-sdk new app-operator --type=helm \
--helm-chart=myrepo/app \
--helm-chart-version=1.2.3
$ operator-sdk new app-operator --type=helm \
--helm-chart=app \
--helm-chart-repo=https://charts.mycompany.com/
$ operator-sdk new app-operator --type=helm \
--helm-chart=app \
--helm-chart-repo=https://charts.mycompany.com/ \
--helm-chart-version=1.2.3
$ operator-sdk new app-operator --type=helm \
--helm-chart=/path/to/local/chart-directories/app/
$ operator-sdk new app-operator --type=helm \
--helm-chart=/path/to/local/chart-archives/app-1.2.3.tgz
`,
RunE: newFunc,
}
newCmd.Flags().StringVar(&operatorType, "type", "",
"Type of operator to initialize (choices: \"ansible\" or \"helm\")")
if err := newCmd.MarkFlagRequired("type"); err != nil {
log.Fatalf("Failed to mark `type` flag for `new` subcommand as required")
}
// todo(camilamacedo86): remove before 1.0.0
newCmd.Flags().BoolVar(&gitInit, "git-init", false,
"Initialize the project directory as a git repository (default false)")
err := newCmd.Flags().MarkDeprecated("git-init",
"instead run `git init` once your project is created to use git")
if err != nil {
log.Fatal(err)
}
newCmd.Flags().BoolVar(&generatePlaybook, "generate-playbook", false,
"Generate a playbook skeleton. (Only used for --type ansible)")
// Initialize flagSet struct with common flags
apiFlags.AddTo(newCmd.Flags())
return newCmd
}
var (
apiFlags apiflags.APIFlags
operatorType string
projectName string
gitInit bool
generatePlaybook bool
)
func newFunc(cmd *cobra.Command, args []string) error {
if err := parse(cmd, args); err != nil {
return err
}
mustBeNewProject()
if err := verifyFlags(); err != nil {
return err
}
log.Infof("Creating new %s operator '%s'.", strings.Title(operatorType), projectName)
switch operatorType {
case projutil.OperatorTypeAnsible:
if err := doAnsibleScaffold(); err != nil {
log.Fatal(err)
}
case projutil.OperatorTypeHelm:
// create the project dir
err := os.MkdirAll(projectName, 0755)
if err != nil {
log.Fatal(err)
}
// go inside of the project dir
err = os.Chdir(filepath.Join(projutil.MustGetwd(), projectName))
if err != nil {
log.Fatal(err)
}
cfg := input.Config{
AbsProjectPath: filepath.Join(projutil.MustGetwd()),
ProjectName: projectName,
}
createOpts := helm.CreateChartOptions{
ResourceAPIVersion: apiFlags.APIVersion,
ResourceKind: apiFlags.Kind,
Chart: apiFlags.HelmChartRef,
Version: apiFlags.HelmChartVersion,
Repo: apiFlags.HelmChartRepo,
CRDVersion: apiFlags.CrdVersion,
}
if err := helm.Init(cfg, createOpts); err != nil {
log.Fatal(err)
}
}
//todo: remove before 1.0.0
if gitInit {
if err := initGit(); err != nil {
log.Fatal(err)
}
}
log.Info("Project creation complete.")
return nil
}
func parse(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("command %s requires exactly one argument", cmd.CommandPath())
}
projectName = args[0]
if len(projectName) == 0 {
return fmt.Errorf("project name must not be empty")
}
return nil
}
// mustBeNewProject checks if the given project exists under the current diretory.
// it exits with error when the project exists.
func mustBeNewProject() {
fp := filepath.Join(projutil.MustGetwd(), projectName)
stat, err := os.Stat(fp)
if err != nil && os.IsNotExist(err) {
return
}
if err != nil {
log.Fatalf("Failed to determine if project (%v) exists", projectName)
}
if stat.IsDir() {
log.Fatalf("Project (%v) in (%v) path already exists. Please use a different project name or delete "+
"the existing one", projectName, fp)
}
}
func doAnsibleScaffold() error {
cfg := &input.Config{
AbsProjectPath: filepath.Join(projutil.MustGetwd(), projectName),
ProjectName: projectName,
}
resource, err := scaffold.NewResource(apiFlags.APIVersion, apiFlags.Kind)
if err != nil {
return fmt.Errorf("invalid apiVersion and kind: %v", err)
}
roleFiles := ansible.RolesFiles{Resource: *resource}
roleTemplates := ansible.RolesTemplates{Resource: *resource}
s := &scaffold.Scaffold{}
err = s.Execute(cfg,
&scaffold.ServiceAccount{},
&scaffold.Role{},
&scaffold.RoleBinding{},
&scaffold.CR{Resource: resource},
&ansible.BuildDockerfile{GeneratePlaybook: generatePlaybook},
&ansible.RolesReadme{Resource: *resource},
&ansible.RolesMetaMain{Resource: *resource},
&roleFiles,
&roleTemplates,
&ansible.RolesVarsMain{Resource: *resource},
&ansible.MoleculeTestLocalConverge{Resource: *resource},
&ansible.RolesDefaultsMain{Resource: *resource},
&ansible.RolesTasksMain{Resource: *resource},
&ansible.MoleculeDefaultMolecule{},
&ansible.MoleculeDefaultPrepare{},
&ansible.MoleculeDefaultConverge{
GeneratePlaybook: generatePlaybook,
Resource: *resource,
},
&ansible.MoleculeDefaultVerify{},
&ansible.RolesHandlersMain{Resource: *resource},
&ansible.Watches{
GeneratePlaybook: generatePlaybook,
Resource: *resource,
},
&ansible.DeployOperator{},
&ansible.Travis{},
&ansible.RequirementsYml{},
&ansible.MoleculeTestLocalMolecule{},
&ansible.MoleculeTestLocalPrepare{},
&ansible.MoleculeTestLocalVerify{},
&ansible.MoleculeClusterMolecule{Resource: *resource},
&ansible.MoleculeClusterCreate{},
&ansible.MoleculeClusterPrepare{Resource: *resource},
&ansible.MoleculeClusterConverge{},
&ansible.MoleculeClusterVerify{Resource: *resource},
&ansible.MoleculeClusterDestroy{Resource: *resource},
&ansible.MoleculeTemplatesOperator{},
)
if err != nil {
return fmt.Errorf("new ansible scaffold failed: %v", err)
}
if err = genutil.GenerateCRDNonGo(projectName, *resource, apiFlags.CrdVersion); err != nil {
return err
}
// Remove placeholders from empty directories
err = os.Remove(filepath.Join(s.AbsProjectPath, roleFiles.Path))
if err != nil {
return fmt.Errorf("new ansible scaffold failed: %v", err)
}
err = os.Remove(filepath.Join(s.AbsProjectPath, roleTemplates.Path))
if err != nil {
return fmt.Errorf("new ansible scaffold failed: %v", err)
}
// Decide on playbook.
if generatePlaybook {
log.Infof("Generating %s playbook.", strings.Title(operatorType))
err := s.Execute(cfg,
&ansible.Playbook{Resource: *resource},
)
if err != nil {
return fmt.Errorf("new ansible playbook scaffold failed: %v", err)
}
}
// update deploy/role.yaml for the given resource r.
if err := scaffold.UpdateRoleForResource(resource, cfg.AbsProjectPath); err != nil {
return fmt.Errorf("failed to update the RBAC manifest for the resource (%v, %v): %v",
resource.APIVersion, resource.Kind, err)
}
return nil
}
func verifyFlags() error {
if operatorType != projutil.OperatorTypeAnsible && operatorType != projutil.OperatorTypeHelm {
return fmt.Errorf("value of --type can only be `ansible`, or `helm`: %v",
projutil.ErrUnknownOperatorType{Type: operatorType})
}
if operatorType != projutil.OperatorTypeAnsible && generatePlaybook {
return fmt.Errorf("value of --generate-playbook can only be used with --type `ansible`")
}
if err := apiFlags.VerifyCommonFlags(operatorType); err != nil {
return err
}
return nil
}
// todo(camilamacedo86): remove before 1.0.0
// Deprecated: the git-init flag was deprecated since has no need to make the command run the git init.
// users are allowed to easily do that when they wish. This func is just used here to run the git-init
func execProjCmd(cmd string, args ...string) error {
dc := exec.Command(cmd, args...)
dc.Dir = filepath.Join(projutil.MustGetwd(), projectName)
return projutil.ExecCmd(dc)
}
// todo(camilamacedo86): remove before 1.0.0
// Deprecated: the git-init flag was deprecated since has no need to make the command run the git init.
// users are allowed to easily do that when they wish.
func initGit() error {
log.Info("Running git init")
if err := execProjCmd("git", "init"); err != nil {
return fmt.Errorf("failed to run git init: %v", err)
}
log.Info("Run git init done")
return nil
}