Merge pull request #108426 from deads2k/e2e-check

add resource enablement check for e2e tests of beta APIs

Kubernetes-commit: f93be6584ea57d1d01a0ca1d2c5e96782713d311
This commit is contained in:
Kubernetes Publisher
2022-03-07 11:34:26 -08:00
3 changed files with 25 additions and 4 deletions

View File

@@ -19,12 +19,33 @@ package discovery
import (
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
apimachineryversion "k8s.io/apimachinery/pkg/version"
)
// IsResourceEnabled queries the server to determine if the resource specified is present on the server.
// This is particularly helpful when writing a controller or an e2e test that requires a particular resource to function.
func IsResourceEnabled(client DiscoveryInterface, resourceToCheck schema.GroupVersionResource) (bool, error) {
// this is a single request. The ServerResourcesForGroupVersion handles the core v1 group as legacy.
resourceList, err := client.ServerResourcesForGroupVersion(resourceToCheck.GroupVersion().String())
if apierrors.IsNotFound(err) { // if the discovery endpoint isn't present, then the resource isn't present.
return false, nil
}
if err != nil {
return false, err
}
for _, actualResource := range resourceList.APIResources {
if actualResource.Name == resourceToCheck.Resource {
return true, nil
}
}
return false, nil
}
// MatchesServerVersion queries the server to compares the build version
// (git hash) of the client with the server's build version. It returns an error
// if it failed to contact the server or if the versions are not an exact match.

4
go.mod
View File

@@ -31,7 +31,7 @@ require (
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8
google.golang.org/protobuf v1.27.1
k8s.io/api v0.0.0-20220226220324-b8c40e080bc5
k8s.io/apimachinery v0.0.0-20220226220127-2936d3f03931
k8s.io/apimachinery v0.0.0-20220307180657-d81a7ed4ab08
k8s.io/klog/v2 v2.40.1
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
@@ -41,5 +41,5 @@ require (
replace (
k8s.io/api => k8s.io/api v0.0.0-20220226220324-b8c40e080bc5
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20220226220127-2936d3f03931
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20220307180657-d81a7ed4ab08
)

4
go.sum
View File

@@ -616,8 +616,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20220226220324-b8c40e080bc5 h1:z4oqfOInb6p7EwsJbKUe2IcKaeSBWmfYEIsIdFHq6ak=
k8s.io/api v0.0.0-20220226220324-b8c40e080bc5/go.mod h1:xmVR3mDgBB2FAJoueQFwuWn03L5odGCiOKivsptcgRU=
k8s.io/apimachinery v0.0.0-20220226220127-2936d3f03931 h1:pb7vtSnIF7dReuA6s+WsakFL6vLZ4xA71kJMln9j4Pc=
k8s.io/apimachinery v0.0.0-20220226220127-2936d3f03931/go.mod h1:6HjHJr7AD3yHuu+gOdE3O1dqE21lBVCDBk5W7wry/WI=
k8s.io/apimachinery v0.0.0-20220307180657-d81a7ed4ab08 h1:vWaGwtEno8UN+KYPc0xGq3lMO5aw2wxaR7AjnDP+G1U=
k8s.io/apimachinery v0.0.0-20220307180657-d81a7ed4ab08/go.mod h1:6HjHJr7AD3yHuu+gOdE3O1dqE21lBVCDBk5W7wry/WI=
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=