Merge pull request #125305 from p0lyn0mial/upstream-dynamic-client-uses-watch-list

dynamic client: add support for API streaming
This commit is contained in:
Kubernetes Prow Robot
2024-07-15 10:05:12 -07:00
committed by GitHub
3 changed files with 273 additions and 25 deletions

View File

@@ -29,12 +29,15 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/dynamic"
clientfeatures "k8s.io/client-go/features"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/consistencydetector"
"k8s.io/component-base/featuregate"
@@ -103,14 +106,7 @@ var _ = SIGDescribe("API Streaming (aka. WatchList)", framework.WithSerial(), fu
expectedSecrets = append(expectedSecrets, *secret)
}
var actualRequestsMadeByKubeClient []string
clientConfig := f.ClientConfig()
clientConfig.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(req *http.Request) (*http.Response, error) {
actualRequestsMadeByKubeClient = append(actualRequestsMadeByKubeClient, req.URL.RawQuery)
return rt.RoundTrip(req)
})
})
rt, clientConfig := clientConfigWithRoundTripper(f)
wrappedKubeClient, err := kubernetes.NewForConfig(clientConfig)
framework.ExpectNoError(err)
@@ -120,26 +116,64 @@ var _ = SIGDescribe("API Streaming (aka. WatchList)", framework.WithSerial(), fu
ginkgo.By("Verifying if the secret list was properly streamed")
streamedSecrets := secretList.Items
sort.Sort(byName(expectedSecrets))
gomega.Expect(cmp.Equal(expectedSecrets, streamedSecrets)).To(gomega.BeTrueBecause("data received via watchlist must match the added data"))
ginkgo.By("Verifying if expected requests were sent to the server")
expectedRequestMadeByKubeClient := []string{
// corresponds to a streaming request made by the kube client to stream the secrets
"allowWatchBookmarks=true&resourceVersionMatch=NotOlderThan&sendInitialEvents=true&watch=true",
expectedRequestMadeByKubeClient := getExpectedRequestMadeByClientFor(secretList.ResourceVersion)
gomega.Expect(rt.actualRequests).To(gomega.Equal(expectedRequestMadeByKubeClient))
})
ginkgo.It("should be requested by dynamic client's List method when WatchListClient is enabled", func(ctx context.Context) {
featuregatetesting.SetFeatureGateDuringTest(ginkgo.GinkgoTB(), utilfeature.DefaultFeatureGate, featuregate.Feature(clientfeatures.WatchListClient), true)
ginkgo.By(fmt.Sprintf("Adding 5 secrets to %s namespace", f.Namespace.Name))
var expectedSecrets []unstructured.Unstructured
for i := 1; i <= 5; i++ {
unstructuredSecret, err := runtime.DefaultUnstructuredConverter.ToUnstructured(newSecret(fmt.Sprintf("secret-%d", i)))
framework.ExpectNoError(err)
secret, err := f.DynamicClient.Resource(v1.SchemeGroupVersion.WithResource("secrets")).Namespace(f.Namespace.Name).Create(ctx, &unstructured.Unstructured{Object: unstructuredSecret}, metav1.CreateOptions{})
framework.ExpectNoError(err)
expectedSecrets = append(expectedSecrets, *secret)
}
if consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
// corresponds to a standard list request made by the consistency detector build in into the kube client
expectedRequestMadeByKubeClient = append(expectedRequestMadeByKubeClient, fmt.Sprintf("resourceVersion=%s&resourceVersionMatch=Exact", secretList.ResourceVersion))
}
gomega.Expect(actualRequestsMadeByKubeClient).To(gomega.Equal(expectedRequestMadeByKubeClient))
rt, clientConfig := clientConfigWithRoundTripper(f)
wrappedDynamicClient, err := dynamic.NewForConfig(clientConfig)
framework.ExpectNoError(err)
ginkgo.By("Streaming secrets from the server")
secretList, err := wrappedDynamicClient.Resource(v1.SchemeGroupVersion.WithResource("secrets")).Namespace(f.Namespace.Name).List(ctx, metav1.ListOptions{})
framework.ExpectNoError(err)
ginkgo.By("Verifying if the secret list was properly streamed")
streamedSecrets := secretList.Items
gomega.Expect(cmp.Equal(expectedSecrets, streamedSecrets)).To(gomega.BeTrueBecause("data received via watchlist must match the added data"))
ginkgo.By("Verifying if expected requests were sent to the server")
expectedRequestMadeByDynamicClient := getExpectedRequestMadeByClientFor(secretList.GetResourceVersion())
gomega.Expect(rt.actualRequests).To(gomega.Equal(expectedRequestMadeByDynamicClient))
})
})
type roundTripFunc func(req *http.Request) (*http.Response, error)
type roundTripper struct {
actualRequests []string
delegate http.RoundTripper
}
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
r.actualRequests = append(r.actualRequests, req.URL.RawQuery)
return r.delegate.RoundTrip(req)
}
func (r *roundTripper) Wrap(delegate http.RoundTripper) http.RoundTripper {
r.delegate = delegate
return r
}
func clientConfigWithRoundTripper(f *framework.Framework) (*roundTripper, *rest.Config) {
clientConfig := f.ClientConfig()
rt := &roundTripper{}
clientConfig.Wrap(rt.Wrap)
return rt, clientConfig
}
func verifyStore(ctx context.Context, expectedSecrets []v1.Secret, store cache.Store) {
@@ -157,6 +191,18 @@ func verifyStore(ctx context.Context, expectedSecrets []v1.Secret, store cache.S
framework.ExpectNoError(err)
}
func getExpectedRequestMadeByClientFor(rv string) []string {
expectedRequestMadeByClient := []string{
// corresponds to a streaming request made by the client to stream the secrets
"allowWatchBookmarks=true&resourceVersionMatch=NotOlderThan&sendInitialEvents=true&watch=true",
}
if consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
// corresponds to a standard list request made by the consistency detector build in into the client
expectedRequestMadeByClient = append(expectedRequestMadeByClient, fmt.Sprintf("resourceVersion=%s&resourceVersionMatch=Exact", rv))
}
return expectedRequestMadeByClient
}
type byName []v1.Secret
func (a byName) Len() int { return len(a) }