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
18 changes: 18 additions & 0 deletions changelog/fragments/refactor-cleanup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
entries:
- description: Updated `operator-sdk cleanup` command to be more generic
kind: change
breaking: true
migration:
header: Update usage of `operator-sdk cleanup`
body: |
The `operator-sdk cleanup packagemanifests` command has been
removed and replaced with a simpler `operator-sdk cleanup`
command.

Update usages of `operator-sdk cleanup packagemanifests` to
use `operator-sdk cleanup <packageName>`.

The value for `<packageName>` can be found in the `*.package.yaml`
file in the root of your packagemanifests folder. It is typically
your project name.

38 changes: 28 additions & 10 deletions internal/cmd/operator-sdk/cleanup/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,42 @@
package cleanup

import (
"context"
"time"

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

"github.com/operator-framework/operator-sdk/internal/cmd/operator-sdk/cleanup/packagemanifests"
"github.com/operator-framework/operator-sdk/internal/operator"
)

func NewCmd() *cobra.Command {
var timeout time.Duration
cfg := &operator.Configuration{}
cfg.Log = log.Infof
cmd := &cobra.Command{
Use: "cleanup",
Use: "cleanup <operatorPackageName>",
Short: "Clean up an Operator deployed with the 'run' subcommand",
Long: `This command has subcommands that will destroy an Operator deployed with OLM.
Currently only the package manifests format is supported via the 'packagemanifests' subcommand.
Run 'operator-sdk cleanup --help' for more information.
`,
}
Long: "This command has subcommands that will destroy an Operator deployed with OLM.",
Args: cobra.ExactArgs(1),
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
return cfg.Load()
},
Run: func(cmd *cobra.Command, args []string) {
u := operator.NewUninstall(cfg)
u.Package = args[0]

cmd.AddCommand(
packagemanifests.NewCmd(),
)
ctx, cancel := context.WithTimeout(cmd.Context(), timeout)
defer cancel()

if err := u.Run(ctx); err != nil {
log.Fatalf("Uninstall operator: %v\n", err)
}
log.Infof("Operator %q uninstalled\n", u.Package)
},
}
cmd.Flags().DurationVar(&timeout, "timeout", 2*time.Minute, "Time to wait for the command to complete before failing")
cfg.BindFlags(cmd.PersistentFlags())

return cmd
}

This file was deleted.

21 changes: 20 additions & 1 deletion internal/olm/operator/internal/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"path"

apimanifests "github.com/operator-framework/api/pkg/manifests"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
log "github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

