Skip to content

Commit f5f80cb

Browse files
author
Eric Stroczynski
authored
cmd/operator-sdk: OLM integration alpha run/cleanup CLI (#2402)
'run/cleanup' commands will create and delete an operator. Currently in alpha stage, and only in-cluster deployment is supported via OLM using manifests stored in a registry-server. * cmd/operator-sdk/alpha/{run,cleanup}: run/cleanup commands * CHANGELOG.md,doc/cli: add run/cleanup additions
1 parent 152e5d5 commit f5f80cb

File tree

10 files changed

+190
-19
lines changed

10 files changed

+190
-19
lines changed

CHANGELOG.md

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

33
### Added
44

5+
- Added [`run`](./doc/cli/operator-sdk_alpha_run.md) and [`cleanup`](./doc/cli/operator-sdk_alpha_cleanup.md) subcommands (under the `alpha` subcommand) to manage deployment/deletion of operators. These commands currently interact with OLM via an in-cluster registry-server created using an operator's on-disk manifests and managed by `operator-sdk`. ([#2402](ttps://github.com/operator-framework/operator-sdk/pull/2402))
6+
57
### Changed
68

79
### Deprecated

cmd/operator-sdk/alpha/cleanup/cmd.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 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 cleanup
16+
17+
import (
18+
olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator"
19+
20+
log "github.com/sirupsen/logrus"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
type cleanupArgs struct {
25+
olm bool
26+
}
27+
28+
func NewCmd() *cobra.Command {
29+
cargs := &cleanupArgs{}
30+
c := &olmoperator.OLMCmd{}
31+
cmd := &cobra.Command{
32+
Use: "cleanup",
33+
Short: "Delete and clean up after a running Operator",
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
switch {
36+
case cargs.olm:
37+
if err := c.Cleanup(); err != nil {
38+
log.Fatalf("Failed to clean up operator: %v", err)
39+
}
40+
}
41+
return nil
42+
},
43+
}
44+
// OLM is the default.
45+
cmd.Flags().BoolVar(&cargs.olm, "olm", true, "The operator to be deleted is managed by OLM in a cluster.")
46+
// TODO(estroz): refactor flag setting when new run mode options are added.
47+
c.AddToFlagSet(cmd.Flags())
48+
cmd.Flags().BoolVar(&c.ForceRegistry, "force-registry", false, "Force deletion of the in-cluster registry.")
49+
return cmd
50+
}

cmd/operator-sdk/alpha/cmd.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package alpha
1616

1717
import (
18+
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/cleanup"
1819
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/kubebuilder"
1920
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/olm"
21+
run "github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/run"
22+
2023
"github.com/spf13/cobra"
2124
)
2225

@@ -26,7 +29,11 @@ func NewCmd() *cobra.Command {
2629
Short: "Run an alpha subcommand",
2730
}
2831

29-
cmd.AddCommand(olm.NewCmd())
30-
cmd.AddCommand(kubebuilder.NewCmd())
32+
cmd.AddCommand(
33+
olm.NewCmd(),
34+
kubebuilder.NewCmd(),
35+
run.NewCmd(),
36+
cleanup.NewCmd(),
37+
)
3138
return cmd
3239
}

cmd/operator-sdk/alpha/run/cmd.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 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+
olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator"
19+
20+
log "github.com/sirupsen/logrus"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
type runArgs struct {
25+
olm bool
26+
}
27+
28+
func NewCmd() *cobra.Command {
29+
cargs := &runArgs{}
30+
c := &olmoperator.OLMCmd{}
31+
cmd := &cobra.Command{
32+
Use: "run",
33+
Short: "Run an Operator in a variety of environments",
34+
RunE: func(cmd *cobra.Command, args []string) error {
35+
switch {
36+
case cargs.olm:
37+
if err := c.Run(); err != nil {
38+
log.Fatalf("Failed to run operator: %v", err)
39+
}
40+
}
41+
return nil
42+
},
43+
}
44+
// OLM is the default.
45+
cmd.Flags().BoolVar(&cargs.olm, "olm", true, "The operator to be run will be managed by OLM in a cluster.")
46+
// TODO(estroz): refactor flag setting when new run mode options are added.
47+
c.AddToFlagSet(cmd.Flags())
48+
return cmd
49+
}

