|
| 1 | +package index |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/sirupsen/logrus" |
| 5 | + "github.com/spf13/cobra" |
| 6 | + "k8s.io/kubectl/pkg/util/templates" |
| 7 | + |
| 8 | + "github.com/operator-framework/operator-registry/pkg/containertools" |
| 9 | + "github.com/operator-framework/operator-registry/pkg/lib/indexer" |
| 10 | +) |
| 11 | + |
| 12 | +var deprecateLong = templates.LongDesc(` |
| 13 | + Deprecate operator bundles from an index. |
| 14 | + |
| 15 | + Deprecated bundles will no longer be installable. Bundles that are replaced by deprecated bundles will be removed enirely from the index. |
| 16 | + |
| 17 | + For example: |
| 18 | +
|
| 19 | + Given the update graph in quay.io/my/index:v1 |
| 20 | + 1.4.0 -- replaces -> 1.3.0 -- replaces -> 1.2.0 -- replaces -> 1.1.0 |
| 21 | +
|
| 22 | + Applying the command: |
| 23 | + opm index deprecate --bundles "quay.io/my/bundle:1.3.0" --from-index "quay.io/my/index:v1" --tag "quay.io/my/index:v2" |
| 24 | +
|
| 25 | + Produces the following update graph in quay.io/my/index:v2 |
| 26 | + 1.4.0 -- replaces -> 1.3.0 [deprecated] |
| 27 | + |
| 28 | + Deprecating a bundle that removes the default channel is not allowed. |
| 29 | + `) |
| 30 | + |
| 31 | +func newIndexDeprecateCmd() *cobra.Command { |
| 32 | + indexCmd := &cobra.Command{ |
| 33 | + Use: "deprecate", |
| 34 | + Short: "Deprecate operator bundles from an index.", |
| 35 | + Long: "", |
| 36 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 37 | + if debug, _ := cmd.Flags().GetBool("debug"); debug { |
| 38 | + logrus.SetLevel(logrus.DebugLevel) |
| 39 | + } |
| 40 | + return nil |
| 41 | + }, |
| 42 | + RunE: runIndexDeprecateCmdFunc, |
| 43 | + } |
| 44 | + |
| 45 | + indexCmd.Flags().Bool("debug", false, "enable debug logging") |
| 46 | + indexCmd.Flags().Bool("generate", false, "if enabled, just creates the dockerfile and saves it to local disk") |
| 47 | + indexCmd.Flags().StringP("out-dockerfile", "d", "", "if generating the dockerfile, this flag is used to (optionally) specify a dockerfile name") |
| 48 | + indexCmd.Flags().StringP("from-index", "f", "", "previous index to add to") |
| 49 | + indexCmd.Flags().StringSliceP("bundles", "b", nil, "comma separated list of bundles to add") |
| 50 | + if err := indexCmd.MarkFlagRequired("bundles"); err != nil { |
| 51 | + logrus.Panic("Failed to set required `bundles` flag for `index add`") |
| 52 | + } |
| 53 | + indexCmd.Flags().StringP("binary-image", "i", "", "container image for on-image `opm` command") |
| 54 | + indexCmd.Flags().StringP("container-tool", "c", "", "tool to interact with container images (save, build, etc.). One of: [docker, podman]") |
| 55 | + indexCmd.Flags().StringP("build-tool", "u", "", "tool to build container images. One of: [docker, podman]. Defaults to podman. Overrides part of container-tool.") |
| 56 | + indexCmd.Flags().StringP("pull-tool", "p", "", "tool to pull container images. One of: [none, docker, podman]. Defaults to none. Overrides part of container-tool.") |
| 57 | + indexCmd.Flags().StringP("tag", "t", "", "custom tag for container image being built") |
| 58 | + indexCmd.Flags().Bool("permissive", false, "allow registry load errors") |
| 59 | + if err := indexCmd.Flags().MarkHidden("debug"); err != nil { |
| 60 | + logrus.Panic(err.Error()) |
| 61 | + } |
| 62 | + |
| 63 | + return indexCmd |
| 64 | +} |
| 65 | + |
| 66 | +func runIndexDeprecateCmdFunc(cmd *cobra.Command, args []string) error { |
| 67 | + generate, err := cmd.Flags().GetBool("generate") |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + outDockerfile, err := cmd.Flags().GetString("out-dockerfile") |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + fromIndex, err := cmd.Flags().GetString("from-index") |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + bundles, err := cmd.Flags().GetStringSlice("bundles") |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + binaryImage, err := cmd.Flags().GetString("binary-image") |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + tag, err := cmd.Flags().GetString("tag") |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + permissive, err := cmd.Flags().GetBool("permissive") |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + pullTool, buildTool, err := getContainerTools(cmd) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + logger := logrus.WithFields(logrus.Fields{"bundles": bundles}) |
| 108 | + |
| 109 | + logger.Info("deprecating bundles from the index") |
| 110 | + |
| 111 | + indexDeprecator := indexer.NewIndexDeprecator( |
| 112 | + containertools.NewContainerTool(buildTool, containertools.PodmanTool), |
| 113 | + containertools.NewContainerTool(pullTool, containertools.NoneTool), |
| 114 | + logger) |
| 115 | + |
| 116 | + request := indexer.DeprecateFromIndexRequest{ |
| 117 | + Generate: generate, |
| 118 | + FromIndex: fromIndex, |
| 119 | + BinarySourceImage: binaryImage, |
| 120 | + OutDockerfile: outDockerfile, |
| 121 | + Tag: tag, |
| 122 | + Bundles: bundles, |
| 123 | + Permissive: permissive, |
| 124 | + } |
| 125 | + |
| 126 | + err = indexDeprecator.DeprecateFromIndex(request) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
0 commit comments