olmclient "github.com/operator-framework/operator-sdk/internal/olm/client"
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
Expand Down Expand Up @@ -110,7 +112,7 @@ func (rr *RegistryResources) IsRegistryDataStale(ctx context.Context, namespace

// CreatePackageManifestsRegistry creates all registry objects required to serve
// manifests from rr.manifests in namespace.
func (rr *RegistryResources) CreatePackageManifestsRegistry(ctx context.Context, namespace string) error {
func (rr *RegistryResources) CreatePackageManifestsRegistry(ctx context.Context, catsrc *v1alpha1.CatalogSource, namespace string) error {
pkgName := rr.Pkg.PackageName
labels := makeRegistryLabels(pkgName)

Expand All @@ -119,6 +121,14 @@ func (rr *RegistryResources) CreatePackageManifestsRegistry(ctx context.Context,
return err
}

catsrcKey := types.NamespacedName{
Namespace: catsrc.Namespace,
Name: catsrc.Name,
}
if err := rr.Client.KubeClient.Get(ctx, catsrcKey, catsrc); err != nil {
return fmt.Errorf("get catalog source: %v", err)
}

// Objects to create.
objs := make([]runtime.Object, 0, len(binaryDataByConfigMap)+2)
// Options for creating a Deployment, since we need to mount all package
Expand All @@ -129,6 +139,9 @@ func (rr *RegistryResources) CreatePackageManifestsRegistry(ctx context.Context,
for cmName, binaryData := range binaryDataByConfigMap {
cm := newConfigMap(cmName, namespace, withBinaryData(binaryData))
cm.SetLabels(labels)
if err := controllerutil.SetOwnerReference(catsrc, cm, olmclient.Scheme); err != nil {
return fmt.Errorf("set configmap %q owner reference: %v", cm.GetName(), err)
}
objs = append(objs, cm)

volName := k8sutil.TrimDNS1123Label(cmName + "-volume")
Expand All @@ -141,8 +154,14 @@ func (rr *RegistryResources) CreatePackageManifestsRegistry(ctx context.Context,
// Add registry Deployment and Service to objects.
dep := newRegistryDeployment(pkgName, namespace, opts...)
dep.SetLabels(labels)
if err := controllerutil.SetOwnerReference(catsrc, dep, olmclient.Scheme); err != nil {
return fmt.Errorf("set deployment %q owner reference: %v", dep.GetName(), err)
}
service := newRegistryService(pkgName, namespace, withTCPPort("grpc", registryGRPCPort))
service.SetLabels(labels)
if err := controllerutil.SetOwnerReference(catsrc, service, olmclient.Scheme); err != nil {
return fmt.Errorf("set service %q owner reference: %v", service.GetName(), err)
}
objs = append(objs, dep, service)

if err := rr.Client.DoCreate(ctx, objs...); err != nil {
Expand Down
15 changes: 2 additions & 13 deletions internal/olm/operator/olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import (
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/operator-framework/operator-sdk/internal/operator"
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
)

// General OperatorGroup for operators created with the SDK.
const sdkOperatorGroupName = "operator-sdk-og"

func getSubscriptionName(csvName string) string {
name := k8sutil.FormatOperatorNameDNS1123(csvName)
return fmt.Sprintf("%s-sub", name)
Expand Down Expand Up @@ -94,15 +92,6 @@ func getCatalogSourceName(pkgName string) string {
return fmt.Sprintf("%s-ocs", name)
}

// withGRPC returns a function that sets the CatalogSource argument's
// server type to GRPC and address at addr.
func withGRPC(addr string) func(*operatorsv1alpha1.CatalogSource) {
return func(catsrc *operatorsv1alpha1.CatalogSource) {
catsrc.Spec.SourceType = operatorsv1alpha1.SourceTypeGrpc
catsrc.Spec.Address = addr
}
}

// newCatalogSource creates a new CatalogSource with a name derived from
// pkgName, the package manifest's packageName, in namespace. opts will
// be applied to the CatalogSource object.
Expand Down Expand Up @@ -151,7 +140,7 @@ func newSDKOperatorGroup(namespace string, opts ...func(*operatorsv1.OperatorGro
Kind: operatorsv1.OperatorGroupKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: sdkOperatorGroupName,
Name: operator.SDKOperatorGroupName,
Namespace: namespace,
},
}
Expand Down
1 change: 1 addition & 0 deletions internal/olm/operator/operator_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func (c *OperatorCmd) newManager() (*operatorManager, error) {
m := &operatorManager{}

// Cluster and operator namespace info.
// TODO(joelanford): Migrate this to use `internal/operator.Configuration`
rc, ns, err := k8sutil.GetKubeconfigAndNamespace(c.KubeconfigPath)
if err != nil {
return nil, fmt.Errorf("failed to get namespace from kubeconfig %s: %w", c.KubeconfigPath, err)
Expand Down
18 changes: 1 addition & 17 deletions internal/olm/operator/packagemanifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type PackageManifestsCmd struct {
// ManifestsDir is a directory containing 1..N package directories and
// a package manifest.
// Version can be set to the version of the desired operator package
// and Run()/Cleanup() will deploy that operator version.
// and Run() will deploy that operator version.
ManifestsDir string
// Version is the version of the operator to deploy. It must be
// a semantic version, ex. 0.0.1.
Expand Down Expand Up @@ -80,19 +80,3 @@ func (c *PackageManifestsCmd) Run() error {
defer cancel()
return m.run(ctx)
}

func (c *PackageManifestsCmd) Cleanup() (err error) {
c.initialize()
if err := c.validate(); err != nil {
return fmt.Errorf("validation error: %w", err)
}
m, err := c.newManager()
if err != nil {
return fmt.Errorf("error initializing operator manager: %w", err)
}
// Cleanups should clean up all resources, which includes the registry.
m.forceRegistry = true
ctx, cancel := context.WithTimeout(context.Background(), c.Timeout)
defer cancel()
return m.cleanup(ctx)
}
Loading