doc/cli/operator-sdk_alpha.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ Run an alpha subcommand
1515
### SEE ALSO
1616

1717
* [operator-sdk](operator-sdk.md) - An SDK for building operators with ease
18+
* [operator-sdk alpha cleanup](operator-sdk_alpha_cleanup.md) - Delete and clean up after a running Operator
1819
* [operator-sdk alpha olm](operator-sdk_alpha_olm.md) - Manage the Operator Lifecycle Manager installation in your cluster
20+
* [operator-sdk alpha run](operator-sdk_alpha_run.md) - Run an Operator in a variety of environments
1921

doc/cli/operator-sdk_alpha_cleanup.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## operator-sdk alpha cleanup
2+
3+
Delete and clean up after a running Operator
4+
5+
### Synopsis
6+
7+
Delete and clean up after a running Operator
8+
9+
```
10+
operator-sdk alpha cleanup [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
--force-registry Force deletion of the in-cluster registry.
17+
-h, --help help for cleanup
18+
--include strings Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by run/cleanup
19+
--install-mode string InstallMode to create OperatorGroup with. Format: InstallModeType=[ns1,ns2[, ...]]
20+
--kubeconfig string Path to kubeconfig
21+
--manifests string Directory containing package manifest and operator bundles.
22+
--namespace string Namespace in which to create resources
23+
--olm The operator to be deleted is managed by OLM in a cluster. (default true)
24+
--operator-version string Version of operator to deploy
25+
--timeout duration Time to wait for the command to complete before failing (default 2m0s)
26+
```
27+
28+
### SEE ALSO
29+
30+
* [operator-sdk alpha](operator-sdk_alpha.md) - Run an alpha subcommand
31+

doc/cli/operator-sdk_alpha_run.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## operator-sdk alpha run
2+
3+
Run an Operator in a variety of environments
4+
5+
### Synopsis
6+
7+
Run an Operator in a variety of environments
8+
9+
```
10+
operator-sdk alpha run [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help help for run
17+
--include strings Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by run/cleanup
18+
--install-mode string InstallMode to create OperatorGroup with. Format: InstallModeType=[ns1,ns2[, ...]]
19+
--kubeconfig string Path to kubeconfig
20+
--manifests string Directory containing package manifest and operator bundles.
21+
--namespace string Namespace in which to create resources
22+
--olm The operator to be run will be managed by OLM in a cluster. (default true)
23+
--operator-version string Version of operator to deploy
24+
--timeout duration Time to wait for the command to complete before failing (default 2m0s)
25+
```
26+
27+
### SEE ALSO
28+
29+
* [operator-sdk alpha](operator-sdk_alpha.md) - Run an alpha subcommand
30+

internal/olm/operator/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (c *OLMCmd) newManager() (*operatorManager, error) {
130130
return m, nil
131131
}
132132

