Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Added

- 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))

### Changed

### Deprecated
Expand Down
50 changes: 50 additions & 0 deletions cmd/operator-sdk/alpha/cleanup/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2020 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cleanup

import (
olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type cleanupArgs struct {
olm bool
}

func NewCmd() *cobra.Command {
cargs := &cleanupArgs{}
c := &olmoperator.OLMCmd{}
cmd := &cobra.Command{
Use: "cleanup",
Short: "Delete and clean up after a running Operator",
RunE: func(cmd *cobra.Command, args []string) error {
switch {
case cargs.olm:
if err := c.Cleanup(); err != nil {
log.Fatalf("Failed to clean up operator: %v", err)
}
}
return nil
},
}
// OLM is the default.
cmd.Flags().BoolVar(&cargs.olm, "olm", true, "The operator to be deleted is managed by OLM in a cluster.")
// TODO(estroz): refactor flag setting when new run mode options are added.
c.AddToFlagSet(cmd.Flags())
cmd.Flags().BoolVar(&c.ForceRegistry, "force-registry", false, "Force deletion of the in-cluster registry.")
return cmd
}
11 changes: 9 additions & 2 deletions cmd/operator-sdk/alpha/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package alpha

import (
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/cleanup"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/kubebuilder"
"github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/olm"
run "github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha/run"

"github.com/spf13/cobra"
)

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

cmd.AddCommand(olm.NewCmd())
cmd.AddCommand(kubebuilder.NewCmd())
cmd.AddCommand(
olm.NewCmd(),
kubebuilder.NewCmd(),
run.NewCmd(),
cleanup.NewCmd(),
)
return cmd
}
49 changes: 49 additions & 0 deletions cmd/operator-sdk/alpha/run/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 The Operator-SDK Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package run

import (
olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type runArgs struct {
olm bool
}

func NewCmd() *cobra.Command {
cargs := &runArgs{}
c := &olmoperator.OLMCmd{}
cmd := &cobra.Command{
Use: "run",
Short: "Run an Operator in a variety of environments",
RunE: func(cmd *cobra.Command, args []string) error {
switch {
case cargs.olm:
if err := c.Run(); err != nil {
log.Fatalf("Failed to run operator: %v", err)
}
}
return nil
},
}
// OLM is the default.
cmd.Flags().BoolVar(&cargs.olm, "olm", true, "The operator to be run will be managed by OLM in a cluster.")
// TODO(estroz): refactor flag setting when new run mode options are added.
c.AddToFlagSet(cmd.Flags())
return cmd
}
2 changes: 2 additions & 0 deletions doc/cli/operator-sdk_alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ Run an alpha subcommand
### SEE ALSO

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

31 changes: 31 additions & 0 deletions doc/cli/operator-sdk_alpha_cleanup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## operator-sdk alpha cleanup

Delete and clean up after a running Operator

### Synopsis

Delete and clean up after a running Operator

```
operator-sdk alpha cleanup [flags]
```

### Options

```
--force-registry Force deletion of the in-cluster registry.
-h, --help help for cleanup
--include strings Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by run/cleanup
--install-mode string InstallMode to create OperatorGroup with. Format: InstallModeType=[ns1,ns2[, ...]]
--kubeconfig string Path to kubeconfig
--manifests string Directory containing package manifest and operator bundles.
--namespace string Namespace in which to create resources
--olm The operator to be deleted is managed by OLM in a cluster. (default true)
--operator-version string Version of operator to deploy
--timeout duration Time to wait for the command to complete before failing (default 2m0s)
```

### SEE ALSO

* [operator-sdk alpha](operator-sdk_alpha.md) - Run an alpha subcommand

30 changes: 30 additions & 0 deletions doc/cli/operator-sdk_alpha_run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## operator-sdk alpha run

Run an Operator in a variety of environments

### Synopsis

Run an Operator in a variety of environments

```
operator-sdk alpha run [flags]
```

### Options

```
-h, --help help for run
--include strings Path to Kubernetes resource manifests, ex. Role, Subscription. These supplement or override defaults generated by run/cleanup
--install-mode string InstallMode to create OperatorGroup with. Format: InstallModeType=[ns1,ns2[, ...]]
--kubeconfig string Path to kubeconfig
--manifests string Directory containing package manifest and operator bundles.
--namespace string Namespace in which to create resources
--olm The operator to be run will be managed by OLM in a cluster. (default true)
--operator-version string Version of operator to deploy
--timeout duration Time to wait for the command to complete before failing (default 2m0s)
```

### SEE ALSO

* [operator-sdk alpha](operator-sdk_alpha.md) - Run an alpha subcommand

4 changes: 2 additions & 2 deletions internal/olm/operator/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c *OLMCmd) newManager() (*operatorManager, error) {
return m, nil
}

