Skip to content

Commit 2a3e6bc

Browse files
authored
commands/operator-sdk/cmd: create operator.yaml in new command
* Only create new operator.yaml if file doesnt exist * accept optional template param * Fix length of args check * Changed for new logic * remove extra gen function * move operator to new from build * go vet mistake * change order * some config garbage * edit operator render func * adjust filepath * missing slash fp * test with file check * roll operator yaml in deploy render * try without config loading * clean up code * update readme * update changelog * update readme * change sed delimiter * remove unused var
1 parent 7e6987c commit 2a3e6bc

File tree

7 files changed

+21
-20
lines changed

7 files changed

+21
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
### Added
44
### Removed
55
### Changed
6+
7+
- Moved the rendering of `deploy/operator.yaml` to the `operator-sdk new` command instead of `operator-sdk build`
8+
69
### Fixed
710
### Deprecated
811
### Security

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ $ cd app-operator
6565
$ operator-sdk build quay.io/example/app-operator
6666
$ docker push quay.io/example/app-operator
6767

68+
# Update the operator manifest to use the built image name
69+
$ sed -i 's|REPLACE_IMAGE|quay.io/example/app-operator|g' deploy/operator.yaml
70+
6871
# Deploy the app-operator
6972
$ kubectl create -f deploy/rbac.yaml
7073
$ kubectl create -f deploy/crd.yaml

commands/operator-sdk/cmd/build.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ import (
1919
"os"
2020
"os/exec"
2121

22-
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil"
2322
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"
24-
"github.com/operator-framework/operator-sdk/pkg/generator"
2523

2624
"github.com/spf13/cobra"
2725
)
@@ -54,7 +52,7 @@ const (
5452

5553
func buildFunc(cmd *cobra.Command, args []string) {
5654
if len(args) != 1 {
57-
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("build command needs 1 argument."))
55+
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("build command needs exactly 1 argument"))
5856
}
5957

6058
bcmd := exec.Command(build)
@@ -72,9 +70,4 @@ func buildFunc(cmd *cobra.Command, args []string) {
7270
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to output build image %v: (%v)", image, string(o)))
7371
}
7472
fmt.Fprintln(os.Stdout, string(o))
75-
76-
c := cmdutil.GetConfig()
77-
if err = generator.RenderOperatorYaml(c, image); err != nil {
78-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate deploy/operator.yaml: (%v)", err))
79-
}
8073
}

commands/operator-sdk/cmd/new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const (
7474

7575
func newFunc(cmd *cobra.Command, args []string) {
7676
if len(args) != 1 {
77-
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("new command needs 1 argument."))
77+
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("new command needs 1 argument"))
7878
}
7979
parse(args)
8080
mustBeNewProject()

doc/user-guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ Run as pod inside a Kubernetes cluster is preferred for production use.
131131
Build the memcached-operator image and push it to a registry:
132132
```
133133
$ operator-sdk build quay.io/example/memcached-operator:v0.0.1
134+
$ sed -i 's|REPLACE_IMAGE|quay.io/example/memcached-operator:v0.0.1|g' deploy/operator.yaml
134135
$ docker push quay.io/example/memcached-operator:v0.0.1
135136
```
136137

pkg/generator/generator.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const (
5858
gopkgtoml = "Gopkg.toml"
5959
gopkglock = "Gopkg.lock"
6060
config = "config.yaml"
61-
operatorYaml = deployDir + "/operator.yaml"
6261
rbacYaml = "rbac.yaml"
6362
crYaml = "cr.yaml"
6463
catalogPackageYaml = "package.yaml"
@@ -233,19 +232,18 @@ func renderDeployFiles(deployDir, projectName, apiVersion, kind string) error {
233232
APIVersion: apiVersion,
234233
Kind: kind,
235234
}
236-
return renderWriteFile(filepath.Join(deployDir, crYaml), crTmplName, crYamlTmpl, crTd)
237-
}
235+
if err := renderWriteFile(filepath.Join(deployDir, crYaml), crTmplName, crYamlTmpl, crTd); err != nil {
236+
return err
237+
}
238238

239-
// RenderOperatorYaml generates "deploy/operator.yaml"
240-
func RenderOperatorYaml(c *Config, image string) error {
241-
td := tmplData{
242-
ProjectName: c.ProjectName,
243-
Image: image,
239+
opTd := tmplData{
240+
ProjectName: projectName,
241+
Image: "REPLACE_IMAGE",
244242
MetricsPort: k8sutil.PrometheusMetricsPort,
245243
MetricsPortName: k8sutil.PrometheusMetricsPortName,
246244
OperatorNameEnv: k8sutil.OperatorNameEnvVar,
247245
}
248-
return renderWriteFile(operatorYaml, operatorTmplName, operatorYamlTmpl, td)
246+
return renderWriteFile(filepath.Join(deployDir, "operator.yaml"), operatorTmplName, operatorYamlTmpl, opTd)
249247
}
250248

251249
// RenderOlmCatalog generates catalog manifests "deploy/olm-catalog/*"

test/e2e/memcached_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,11 @@ func MemcachedCluster(t *testing.T) {
220220
if err != nil {
221221
t.Fatalf("error: %v\nCommand Output: %s\n", err, string(cmdOut))
222222
}
223+
224+
operatorYAML, err := ioutil.ReadFile("deploy/operator.yaml")
225+
operatorYAML = bytes.Replace(operatorYAML, []byte("REPLACE_IMAGE"), []byte(*f.ImageName), 1)
226+
223227
if local {
224-
operatorYAML, err := ioutil.ReadFile("deploy/operator.yaml")
225228
if err != nil {
226229
t.Fatal(err)
227230
}
@@ -250,7 +253,7 @@ func MemcachedCluster(t *testing.T) {
250253
t.Log("Created rbac")
251254

252255
// create operator
253-
operatorYAML, err := ioutil.ReadFile("deploy/operator.yaml")
256+
operatorYAML, err = ioutil.ReadFile("deploy/operator.yaml")
254257
if err != nil {
255258
t.Fatal(err)
256259
}

0 commit comments

Comments
 (0)