Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checks for k8s 1.25 deprecated APIs #151

Closed
Closed
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
43 changes: 43 additions & 0 deletions pkg/validation/external/removed_apis.go
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deprecatedAPIs := make(map[string][]string)
for _, obj := range bundle.Objects {
Copy link
Contributor

@camilamacedo86 camilamacedo86 Oct 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check has not been called in any validator so it will not be useful.
See that we would need to ensure that it will be called via the operatorhub (https://github.com/operator-framework/api/blob/master/pkg/validation/internal/operatorhub.go#L106) and the alpha-deprecated-apis one (https://github.com/operator-framework/api/blob/master/pkg/validation/internal/removed_apis.go#L127)

we also need to address improvements for these scenario checks such as make clear what are the k8s versions supported by the checks and some cleanups.

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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you getting the name of the resource where the api was found? See that it is used in more places so, why not address the cleanup across the project?

57 changes: 57 additions & 0 deletions pkg/validation/external/removed_apis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package external

import (
"github.com/operator-framework/api/pkg/manifests"
"github.com/stretchr/testify/require"
"reflect"
"testing"
)

func Test_getDeprecated1_25APIs(t *testing.T) {

// 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
otherKindsMock := make(map[string][]string)
otherKindsMock["PodDisruptionBudget"] = []string{"busybox-pdb"}

bundleDirPrefix := "../internal/testdata/" // let's reuse the testdata, so we can avoid creating more

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: bundleDirPrefix + "valid_bundle_v1",
},
want: map[string][]string{},
},
{
name: "should return map with others kinds which are deprecated",
args: args{
bundleDir: bundleDirPrefix + "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_25From() = %v, want %v", got, tt.want)
}
})
}
}