func (m *operatorManager) up(ctx context.Context) (err error) {
func (m *operatorManager) run(ctx context.Context) (err error) {
// Ensure OLM is installed.
olmVer, err := m.client.GetInstalledVersion(ctx)
if err != nil {
Expand Down Expand Up @@ -221,7 +221,7 @@ func (m *operatorManager) up(ctx context.Context) (err error) {
return nil
}

func (m *operatorManager) down(ctx context.Context) (err error) {
func (m *operatorManager) cleanup(ctx context.Context) (err error) {
// Ensure OLM is installed.
olmVer, err := m.client.GetInstalledVersion(ctx)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions internal/olm/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
type OLMCmd struct { // nolint:golint
// ManifestsDir is a directory containing a package manifest and N bundles
// of the operator's CSV and CRD's. OperatorVersion can be set to the
// version of the desired operator version's subdir and Up()/Down() will
// version of the desired operator version's subdir and Run()/Cleanup() will
// deploy the operator version in that subdir.
ManifestsDir string
// OperatorVersion is the version of the operator to deploy. It must be
Expand Down Expand Up @@ -85,13 +85,13 @@ type OLMCmd struct { // nolint:golint
var installModeFormat = "InstallModeType=[ns1,ns2[, ...]]"

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

func (c *OLMCmd) validate() error {
Expand All @@ -117,7 +117,7 @@ func (c *OLMCmd) initialize() {
})
}

func (c *OLMCmd) Up() error {
func (c *OLMCmd) Run() error {
c.initialize()
if err := c.validate(); err != nil {
return fmt.Errorf("validation error: %w", err)
Expand All @@ -128,10 +128,10 @@ func (c *OLMCmd) Up() error {
}
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
return m.up(ctx)
return m.run(ctx)
}

func (c *OLMCmd) Down() (err error) {
func (c *OLMCmd) Cleanup() (err error) {
c.initialize()
if err := c.validate(); err != nil {
return fmt.Errorf("validation error: %w", err)
Expand All @@ -142,5 +142,5 @@ func (c *OLMCmd) Down() (err error) {
}
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
return m.down(ctx)
return m.cleanup(ctx)
}
16 changes: 8 additions & 8 deletions test/integration/operator_olm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,27 @@ func SingleOperator(t *testing.T) {
// Cleanup.
defer func() {
opcmd.ForceRegistry = true
if err := opcmd.Down(); err != nil {
if err := opcmd.Cleanup(); err != nil {
t.Fatal(err)
}
}()

// "Remove operator before deploy"
assert.NoError(t, opcmd.Down())
assert.NoError(t, opcmd.Cleanup())
// "Remove operator before deploy (force delete registry)"
opcmd.ForceRegistry = true
assert.NoError(t, opcmd.Down())
assert.NoError(t, opcmd.Cleanup())

// "Deploy operator"
assert.NoError(t, opcmd.Up())
assert.NoError(t, opcmd.Run())
// "Fail to deploy operator after deploy"
assert.Error(t, opcmd.Up())
assert.Error(t, opcmd.Run())

// "Remove operator after deploy"
assert.NoError(t, opcmd.Down())
assert.NoError(t, opcmd.Cleanup())
// "Remove operator after removal"
assert.NoError(t, opcmd.Down())
assert.NoError(t, opcmd.Cleanup())
// "Remove operator after removal (force delete registry)"
opcmd.ForceRegistry = true
assert.NoError(t, opcmd.Down())
assert.NoError(t, opcmd.Cleanup())
}