Skip to content

Commit be5ae8d

Browse files
committed
commands/operator-sdk/cmd/*,pkg/scaffold/constants.go: use scaffold constants instead of hard-coded strings for dirs and filenames
1 parent 2477da4 commit be5ae8d

File tree

7 files changed

+41
-42
lines changed

7 files changed

+41
-42
lines changed

commands/operator-sdk/cmd/add/crd.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ import (
2828
"github.com/spf13/cobra"
2929
)
3030

31-
const (
32-
goDir = "GOPATH"
33-
deployCrdDir = "deploy"
34-
)
35-
3631
// NewAddCrdCmd - add crd command
3732
func NewAddCrdCmd() *cobra.Command {
3833
crdCmd := &cobra.Command{
@@ -111,8 +106,8 @@ func verifyCrdDeployPath() {
111106
log.Fatalf("failed to determine the full path of the current directory: %v", err)
112107
}
113108
// check if the deploy sub-directory exist
114-
_, err = os.Stat(filepath.Join(wd, deployCrdDir))
109+
_, err = os.Stat(filepath.Join(wd, scaffold.DeployDir))
115110
if err != nil {
116-
log.Fatalf("the path (./%v) does not exist. run this command in your project directory", deployCrdDir)
111+
log.Fatalf("the path (./%v) does not exist. run this command in your project directory", scaffold.DeployDir)
117112
}
118113
}

commands/operator-sdk/cmd/build.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ func verifyTestManifest(image string) {
132132
}
133133
}
134134

135-
const (
136-
mainGo = "./cmd/manager/main.go"
137-
)
138-
139135
func buildFunc(cmd *cobra.Command, args []string) {
140136
if len(args) != 1 {
141137
log.Fatalf("build command needs exactly 1 argument")
@@ -150,8 +146,8 @@ func buildFunc(cmd *cobra.Command, args []string) {
150146

151147
// Don't need to buld go code if Ansible Operator
152148
if mainExists() {
153-
managerDir := filepath.Join(cmdutil.CheckAndGetCurrPkg(), "cmd/manager")
154-
outputBinName := filepath.Join(wd, "build/_output/bin", filepath.Base(wd))
149+
managerDir := filepath.Join(cmdutil.CheckAndGetCurrPkg(), scaffold.ManagerDir)
150+
outputBinName := filepath.Join(wd, scaffold.BuildBinDir, filepath.Base(wd))
155151
buildCmd := exec.Command("go", "build", "-o", outputBinName, managerDir)
156152
buildCmd.Env = goBuildEnv
157153
o, err := buildCmd.CombinedOutput()
@@ -178,15 +174,17 @@ func buildFunc(cmd *cobra.Command, args []string) {
178174
fmt.Fprintln(os.Stdout, string(o))
179175

180176
if enableTests {
181-
buildTestCmd := exec.Command("go", "test", "-c", "-o", filepath.Join(wd, "build/_output/bin", filepath.Base(wd)+"-test"), testLocationBuild+"/...")
177+
testBinary := filepath.Join(wd, scaffold.BuildBinDir, filepath.Base(wd)+"-test")
178+
buildTestCmd := exec.Command("go", "test", "-c", "-o", testBinary, testLocationBuild+"/...")
182179
buildTestCmd.Env = goBuildEnv
183180
o, err := buildTestCmd.CombinedOutput()
184181
if err != nil {
185182
log.Fatalf("failed to build test binary: %v (%v)", err, string(o))
186183
}
187184
fmt.Fprintln(os.Stdout, string(o))
188185
// if a user is using an older sdk repo as their library, make sure they have required build files
189-
_, err = os.Stat("build/test-framework/Dockerfile")
186+
testDockerfile := filepath.Join(scaffold.BuildTestDir, scaffold.DockerfileFile)
187+
_, err = os.Stat(testDockerfile)
190188
if err != nil && os.IsNotExist(err) {
191189

192190
absProjectPath := cmdutil.MustGetwd()
@@ -207,7 +205,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
207205
}
208206
}
209207

210-
testDbcmd := exec.Command("docker", "build", ".", "-f", "build/test-framework/Dockerfile", "-t", image, "--build-arg", "NAMESPACEDMAN="+namespacedManBuild, "--build-arg", "BASEIMAGE="+baseImageName)
208+
testDbcmd := exec.Command("docker", "build", ".", "-f", testDockerfile, "-t", image, "--build-arg", "NAMESPACEDMAN="+namespacedManBuild, "--build-arg", "BASEIMAGE="+baseImageName)
211209
o, err = testDbcmd.CombinedOutput()
212210
if err != nil {
213211
log.Fatalf("failed to output build image %s: %v (%s)", image, err, string(o))
@@ -219,8 +217,6 @@ func buildFunc(cmd *cobra.Command, args []string) {
219217
}
220218

221219
func mainExists() bool {
222-
if _, err := os.Stat(mainGo); err == nil {
223-
return true
224-
}
225-
return false
220+
_, err := os.Stat(filepath.Join(scaffold.ManagerDir, scaffold.CmdFile))
221+
return err == nil
226222
}

commands/operator-sdk/cmd/generate/k8s.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func K8sCodegen() {
5151
cmdutil.MustInProjectRoot()
5252
repoPkg := cmdutil.CheckAndGetCurrPkg()
5353
outputPkg := filepath.Join(repoPkg, "pkg/generated")
54-
apisPkg := filepath.Join(repoPkg, "pkg/apis")
54+
apisPkg := filepath.Join(repoPkg, scaffold.ApisDir)
5555
groupVersions, err := parseGroupVersions()
5656
if err != nil {
5757
log.Fatalf("failed to parse group versions: (%v)", err)
@@ -79,16 +79,15 @@ func K8sCodegen() {
7979
// in the format "groupA:v1,v2 groupB:v1 groupC:v2",
8080
// as required by the generate-groups.sh script
8181
func parseGroupVersions() (string, error) {
82-
groupVersions := ""
83-
apisDir := filepath.Join("pkg", "apis")
84-
groups, err := ioutil.ReadDir(apisDir)
82+
var groupVersions string
83+
groups, err := ioutil.ReadDir(scaffold.ApisDir)
8584
if err != nil {
8685
return "", fmt.Errorf("could not read pkg/apis directory to find api Versions: %v", err)
8786
}
8887

8988
for _, g := range groups {
9089
if g.IsDir() {
91-
groupDir := filepath.Join(apisDir, g.Name())
90+
groupDir := filepath.Join(scaffold.ApisDir, g.Name())
9291
versions, err := ioutil.ReadDir(groupDir)
9392
if err != nil {
9493
return "", fmt.Errorf("could not read %s directory to find api Versions: %v", groupDir, err)

commands/operator-sdk/cmd/test/cluster.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222
"time"
2323

24+
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2425
"github.com/operator-framework/operator-sdk/pkg/test"
2526

2627
"github.com/spf13/cobra"
@@ -89,7 +90,7 @@ func testClusterFunc(cmd *cobra.Command, args []string) error {
8990
Name: "operator-test",
9091
Image: args[0],
9192
ImagePullPolicy: pullPolicy,
92-
Command: []string{"/go-test.sh"},
93+
Command: []string{"/" + scaffold.GoTestScriptFile},
9394
Env: []v1.EnvVar{{
9495
Name: test.TestNamespaceEnv,
9596
ValueFrom: &v1.EnvVarSource{FieldRef: &v1.ObjectFieldSelector{FieldPath: "metadata.namespace"}},

commands/operator-sdk/cmd/test/local.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ import (
2424
"strings"
2525

2626
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
27+
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2728
"github.com/operator-framework/operator-sdk/pkg/test"
2829

2930
"github.com/spf13/cobra"
3031
)
3132

33+
var deployTestDir = filepath.Join(scaffold.DeployDir, "test")
34+
3235
type testLocalConfig struct {
3336
kubeconfig string
3437
globalManPath string
@@ -65,25 +68,26 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
6568
}
6669
// if no namespaced manifest path is given, combine deploy/service_account.yaml, deploy/role.yaml, deploy/role_binding.yaml and deploy/operator.yaml
6770
if tlConfig.namespacedManPath == "" {
68-
err := os.MkdirAll("deploy/test", os.FileMode(cmdutil.DefaultDirFileMode))
71+
err := os.MkdirAll(deployTestDir, os.FileMode(cmdutil.DefaultDirFileMode))
6972
if err != nil {
70-
log.Fatalf("could not create deploy/test: %v", err)
73+
log.Fatalf("could not create %s: %v", deployTestDir, err)
7174
}
72-
tlConfig.namespacedManPath = "deploy/test/namespace-manifests.yaml"
75+
tlConfig.namespacedManPath = filepath.Join(deployTestDir, "namespace-manifests.yaml")
7376

74-
sa, err := ioutil.ReadFile("deploy/service_account.yaml")
77+
saFile := filepath.Join(scaffold.DeployDir, scaffold.ServiceAccountYamlFile)
78+
sa, err := ioutil.ReadFile(saFile)
7579
if err != nil {
76-
log.Fatalf("could not find the manifest deploy/service_account.yaml: %v", err)
80+
log.Fatalf("could not find the manifest %s: %v", saFile, err)
7781
}
78-
role, err := ioutil.ReadFile("deploy/role.yaml")
82+
role, err := ioutil.ReadFile(filepath.Join(scaffold.DeployDir, scaffold.RoleYamlFile))
7983
if err != nil {
8084
log.Fatalf("could not find role manifest: %v", err)
8185
}
82-
roleBinding, err := ioutil.ReadFile("deploy/role_binding.yaml")
86+
roleBinding, err := ioutil.ReadFile(filepath.Join(scaffold.DeployDir, scaffold.RoleBindingYamlFile))
8387
if err != nil {
8488
log.Fatalf("could not find role_binding manifest: %v", err)
8589
}
86-
operator, err := ioutil.ReadFile("deploy/operator.yaml")
90+
operator, err := ioutil.ReadFile(filepath.Join(scaffold.DeployDir, scaffold.OperatorYamlFile))
8791
if err != nil {
8892
log.Fatalf("could not find operator manifest: %v", err)
8993
}
@@ -105,21 +109,21 @@ func testLocalFunc(cmd *cobra.Command, args []string) {
105109
}()
106110
}
107111
if tlConfig.globalManPath == "" {
108-
err := os.MkdirAll("deploy/test", os.FileMode(cmdutil.DefaultDirFileMode))
112+
err := os.MkdirAll(deployTestDir, os.FileMode(cmdutil.DefaultDirFileMode))
109113
if err != nil {
110-
log.Fatalf("could not create deploy/test: %v", err)
114+
log.Fatalf("could not create %s: %v", deployTestDir, err)
111115
}
112-
tlConfig.globalManPath = "deploy/test/global-manifests.yaml"
113-
files, err := ioutil.ReadDir("deploy/crds")
116+
tlConfig.globalManPath = filepath.Join(deployTestDir, "global-manifests.yaml")
117+
files, err := ioutil.ReadDir(scaffold.CrdsDir)
114118
if err != nil {
115119
log.Fatalf("could not read deploy directory: %v", err)
116120
}
117121
var combined []byte
118122
for _, file := range files {
119123
if strings.HasSuffix(file.Name(), "crd.yaml") {
120-
fileBytes, err := ioutil.ReadFile(filepath.Join("deploy/crds", file.Name()))
124+
fileBytes, err := ioutil.ReadFile(filepath.Join(scaffold.CrdsDir, file.Name()))
121125
if err != nil {
122-
log.Fatalf("could not read file deploy/crds/%s: %v", file.Name(), err)
126+
log.Fatalf("could not read file %s: %v", filepath.Join(scaffold.CrdsDir, file.Name()), err)
123127
}
124128
if combined == nil {
125129
combined = []byte{}

commands/operator-sdk/cmd/up/local.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ import (
3030
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
3131
ansibleOperator "github.com/operator-framework/operator-sdk/pkg/ansible/operator"
3232
proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy"
33+
"github.com/operator-framework/operator-sdk/pkg/scaffold"
34+
ansibleScaffold "github.com/operator-framework/operator-sdk/pkg/scaffold/ansible"
3335
"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
3436
sdkVersion "github.com/operator-framework/operator-sdk/version"
37+
3538
"github.com/sirupsen/logrus"
3639
"github.com/spf13/cobra"
3740
"sigs.k8s.io/controller-runtime/pkg/client/config"
@@ -97,7 +100,7 @@ func mustKubeConfig() {
97100
}
98101

99102
func upLocal() {
100-
args := []string{"run", filepath.Join("cmd", "manager", "main.go")}
103+
args := []string{"run", filepath.Join(scaffold.ManagerDir, scaffold.CmdFile)}
101104
if operatorFlags != "" {
102105
extraArgs := strings.Split(operatorFlags, " ")
103106
args = append(args, extraArgs...)
@@ -143,7 +146,7 @@ func upLocalAnsible() {
143146
}
144147

145148
// start the operator
146-
go ansibleOperator.Run(done, mgr, "./watches.yaml", time.Minute)
149+
go ansibleOperator.Run(done, mgr, "./"+ansibleScaffold.WatchesYamlFile, time.Minute)
147150

148151
// wait for either to finish
149152
err = <-done

pkg/scaffold/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
ControllerDir = PkgDir + filePathSep + "controller"
3131
BuildDir = "build"
3232
BuildTestDir = BuildDir + filePathSep + "test-framework"
33+
BuildBinDir = BuildDir + filePathSep + "_output" + filePathSep + "bin"
3334
DeployDir = "deploy"
3435
OlmCatalogDir = DeployDir + filePathSep + "olm-catalog"
3536
CrdsDir = DeployDir + filePathSep + "crds"

0 commit comments

Comments
 (0)