-
Notifications
You must be signed in to change notification settings - Fork 1.2k
✨ Use aggregated discovery if available #2901
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package apiutil_test | |
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
|
||
gmg "github.com/onsi/gomega" | ||
|
@@ -32,127 +33,130 @@ import ( | |
) | ||
|
||
func TestApiMachinery(t *testing.T) { | ||
restCfg, tearDownFn := setupEnvtest(t) | ||
defer tearDownFn(t) | ||
|
||
// Details of the GVK registered at initialization. | ||
initialGvk := metav1.GroupVersionKind{ | ||
Group: "crew.example.com", | ||
Version: "v1", | ||
Kind: "Driver", | ||
} | ||
for _, aggregatedDiscovery := range []bool{true, false} { | ||
t.Run("aggregatedDiscovery="+strconv.FormatBool(aggregatedDiscovery), func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely fine for now, but we're going to lose coverage once we bump hack/check-everything.sh to a version where the feature gate doesn't function anymore (so maybe we should use a different Kubernetes version long-term (after #2810)) WDYT about opening an issue to track that? |
||
restCfg := setupEnvtest(t, !aggregatedDiscovery) | ||
|
||
// A set of GVKs to register at runtime with varying properties. | ||
runtimeGvks := []struct { | ||
name string | ||
gvk metav1.GroupVersionKind | ||
plural string | ||
}{ | ||
{ | ||
name: "new Kind and Version added to existing Group", | ||
gvk: metav1.GroupVersionKind{ | ||
Group: "crew.example.com", | ||
Version: "v1alpha1", | ||
Kind: "Passenger", | ||
}, | ||
plural: "passengers", | ||
}, | ||
{ | ||
name: "new Kind added to existing Group and Version", | ||
gvk: metav1.GroupVersionKind{ | ||
// Details of the GVK registered at initialization. | ||
initialGvk := metav1.GroupVersionKind{ | ||
Group: "crew.example.com", | ||
Version: "v1", | ||
Kind: "Garage", | ||
}, | ||
plural: "garages", | ||
}, | ||
{ | ||
name: "new GVK", | ||
gvk: metav1.GroupVersionKind{ | ||
Group: "inventory.example.com", | ||
Version: "v1", | ||
Kind: "Taxi", | ||
}, | ||
plural: "taxis", | ||
}, | ||
} | ||
Kind: "Driver", | ||
} | ||
|
||
// A set of GVKs to register at runtime with varying properties. | ||
runtimeGvks := []struct { | ||
name string | ||
gvk metav1.GroupVersionKind | ||
plural string | ||
}{ | ||
{ | ||
name: "new Kind and Version added to existing Group", | ||
gvk: metav1.GroupVersionKind{ | ||
Group: "crew.example.com", | ||
Version: "v1alpha1", | ||
Kind: "Passenger", | ||
}, | ||
plural: "passengers", | ||
}, | ||
{ | ||
name: "new Kind added to existing Group and Version", | ||
gvk: metav1.GroupVersionKind{ | ||
Group: "crew.example.com", | ||
Version: "v1", | ||
Kind: "Garage", | ||
}, | ||
plural: "garages", | ||
}, | ||
{ | ||
name: "new GVK", | ||
gvk: metav1.GroupVersionKind{ | ||
Group: "inventory.example.com", | ||
Version: "v1", | ||
Kind: "Taxi", | ||
}, | ||
plural: "taxis", | ||
}, | ||
} | ||
|
||
t.Run("IsGVKNamespaced should report scope for GVK registered at initialization", func(t *testing.T) { | ||
g := gmg.NewWithT(t) | ||
|
||
httpClient, err := rest.HTTPClientFor(restCfg) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
t.Run("IsGVKNamespaced should report scope for GVK registered at initialization", func(t *testing.T) { | ||
g := gmg.NewWithT(t) | ||
|
||
httpClient, err := rest.HTTPClientFor(restCfg) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
s := scheme.Scheme | ||
err = apiextensionsv1.AddToScheme(s) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
// Query the scope of a GVK that was registered at initialization. | ||
scope, err := apiutil.IsGVKNamespaced( | ||
schema.GroupVersionKind(initialGvk), | ||
lazyRestMapper, | ||
) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
g.Expect(scope).To(gmg.BeTrue()) | ||
}) | ||
|
||
for _, runtimeGvk := range runtimeGvks { | ||
t.Run("IsGVKNamespaced should report scope for "+runtimeGvk.name, func(t *testing.T) { | ||
g := gmg.NewWithT(t) | ||
ctx := context.Background() | ||
|
||
httpClient, err := rest.HTTPClientFor(restCfg) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
s := scheme.Scheme | ||
err = apiextensionsv1.AddToScheme(s) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
c, err := client.New(restCfg, client.Options{Scheme: s}) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
// Run a valid query to initialize cache. | ||
scope, err := apiutil.IsGVKNamespaced( | ||
schema.GroupVersionKind(initialGvk), | ||
lazyRestMapper, | ||
) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
g.Expect(scope).To(gmg.BeTrue()) | ||
|
||
// Register a new CRD at runtime. | ||
crd := newCRD(ctx, g, c, runtimeGvk.gvk.Group, runtimeGvk.gvk.Kind, runtimeGvk.plural) | ||
version := crd.Spec.Versions[0] | ||
version.Name = runtimeGvk.gvk.Version | ||
version.Storage = true | ||
version.Served = true | ||
crd.Spec.Versions = []apiextensionsv1.CustomResourceDefinitionVersion{version} | ||
crd.Spec.Scope = apiextensionsv1.NamespaceScoped | ||
|
||
g.Expect(c.Create(ctx, crd)).To(gmg.Succeed()) | ||
t.Cleanup(func() { | ||
g.Expect(c.Delete(ctx, crd)).To(gmg.Succeed()) | ||
}) | ||
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
s := scheme.Scheme | ||
err = apiextensionsv1.AddToScheme(s) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
// Wait until the CRD is registered. | ||
g.Eventually(func(g gmg.Gomega) { | ||
isRegistered, err := isCrdRegistered(restCfg, runtimeGvk.gvk) | ||
// Query the scope of a GVK that was registered at initialization. | ||
scope, err := apiutil.IsGVKNamespaced( | ||
schema.GroupVersionKind(initialGvk), | ||
lazyRestMapper, | ||
) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
g.Expect(isRegistered).To(gmg.BeTrue()) | ||
}).Should(gmg.Succeed(), "GVK should be available") | ||
|
||
// Query the scope of the GVK registered at runtime. | ||
scope, err = apiutil.IsGVKNamespaced( | ||
schema.GroupVersionKind(runtimeGvk.gvk), | ||
lazyRestMapper, | ||
) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
g.Expect(scope).To(gmg.BeTrue()) | ||
g.Expect(scope).To(gmg.BeTrue()) | ||
}) | ||
|
||
for _, runtimeGvk := range runtimeGvks { | ||
t.Run("IsGVKNamespaced should report scope for "+runtimeGvk.name, func(t *testing.T) { | ||
g := gmg.NewWithT(t) | ||
ctx := context.Background() | ||
|
||
httpClient, err := rest.HTTPClientFor(restCfg) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
s := scheme.Scheme | ||
err = apiextensionsv1.AddToScheme(s) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
c, err := client.New(restCfg, client.Options{Scheme: s}) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
|
||
// Run a valid query to initialize cache. | ||
scope, err := apiutil.IsGVKNamespaced( | ||
schema.GroupVersionKind(initialGvk), | ||
lazyRestMapper, | ||
) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
g.Expect(scope).To(gmg.BeTrue()) | ||
|
||
// Register a new CRD at runtime. | ||
crd := newCRD(ctx, g, c, runtimeGvk.gvk.Group, runtimeGvk.gvk.Kind, runtimeGvk.plural) | ||
version := crd.Spec.Versions[0] | ||
version.Name = runtimeGvk.gvk.Version | ||
version.Storage = true | ||
version.Served = true | ||
crd.Spec.Versions = []apiextensionsv1.CustomResourceDefinitionVersion{version} | ||
crd.Spec.Scope = apiextensionsv1.NamespaceScoped | ||
|
||
g.Expect(c.Create(ctx, crd)).To(gmg.Succeed()) | ||
t.Cleanup(func() { | ||
g.Expect(c.Delete(ctx, crd)).To(gmg.Succeed()) | ||
}) | ||
|
||
// Wait until the CRD is registered. | ||
g.Eventually(func(g gmg.Gomega) { | ||
isRegistered, err := isCrdRegistered(restCfg, runtimeGvk.gvk) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
g.Expect(isRegistered).To(gmg.BeTrue()) | ||
}).Should(gmg.Succeed(), "GVK should be available") | ||
|
||
// Query the scope of the GVK registered at runtime. | ||
scope, err = apiutil.IsGVKNamespaced( | ||
schema.GroupVersionKind(runtimeGvk.gvk), | ||
lazyRestMapper, | ||
) | ||
g.Expect(err).NotTo(gmg.HaveOccurred()) | ||
g.Expect(scope).To(gmg.BeTrue()) | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.