Skip to content

Commit

Permalink
don't migrate storage version if CRD has one storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kauana dos Santos committed Oct 11, 2023
1 parent ca8c009 commit e2ebc66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apiextensions/storageversion/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (m *Migrator) Migrate(ctx context.Context, gr schema.GroupResource) error {
return fmt.Errorf("unable to determine storage version for %s", gr)
}

// don't migrate storage version if CRD has a single valid storage in its status
if len(crd.Status.StoredVersions) == 1 && crd.Status.StoredVersions[0] == version {
return nil
}

if err := m.migrateResources(ctx, gr.WithVersion(version)); err != nil {
return err
}
Expand Down
31 changes: 31 additions & 0 deletions apiextensions/storageversion/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ var (
},
},
}

singleVersionFakeCRD = &apix.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: fakeGR.String(),
},
Spec: apix.CustomResourceDefinitionSpec{
Group: fakeGK.Group,
Versions: []apix.CustomResourceDefinitionVersion{
{Name: "v1", Served: true, Storage: true},
},
},
Status: apix.CustomResourceDefinitionStatus{
StoredVersions: []string{"v1"},
},
}
)

func TestMigrate(t *testing.T) {
Expand All @@ -88,6 +103,22 @@ func TestMigrate(t *testing.T) {
)
}

func TestMigrate_SingleStoredVersion(t *testing.T) {
// setup
dclient := dynamicFake.NewSimpleDynamicClient(runtime.NewScheme())
cclient := apixFake.NewSimpleClientset(singleVersionFakeCRD)
m := NewMigrator(dclient, cclient)

if err := m.Migrate(context.Background(), fakeGR); err != nil {
t.Fatal("Migrate() =", err)
}

assertPatches(t, cclient.Actions(),
// patch resource definition dropping non-storage version
crdStorageVersionPatch(singleVersionFakeCRD.Name, "v1"),
)
}

// func TestMigrate_Paging(t *testing.T) {
// t.Skip("client-go lacks testing pagination " +
// "since list options aren't captured in actions")
Expand Down

0 comments on commit e2ebc66

Please sign in to comment.