Skip to content

Commit 6df8bfe

Browse files
committed
Add --type flag to new command
Break out Ansible Operator logic from main func More updates Finish work on --type ansible Add build command Add Ansible Galaxy init Add --generate-playbook flag small updates Fix build command to detect cmd folder move stuff around so we can clean up tmp Make default not generate a playbook Add removing of temp init dir Fix generator test Add test for ansibleOperator yaml generation Add more tests and fix whitespace on Dockerfile
1 parent 23329a2 commit 6df8bfe

File tree

5 files changed

+385
-40
lines changed

5 files changed

+385
-40
lines changed

commands/operator-sdk/cmd/build.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"os"
2020
"os/exec"
21+
"strings"
2122

2223
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"
2324

@@ -48,26 +49,43 @@ const (
4849
build = "./tmp/build/build.sh"
4950
dockerBuild = "./tmp/build/docker_build.sh"
5051
configYaml = "./config/config.yaml"
52+
mainGo = "./cmd/%s/main.go"
5153
)
5254

5355
func buildFunc(cmd *cobra.Command, args []string) {
5456
if len(args) != 1 {
5557
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("build command needs exactly 1 argument"))
5658
}
5759

58-
bcmd := exec.Command(build)
59-
o, err := bcmd.CombinedOutput()
60-
if err != nil {
61-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to build: (%v)", string(o)))
60+
// Don't need to buld go code if Ansible Operator
61+
if buildCmd() {
62+
bcmd := exec.Command(build)
63+
o, err := bcmd.CombinedOutput()
64+
if err != nil {
65+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to build: (%v)", string(o)))
66+
}
67+
fmt.Fprintln(os.Stdout, string(o))
6268
}
63-
fmt.Fprintln(os.Stdout, string(o))
6469

6570
image := args[0]
6671
dbcmd := exec.Command(dockerBuild)
6772
dbcmd.Env = append(os.Environ(), fmt.Sprintf("IMAGE=%v", image))
68-
o, err = dbcmd.CombinedOutput()
73+
o, err := dbcmd.CombinedOutput()
6974
if err != nil {
7075
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to output build image %v: (%v)", image, string(o)))
7176
}
7277
fmt.Fprintln(os.Stdout, string(o))
7378
}
79+
80+
func buildCmd() bool {
81+
dir, err := os.Getwd()
82+
if err != nil {
83+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to get current working dir: %v", err))
84+
}
85+
dirSplit := strings.Split(dir, "/")
86+
projectName := dirSplit[len(dirSplit)-1]
87+
if _, err = os.Stat(fmt.Sprintf(mainGo, projectName)); err == nil {
88+
return true
89+
}
90+
return false
91+
}

commands/operator-sdk/cmd/new.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,29 @@ generates a skeletal app-operator application in $GOPATH/src/github.com/example.
5353
newCmd.MarkFlagRequired("api-version")
5454
newCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes CustomResourceDefintion kind. (e.g AppService)")
5555
newCmd.MarkFlagRequired("kind")
56+
newCmd.Flags().StringVar(&operatorType, "type", "go", "Type of operator to initialize (e.g \"ansible\")")
5657
newCmd.Flags().BoolVar(&skipGit, "skip-git-init", false, "Do not init the directory as a git repository")
58+
newCmd.Flags().BoolVar(&generatePlaybook, "generate-playbook", false, "Generate a playbook skeleton in watches.yaml. (Only used for --type ansible)")
5759

5860
return newCmd
5961
}
6062

6163
var (
62-
apiVersion string
63-
kind string
64-
projectName string
65-
skipGit bool
64+
apiVersion string
65+
kind string
66+
operatorType string
67+
projectName string
68+
skipGit bool
69+
generatePlaybook bool
6670
)
6771

6872
const (
69-
gopath = "GOPATH"
70-
src = "src"
71-
dep = "dep"
72-
ensureCmd = "ensure"
73+
gopath = "GOPATH"
74+
src = "src"
75+
dep = "dep"
76+
ensureCmd = "ensure"
77+
goOperatorType = "go"
78+
ansibleOperatorType = "ansible"
7379
)
7480

7581
func newFunc(cmd *cobra.Command, args []string) {
@@ -79,13 +85,15 @@ func newFunc(cmd *cobra.Command, args []string) {
7985
parse(args)
8086
mustBeNewProject()
8187
verifyFlags()
82-
g := generator.NewGenerator(apiVersion, kind, projectName, repoPath())
88+
g := generator.NewGenerator(apiVersion, kind, operatorType, projectName, repoPath(), generatePlaybook)
8389
err := g.Render()
8490
if err != nil {
8591
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to create project %v: %v", projectName, err))
8692
}
87-
pullDep()
88-
generate.K8sCodegen(projectName)
93+
if operatorType == goOperatorType {
94+
pullDep()
95+
generate.K8sCodegen(projectName)
96+
}
8997
initGit()
9098
}
9199

@@ -136,6 +144,9 @@ func verifyFlags() {
136144
if len(kind) == 0 {
137145
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must not have empty value"))
138146
}
147+
if operatorType != goOperatorType && operatorType != ansibleOperatorType {
148+
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--type can only be `go` or `ansible`"))
149+
}
139150
kindFirstLetter := string(kind[0])
140151
if kindFirstLetter != strings.ToUpper(kindFirstLetter) {
141152
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must start with an uppercase letter"))

0 commit comments

Comments
 (0)