Merge pull request #108426 from deads2k/e2e-check

add resource enablement check for e2e tests of beta APIs
This commit is contained in:
Kubernetes Prow Robot 2022-03-07 11:34:26 -08:00 committed by GitHub
commit f93be6584e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

@ -19,12 +19,33 @@ package discovery
import ( import (
"fmt" "fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
apimachineryversion "k8s.io/apimachinery/pkg/version" 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 // MatchesServerVersion queries the server to compares the build version
// (git hash) of the client with the server's build version. It returns an error // (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. // if it failed to contact the server or if the versions are not an exact match.

View File

@ -21,8 +21,10 @@ import (
"strings" "strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
utilversion "k8s.io/apimachinery/pkg/util/version" utilversion "k8s.io/apimachinery/pkg/util/version"
"k8s.io/apiserver/pkg/endpoints/discovery" "k8s.io/apiserver/pkg/endpoints/discovery"
clientdiscovery "k8s.io/client-go/discovery"
"k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
"k8s.io/kubernetes/test/utils/crd" "k8s.io/kubernetes/test/utils/crd"
@ -45,6 +47,33 @@ var _ = SIGDescribe("Discovery", func() {
setupServerCert(namespaceName, serviceName) setupServerCert(namespaceName, serviceName)
}) })
ginkgo.It("should accurately determine present and missing resources", func() {
// checks that legacy api group resources function
ok, err := clientdiscovery.IsResourceEnabled(f.ClientSet.Discovery(), schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"})
framework.ExpectNoError(err)
if !ok {
framework.Failf("namespace.v1 should always be present")
}
// checks that non-legacy api group resources function
ok, err = clientdiscovery.IsResourceEnabled(f.ClientSet.Discovery(), schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"})
framework.ExpectNoError(err)
if !ok {
framework.Failf("deployments.v1.apps should always be present")
}
// checks that nonsense resources in existing api groups function
ok, err = clientdiscovery.IsResourceEnabled(f.ClientSet.Discovery(), schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "please-dont-ever-create-this"})
framework.ExpectNoError(err)
if ok {
framework.Failf("please-dont-ever-create-this.v1.apps should never be present")
}
// checks that resources resources in nonsense api groups function
ok, err = clientdiscovery.IsResourceEnabled(f.ClientSet.Discovery(), schema.GroupVersionResource{Group: "not-these-apps", Version: "v1", Resource: "deployments"})
framework.ExpectNoError(err)
if ok {
framework.Failf("deployments.v1.not-these-apps should never be present")
}
})
ginkgo.It("Custom resource should have storage version hash", func() { ginkgo.It("Custom resource should have storage version hash", func() {
testcrd, err := crd.CreateTestCRD(f) testcrd, err := crd.CreateTestCRD(f)
if err != nil { if err != nil {