...

Source file src/sigs.k8s.io/controller-runtime/pkg/client/watch_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/client

     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/v2"
    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() {
    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  	})
    63  
    64  	AfterEach(func() {
    65  		deleteDeployment(ctx, dep, ns)
    66  	})
    67  
    68  	Describe("NewWithWatch", func() {
    69  		It("should return a new Client", func() {
    70  			cl, err := client.NewWithWatch(cfg, client.Options{})
    71  			Expect(err).NotTo(HaveOccurred())
    72  			Expect(cl).NotTo(BeNil())
    73  		})
    74  
    75  		watchSuite := func(through client.ObjectList, expectedType client.Object, checkGvk bool) {
    76  			cl, err := client.NewWithWatch(cfg, client.Options{})
    77  			Expect(err).NotTo(HaveOccurred())
    78  			Expect(cl).NotTo(BeNil())
    79  
    80  			watchInterface, err := cl.Watch(ctx, through, &client.ListOptions{
    81  				FieldSelector: fields.OneTermEqualSelector("metadata.name", dep.Name),
    82  				Namespace:     dep.Namespace,
    83  			})
    84  			Expect(err).NotTo(HaveOccurred())
    85  			Expect(watchInterface).NotTo(BeNil())
    86  
    87  			defer watchInterface.Stop()
    88  
    89  			event, ok := <-watchInterface.ResultChan()
    90  			Expect(ok).To(BeTrue())
    91  			Expect(event.Type).To(BeIdenticalTo(watch.Added))
    92  			Expect(event.Object).To(BeAssignableToTypeOf(expectedType))
    93  
    94  			// The metadata client doesn't set GVK so we just use the
    95  			// name and UID as a proxy to confirm that we got the right
    96  			// object.
    97  			metaObject, ok := event.Object.(metav1.Object)
    98  			Expect(ok).To(BeTrue())
    99  			Expect(metaObject.GetName()).To(Equal(dep.Name))
   100  			Expect(metaObject.GetUID()).To(Equal(dep.UID))
   101  
   102  			if checkGvk {
   103  				runtimeObject := event.Object
   104  				gvk := runtimeObject.GetObjectKind().GroupVersionKind()
   105  				Expect(gvk).To(Equal(schema.GroupVersionKind{
   106  					Group:   "apps",
   107  					Kind:    "Deployment",
   108  					Version: "v1",
   109  				}))
   110  			}
   111  		}
   112  
   113  		It("should receive a create event when watching the typed object", func() {
   114  			watchSuite(&appsv1.DeploymentList{}, &appsv1.Deployment{}, false)
   115  		})
   116  
   117  		It("should receive a create event when watching the unstructured object", func() {
   118  			u := &unstructured.UnstructuredList{}
   119  			u.SetGroupVersionKind(schema.GroupVersionKind{
   120  				Group:   "apps",
   121  				Kind:    "Deployment",
   122  				Version: "v1",
   123  			})
   124  			watchSuite(u, &unstructured.Unstructured{}, true)
   125  		})
   126  
   127  		It("should receive a create event when watching the metadata object", func() {
   128  			m := &metav1.PartialObjectMetadataList{TypeMeta: metav1.TypeMeta{Kind: "Deployment", APIVersion: "apps/v1"}}
   129  			watchSuite(m, &metav1.PartialObjectMetadata{}, false)
   130  		})
   131  	})
   132  
   133  })
   134  

View as plain text