From ec75abc0b973158efe424df2c4f6dbe5c13f89d1 Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Tue, 21 Jul 2020 12:48:41 -0700 Subject: [PATCH] cmd/operator-sdk/generate: remove unused legacy code --- cmd/operator-sdk/generate/bundle/bundle.go | 4 +- .../generate/bundle/bundle_legacy.go | 203 ------------------ cmd/operator-sdk/generate/bundle/cmd.go | 89 +------- .../generate/packagemanifests/cmd.go | 4 +- 4 files changed, 7 insertions(+), 293 deletions(-) delete mode 100644 cmd/operator-sdk/generate/bundle/bundle_legacy.go diff --git a/cmd/operator-sdk/generate/bundle/bundle.go b/cmd/operator-sdk/generate/bundle/bundle.go index 3f75ac6dff..e2360feecc 100644 --- a/cmd/operator-sdk/generate/bundle/bundle.go +++ b/cmd/operator-sdk/generate/bundle/bundle.go @@ -98,8 +98,8 @@ https://github.com/operator-framework/operator-registry/#manifest-format // defaultRootDir is the default root directory in which to generate bundle files. const defaultRootDir = "bundle" -// setCommonDefaults sets defaults useful to all modes of this subcommand. -func (c *bundleCmd) setCommonDefaults(cfg *config.Config) { +// setDefaults sets defaults useful to all modes of this subcommand. +func (c *bundleCmd) setDefaults(cfg *config.Config) { if c.operatorName == "" { c.operatorName = filepath.Base(cfg.Repo) } diff --git a/cmd/operator-sdk/generate/bundle/bundle_legacy.go b/cmd/operator-sdk/generate/bundle/bundle_legacy.go deleted file mode 100644 index 4a84e9082d..0000000000 --- a/cmd/operator-sdk/generate/bundle/bundle_legacy.go +++ /dev/null @@ -1,203 +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 bundle - -import ( - "fmt" - "path/filepath" - "strings" - - "github.com/operator-framework/operator-registry/pkg/lib/bundle" - log "github.com/sirupsen/logrus" - - genutil "github.com/operator-framework/operator-sdk/cmd/operator-sdk/generate/internal" - gencsv "github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion" - "github.com/operator-framework/operator-sdk/internal/generate/collector" - "github.com/operator-framework/operator-sdk/internal/util/projutil" -) - -const ( - longHelpLegacy = ` -Running 'generate bundle' is the first step to publishing your operator to a catalog -and/or deploying it with OLM. This command generates a set of bundle manifests, -metadata, and a bundle.Dockerfile for your operator, and will interactively ask -for UI metadata, an important component of publishing your operator, by default unless -a bundle for your operator exists or you set '--interactive=false'. - -Set '--version' to supply a semantic version for your bundle if you are creating one -for the first time or upgrading an existing one. - -If '--output-dir' is set and you wish to build bundle images from that directory, -either manually update your bundle.Dockerfile or set '--overwrite'. - -More information on bundles: -https://github.com/operator-framework/operator-registry/#manifest-format -` - - examplesLegacy = ` - # Create bundle manifests, metadata, and a bundle.Dockerfile: - $ operator-sdk generate bundle --version 0.0.1 - INFO[0000] Generating bundle manifest version 0.0.1 - - Display name for the operator (required): - > memcached-operator - ... - - # After running the above commands, you should see: - $ tree deploy/olm-catalog - deploy/olm-catalog - └── memcached-operator - ├── manifests - │ ├── cache.example.com_memcacheds_crd.yaml - │ └── memcached-operator.clusterserviceversion.yaml - └── metadata - └── annotations.yaml - - # Then build and push your bundle image: - $ export USERNAME= - $ export BUNDLE_IMG=quay.io/$USERNAME/memcached-operator-bundle:v0.0.1 - $ docker build -f bundle.Dockerfile -t $BUNDLE_IMG . - Sending build context to Docker daemon 42.33MB - Step 1/9 : FROM scratch - ... - $ docker push $BUNDLE_IMG -` -) - -// setCommonDefaultsLegacy sets defaults useful to all modes of this subcommand. -func (c *bundleCmdLegacy) setCommonDefaults() { - if c.operatorName == "" { - c.operatorName = filepath.Base(projutil.MustGetwd()) - } - // A default channel can be inferred if there is only one channel. Don't infer - // default otherwise; the user must set this value. - if c.defaultChannel == "" && strings.Count(c.channels, ",") == 0 { - c.defaultChannel = c.channels - } -} - -// validateManifestsLegacy validates c for bundle manifests generation for -// legacy project layouts. -func (c bundleCmdLegacy) validateManifests() error { - if c.version != "" { - if err := genutil.ValidateVersion(c.version); err != nil { - return err - } - } - return nil -} - -// runManifestsLegacy generates bundle manifests for legacy project layouts. -func (c bundleCmdLegacy) runManifests() (err error) { - - if !c.quiet { - if c.version == "" { - log.Info("Generating bundle manifests") - } else { - log.Infoln("Generating bundle manifests version", c.version) - } - } - - if c.apisDir == "" { - c.apisDir = filepath.Join("pkg", "apis") - } - if c.deployDir == "" { - c.deployDir = "deploy" - } - if c.crdsDir == "" { - c.crdsDir = filepath.Join(c.deployDir, "crds") - } - defaultBundleDir := filepath.Join(c.deployDir, "olm-catalog", c.operatorName) - if c.inputDir == "" { - c.inputDir = defaultBundleDir - } - if c.outputDir == "" { - c.outputDir = defaultBundleDir - } - - col := &collector.Manifests{} - if err := col.UpdateFromDirs(c.deployDir, c.crdsDir); err != nil { - return err - } - - csvGen := gencsv.Generator{ - OperatorName: c.operatorName, - OperatorType: projutil.GetOperatorType(), - Version: c.version, - Collector: col, - } - - opts := []gencsv.LegacyOption{ - gencsv.WithBundleBase(c.inputDir, c.apisDir, c.interactiveLevel), - gencsv.LegacyOption(gencsv.WithBundleWriter(c.outputDir)), - } - if err := csvGen.GenerateLegacy(opts...); err != nil { - return fmt.Errorf("error generating ClusterServiceVersion: %v", err) - } - - var objs []interface{} - for _, crd := range col.V1CustomResourceDefinitions { - objs = append(objs, crd) - } - for _, crd := range col.V1beta1CustomResourceDefinitions { - objs = append(objs, crd) - } - dir := filepath.Join(c.outputDir, bundle.ManifestsDir) - if err := genutil.WriteObjectsToFilesLegacy(dir, objs...); err != nil { - return err - } - - if !c.quiet { - log.Infoln("Bundle manifests generated successfully in", c.outputDir) - } - - return nil -} - -// validateMetadataLegacy validates c for bundle metadata generation for -// legacy project layouts. -func (c bundleCmdLegacy) validateMetadata() (err error) { - // Ensure a default channel is present. - if c.defaultChannel == "" { - return fmt.Errorf("--default-channel must be set if setting multiple channels") - } - - return nil -} - -// runMetadataLegacy generates a bundle.Dockerfile and bundle metadata for -// legacy project layouts. -func (c bundleCmdLegacy) runMetadata() error { - - directory := c.inputDir - if directory == "" { - // There may be no existing bundle at the default path, so assume manifests - // were generated in the output directs. - defaultDirectory := filepath.Join("deploy", "olm-catalog", c.operatorName, bundle.ManifestsDir) - if c.outputDir != "" && genutil.IsNotExist(defaultDirectory) { - directory = filepath.Join(c.outputDir, bundle.ManifestsDir) - } else { - directory = defaultDirectory - } - } else { - directory = filepath.Join(directory, bundle.ManifestsDir) - } - outputDir := c.outputDir - if filepath.Clean(outputDir) == filepath.Clean(directory) { - outputDir = "" - } - - return c.generateMetadata(directory, outputDir) -} diff --git a/cmd/operator-sdk/generate/bundle/cmd.go b/cmd/operator-sdk/generate/bundle/cmd.go index 1c7d2925be..13719e9615 100644 --- a/cmd/operator-sdk/generate/bundle/cmd.go +++ b/cmd/operator-sdk/generate/bundle/cmd.go @@ -23,7 +23,6 @@ import ( "github.com/spf13/pflag" kbutil "github.com/operator-framework/operator-sdk/internal/util/kubebuilder" - "github.com/operator-framework/operator-sdk/internal/util/projutil" ) //nolint:maligned @@ -49,15 +48,6 @@ type bundleCmd struct { overwrite bool } -//nolint:maligned -type bundleCmdLegacy struct { - bundleCmd - - apisDir string - interactiveLevel projutil.InteractiveLevel - interactive bool -} - // NewCmd returns the 'bundle' command configured for the new project layout. func NewCmd() *cobra.Command { c := &bundleCmd{} @@ -82,7 +72,7 @@ func NewCmd() *cobra.Command { if err != nil { return fmt.Errorf("error reading configuration: %v", err) } - c.setCommonDefaults(cfg) + c.setDefaults(cfg) // Validate command args before running so a preceding mode doesn't run // before a following validation fails. @@ -117,85 +107,12 @@ func NewCmd() *cobra.Command { "Directory containing kustomize bases and a kustomization.yaml for operator-framework manifests") cmd.Flags().BoolVar(&c.stdout, "stdout", false, "Write bundle manifest to stdout") - c.addCommonFlagsTo(cmd.Flags()) - - return cmd -} - -// NewCmdLegacy returns the 'bundle' command configured for the legacy project layout. -func NewCmdLegacy() *cobra.Command { - c := &bundleCmdLegacy{} - cmd := &cobra.Command{ - Use: "bundle", - Short: "Generates bundle data for the operator", - Long: longHelpLegacy, - Example: examplesLegacy, - RunE: func(cmd *cobra.Command, args []string) (err error) { - if len(args) != 0 { - return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath()) - } - - // Check if the user has any specific preference to enable/disable - // interactive prompts. Default behaviour is to disable the prompt - // unless a base bundle does not exist. - if cmd.Flags().Changed("interactive") { - if c.interactive { - c.interactiveLevel = projutil.InteractiveOnAll - } else { - c.interactiveLevel = projutil.InteractiveHardOff - } - } - - // Generate manifests and metadata by default if no flags are set so - // the default behavior is "do everything". - fs := cmd.Flags() - if !fs.Changed("metadata") && !fs.Changed("manifests") { - c.metadata = true - c.manifests = true - } - - c.setCommonDefaults() - - // Validate command args before running so a preceding mode doesn't run - // before a following validation fails. - if c.manifests { - if err = c.validateManifests(); err != nil { - return fmt.Errorf("invalid command options: %v", err) - } - } - if c.metadata { - if err = c.validateMetadata(); err != nil { - return fmt.Errorf("invalid command options: %v", err) - } - } - - // Run command logic. - if c.manifests { - if err = c.runManifests(); err != nil { - log.Fatalf("Error generating bundle manifests: %v", err) - } - } - if c.metadata { - if err = c.runMetadata(); err != nil { - log.Fatalf("Error generating bundle metadata: %v", err) - } - } - - return nil - }, - } - - cmd.Flags().StringVar(&c.apisDir, "apis-dir", "", "Root directory for API type defintions") - cmd.Flags().BoolVar(&c.interactive, "interactive", false, "When set or no bundle base exists, an interactive "+ - "command prompt will be presented to accept bundle ClusterServiceVersion metadata") - - c.addCommonFlagsTo(cmd.Flags()) + c.addFlagsTo(cmd.Flags()) return cmd } -// TODO(estroz): add flag to skip API metadata regeneration. -func (c *bundleCmd) addCommonFlagsTo(fs *pflag.FlagSet) { +func (c *bundleCmd) addFlagsTo(fs *pflag.FlagSet) { fs.BoolVar(&c.manifests, "manifests", false, "Generate bundle manifests") fs.BoolVar(&c.metadata, "metadata", false, "Generate bundle metadata and Dockerfile") diff --git a/cmd/operator-sdk/generate/packagemanifests/cmd.go b/cmd/operator-sdk/generate/packagemanifests/cmd.go index 4113f8f3e3..31ebee48a2 100644 --- a/cmd/operator-sdk/generate/packagemanifests/cmd.go +++ b/cmd/operator-sdk/generate/packagemanifests/cmd.go @@ -80,12 +80,12 @@ func NewCmd() *cobra.Command { "Directory containing kustomize bases and a kustomization.yaml for operator-framework manifests") cmd.Flags().BoolVar(&c.stdout, "stdout", false, "Write package to stdout") - c.addCommonFlagsTo(cmd.Flags()) + c.addFlagsTo(cmd.Flags()) return cmd } -func (c *packagemanifestsCmd) addCommonFlagsTo(fs *pflag.FlagSet) { +func (c *packagemanifestsCmd) addFlagsTo(fs *pflag.FlagSet) { fs.StringVar(&c.operatorName, "operator-name", "", "Name of the packaged operator") fs.StringVarP(&c.version, "version", "v", "", "Semantic version of the packaged operator") fs.StringVar(&c.inputDir, "input-dir", "", "Directory to read existing package manifests from. "+