Skip to content

Commit

Permalink
Don't migrate storage version if CRD has one storage version (#2861)
Browse files Browse the repository at this point in the history
* don't migrate storage version if CRD has one storage

* add migrator test for single version crds

- fakes patch and expect it doesn't happen

* address PR feedback
  • Loading branch information
Kauana Santos authored Oct 16, 2023
1 parent d0c133d commit 283df0b
Show file tree
Hide file tree
Showing 2 changed files with 42 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
37 changes: 37 additions & 0 deletions apiextensions/storageversion/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package storageversion
import (
"context"
"errors"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -88,6 +89,42 @@ func TestMigrate(t *testing.T) {
)
}

func TestMigrate_SingleStoredVersion(t *testing.T) {
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"},
},
}

// setup
dclient := dynamicFake.NewSimpleDynamicClient(runtime.NewScheme())
cclient := apixFake.NewSimpleClientset(singleVersionFakeCRD)

cclient.Fake.PrependReactor("patch", "customresourcedefinitions", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
if pa, ok := action.(k8stesting.PatchAction); ok {
if pa.GetName() == singleVersionFakeCRD.Name {
return false, nil, fmt.Errorf("resource shouldn't have been patched")
}
}

return false, nil, nil
})

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

// 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 283df0b

Please sign in to comment.