Skip to content

Commit 419cfd3

Browse files
authored
commands,pkg/scaffold,hack,pkg/helm: run and migrate command support for helm (#897)
* [travis deploy] commands,pkg/scaffold,hack,pkg/helm: run and migrate command support for helm * CHANGELOG.md,doc,commands/.../run/helm.go: document helm support for run and migrate commands * doc/dev/testing/travis-build.md: ansible/helm e2e doc update * doc/dev/release.md: documenting new release steps for ansible/helm gopkgtoml.go
1 parent 3c2fee5 commit 419cfd3

26 files changed

+569
-208
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
### Added
44

5-
- A new command [`operator-sdk migrate`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#migrate) which adds a main.go source file and any associated source files for an operator that is not of the "go" type. ([#887](https://github.com/operator-framework/operator-sdk/pull/887))
6-
- A new command [`operator-sdk run ansible`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#ansible) which runs as an ansible operator process. This is intended to be used when running in a Pod inside a cluster. Developers wanting to run their operator locally should continue to use `up local`. ([#887](https://github.com/operator-framework/operator-sdk/pull/887))
5+
- A new command [`operator-sdk migrate`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#migrate) which adds a main.go source file and any associated source files for an operator that is not of the "go" type. ([#887](https://github.com/operator-framework/operator-sdk/pull/887) and [#897](https://github.com/operator-framework/operator-sdk/pull/897))
6+
- New commands [`operator-sdk run ansible`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#ansible) and [`operator-sdk run helm`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#helm) which run the SDK as ansible and helm operator processes, respectively. These are intended to be used when running in a Pod inside a cluster. Developers wanting to run their operator locally should continue to use `up local`. ([#887](https://github.com/operator-framework/operator-sdk/pull/887) and [#897](https://github.com/operator-framework/operator-sdk/pull/897))
77

88
### Changed
99

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ image/build: image/build/ansible image/build/helm
108108
image/build/ansible: build/operator-sdk-dev-x86_64-linux-gnu
109109
./hack/image/build-ansible-image.sh $(ANSIBLE_BASE_IMAGE):dev
110110

111-
image/build/helm:
111+
image/build/helm: build/operator-sdk-dev-x86_64-linux-gnu
112112
./hack/image/build-helm-image.sh $(HELM_BASE_IMAGE):dev
113113

114114
image/push: image/push/ansible image/push/helm

commands/operator-sdk/cmd/migrate.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2222
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2323
"github.com/operator-framework/operator-sdk/pkg/scaffold/ansible"
24+
"github.com/operator-framework/operator-sdk/pkg/scaffold/helm"
2425
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
2526

2627
log "github.com/sirupsen/logrus"
@@ -48,6 +49,8 @@ func migrateRun(cmd *cobra.Command, args []string) {
4849
switch opType {
4950
case projutil.OperatorTypeAnsible:
5051
migrateAnsible()
52+
case projutil.OperatorTypeHelm:
53+
migrateHelm()
5154
default:
5255
log.Fatalf("Operator of type %s cannot be migrated.", opType)
5356
}
@@ -76,13 +79,7 @@ func migrateAnsible() {
7679
log.Fatalf("Error trying to stat playbook.yaml: (%v)", err)
7780
}
7881

79-
dockerfilePath := filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile)
80-
newDockerfilePath := dockerfilePath + ".sdkold"
81-
err = os.Rename(dockerfilePath, newDockerfilePath)
82-
if err != nil {
83-
log.Fatalf("Failed to rename Dockerfile: (%v)", err)
84-
}
85-
log.Infof("Renamed Dockerfile to %s and replaced with newer version. Compare the new Dockerfile to your old one and manually migrate any customizations", newDockerfilePath)
82+
renameDockerfile()
8683

8784
s := &scaffold.Scaffold{}
8885
err = s.Execute(cfg,
@@ -96,3 +93,40 @@ func migrateAnsible() {
9693
log.Fatalf("Migrate scaffold failed: (%v)", err)
9794
}
9895
}
96+
97+
// migrateHelm runs the migration process for a helm-based operator
98+
func migrateHelm() {
99+
wd := projutil.MustGetwd()
100+
101+
cfg := &input.Config{
102+
AbsProjectPath: wd,
103+
ProjectName: filepath.Base(wd),
104+
}
105+
106+
renameDockerfile()
107+
108+
s := &scaffold.Scaffold{}
109+
err := s.Execute(cfg,
110+
&helm.Main{},
111+
&helm.GopkgToml{},
112+
&helm.DockerfileHybrid{
113+
Watches: true,
114+
HelmCharts: true,
115+
},
116+
&helm.Entrypoint{},
117+
&helm.UserSetup{},
118+
)
119+
if err != nil {
120+
log.Fatalf("Migrate scaffold failed: (%v)", err)
121+
}
122+
}
123+
124+
func renameDockerfile() {
125+
dockerfilePath := filepath.Join(scaffold.BuildDir, scaffold.DockerfileFile)
126+
newDockerfilePath := dockerfilePath + ".sdkold"
127+
err := os.Rename(dockerfilePath, newDockerfilePath)
128+
if err != nil {
129+
log.Fatalf("Failed to rename Dockerfile: (%v)", err)
130+
}
131+
log.Infof("Renamed Dockerfile to %s and replaced with newer version. Compare the new Dockerfile to your old one and manually migrate any customizations", newDockerfilePath)
132+
}

commands/operator-sdk/cmd/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ should use "up local" instead.`,
3232
}
3333

3434
runCmd.AddCommand(run.NewAnsibleCmd())
35+
runCmd.AddCommand(run.NewHelmCmd())
3536
return runCmd
3637
}

commands/operator-sdk/cmd/run/ansible.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ import (
2121
"github.com/spf13/cobra"
2222
)
2323

24-
var flags *aoflags.AnsibleOperatorFlags
25-
2624
// NewAnsibleCmd returns a command that will run an ansible operator
2725
func NewAnsibleCmd() *cobra.Command {
26+
var flags *aoflags.AnsibleOperatorFlags
2827
newCmd := &cobra.Command{
2928
Use: "ansible",
3029
Short: "Runs as an ansible operator",

commands/operator-sdk/cmd/run/helm.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2019 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package run
16+
17+
import (
18+
"github.com/operator-framework/operator-sdk/pkg/helm"
19+
hoflags "github.com/operator-framework/operator-sdk/pkg/helm/flags"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// NewHelmCmd returns a command that will run a helm operator
25+
func NewHelmCmd() *cobra.Command {
26+
var flags *hoflags.HelmOperatorFlags
27+
newCmd := &cobra.Command{
28+
Use: "helm",
29+
Short: "Runs as a helm operator",
30+
Long: `Runs as a helm operator. This is intended to be used when running
31+
in a Pod inside a cluster. Developers wanting to run their operator locally
32+
should use "up local" instead.`,
33+
Run: func(cmd *cobra.Command, args []string) {
34+
helm.Run(flags)
35+
},
36+
}
37+
flags = hoflags.AddTo(newCmd.Flags())
38+
39+
return newCmd
40+
}

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

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,17 @@ import (
2525
"strings"
2626
"syscall"
2727

28-
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
29-
3028
"github.com/operator-framework/operator-sdk/internal/util/projutil"
3129
"github.com/operator-framework/operator-sdk/pkg/ansible"
3230
aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags"
33-
"github.com/operator-framework/operator-sdk/pkg/helm/client"
34-
"github.com/operator-framework/operator-sdk/pkg/helm/controller"
31+
"github.com/operator-framework/operator-sdk/pkg/helm"
3532
hoflags "github.com/operator-framework/operator-sdk/pkg/helm/flags"
36-
"github.com/operator-framework/operator-sdk/pkg/helm/release"
3733
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
3834
"github.com/operator-framework/operator-sdk/pkg/scaffold"
3935
sdkVersion "github.com/operator-framework/operator-sdk/version"
40-
"k8s.io/helm/pkg/storage"
41-
"k8s.io/helm/pkg/storage/driver"
4236

4337
log "github.com/sirupsen/logrus"
4438
"github.com/spf13/cobra"
45-
"sigs.k8s.io/controller-runtime/pkg/client/config"
46-
"sigs.k8s.io/controller-runtime/pkg/manager"
47-
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
4839
)
4940

5041
// NewLocalCmd - up local command to run an operator loccally
@@ -172,50 +163,14 @@ func upLocalHelm() {
172163
log.Fatalf("Failed to set %s environment variable: (%v)", k8sutil.KubeConfigEnvVar, err)
173164
}
174165

175-
logf.SetLogger(logf.ZapLogger(false))
176-
177-
printVersion()
178-
179-
cfg, err := config.GetConfig()
180-
if err != nil {
181-
log.Fatal(err)
182-
}
183-
184-
mgr, err := manager.New(cfg, manager.Options{Namespace: namespace})
185-
if err != nil {
186-
log.Fatal(err)
187-
}
188-
189-
// Create Tiller's storage backend and kubernetes client
190-
storageBackend := storage.Init(driver.NewMemory())
191-
tillerKubeClient, err := client.NewFromManager(mgr)
192-
if err != nil {
193-
log.Fatal(err)
194-
}
195-
196-
factories, err := release.NewManagerFactoriesFromFile(storageBackend, tillerKubeClient, helmOperatorFlags.WatchesFile)
197-
if err != nil {
198-
log.Fatal(err)
199-
}
200-
201-
for gvk, factory := range factories {
202-
// Register the controller with the factory.
203-
err := controller.Add(mgr, controller.WatchOptions{
204-
Namespace: namespace,
205-
GVK: gvk,
206-
ManagerFactory: factory,
207-
ReconcilePeriod: helmOperatorFlags.ReconcilePeriod,
208-
WatchDependentResources: true,
209-
})
210-
if err != nil {
211-
log.Fatal(err)
166+
// Set the kubeconfig that the manager will be able to grab
167+
if namespace != "" {
168+
if err := os.Setenv(k8sutil.WatchNamespaceEnvVar, namespace); err != nil {
169+
log.Fatalf("Failed to set %s environment variable: (%v)", k8sutil.WatchNamespaceEnvVar, err)
212170
}
213171
}
214172

215-
// Start the Cmd
216-
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
217-
log.Fatal(err)
218-
}
173+
helm.Run(helmOperatorFlags)
219174
}
220175

221176
func printVersion() {

doc/dev/release.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,15 @@ Create a new branch to push release commits:
171171
$ git checkout -b release-v1.3.0
172172
```
173173

174-
Commit changes to the following four files:
174+
Commit changes to the following six files:
175175
* `version/version.go`: update `Version` to `v1.3.0`.
176176
* `pkg/scaffold/gopkgtoml.go`, under the `[[constraint]]` for `github.com/operator-framework/operator-sdk`:
177177
* Comment out `branch = "master"`
178178
* Un-comment `version = "v1.2.0"`
179179
* Change `v1.2.0` to `v1.3.0`
180180
* `pkg/scaffold/gopkgtoml_test.go`: same as for `pkg/scaffold/gopkgtoml.go`.
181+
* `pkg/scaffold/ansible/gopkgtoml.go`: same as for `pkg/scaffold/gopkgtoml.go`.
182+
* `pkg/scaffold/helm/gopkgtoml.go`: same as for `pkg/scaffold/gopkgtoml.go`.
181183
* `CHANGELOG.md`: update the `## Unreleased` header to `## v1.3.0`.
182184

183185
Create a new PR for `release-v1.3.0`.
@@ -216,6 +218,8 @@ Check out a new branch from master (or use your `release-v1.3.0`) and commit the
216218
* Comment out `version = "v1.3.0"`
217219
* Un-comment `branch = "master"`
218220
* `pkg/scaffold/gopkgtoml_test.go`: same as for `pkg/scaffold/gopkgtoml.go`.
221+
* `pkg/scaffold/ansible/gopkgtoml.go`: same as for `pkg/scaffold/gopkgtoml.go`.
222+
* `pkg/scaffold/helm/gopkgtoml.go`: same as for `pkg/scaffold/gopkgtoml.go`.
219223
* `CHANGELOG.md`: add the following as a new set of headers above `## v1.3.0`:
220224
```
221225
## Unreleased

doc/dev/testing/travis-build.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The Go, Ansible, and Helm tests then differ in what tests they run.
6969
### Ansible tests
7070

7171
1. Run [ansible e2e tests][ansible-e2e].
72-
1. Create base ansible operator image by running [`hack/image/scaffold-ansible-image.go`][ansible-base].
72+
1. Create base ansible operator project by running [`hack/image/ansible/scaffold-ansible-image.go`][ansible-base].
7373
2. Build base ansible operator image.
7474
3. Create and configure a new ansible type memcached-operator.
7575
4. Create cluster resources.
@@ -86,12 +86,18 @@ The Go, Ansible, and Helm tests then differ in what tests they run.
8686
### Helm Tests
8787

8888
1. Run [helm e2e tests][helm-e2e].
89-
1. Build base helm operator image from [`test/helm-operator`][helm-base].
90-
2. Create and configure a new helm type nginx-operator.
91-
3. Create cluster resources.
92-
4. Wait for operator to be ready.
93-
5. Create nginx CR and wait for it to be ready.
94-
6. Delete nginx CR and verify that finalizer (which writes a message in the operator logs) ran.
89+
1. Create base helm operator project by running [`hack/image/helm/scaffold-helm-image.go`][helm-base].
90+
2. Build base helm operator image.
91+
3. Create and configure a new helm type nginx-operator.
92+
4. Create cluster resources.
93+
5. Wait for operator to be ready.
94+
6. Create nginx CR and wait for it to be ready.
95+
7. Scale up the dependent deployment and verify the operator reconciles it back down.
96+
8. Scale up the CR and verify the dependent deployment scales up accordingly.
97+
9. Delete nginx CR and verify that finalizer (which writes a message in the operator logs) ran.
98+
10. Run `operator-sdk migrate` to add go source to the operator.
99+
11. Run `operator-sdk build` to compile the new binary and build a new image.
100+
12. Re-run steps 4-9 to test the migrated operator.
95101

96102
**NOTE**: All created resources, including the namespace, are deleted using a bash trap when the test finishes
97103

@@ -110,8 +116,8 @@ The markdown test does not create a new cluster and runs in a barebones travis V
110116
[go-e2e]: ../../../hack/tests/e2e-go.sh
111117
[tls-tests]: ../../../test/e2e/tls_util_test.go
112118
[ansible-e2e]: ../../../hack/tests/e2e-ansible.sh
113-
[ansible-base]: ../../../hack/image/scaffold-ansible-image.go
119+
[ansible-base]: ../../../hack/image/ansible/scaffold-ansible-image.go
114120
[helm-e2e]: ../../../hack/tests/e2e-helm.sh
115-
[helm-base]: ../../../test/helm-operator
121+
[helm-base]: ../../../hack/image/helm/scaffold-helm-image.go
116122
[marker-github]: https://github.com/crawford/marker
117123
[marker-local]: ../../../hack/ci/marker

doc/sdk-cli-reference.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,23 @@ should use `up local` instead.
300300
$ operator-sdk run ansible --watches-file=/opt/ansible/watches.yaml --reconcile-period=30s
301301
```
302302

303+
### helm
304+
305+
Runs as a helm operator process. This is intended to be used when running
306+
in a Pod inside a cluster. Developers wanting to run their operator locally
307+
should use `up local` instead.
308+
309+
#### Flags
310+
311+
* `--reconcile-period` string - Default reconcile period for controllers (default 1m0s)
312+
* `--watches-file` string - Path to the watches file to use (default "./watches.yaml")
313+
314+
#### Example
315+
316+
```bash
317+
$ operator-sdk run helm --watches-file=/opt/helm/watches.yaml --reconcile-period=30s
318+
```
319+
303320
## test
304321

305322
### Available Commands

hack/image/build-ansible-image.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mkdir -p "$BASEIMAGEDIR"
1212

1313
# build operator binary and base image
1414
pushd "$BASEIMAGEDIR"
15-
go run "$ROOTDIR/hack/image/scaffold-ansible-image.go"
15+
go run "$ROOTDIR/hack/image/ansible/scaffold-ansible-image.go"
1616

1717
mkdir -p build/_output/bin/
1818
cp $ROOTDIR/build/operator-sdk-dev-x86_64-linux-gnu build/_output/bin/ansible-operator

hack/image/build-helm-image.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22

33
set -eux
44

5+
source hack/lib/test_lib.sh
6+
7+
ROOTDIR="$(pwd)"
8+
GOTMP="$(mktemp -d -p $GOPATH/src)"
9+
trap_add 'rm -rf $GOTMP' EXIT
10+
BASEIMAGEDIR="$GOTMP/helm-operator"
11+
mkdir -p "$BASEIMAGEDIR"
12+
513
# build operator binary and base image
6-
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o test/helm-operator/helm-operator test/helm-operator/cmd/helm-operator/main.go
7-
pushd test/helm-operator
8-
docker build -t "$1" .
14+
pushd "$BASEIMAGEDIR"
15+
go run "$ROOTDIR/hack/image/helm/scaffold-helm-image.go"
16+
17+
mkdir -p build/_output/bin/
18+
cp $ROOTDIR/build/operator-sdk-dev-x86_64-linux-gnu build/_output/bin/helm-operator
19+
operator-sdk build $1
920
popd

0 commit comments

Comments
 (0)