|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package client_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "sync/atomic" |
| 23 | + |
| 24 | + . "github.com/onsi/ginkgo" |
| 25 | + . "github.com/onsi/gomega" |
| 26 | + appsv1 "k8s.io/api/apps/v1" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 30 | + "k8s.io/apimachinery/pkg/fields" |
| 31 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 32 | + "k8s.io/apimachinery/pkg/watch" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 34 | +) |
| 35 | + |
| 36 | +var _ = Describe("ClientWithWatch", func() { |
| 37 | + var dep *appsv1.Deployment |
| 38 | + var count uint64 = 0 |
| 39 | + var replicaCount int32 = 2 |
| 40 | + var ns = "kube-public" |
| 41 | + ctx := context.TODO() |
| 42 | + |
| 43 | + BeforeEach(func(done Done) { |
| 44 | + atomic.AddUint64(&count, 1) |
| 45 | + dep = &appsv1.Deployment{ |
| 46 | + ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("watch-deployment-name-%v", count), Namespace: ns, Labels: map[string]string{"app": fmt.Sprintf("bar-%v", count)}}, |
| 47 | + Spec: appsv1.DeploymentSpec{ |
| 48 | + Replicas: &replicaCount, |
| 49 | + Selector: &metav1.LabelSelector{ |
| 50 | + MatchLabels: map[string]string{"foo": "bar"}, |
| 51 | + }, |
| 52 | + Template: corev1.PodTemplateSpec{ |
| 53 | + ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"foo": "bar"}}, |
| 54 | + Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + var err error |
| 60 | + dep, err = clientset.AppsV1().Deployments(ns).Create(ctx, dep, metav1.CreateOptions{}) |
| 61 | + Expect(err).NotTo(HaveOccurred()) |
| 62 | + close(done) |
| 63 | + }, serverSideTimeoutSeconds) |
| 64 | + |
| 65 | + AfterEach(func(done Done) { |
| 66 | + deleteDeployment(ctx, dep, ns) |
| 67 | + close(done) |
| 68 | + }, serverSideTimeoutSeconds) |
| 69 | + |
| 70 | + Describe("NewWithWatch", func() { |
| 71 | + It("should return a new Client", func(done Done) { |
| 72 | + cl, err := client.NewWithWatch(cfg, client.Options{}) |
| 73 | + Expect(err).NotTo(HaveOccurred()) |
| 74 | + Expect(cl).NotTo(BeNil()) |
| 75 | + |
| 76 | + close(done) |
| 77 | + }) |
| 78 | + |
| 79 | + watchSuite := func(through client.ObjectList, expectedType client.Object) { |
| 80 | + cl, err := client.NewWithWatch(cfg, client.Options{}) |
| 81 | + Expect(err).NotTo(HaveOccurred()) |
| 82 | + Expect(cl).NotTo(BeNil()) |
| 83 | + |
| 84 | + watchInterface, err := cl.Watch(ctx, through, &client.ListOptions{ |
| 85 | + FieldSelector: fields.OneTermEqualSelector("metadata.name", dep.Name), |
| 86 | + Namespace: dep.Namespace, |
| 87 | + }) |
| 88 | + Expect(err).NotTo(HaveOccurred()) |
| 89 | + Expect(watchInterface).NotTo(BeNil()) |
| 90 | + |
| 91 | + defer watchInterface.Stop() |
| 92 | + |
| 93 | + event, ok := <-watchInterface.ResultChan() |
| 94 | + Expect(ok).To(BeTrue()) |
| 95 | + Expect(event.Type).To(BeIdenticalTo(watch.Added)) |
| 96 | + Expect(event.Object).To(BeAssignableToTypeOf(expectedType)) |
| 97 | + |
| 98 | + // The metadata client doesn't set GVK so we just use the |
| 99 | + // name and UID as a proxy to confirm that we got the right |
| 100 | + // object. |
| 101 | + metaObject, ok := event.Object.(metav1.Object) |
| 102 | + Expect(ok).To(BeTrue()) |
| 103 | + Expect(metaObject.GetName()).To(Equal(dep.Name)) |
| 104 | + Expect(metaObject.GetUID()).To(Equal(dep.UID)) |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + It("should receive a create event when watching the typed object", func(done Done) { |
| 109 | + watchSuite(&appsv1.DeploymentList{}, &appsv1.Deployment{}) |
| 110 | + close(done) |
| 111 | + }, 15) |
| 112 | + |
| 113 | + It("should receive a create event when watching the unstructured object", func(done Done) { |
| 114 | + u := &unstructured.UnstructuredList{} |
| 115 | + u.SetGroupVersionKind(schema.GroupVersionKind{ |
| 116 | + Group: "apps", |
| 117 | + Kind: "Deployment", |
| 118 | + Version: "v1", |
| 119 | + }) |
| 120 | + watchSuite(u, &unstructured.Unstructured{}) |
| 121 | + close(done) |
| 122 | + }, 15) |
| 123 | + |
| 124 | + It("should receive a create event when watching the metadata object", func(done Done) { |
| 125 | + m := &metav1.PartialObjectMetadataList{TypeMeta: metav1.TypeMeta{Kind: "Deployment", APIVersion: "apps/v1"}} |
| 126 | + watchSuite(m, &metav1.PartialObjectMetadata{}) |
| 127 | + close(done) |
| 128 | + }, 15) |
| 129 | + }) |
| 130 | + |
| 131 | +}) |
0 commit comments