knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/migrate/checks.go (about) 1 package migrate 2 3 /* 4 Copyright 2021 The Knative Authors 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 import ( 20 "context" 21 "strings" 22 "testing" 23 24 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 ) 27 28 // ExpectSingleStoredVersion verifies that status.storedVersions on specific CRDs has only one version 29 // and the version is listed in spec.Versions with storage: true. It means the CRDs 30 // have been migrated and previous/unused API versions can be safely removed from the spec. 31 func ExpectSingleStoredVersion(t *testing.T, crdClient apiextensionsv1.CustomResourceDefinitionInterface, crdGroup string) { 32 t.Helper() 33 34 crdList, err := crdClient.List(context.Background(), metav1.ListOptions{}) 35 if err != nil { 36 t.Fatal("Unable to fetch crd list:", err) 37 } 38 39 for _, crd := range crdList.Items { 40 if strings.Contains(crd.Name, crdGroup) { 41 if len(crd.Status.StoredVersions) != 1 { 42 t.Errorf("%q does not have a single stored version: %+v", crd.Name, crd) 43 } 44 stored := crd.Status.StoredVersions[0] 45 for _, v := range crd.Spec.Versions { 46 if stored == v.Name && !v.Storage { 47 t.Errorf("%q is invalid: spec.versions.storage must be true for %q or "+ 48 "version %q must be removed from status.storageVersions: %+v", crd.Name, v.Name, v.Name, crd) 49 } 50 } 51 } 52 } 53 }