From ff3a93c43ad44b4fcfbf0bcbb2211a98f3886b58 Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Mon, 13 Jul 2020 14:58:02 -0400 Subject: [PATCH 1/2] cmd/operator-sdk/run: remove legacy run subcommand --- CHANGELOG.md | 4 +- cmd/operator-sdk/cleanup/cmd.go | 111 +------- .../packagemanifests/packagemanifests.go | 12 +- cmd/operator-sdk/cli/legacy.go | 4 - cmd/operator-sdk/run/cmd.go | 178 ------------ cmd/operator-sdk/run/local/cmd.go | 71 ----- cmd/operator-sdk/run/local/local.go | 256 ------------------ .../run/packagemanifests/packagemanifests.go | 12 +- website/content/en/docs/cli/operator-sdk.md | 2 - .../en/docs/cli/operator-sdk_cleanup.md | 26 -- .../operator-sdk_cleanup_packagemanifests.md | 34 --- .../content/en/docs/cli/operator-sdk_run.md | 31 --- .../en/docs/cli/operator-sdk_run_local.md | 31 --- .../cli/operator-sdk_run_packagemanifests.md | 33 --- 14 files changed, 5 insertions(+), 800 deletions(-) delete mode 100644 cmd/operator-sdk/run/local/cmd.go delete mode 100644 cmd/operator-sdk/run/local/local.go delete mode 100644 website/content/en/docs/cli/operator-sdk_cleanup.md delete mode 100644 website/content/en/docs/cli/operator-sdk_cleanup_packagemanifests.md delete mode 100644 website/content/en/docs/cli/operator-sdk_run.md delete mode 100644 website/content/en/docs/cli/operator-sdk_run_local.md delete mode 100644 website/content/en/docs/cli/operator-sdk_run_packagemanifests.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 87b1929783..2f0245c470 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -254,7 +254,7 @@ ### Added -- Added the [`cleanup`](./website/content/en/docs/cli/operator-sdk_cleanup.md) subcommand and [`run --olm`](./website/content/en/docs/cli/operator-sdk_run.md) 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](https://github.com/operator-framework/operator-sdk/pull/2402), [#2441](https://github.com/operator-framework/operator-sdk/pull/2441)) +- Added the [`cleanup`](https://github.com/operator-framework/operator-sdk/blob/v0.15.0/doc/cli/operator-sdk_cleanup.md) subcommand and [`run --olm`](https://github.com/operator-framework/operator-sdk/blob/v0.15.0/doc/cli/operator-sdk_run.md) 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](https://github.com/operator-framework/operator-sdk/pull/2402), [#2441](https://github.com/operator-framework/operator-sdk/pull/2441)) - Added [`bundle create`](https://github.com/operator-framework/operator-sdk/blob/v0.15.0/doc/cli/operator-sdk_bundle_create.md) which builds, and optionally generates metadata for, [operator bundle images](https://github.com/openshift/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md). ([#2076](https://github.com/operator-framework/operator-sdk/pull/2076), [#2438](https://github.com/operator-framework/operator-sdk/pull/2438)) - Added [`bundle validate`](https://github.com/operator-framework/operator-sdk/blob/v0.15.0/doc/cli/operator-sdk_bundle_validate.md) which validates [operator bundle images](https://github.com/openshift/enhancements/blob/ec2cf96/enhancements/olm/operator-registry.md). ([#2411](https://github.com/operator-framework/operator-sdk/pull/2411)) - Added `blacklist` field to the `watches.yaml` for Ansible based operators. Blacklisted secondary resources will not be watched or cached.([#2374](https://github.com/operator-framework/operator-sdk/pull/2374)) @@ -265,7 +265,7 @@ - Added retry logic to the cleanup function from the e2e test framework in order to allow it to be achieved in the scenarios where temporary network issues are faced. ([#2277](https://github.com/operator-framework/operator-sdk/pull/2277)) - **Breaking Change:** Moved `olm-catalog gen-csv` to the `generate csv` subcommand. ([#2439](https://github.com/operator-framework/operator-sdk/pull/2439)) - **Breaking Change:** `run ansible/helm` are now the hidden commands `exec-entrypoint ansible/helm`. All functionality of each subcommand is the same. ([#2441](https://github.com/operator-framework/operator-sdk/pull/2441)) -- **Breaking Change:** `up local` is now [`run --local`](./website/content/en/docs/cli/operator-sdk_run.md). All functionality of this command is the same. ([#2441](https://github.com/operator-framework/operator-sdk/pull/2441)) +- **Breaking Change:** `up local` is now [`run --local`](https://github.com/operator-framework/operator-sdk/blob/v0.15.0/doc/cli/operator-sdk_run.md). All functionality of this command is the same. ([#2441](https://github.com/operator-framework/operator-sdk/pull/2441)) - **Breaking Change:** Moved the `olm` subcommand from `alpha` to its own subcommand. All functionality of this command is the same. ([#2447](https://github.com/operator-framework/operator-sdk/pull/2447)) ### Deprecated diff --git a/cmd/operator-sdk/cleanup/cmd.go b/cmd/operator-sdk/cleanup/cmd.go index 63ef4d7e1e..0a1aa27130 100644 --- a/cmd/operator-sdk/cleanup/cmd.go +++ b/cmd/operator-sdk/cleanup/cmd.go @@ -15,17 +15,9 @@ package cleanup import ( - "errors" - "path/filepath" + "github.com/spf13/cobra" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/cleanup/packagemanifests" - olmcatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog" - olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator" - "github.com/operator-framework/operator-sdk/internal/util/projutil" - - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/spf13/pflag" ) func NewCmd() *cobra.Command { @@ -44,104 +36,3 @@ Run 'operator-sdk cleanup --help' for more information. return cmd } - -type cleanupCmd struct { - // Common options. - kubeconfig string - // TODO: remove --namespace and c.namespace - //Deprecated: use olmArgs.OperatorNamespace instead - namespace string - - // Cleanup type. - olm bool - - // Cleanup type-specific options. - olmArgs olmoperator.PackageManifestsCmd -} - -// checkCleanupType ensures exactly one cleanup type has been selected. -func (c *cleanupCmd) checkCleanupType() error { - if !c.olm { - return errors.New("exactly one run-type flag must be set: --olm") - } - return nil -} - -func NewCmdLegacy() *cobra.Command { - c := &cleanupCmd{} - cmd := &cobra.Command{ - Use: "cleanup", - Short: "Delete and clean up after a running Operator", - RunE: func(cmd *cobra.Command, args []string) error { - if err := c.checkCleanupType(); err != nil { - return err - } - projutil.MustInProjectRoot() - - switch { - case c.olm: - c.olmArgs.KubeconfigPath = c.kubeconfig - //TODO: remove --namespace and c.namespace - //use olmArgs.OperatorNamespace directly - if cmd.Flags().Changed("namespace") { - log.Warn("--namespace is deprecates use --operator-namespace instead") - if !cmd.Flags().Changed("operator-namespace") { - c.olmArgs.OperatorNamespace = c.namespace - } else { - log.Warn("--operator-namespace present; ignoring --namespace") - } - } - if c.olmArgs.ManifestsDir == "" { - operatorName := filepath.Base(projutil.MustGetwd()) - c.olmArgs.ManifestsDir = filepath.Join(olmcatalog.OLMCatalogDir, operatorName) - } - if err := c.olmArgs.Cleanup(); err != nil { - log.Fatalf("Failed to clean up operator using OLM: %v", err) - } - } - return nil - }, - } - - // Shared flags. - cmd.Flags().StringVar(&c.kubeconfig, "kubeconfig", "", - "The file path to kubernetes configuration file. Defaults to location "+ - "specified by $KUBECONFIG, or to default file rules if not set") - err := cmd.Flags().MarkDeprecated("kubeconfig", "use this flag with 'cleanup packagemanifests' instead") - if err != nil { - panic(err) - } - cmd.Flags().StringVar(&c.namespace, "namespace", "", - "The namespace from which operator and namespaces resources are cleaned up") - err = cmd.Flags().MarkDeprecated("namespace", "use --operator-namespace instead") - if err != nil { - panic(err) - } - - // 'cleanup --olm' and related flags. Set as default since this is the only - // cleanup type. - cmd.Flags().BoolVar(&c.olm, "olm", true, - "The operator to be cleaned up is managed by OLM in a cluster. "+ - "Cannot be set with another cleanup-type flag") - err = cmd.Flags().MarkDeprecated("olm", "use 'cleanup packagemanifests' instead") - if err != nil { - panic(err) - } - // Mark all flags used with '--olm' as deprecated and hidden separately so - // all other 'cleanup' flags are still available. - fs := pflag.NewFlagSet("olm", pflag.ExitOnError) - fs.StringVar(&c.olmArgs.ManifestsDir, "manifests", "", - "Directory containing operator package directories and a package manifest file") - c.olmArgs.AddToFlagSet(fs) - fs.VisitAll(func(f *pflag.Flag) { - f.Deprecated = "use this flag with 'cleanup packagemanifests' instead" - f.Hidden = true - }) - cmd.Flags().AddFlagSet(fs) - - cmd.AddCommand( - packagemanifests.NewCmd(), - ) - - return cmd -} diff --git a/cmd/operator-sdk/cleanup/packagemanifests/packagemanifests.go b/cmd/operator-sdk/cleanup/packagemanifests/packagemanifests.go index ab51923ffe..30a45b3c40 100644 --- a/cmd/operator-sdk/cleanup/packagemanifests/packagemanifests.go +++ b/cmd/operator-sdk/cleanup/packagemanifests/packagemanifests.go @@ -16,15 +16,11 @@ package packagemanifests import ( "fmt" - "path/filepath" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - olmcatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog" olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator" - kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder" - "github.com/operator-framework/operator-sdk/internal/util/projutil" ) type packagemanifestsCmd struct { @@ -47,13 +43,7 @@ ex. '/packagemanifests'.`, } c.ManifestsDir = args[0] } else { - // Choose the default path depending on project configuration. - if kbutil.HasProjectFile() { - c.ManifestsDir = "packagemanifests" - } else { - operatorName := filepath.Base(projutil.MustGetwd()) - c.ManifestsDir = filepath.Join(olmcatalog.OLMCatalogDir, operatorName) - } + c.ManifestsDir = "packagemanifests" } log.Infof("Cleaning up operator in directory %s", c.ManifestsDir) diff --git a/cmd/operator-sdk/cli/legacy.go b/cmd/operator-sdk/cli/legacy.go index 2a07a9212d..344b9cc537 100644 --- a/cmd/operator-sdk/cli/legacy.go +++ b/cmd/operator-sdk/cli/legacy.go @@ -23,12 +23,10 @@ import ( "github.com/operator-framework/operator-sdk/cmd/operator-sdk/alpha" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/build" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/bundle" - "github.com/operator-framework/operator-sdk/cmd/operator-sdk/cleanup" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/completion" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/new" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/olm" - "github.com/operator-framework/operator-sdk/cmd/operator-sdk/run" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/version" "github.com/operator-framework/operator-sdk/internal/flags" "github.com/operator-framework/operator-sdk/internal/util/projutil" @@ -69,12 +67,10 @@ func GetCLIRoot() *cobra.Command { alpha.NewCmd(), build.NewCmd(), bundle.NewCmdLegacy(), - cleanup.NewCmdLegacy(), completion.NewCmd(), generate.NewCmdLegacy(), new.NewCmd(), olm.NewCmd(), - run.NewCmdLegacy(), version.NewCmd(), ) diff --git a/cmd/operator-sdk/run/cmd.go b/cmd/operator-sdk/run/cmd.go index 437338cec2..4aa429d391 100644 --- a/cmd/operator-sdk/run/cmd.go +++ b/cmd/operator-sdk/run/cmd.go @@ -15,23 +15,9 @@ package run import ( - "errors" - "fmt" - "path/filepath" - - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/pflag" - "github.com/operator-framework/operator-sdk/cmd/operator-sdk/run/local" "github.com/operator-framework/operator-sdk/cmd/operator-sdk/run/packagemanifests" - olmcatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog" - olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator" - k8sinternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil" - kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder" - "github.com/operator-framework/operator-sdk/internal/util/projutil" - aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags" - hoflags "github.com/operator-framework/operator-sdk/pkg/helm/flags" ) func NewCmd() *cobra.Command { @@ -50,167 +36,3 @@ Run 'operator-sdk run --help' for more information. return cmd } - -type runCmd struct { - // Common options. - kubeconfig string - //TODO: remove namespace flag before 1.0.0 - //namespace is deprecated - namespace string - - // Run type. - olm, local bool - - // Run type-specific options. - olmArgs olmoperator.PackageManifestsCmd - localArgs local.RunLocalCmd -} - -// checkRunType ensures exactly one run type has been selected. -func (c *runCmd) checkRunType() error { - if c.olm && c.local || !c.olm && !c.local { - return errors.New("exactly one run-type flag must be set: --olm, --local") - } - return nil -} - -func NewCmdLegacy() *cobra.Command { - c := &runCmd{} - cmd := &cobra.Command{ - Use: "run", - Short: "Run an Operator in a variety of environments", - Long: `This command has subcommands that will run or deploy your Operator in two -different modes: locally and using OLM. These modes are controlled by using 'local' -or 'packagemanifests' subcommands. Run 'operator-sdk run --help' for more -information on these subcommands. -`, - RunE: func(cmd *cobra.Command, args []string) error { - if err := c.checkRunType(); err != nil { - return err - } - projutil.MustInProjectRoot() - - switch { - case c.olm: - c.olmArgs.KubeconfigPath = c.kubeconfig - // operator-namespace flag is not set - // use default namespace from kubeconfig to deploy operator resources - if !cmd.Flags().Changed("operator-namespace") { - _, defaultNamespace, err := k8sinternal.GetKubeconfigAndNamespace(c.kubeconfig) - if err != nil { - return fmt.Errorf("error getting kubeconfig and default namespace: %v", err) - } - c.olmArgs.OperatorNamespace = defaultNamespace - } - if c.olmArgs.ManifestsDir == "" { - operatorName := filepath.Base(projutil.MustGetwd()) - c.olmArgs.ManifestsDir = filepath.Join(olmcatalog.OLMCatalogDir, operatorName) - } - if err := c.olmArgs.Run(); err != nil { - log.Fatalf("Failed to run operator using OLM: %v", err) - } - case c.local: - // The main.go and manager.yaml scaffolds in the new layout do not support the WATCH_NAMESPACE - // env var to configure the namespace that the operator watches. The default is all namespaces. - // So this flag is unsupported for the new layout. - if !kbutil.HasProjectFile() { - //TODO: remove namespace flag before 1.0.0 - // set --watch-namespace flag if the --namespace flag is set - // (only if --watch-namespace flag is not set) - if cmd.Flags().Changed("namespace") { // not valid for te new layout - log.Info("--namespace is deprecated; use --watch-namespace instead.") - if !cmd.Flags().Changed("watch-namespace") { - err := cmd.Flags().Set("watch-namespace", c.namespace) - return err - } - } - // Get default namespace to watch if unset. - if !cmd.Flags().Changed("watch-namespace") { - _, defaultNamespace, err := k8sinternal.GetKubeconfigAndNamespace(c.kubeconfig) - if err != nil { - return fmt.Errorf("error getting kubeconfig and default namespace: %v", err) - } - c.localArgs.WatchNamespace = defaultNamespace - } - } - - c.localArgs.Kubeconfig = c.kubeconfig - if err := c.localArgs.Run(); err != nil { - log.Fatalf("Failed to run operator locally: %v", err) - } - } - return nil - }, - } - - // Shared flags. - cmd.Flags().StringVar(&c.kubeconfig, "kubeconfig", "", - "The file path to kubernetes configuration file. Defaults to location "+ - "specified by $KUBECONFIG, or to default file rules if not set") - err := cmd.Flags().MarkDeprecated("kubeconfig", - "use --kubeconfig with 'local' or 'packagemanifests' subcommands instead") - if err != nil { - panic(err) - } - // Deprecated: namespace exists for historical compatibility. Use watch-namespace instead. - //TODO: remove namespace flag before 1.0.0 - if !kbutil.HasProjectFile() { // not show for the kb layout projects - cmd.Flags().StringVar(&c.namespace, "namespace", "", - "The namespace in which operator and namespaces resources are run") - err := cmd.Flags().MarkDeprecated("namespace", "use --watch-namespaces (with --local) "+ - "or --operator-namespace (with --olm) instead") - if err != nil { - panic(err) - } - } - - // 'run --olm' and related flags. - cmd.Flags().BoolVar(&c.olm, "olm", false, - "The operator to be run will be managed by OLM in a cluster. "+ - "Cannot be set with another run-type flag") - err = cmd.Flags().MarkDeprecated("olm", "use 'run packagemanifests' instead") - if err != nil { - panic(err) - } - // Mark all flags used with '--olm' as deprecated and hidden separately so - // all other 'run' flags are still available. - olmFS := pflag.NewFlagSet("olm", pflag.ExitOnError) - olmFS.StringVar(&c.olmArgs.ManifestsDir, "manifests", "", - "Directory containing operator package directories and a package manifest file") - c.olmArgs.AddToFlagSet(olmFS) - olmFS.VisitAll(func(f *pflag.Flag) { - f.Deprecated = "use this flag with 'run packagemanifests' instead" - f.Hidden = true - }) - cmd.Flags().AddFlagSet(olmFS) - - // 'run --local' and related flags. - cmd.Flags().BoolVar(&c.local, "local", false, - "The operator will be run locally by building the operator binary with "+ - "the ability to access a kubernetes cluster using a kubeconfig file. "+ - "Cannot be set with another run-type flag.") - err = cmd.Flags().MarkDeprecated("local", "use 'run local' instead") - if err != nil { - panic(err) - } - localFS := pflag.NewFlagSet("local", pflag.ExitOnError) - c.localArgs.AddToFlags(localFS) - switch projutil.GetOperatorType() { - case projutil.OperatorTypeAnsible: - c.localArgs.AnsibleOperatorFlags = aoflags.AddTo(localFS, "(ansible operator)") - case projutil.OperatorTypeHelm: - c.localArgs.HelmOperatorFlags = hoflags.AddTo(localFS, "(helm operator)") - } - localFS.VisitAll(func(f *pflag.Flag) { - f.Deprecated = "use this flag with 'run local' instead" - f.Hidden = true - }) - cmd.Flags().AddFlagSet(localFS) - - cmd.AddCommand( - packagemanifests.NewCmd(), - local.NewCmd(), - ) - - return cmd -} diff --git a/cmd/operator-sdk/run/local/cmd.go b/cmd/operator-sdk/run/local/cmd.go deleted file mode 100644 index 6a94b00e9c..0000000000 --- a/cmd/operator-sdk/run/local/cmd.go +++ /dev/null @@ -1,71 +0,0 @@ -// 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 local - -import ( - "fmt" - - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - - k8sinternal "github.com/operator-framework/operator-sdk/internal/util/k8sutil" - kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder" - "github.com/operator-framework/operator-sdk/internal/util/projutil" - aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags" - hoflags "github.com/operator-framework/operator-sdk/pkg/helm/flags" -) - -func NewCmd() *cobra.Command { - c := &RunLocalCmd{} - - cmd := &cobra.Command{ - Use: "local", - Short: "Run an Operator locally", - Long: `This command will run your Operator locally by building the operator binary -with the ability to access a kubernetes cluster using a kubeconfig file`, - RunE: func(cmd *cobra.Command, args []string) error { - projutil.MustInProjectRoot() - - // The main.go and manager.yaml scaffolds in the new layout do not support the WATCH_NAMESPACE - // env var to configure the namespace that the operator watches. The default is all namespaces. - // So this flag is unsupported for the new layout. - if !kbutil.HasProjectFile() { - // Get default namespace to watch if unset. - if !cmd.Flags().Changed("watch-namespace") { - _, defaultNamespace, err := k8sinternal.GetKubeconfigAndNamespace(c.Kubeconfig) - if err != nil { - return fmt.Errorf("error getting kubeconfig and default namespace: %v", err) - } - c.WatchNamespace = defaultNamespace - } - } - - if err := c.Run(); err != nil { - log.Fatalf("Failed to run operator: %v", err) - } - return nil - }, - } - - c.AddToFlags(cmd.Flags()) - switch projutil.GetOperatorType() { - case projutil.OperatorTypeAnsible: - c.AnsibleOperatorFlags = aoflags.AddTo(cmd.Flags(), "(ansible operator)") - case projutil.OperatorTypeHelm: - c.HelmOperatorFlags = hoflags.AddTo(cmd.Flags(), "(helm operator)") - } - - return cmd -} diff --git a/cmd/operator-sdk/run/local/local.go b/cmd/operator-sdk/run/local/local.go deleted file mode 100644 index bd5639b6f5..0000000000 --- a/cmd/operator-sdk/run/local/local.go +++ /dev/null @@ -1,256 +0,0 @@ -// Copyright 2018 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 local - -import ( - "fmt" - "os" - "os/exec" - "os/signal" - "path" - "path/filepath" - "runtime" - "strings" - "syscall" - - log "github.com/sirupsen/logrus" - "github.com/spf13/pflag" - logf "sigs.k8s.io/controller-runtime/pkg/log" - - "github.com/operator-framework/operator-sdk/internal/scaffold" - kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder" - "github.com/operator-framework/operator-sdk/internal/util/projutil" - "github.com/operator-framework/operator-sdk/pkg/ansible" - aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags" - "github.com/operator-framework/operator-sdk/pkg/helm" - hoflags "github.com/operator-framework/operator-sdk/pkg/helm/flags" - "github.com/operator-framework/operator-sdk/pkg/k8sutil" - "github.com/operator-framework/operator-sdk/pkg/log/zap" -) - -type RunLocalCmd struct { - Kubeconfig string - WatchNamespace string - OperatorFlags string - LDFlags string - EnableDelve bool - AnsibleOperatorFlags *aoflags.AnsibleOperatorFlags - HelmOperatorFlags *hoflags.HelmOperatorFlags -} - -func (c *RunLocalCmd) AddToFlags(fs *pflag.FlagSet) { - // The main.go and manager.yaml scaffolds in the new layout do not support the WATCH_NAMESPACE - // env var to configure the namespace that the operator watches. The default is all namespaces. - // So this flag is unsupported for the new layout. - if !kbutil.HasProjectFile() { - fs.StringVar(&c.WatchNamespace, "watch-namespace", "", - "The namespace where the operator watches for changes. Set \"\" for AllNamespaces, "+ - "set \"ns1,ns2\" for MultiNamespace") - } - - fs.StringVar(&c.Kubeconfig, "kubeconfig", "", - "The file path to kubernetes configuration file. Defaults to location "+ - "specified by $KUBECONFIG, or to default file rules if not set") - fs.StringVar(&c.OperatorFlags, "operator-flags", "", - "The flags that the operator needs. Example: \"--flag1 value1 --flag2=value2\"") - fs.StringVar(&c.LDFlags, "go-ldflags", "", "Set Go linker options") - fs.BoolVar(&c.EnableDelve, "enable-delve", false, - "Start the operator using the delve debugger") -} - -func (c RunLocalCmd) Run() error { - // The new layout will not have c.WatchNamespace - if kbutil.HasProjectFile() { - log.Infof("Running the operator locally ...") - } else { - log.Infof("Running the operator locally; watching namespace %q", c.WatchNamespace) - } - switch t := projutil.GetOperatorType(); t { - case projutil.OperatorTypeGo: - return c.runGo() - case projutil.OperatorTypeAnsible: - return c.runAnsible() - case projutil.OperatorTypeHelm: - return c.runHelm() - } - return projutil.ErrUnknownOperatorType{} -} - -// runGo will run the project locally for the Go Type projects -func (c RunLocalCmd) runGo() error { - // Build the project and generate binary that will be executed - binName, err := c.generateBinary() - if err != nil { - return err - } - // Get the args that will be used to exec the binary. - // Users are allowed to use the flag operator-flags to pass any value that they may wish - args := c.argsFromOperatorFlags() - // Build the command - var cmd *exec.Cmd - if c.EnableDelve { - cmd = getExecCmdWithDebugger(binName, args) - } else { - cmd = exec.Command(binName, args...) - } - // Kill the command if an exit signal is received. - ch := make(chan os.Signal) - signal.Notify(ch, os.Interrupt, syscall.SIGTERM) - go func() { - <-ch - err := cmd.Process.Kill() - if err != nil { - log.Fatalf("Failed to terminate the operator: (%v)", err) - } - os.Exit(0) - }() - // Add default env vars and values informed via flags - c.addEnvVars(cmd) - if err := projutil.ExecCmd(cmd); err != nil { - return fmt.Errorf("failed to run operator locally: %v", err) - } - return nil -} - -func (c RunLocalCmd) runAnsible() error { - logf.SetLogger(zap.Logger()) - if err := setupOperatorEnv(c.Kubeconfig, c.WatchNamespace); err != nil { - return err - } - return ansible.Run(c.AnsibleOperatorFlags) -} - -func (c RunLocalCmd) runHelm() error { - logf.SetLogger(zap.Logger()) - if err := setupOperatorEnv(c.Kubeconfig, c.WatchNamespace); err != nil { - return err - } - return helm.Run(c.HelmOperatorFlags) -} - -// setupOperatorEnv will add envvar for the kubeconfig and namespace informed -func setupOperatorEnv(kubeconfig, namespace string) error { - // Set the kubeconfig that the manager will be able to grab - // only set env var if user explicitly specified a kubeconfig path - if kubeconfig != "" { - if err := os.Setenv(k8sutil.KubeConfigEnvVar, kubeconfig); err != nil { - return fmt.Errorf("failed to set %s environment variable: %v", k8sutil.KubeConfigEnvVar, err) - } - } - // Set the namespace that the manager will be able to grab - if namespace != "" { - if err := os.Setenv(k8sutil.WatchNamespaceEnvVar, namespace); err != nil { - return fmt.Errorf("failed to set %s environment variable: %v", k8sutil.WatchNamespaceEnvVar, err) - } - } - if _, err := k8sutil.GetOperatorName(); err != nil { - operatorName := filepath.Base(projutil.MustGetwd()) - if err := os.Setenv(k8sutil.OperatorNameEnvVar, operatorName); err != nil { - return fmt.Errorf("failed to set %s environment variable: %v", k8sutil.OperatorNameEnvVar, err) - } - } - return nil -} - -// getBuildRunLocalArgs returns go build args for -ldflags and -gcflags -func (c RunLocalCmd) getBuildRunLocalArgs() []string { - var args []string - if c.LDFlags != "" { - args = []string{"-ldflags", c.LDFlags} - } - if c.EnableDelve { - args = append(args, "-gcflags=\"all=-N -l\"") - } - return args -} - -// generateBinary will build the Go project by running the command `go build -o bin/manager main.go` -func (c RunLocalCmd) generateBinary() (string, error) { - // Define name of the bin and where is the main.go pkg for each layout - var outputBinName, packagePath string - if kbutil.HasProjectFile() { - outputBinName = filepath.Join(kbutil.BinBuildDir, getProjectName()+"-local") - packagePath = projutil.GetGoPkg() - } else { - // todo: remove the if, else when the legacy code is no longer supported - packagePath = path.Join(projutil.GetGoPkg(), filepath.ToSlash(scaffold.ManagerDir)) - outputBinName = filepath.Join(scaffold.BuildBinDir, getProjectName()+"-local") - } - // allow the command works in windows SO - if runtime.GOOS == "windows" { - outputBinName += ".exe" - } - opts := projutil.GoCmdOptions{ - BinName: outputBinName, - PackagePath: packagePath, - Args: c.getBuildRunLocalArgs(), - } - if err := projutil.GoBuild(opts); err != nil { - return "", err - } - return outputBinName, nil -} - -// execCmdForBinWithDebugger will exec the command with the delve required args -// Note that delve is a debugger for the Go programming language. -// More info: https://github.com/go-delve/delve -func getExecCmdWithDebugger(binName string, args []string) *exec.Cmd { - delveArgs := []string{"--listen=:2345", "--headless=true", "--api-version=2", "exec", binName, "--"} - delveArgs = append(delveArgs, args...) - log.Infof("Delve debugger enabled with args %s", delveArgs) - return exec.Command("dlv", delveArgs...) -} - -// addEnvVars will add the EnvVars to the command informed -func (c RunLocalCmd) addEnvVars(cmd *exec.Cmd) { - cmd.Env = os.Environ() - - // Set EnvVar to let the project knows that it is running outside of the cluster - cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k8sutil.ForceRunModeEnv, k8sutil.LocalRunMode)) - - // Only set env var if user explicitly specified a kubeconfig path - if c.Kubeconfig != "" { - cmd.Env = append(cmd.Env, fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, c.Kubeconfig)) - } - - // Set WATCH_NAMESPACE with the value informed via flag - cmd.Env = append(cmd.Env, fmt.Sprintf("%v=%v", k8sutil.WatchNamespaceEnvVar, c.WatchNamespace)) - - // todo: check if it should really be here. Shows that it should be part of AnsibleRun only. - // Set the ANSIBLE_ROLES_PATH - if c.AnsibleOperatorFlags != nil && len(c.AnsibleOperatorFlags.AnsibleRolesPath) > 0 { - log.Info(fmt.Sprintf("set the value %v for environment variable %v.", - c.AnsibleOperatorFlags.AnsibleRolesPath, aoflags.AnsibleRolesPathEnvVar)) - cmd.Env = append(cmd.Env, fmt.Sprintf("%v=%v", aoflags.AnsibleRolesPathEnvVar, - c.AnsibleOperatorFlags.AnsibleRolesPath)) - } -} - -// argsFromOperatorFlags will return an array with all args used in the flags -func (c RunLocalCmd) argsFromOperatorFlags() []string { - args := []string{} - if c.OperatorFlags != "" { - extraArgs := strings.Split(c.OperatorFlags, " ") - args = append(args, extraArgs...) - } - return args -} - -// getProjectName will return the name of the project. This function only works if the current working directory -// is the project root. -func getProjectName() string { - absProjectPath := projutil.MustGetwd() - return filepath.Base(absProjectPath) -} diff --git a/cmd/operator-sdk/run/packagemanifests/packagemanifests.go b/cmd/operator-sdk/run/packagemanifests/packagemanifests.go index 93221439e1..736e4931ab 100644 --- a/cmd/operator-sdk/run/packagemanifests/packagemanifests.go +++ b/cmd/operator-sdk/run/packagemanifests/packagemanifests.go @@ -16,15 +16,11 @@ package packagemanifests import ( "fmt" - "path/filepath" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - olmcatalog "github.com/operator-framework/operator-sdk/internal/generate/olm-catalog" olmoperator "github.com/operator-framework/operator-sdk/internal/olm/operator" - kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder" - "github.com/operator-framework/operator-sdk/internal/util/projutil" ) type packagemanifestsCmd struct { @@ -47,13 +43,7 @@ must be set to a valid package manifests root directory, ex. '/pac } c.ManifestsDir = args[0] } else { - // Choose the default path depending on project configuration. - if kbutil.HasProjectFile() { - c.ManifestsDir = "packagemanifests" - } else { - operatorName := filepath.Base(projutil.MustGetwd()) - c.ManifestsDir = filepath.Join(olmcatalog.OLMCatalogDir, operatorName) - } + c.ManifestsDir = "packagemanifests" } log.Infof("Running operator from directory %s", c.ManifestsDir) diff --git a/website/content/en/docs/cli/operator-sdk.md b/website/content/en/docs/cli/operator-sdk.md index e6423bd9f9..c4f8c49d0c 100644 --- a/website/content/en/docs/cli/operator-sdk.md +++ b/website/content/en/docs/cli/operator-sdk.md @@ -21,11 +21,9 @@ An SDK for building operators with ease * [operator-sdk alpha](../operator-sdk_alpha) - Run an alpha subcommand * [operator-sdk build](../operator-sdk_build) - Compiles code and builds artifacts * [operator-sdk bundle](../operator-sdk_bundle) - Manage operator bundle metadata -* [operator-sdk cleanup](../operator-sdk_cleanup) - Delete and clean up after a running Operator * [operator-sdk completion](../operator-sdk_completion) - Generators for shell completions * [operator-sdk generate](../operator-sdk_generate) - Invokes a specific generator * [operator-sdk new](../operator-sdk_new) - Creates a new operator application * [operator-sdk olm](../operator-sdk_olm) - Manage the Operator Lifecycle Manager installation in your cluster -* [operator-sdk run](../operator-sdk_run) - Run an Operator in a variety of environments * [operator-sdk version](../operator-sdk_version) - Prints the version of operator-sdk diff --git a/website/content/en/docs/cli/operator-sdk_cleanup.md b/website/content/en/docs/cli/operator-sdk_cleanup.md deleted file mode 100644 index 40c3b5d5b4..0000000000 --- a/website/content/en/docs/cli/operator-sdk_cleanup.md +++ /dev/null @@ -1,26 +0,0 @@ ---- -title: "operator-sdk cleanup" ---- -## operator-sdk cleanup - -Delete and clean up after a running Operator - -### Synopsis - -Delete and clean up after a running Operator - -``` -operator-sdk cleanup [flags] -``` - -### Options - -``` - -h, --help help for cleanup -``` - -### SEE ALSO - -* [operator-sdk](../operator-sdk) - An SDK for building operators with ease -* [operator-sdk cleanup packagemanifests](../operator-sdk_cleanup_packagemanifests) - Clean up an Operator in the package manifests format deployed with OLM - diff --git a/website/content/en/docs/cli/operator-sdk_cleanup_packagemanifests.md b/website/content/en/docs/cli/operator-sdk_cleanup_packagemanifests.md deleted file mode 100644 index 289636f060..0000000000 --- a/website/content/en/docs/cli/operator-sdk_cleanup_packagemanifests.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -title: "operator-sdk cleanup packagemanifests" ---- -## operator-sdk cleanup packagemanifests - -Clean up an Operator in the package manifests format deployed with OLM - -### Synopsis - -'cleanup packagemanifests' destroys an Operator deployed with OLM using the 'run packagemanifests' command. -The command's argument must be set to a valid package manifests root directory, -ex. '/packagemanifests'. - -``` -operator-sdk cleanup packagemanifests [flags] -``` - -### Options - -``` - -h, --help help for packagemanifests - --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 The file path to kubernetes configuration file. Defaults to location specified by $KUBECONFIG, or to default file rules if not set - --olm-namespace string The namespace where OLM is installed (default "olm") - --operator-namespace string The namespace where operator resources are created. It must already exist in the cluster or be defined in a manifest passed to --include - --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 cleanup](../operator-sdk_cleanup) - Delete and clean up after a running Operator - diff --git a/website/content/en/docs/cli/operator-sdk_run.md b/website/content/en/docs/cli/operator-sdk_run.md deleted file mode 100644 index 3901d5fafe..0000000000 --- a/website/content/en/docs/cli/operator-sdk_run.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -title: "operator-sdk run" ---- -## operator-sdk run - -Run an Operator in a variety of environments - -### Synopsis - -This command has subcommands that will run or deploy your Operator in two -different modes: locally and using OLM. These modes are controlled by using 'local' -or 'packagemanifests' subcommands. Run 'operator-sdk run --help' for more -information on these subcommands. - - -``` -operator-sdk run [flags] -``` - -### Options - -``` - -h, --help help for run -``` - -### SEE ALSO - -* [operator-sdk](../operator-sdk) - An SDK for building operators with ease -* [operator-sdk run local](../operator-sdk_run_local) - Run an Operator locally -* [operator-sdk run packagemanifests](../operator-sdk_run_packagemanifests) - Deploy an Operator in the package manifests format with OLM - diff --git a/website/content/en/docs/cli/operator-sdk_run_local.md b/website/content/en/docs/cli/operator-sdk_run_local.md deleted file mode 100644 index 6eacc0f307..0000000000 --- a/website/content/en/docs/cli/operator-sdk_run_local.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -title: "operator-sdk run local" ---- -## operator-sdk run local - -Run an Operator locally - -### Synopsis - -This command will run your Operator locally by building the operator binary -with the ability to access a kubernetes cluster using a kubeconfig file - -``` -operator-sdk run local [flags] -``` - -### Options - -``` - --enable-delve Start the operator using the delve debugger - --go-ldflags string Set Go linker options - -h, --help help for local - --kubeconfig string The file path to kubernetes configuration file. Defaults to location specified by $KUBECONFIG, or to default file rules if not set - --operator-flags string The flags that the operator needs. Example: "--flag1 value1 --flag2=value2" - --watch-namespace string The namespace where the operator watches for changes. Set "" for AllNamespaces, set "ns1,ns2" for MultiNamespace -``` - -### SEE ALSO - -* [operator-sdk run](../operator-sdk_run) - Run an Operator in a variety of environments - diff --git a/website/content/en/docs/cli/operator-sdk_run_packagemanifests.md b/website/content/en/docs/cli/operator-sdk_run_packagemanifests.md deleted file mode 100644 index 3302f484c7..0000000000 --- a/website/content/en/docs/cli/operator-sdk_run_packagemanifests.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -title: "operator-sdk run packagemanifests" ---- -## operator-sdk run packagemanifests - -Deploy an Operator in the package manifests format with OLM - -### Synopsis - -'run packagemanifests' deploys an Operator's package manifests with OLM. The command's argument -must be set to a valid package manifests root directory, ex. '/packagemanifests'. - -``` -operator-sdk run packagemanifests [flags] -``` - -### Options - -``` - -h, --help help for packagemanifests - --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 The file path to kubernetes configuration file. Defaults to location specified by $KUBECONFIG, or to default file rules if not set - --olm-namespace string The namespace where OLM is installed (default "olm") - --operator-namespace string The namespace where operator resources are created. It must already exist in the cluster or be defined in a manifest passed to --include - --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 run](../operator-sdk_run) - Run an Operator in a variety of environments - From 9149775e14d2d3db042f8e66ea99356588917b41 Mon Sep 17 00:00:00 2001 From: Joe Lanford Date: Mon, 13 Jul 2020 18:22:29 -0400 Subject: [PATCH 2/2] add changelog fragment --- changelog/fragments/rm-legacy-run.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 changelog/fragments/rm-legacy-run.yaml diff --git a/changelog/fragments/rm-legacy-run.yaml b/changelog/fragments/rm-legacy-run.yaml new file mode 100644 index 0000000000..41538803e5 --- /dev/null +++ b/changelog/fragments/rm-legacy-run.yaml @@ -0,0 +1,14 @@ +# entries is a list of entries to include in +# release notes and/or the migration guide +entries: + - description: > + Remove legacy run amd cleanup commands + + kind: "removal" + + # Is this a breaking change? + breaking: true + + migration: + header: Remove legacy run amd cleanup commands + body: TBD