diff --git a/cmd/upgrade_command.go b/cmd/upgrade_command.go index 82a0309..92148f3 100644 --- a/cmd/upgrade_command.go +++ b/cmd/upgrade_command.go @@ -190,7 +190,7 @@ func (uc *UpgradeCommand) process(ctx context.Context, path config.UpgradePath) logger.Infow("deploying operator version", "version", version.Name) // Install artifact version - if err := uc.operator.UpgradeOperator(ctx, version.BundleVersion); err != nil { + if err := uc.operator.UpgradeOperator(ctx, version); err != nil { return fmt.Errorf("failed to prepare operator: %v", err) } diff --git a/pkg/operator/interface.go b/pkg/operator/interface.go index 1e78c40..0a6fc01 100644 --- a/pkg/operator/interface.go +++ b/pkg/operator/interface.go @@ -1,9 +1,13 @@ package operator -import "context" +import ( + "context" + + "github.com/AlaudaDevops/upgrade-test/pkg/config" +) // OperatorInterface defines the interface for operator operations type OperatorInterface interface { // UpgradeOperator upgrades the operator to the given version - UpgradeOperator(ctx context.Context, version string) error + UpgradeOperator(ctx context.Context, version config.Version) error } diff --git a/pkg/operator/local/operator.go b/pkg/operator/local/operator.go index 545ac27..9ea6696 100644 --- a/pkg/operator/local/operator.go +++ b/pkg/operator/local/operator.go @@ -22,20 +22,20 @@ func NewLocalOperator(options config.OperatorConfig) (*LocalOperator, error) { }, nil } -func (o *LocalOperator) UpgradeOperator(ctx context.Context, version string) error { +func (o *LocalOperator) UpgradeOperator(ctx context.Context, version config.Version) error { log := logging.FromContext(ctx) if o.command == "" { o.command = "make deploy" } - log.Infof("upgrading operator version: %s", version) + log.Infof("upgrading operator version: %s", version.Name) - if err := o.runDeployCommand(ctx, version); err != nil { + if err := o.runDeployCommand(ctx, version.BundleVersion); err != nil { return fmt.Errorf("failed to run deploy command: %v", err) } - log.Infof("operator version %s upgraded successfully", version) + log.Infof("operator version %s upgraded successfully", version.Name) return nil } diff --git a/pkg/operator/operatorhub/operator.go b/pkg/operator/operatorhub/operator.go index 1b84e01..f3b107d 100644 --- a/pkg/operator/operatorhub/operator.go +++ b/pkg/operator/operatorhub/operator.go @@ -95,16 +95,20 @@ func (o *Operator) GetResource(ctx context.Context, name, namespace string, gvr return o.client.Resource(gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{}) } -func (o *Operator) UpgradeOperator(ctx context.Context, version string) error { +func (o *Operator) UpgradeOperator(ctx context.Context, version config.Version) error { // Install artifact version - av, err := o.InstallArtifactVersion(ctx, version) + av, err := o.InstallArtifactVersion(ctx, version.BundleVersion) if err != nil { return fmt.Errorf("failed to prepare operator: %v", err) } // Get CSV version from artifact version csv, _, _ := unstructured.NestedString(av.Object, "status", "version") - if err := o.InstallSubscription(ctx, csv); err != nil { + channel := version.Channel + if channel == "" { + channel = "stable" // default fallback + } + if err := o.InstallSubscription(ctx, csv, channel); err != nil { return fmt.Errorf("failed to install subscription: %v", err) } diff --git a/pkg/operator/operatorhub/subscription.go b/pkg/operator/operatorhub/subscription.go index ef47f87..59da0fb 100644 --- a/pkg/operator/operatorhub/subscription.go +++ b/pkg/operator/operatorhub/subscription.go @@ -15,7 +15,7 @@ import ( "knative.dev/pkg/logging" ) -func (o *Operator) InstallSubscription(ctx context.Context, csv string) error { +func (o *Operator) InstallSubscription(ctx context.Context, csv string, channel string) error { if csv == "" { return fmt.Errorf("csv is empty") } @@ -31,8 +31,8 @@ func (o *Operator) InstallSubscription(ctx context.Context, csv string) error { return fmt.Errorf("failed to delete old csv: %v", err) } - log.Infow("creating subscription", "name", o.name, "namespace", o.namespace, "csv", csv) - _, err := o.createSubscription(ctx, o.name, o.namespace, csv) + log.Infow("creating subscription", "name", o.name, "namespace", o.namespace, "csv", csv, "channel", channel) + _, err := o.createSubscription(ctx, o.name, o.namespace, csv, channel) if err != nil { return fmt.Errorf("failed to create subscription: %v", err) } @@ -95,7 +95,7 @@ func (o *Operator) deleteResource(ctx context.Context, gvr schema.GroupVersionRe return nil } -func (o *Operator) createSubscription(ctx context.Context, name, namespace, csv string) (*unstructured.Unstructured, error) { +func (o *Operator) createSubscription(ctx context.Context, name, namespace, csv string, channel string) (*unstructured.Unstructured, error) { log := logging.FromContext(ctx) _, err := o.client.Resource(namespaceGVR).Create(ctx, &unstructured.Unstructured{ @@ -123,7 +123,7 @@ func (o *Operator) createSubscription(ctx context.Context, name, namespace, csv }, }, "spec": map[string]interface{}{ - "channel": "stable", + "channel": channel, "installPlanApproval": "Manual", "name": name, "source": "platform",