133-
func (m *operatorManager) up(ctx context.Context) (err error) {
133+
func (m *operatorManager) run(ctx context.Context) (err error) {
134134
// Ensure OLM is installed.
135135
olmVer, err := m.client.GetInstalledVersion(ctx)
136136
if err != nil {
@@ -221,7 +221,7 @@ func (m *operatorManager) up(ctx context.Context) (err error) {
221221
return nil
222222
}
223223

224-
func (m *operatorManager) down(ctx context.Context) (err error) {
224+
func (m *operatorManager) cleanup(ctx context.Context) (err error) {
225225
// Ensure OLM is installed.
226226
olmVer, err := m.client.GetInstalledVersion(ctx)
227227
if err != nil {

internal/olm/operator/operator.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
type OLMCmd struct { // nolint:golint
3737
// ManifestsDir is a directory containing a package manifest and N bundles
3838
// of the operator's CSV and CRD's. OperatorVersion can be set to the
39-
// version of the desired operator version's subdir and Up()/Down() will
39+
// version of the desired operator version's subdir and Run()/Cleanup() will
4040
// deploy the operator version in that subdir.
4141
ManifestsDir string
4242
// OperatorVersion is the version of the operator to deploy. It must be
@@ -85,13 +85,13 @@ type OLMCmd struct { // nolint:golint
8585
var installModeFormat = "InstallModeType=[ns1,ns2[, ...]]"
8686

8787
func (c *OLMCmd) AddToFlagSet(fs *pflag.FlagSet) {
88+
fs.StringVar(&c.ManifestsDir, "manifests", "", "Directory containing package manifest and operator bundles.")
8889
fs.StringVar(&c.OperatorVersion, "operator-version", "", "Version of operator to deploy")
8990
fs.StringVar(&c.InstallMode, "install-mode", "", "InstallMode to create OperatorGroup with. Format: "+installModeFormat)
9091
fs.StringVar(&c.KubeconfigPath, "kubeconfig", "", "Path to kubeconfig")
9192
fs.StringVar(&c.OperatorNamespace, "namespace", "", "Namespace in which to create resources")
92-
fs.StringSliceVar(&c.IncludePaths, "include", nil, "Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by up/down")
93+
fs.StringSliceVar(&c.IncludePaths, "include", nil, "Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by run/cleanup")
9394
fs.DurationVar(&c.Timeout, "timeout", defaultTimeout, "Time to wait for the command to complete before failing")
94-
fs.BoolVar(&c.ForceRegistry, "force-registry", false, "Force deletion of the in-cluster registry. This option is a no-op on 'up'.")
9595
}
9696

9797
func (c *OLMCmd) validate() error {
@@ -117,7 +117,7 @@ func (c *OLMCmd) initialize() {
117117
})
118118
}
119119

120-
func (c *OLMCmd) Up() error {
120+
func (c *OLMCmd) Run() error {
121121
c.initialize()
122122
if err := c.validate(); err != nil {
123123
return fmt.Errorf("validation error: %w", err)
@@ -128,10 +128,10 @@ func (c *OLMCmd) Up() error {
128128
}
129129
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
130130
defer cancel()
131-
return m.up(ctx)
131+
return m.run(ctx)
132132
}
133133

134-
func (c *OLMCmd) Down() (err error) {
134+
func (c *OLMCmd) Cleanup() (err error) {
135135
c.initialize()
136136
if err := c.validate(); err != nil {
137137
return fmt.Errorf("validation error: %w", err)
@@ -142,5 +142,5 @@ func (c *OLMCmd) Down() (err error) {
142142
}
143143
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
144144
defer cancel()
145-
return m.down(ctx)
145+
return m.cleanup(ctx)
146146
}

test/integration/operator_olm_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,27 @@ func SingleOperator(t *testing.T) {
9595
// Cleanup.
9696
defer func() {
9797
opcmd.ForceRegistry = true
98-
if err := opcmd.Down(); err != nil {
98+
if err := opcmd.Cleanup(); err != nil {
9999
t.Fatal(err)
100100
}
101101
}()
102102

103103
// "Remove operator before deploy"
104-
assert.NoError(t, opcmd.Down())
104+
assert.NoError(t, opcmd.Cleanup())
105105
// "Remove operator before deploy (force delete registry)"
106106
opcmd.ForceRegistry = true
107-
assert.NoError(t, opcmd.Down())
107+
assert.NoError(t, opcmd.Cleanup())
108108

109109
// "Deploy operator"
110-
assert.NoError(t, opcmd.Up())
110+
assert.NoError(t, opcmd.Run())
111111
// "Fail to deploy operator after deploy"
112-
assert.Error(t, opcmd.Up())
112+
assert.Error(t, opcmd.Run())
113113

114114
// "Remove operator after deploy"
115-
assert.NoError(t, opcmd.Down())
115+
assert.NoError(t, opcmd.Cleanup())
116116
// "Remove operator after removal"
117-
assert.NoError(t, opcmd.Down())
117+
assert.NoError(t, opcmd.Cleanup())
118118
// "Remove operator after removal (force delete registry)"
119119
opcmd.ForceRegistry = true
120-
assert.NoError(t, opcmd.Down())
120+
assert.NoError(t, opcmd.Cleanup())
121121
}

0 commit comments

Comments
 (0)