From 54473b2118fb6f8d2c9f59bf21e749dcbb31fcb4 Mon Sep 17 00:00:00 2001 From: Brett Tofel Date: Wed, 25 Aug 2021 16:56:11 -0400 Subject: [PATCH 1/2] Add external k8s 1.25 deprecation check function --- pkg/validation/external/removed_apis.go | 43 ++++++++++++ pkg/validation/external/removed_apis_test.go | 71 ++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 pkg/validation/external/removed_apis.go create mode 100644 pkg/validation/external/removed_apis_test.go diff --git a/pkg/validation/external/removed_apis.go b/pkg/validation/external/removed_apis.go new file mode 100644 index 000000000..b93c9b311 --- /dev/null +++ b/pkg/validation/external/removed_apis.go @@ -0,0 +1,43 @@ +package external + +import ( + "github.com/operator-framework/api/pkg/manifests" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func GetRemovedAPIsOn1_25From(bundle *manifests.Bundle) map[string][]string { + deprecatedAPIs := make(map[string][]string) + for _, obj := range bundle.Objects { + switch usage := obj.GetObjectKind().(type) { + case *unstructured.Unstructured: + switch usage.GetAPIVersion() { + case "batch/v1beta1": + if usage.GetKind() == "CronJob" { + addDepUsage(deprecatedAPIs, usage) + } + case "discovery.k8s.io/v1beta1": + if usage.GetKind() == "EndpointSlice" { + addDepUsage(deprecatedAPIs, usage) + } + case "events.k8s.io/v1beta1": + if usage.GetKind() == "Event" { + addDepUsage(deprecatedAPIs, usage) + } + case "policy/v1beta1": + if usage.GetKind() == "PodDisruptionBudget" || usage.GetKind() == "PodSecurityPolicy" { + addDepUsage(deprecatedAPIs, usage) + } + case "node.k8s.io/v1beta1": + if usage.GetKind() == "RuntimeClass" { + addDepUsage(deprecatedAPIs, usage) + } + } + } + } + return deprecatedAPIs +} + +func addDepUsage(deprecatedAPIs map[string][]string, u *unstructured.Unstructured) map[string][]string { + deprecatedAPIs[u.GetKind()] = append(deprecatedAPIs[u.GetKind()], u.GetName()) + return deprecatedAPIs +} diff --git a/pkg/validation/external/removed_apis_test.go b/pkg/validation/external/removed_apis_test.go new file mode 100644 index 000000000..26fc10a3a --- /dev/null +++ b/pkg/validation/external/removed_apis_test.go @@ -0,0 +1,71 @@ +package external + +import ( + "github.com/operator-framework/api/pkg/manifests" + "github.com/stretchr/testify/require" + "reflect" + "testing" +) + +const ( + PriorityClassKind = "PriorityClass" + RoleKind = "Role" + ClusterRoleKind = "ClusterRole" +) + +func Test_getDeprecatedAPIs(t *testing.T) { + + // Mock the expected result for ./testdata/valid_bundle_v1beta1 + crdMock := make(map[string][]string) + crdMock["CRD"] = []string{"etcdbackups.etcd.database.coreos.com", "etcdclusters.etcd.database.coreos.com", "etcdrestores.etcd.database.coreos.com"} + + // Mock the expected result for ./testdata/valid_bundle_with_v1beta1_clusterrole + otherKindsMock := make(map[string][]string) + otherKindsMock[ClusterRoleKind] = []string{"memcached-operator-metrics-reader"} + otherKindsMock[PriorityClassKind] = []string{"super-priority"} + otherKindsMock[RoleKind] = []string{"memcached-role"} + otherKindsMock["MutatingWebhookConfiguration"] = []string{"mutating-webhook-configuration"} + + type args struct { + bundleDir string + } + tests := []struct { + name string + args args + want map[string][]string + }{ + { + name: "should return an empty map when no deprecated apis are found", + args: args{ + bundleDir: "./testdata/valid_bundle_v1", + }, + want: map[string][]string{}, + }, + { + name: "should return map with CRDs when this kind of resource is deprecated", + args: args{ + bundleDir: "./testdata/valid_bundle_v1beta1", + }, + want: crdMock, + }, + { + name: "should return map with others kinds which are deprecated", + args: args{ + bundleDir: "./testdata/bundle_with_deprecated_resources", + }, + want: otherKindsMock, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + // Validate the bundle object + bundle, err := manifests.GetBundleFromDir(tt.args.bundleDir) + require.NoError(t, err) + + if got := GetRemovedAPIsOn1_25From(bundle); !reflect.DeepEqual(got, tt.want) { + t.Errorf("getRemovedAPIsOn1_22From() = %v, want %v", got, tt.want) + } + }) + } +} From efa5fe60a572c5984c7e47bd514c549a8a8a3837 Mon Sep 17 00:00:00 2001 From: Brett Tofel Date: Thu, 26 Aug 2021 13:11:17 -0400 Subject: [PATCH 2/2] Fixup k8s 1_25 deprecated tests --- pkg/validation/external/removed_apis_test.go | 32 ++++++-------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/pkg/validation/external/removed_apis_test.go b/pkg/validation/external/removed_apis_test.go index 26fc10a3a..139cdef1e 100644 --- a/pkg/validation/external/removed_apis_test.go +++ b/pkg/validation/external/removed_apis_test.go @@ -7,24 +7,17 @@ import ( "testing" ) -const ( - PriorityClassKind = "PriorityClass" - RoleKind = "Role" - ClusterRoleKind = "ClusterRole" -) - -func Test_getDeprecatedAPIs(t *testing.T) { +func Test_getDeprecated1_25APIs(t *testing.T) { - // Mock the expected result for ./testdata/valid_bundle_v1beta1 + // Mock the expected result for ../internal/testdata/valid_bundle_v1beta1 crdMock := make(map[string][]string) crdMock["CRD"] = []string{"etcdbackups.etcd.database.coreos.com", "etcdclusters.etcd.database.coreos.com", "etcdrestores.etcd.database.coreos.com"} - // Mock the expected result for ./testdata/valid_bundle_with_v1beta1_clusterrole + // Mock the expected result otherKindsMock := make(map[string][]string) - otherKindsMock[ClusterRoleKind] = []string{"memcached-operator-metrics-reader"} - otherKindsMock[PriorityClassKind] = []string{"super-priority"} - otherKindsMock[RoleKind] = []string{"memcached-role"} - otherKindsMock["MutatingWebhookConfiguration"] = []string{"mutating-webhook-configuration"} + otherKindsMock["PodDisruptionBudget"] = []string{"busybox-pdb"} + + bundleDirPrefix := "../internal/testdata/" // let's reuse the testdata, so we can avoid creating more type args struct { bundleDir string @@ -37,21 +30,14 @@ func Test_getDeprecatedAPIs(t *testing.T) { { name: "should return an empty map when no deprecated apis are found", args: args{ - bundleDir: "./testdata/valid_bundle_v1", + bundleDir: bundleDirPrefix + "valid_bundle_v1", }, want: map[string][]string{}, }, - { - name: "should return map with CRDs when this kind of resource is deprecated", - args: args{ - bundleDir: "./testdata/valid_bundle_v1beta1", - }, - want: crdMock, - }, { name: "should return map with others kinds which are deprecated", args: args{ - bundleDir: "./testdata/bundle_with_deprecated_resources", + bundleDir: bundleDirPrefix + "bundle_with_deprecated_resources", }, want: otherKindsMock, }, @@ -64,7 +50,7 @@ func Test_getDeprecatedAPIs(t *testing.T) { require.NoError(t, err) if got := GetRemovedAPIsOn1_25From(bundle); !reflect.DeepEqual(got, tt.want) { - t.Errorf("getRemovedAPIsOn1_22From() = %v, want %v", got, tt.want) + t.Errorf("getRemovedAPIsOn1_25From() = %v, want %v", got, tt.want) } }) }