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
20 changes: 10 additions & 10 deletions migration/pkg/migration/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (m *Migrator) prepareClusterObjectDeployment(ctx context.Context, name stri
return fmt.Errorf("check ClusterObjectDeployment %s: %w", name, err)
}
// An existing group must not be taken over merely because its deployment
// is absent. Check legacy labels too, before any OLMv0 objects are deleted.
// is absent. Check conflicting labels and names too, before any OLMv0 objects are deleted.
var revisions ocv1.ClusterObjectSetList
if err := m.Client.List(ctx, &revisions); err != nil {
return fmt.Errorf("check migration revision group: %w", err)
Expand Down Expand Up @@ -96,6 +96,7 @@ func (m *Migrator) createClusterObjectDeployment(ctx context.Context, opts Optio
return m.waitForDeployment(ctx, cod)
}

// waitForDeployment waits for the created deployment to complete its current rollout and become available.
func (m *Migrator) waitForDeployment(ctx context.Context, created *ocv1.ClusterObjectDeployment) error {
return wait.PollUntilContextTimeout(ctx, cosWaitPollInterval, cosWaitTimeout, true, func(ctx context.Context) (bool, error) {
var cod ocv1.ClusterObjectDeployment
Expand All @@ -121,7 +122,7 @@ func (m *Migrator) waitForDeployment(ctx context.Context, created *ocv1.ClusterO
})
}

// rollbackDeployment permits the legacy API to be absent, but never hides
// rollbackDeployment permits the COD API to be absent, but never hides
// authorization or transport errors. A foreign deployment blocks rollback.
func (m *Migrator) rollbackDeployment(ctx context.Context, ce *ocv1.ClusterExtension) (*ocv1.ClusterObjectDeployment, error) {
var cod ocv1.ClusterObjectDeployment
Expand Down Expand Up @@ -215,13 +216,14 @@ func (m *Migrator) rememberRollbackDeployment(ctx context.Context, ce *ocv1.Clus
return nil
}

// listMigrationRevisions selects migration revisions and rejects conflicting ownership identities.
func (m *Migrator) listMigrationRevisions(ctx context.Context, ce *ocv1.ClusterExtension, list *ocv1.ClusterObjectSetList) error {
if err := m.Client.List(ctx, list); err != nil {
return err
}
selected := list.Items[:0]
for _, revision := range list.Items {
if revision.Spec.Group == ce.Name || revision.Spec.Group == "" && revision.Labels[LabelOwnerName] == ce.Name {
if revision.Spec.Group == ce.Name || revision.Labels[LabelOwnerName] == ce.Name || revision.Name == ce.Name+"-1" {
if err := m.validateRollbackRevision(ctx, &revision, ce); err != nil {
return err
}
Expand All @@ -232,22 +234,20 @@ func (m *Migrator) listMigrationRevisions(ctx context.Context, ce *ocv1.ClusterE
return nil
}

// validateRollbackRevision requires the expected revision group and verifies controller ancestry before deletion.
func (m *Migrator) validateRollbackRevision(ctx context.Context, revision *ocv1.ClusterObjectSet, ce *ocv1.ClusterExtension) error {
name := ce.Name
if revision.Spec.Group != "" && revision.Spec.Group != name {
if revision.Spec.Group != name {
return fmt.Errorf("migration ClusterObjectSet %s belongs to another group %q", revision.Name, revision.Spec.Group)
}
if owner := revision.Labels[LabelOwnerName]; owner != "" && owner != name {
return fmt.Errorf("migration ClusterObjectSet %s belongs to another extension %q", revision.Name, owner)
}
owner := metav1.GetControllerOf(revision)
if owner == nil {
// Legacy staging revisions and revisions orphaned during rollback have
// no controller. Require a matching group, label, or legacy initial name.
if revision.Spec.Group == name || revision.Labels[LabelOwnerName] == name || revision.Name == name+"-1" {
return nil
}
return fmt.Errorf("migration ClusterObjectSet %s has no verified migration identity", revision.Name)
// Staging revisions and revisions orphaned during rollback have no
// controller. Their required group was verified above.
return nil
}
unverified := fmt.Errorf("migration ClusterObjectSet %s is controlled by another or unverified owner", revision.Name)
if owner.APIVersion != ocv1.GroupVersion.String() || owner.Name != name || owner.UID == "" {
Expand Down
10 changes: 5 additions & 5 deletions migration/pkg/migration/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ func TestRollbackRejectsForeignRevisionsBeforeStoppingDeployment(t *testing.T) {
}
}

// TestRollbackRevisionControllerOwnership checks that rollback rejects unrelated or unverified revision controllers.
func TestRollbackRevisionControllerOwnership(t *testing.T) {
ce := &ocv1.ClusterExtension{ObjectMeta: metav1.ObjectMeta{Name: "example", UID: "extension"}}
cod := readyDeployment()
Expand All @@ -354,7 +355,7 @@ func TestRollbackRevisionControllerOwnership(t *testing.T) {
missingCOD bool
wantError bool
}{
{name: "orphaned or legacy revision"},
{name: "orphaned revision"},
{name: "CE controller", owner: metav1.NewControllerRef(ce, ocv1.GroupVersion.WithKind(ocv1.ClusterExtensionKind))},
{name: "COD controller", owner: metav1.NewControllerRef(cod, ocv1.GroupVersion.WithKind(ocv1.ClusterObjectDeploymentKind))},
{name: "missing COD cannot verify UID", owner: metav1.NewControllerRef(cod, ocv1.GroupVersion.WithKind(ocv1.ClusterObjectDeploymentKind)), missingCOD: true, wantError: true},
Expand All @@ -377,12 +378,11 @@ func TestRollbackRevisionControllerOwnership(t *testing.T) {
}
})
}
// The old initial revision may lack both group and label; other unowned
// objects without any migration identity must not be admitted by name alone.
// A matching name cannot substitute for the required group.
m := migrationTestClient(t, ce)
for _, name := range []string{"example-1", "unrelated"} {
revision := &ocv1.ClusterObjectSet{ObjectMeta: metav1.ObjectMeta{Name: name}}
if err := m.validateRollbackRevision(context.Background(), revision, ce); (err != nil) != (name == "unrelated") {
revision := &ocv1.ClusterObjectSet{ObjectMeta: metav1.ObjectMeta{Name: name, Labels: map[string]string{LabelOwnerName: ce.Name}}}
if err := m.validateRollbackRevision(context.Background(), revision, ce); err == nil {
t.Fatalf("validation of %s = %v", name, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion migration/pkg/migration/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const (
MigratedFromCatalogSourceAnnotation = "olm.operatorframework.io/migrated-from-catalogsource"

// cosWaitPollInterval / cosWaitTimeout control how long to wait for a
// ClusterObjectSet to reach Succeeded=True.
// ClusterObjectSet to set status.completedAt.
cosWaitPollInterval = 5 * time.Second
cosWaitTimeout = 5 * time.Minute

Expand Down
23 changes: 8 additions & 15 deletions migration/pkg/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ const (
clusterObjectSetCRDName = "clusterobjectsets.olm.operatorframework.io"
clusterExtensionCRDName = "clusterextensions.olm.operatorframework.io"
operatorControllerDeployName = "operator-controller-controller-manager"

// operator-controller v1.12 uses these COS conditions. The COD-capable API
// replaced them with status.completedAt and ClusterObjectSetTypeReady and
// removed their constants; retain the values for legacy COS-only clusters.
legacyCOSConditionSucceeded = "Succeeded"
legacyCOSConditionAvailable = "Available"
)

// annotationPrefixesToStrip are annotation prefixes that should be removed from migrated resources.
Expand Down Expand Up @@ -769,6 +763,7 @@ func (m *Migrator) createClusterObjectSet(ctx context.Context, opts Options, inf
return resources, nil
}

// cleanupCreatedClusterObjectSet cleans staged resources using their recorded creation identities.
func (m *Migrator) cleanupCreatedClusterObjectSet(ctx context.Context, resources *createdMigrationResources) error {
if resources == nil {
return nil
Expand All @@ -789,7 +784,7 @@ func (m *Migrator) cleanupCreatedClusterObjectSet(ctx context.Context, resources
return m.cleanupCreatedSecrets(cleanupCtx, resources.secrets)
}

// WaitForCOSSucceeded waits for status.completedAt or legacy Succeeded=True.
// WaitForCOSSucceeded waits for the current COS completion timestamp.
func (m *Migrator) WaitForCOSSucceeded(ctx context.Context, cosName string) error {
return wait.PollUntilContextTimeout(ctx, cosWaitPollInterval, cosWaitTimeout, true, func(ctx context.Context) (bool, error) {
var cos ocv1.ClusterObjectSet
Expand All @@ -802,15 +797,13 @@ func (m *Migrator) WaitForCOSSucceeded(ctx context.Context, cosName string) erro
return true, nil
}
for _, c := range cos.Status.Conditions {
if c.Type == legacyCOSConditionSucceeded && c.Status == metav1.ConditionTrue {
return true, nil
}
if c.Type == legacyCOSConditionSucceeded && c.Reason == ocv1.ReasonBlocked {
return false, fmt.Errorf("ClusterObjectSet %s is blocked: %s", cosName, c.Message)
if c.Type == ocv1.ClusterObjectSetTypeReady && c.Status == metav1.ConditionFalse &&
c.ObservedGeneration == cos.Generation && c.Reason == ocv1.ClusterObjectSetReasonInvalidSpecification {
return false, fmt.Errorf("ClusterObjectSet %s has invalid specification: %s", cosName, c.Message)
}
}

m.progress(ProgressEvent{Step: ProgressStepCreate, Status: ProgressWaiting, Message: fmt.Sprintf("Waiting for ClusterObjectSet %s to reach Succeeded=True...", cosName)})
m.progress(ProgressEvent{Step: ProgressStepCreate, Status: ProgressWaiting, Message: fmt.Sprintf("Waiting for ClusterObjectSet %s to set status.completedAt...", cosName)})
return false, nil
})
}
Expand Down Expand Up @@ -977,11 +970,11 @@ func (m *Migrator) WaitForClusterObjectSetAvailable(ctx context.Context, cosName
return false, err
}
for _, condition := range cos.Status.Conditions {
if (condition.Type == legacyCOSConditionAvailable || condition.Type == ocv1.ClusterObjectSetTypeReady) && condition.Status == metav1.ConditionTrue {
if condition.Type == ocv1.ClusterObjectSetTypeReady && condition.Status == metav1.ConditionTrue {
return true, nil
}
}
m.progress(ProgressEvent{Step: ProgressStepCreate, Status: ProgressWaiting, Message: fmt.Sprintf("Waiting for migration ClusterObjectSet %s to reach Available=True...", cosName)})
m.progress(ProgressEvent{Step: ProgressStepCreate, Status: ProgressWaiting, Message: fmt.Sprintf("Waiting for migration ClusterObjectSet %s to reach Ready=True...", cosName)})
return false, nil
})
}
Expand Down
2 changes: 1 addition & 1 deletion migration/pkg/migration/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func clearServiceAllocations(obj *unstructured.Unstructured) {

// DeleteSourceNamespaceResources removes the old copies of objects that were
// applied in a different install namespace. It runs only after the target CE
// has reached Installed=True and the migration COS reported Available=True, so
// has reached Installed=True and the migration COS reported Ready=True, so
// a failed handoff retains the OLMv0 workload.
// It intentionally does not delete the Namespace itself; that requires the
// separate AcknowledgeNamespaceDelete opt-in.
Expand Down
62 changes: 62 additions & 0 deletions migration/pkg/migration/revision_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package migration

import (
"context"
"errors"
"strings"
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

ocv1 "github.com/operator-framework/operator-controller/api/v1"
)

// TestCOSWaitsRejectLegacyConditions requires current completion and readiness signals instead of legacy status conditions.
func TestCOSWaitsRejectLegacyConditions(t *testing.T) {
cos := &ocv1.ClusterObjectSet{
ObjectMeta: metav1.ObjectMeta{Name: "example-1"},
Status: ocv1.ClusterObjectSetStatus{Conditions: []metav1.Condition{
{Type: "Succeeded", Status: metav1.ConditionTrue},
{Type: "Available", Status: metav1.ConditionTrue},
}},
}
m := migrationTestClient(t, cos)
for name, wait := range map[string]func(context.Context, string) error{
"completion": m.WaitForCOSSucceeded,
"readiness": m.WaitForClusterObjectSetAvailable,
} {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
if err := wait(ctx, cos.Name); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("legacy conditions must not satisfy wait: %v", err)
}
})
}
}

// TestCOSCompletionInvalidSpecification checks that a current invalid-specification condition fails completion promptly.
func TestCOSCompletionInvalidSpecification(t *testing.T) {
for _, generation := range []int64{1, 2} {
cos := &ocv1.ClusterObjectSet{
ObjectMeta: metav1.ObjectMeta{Name: "example-1", Generation: 2},
Status: ocv1.ClusterObjectSetStatus{Conditions: []metav1.Condition{{
Type: ocv1.ClusterObjectSetTypeReady, Status: metav1.ConditionFalse,
Reason: ocv1.ClusterObjectSetReasonInvalidSpecification,
Message: "invalid reference", ObservedGeneration: generation,
}}},
}
m := migrationTestClient(t, cos)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
err := m.WaitForCOSSucceeded(ctx, cos.Name)
cancel()
if generation == cos.Generation {
if err == nil || !strings.Contains(err.Error(), "invalid reference") {
t.Fatalf("expected current failure diagnostic, got %v", err)
}
} else if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("stale condition must not terminate wait: %v", err)
}
}
}
Loading
Loading