|
| 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 endpoints |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + v1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 27 | + "k8s.io/apimachinery/pkg/util/wait" |
| 28 | + "k8s.io/client-go/informers" |
| 29 | + clientset "k8s.io/client-go/kubernetes" |
| 30 | + restclient "k8s.io/client-go/rest" |
| 31 | + "k8s.io/kubernetes/pkg/controller/endpoint" |
| 32 | + "k8s.io/kubernetes/test/integration/framework" |
| 33 | +) |
| 34 | + |
| 35 | +func TestEndpointUpdates(t *testing.T) { |
| 36 | + masterConfig := framework.NewIntegrationTestMasterConfig() |
| 37 | + _, server, closeFn := framework.RunAMaster(masterConfig) |
| 38 | + defer closeFn() |
| 39 | + |
| 40 | + config := restclient.Config{Host: server.URL} |
| 41 | + client, err := clientset.NewForConfig(&config) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("Error creating clientset: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + informers := informers.NewSharedInformerFactory(client, 0) |
| 47 | + |
| 48 | + epController := endpoint.NewEndpointController( |
| 49 | + informers.Core().V1().Pods(), |
| 50 | + informers.Core().V1().Services(), |
| 51 | + informers.Core().V1().Endpoints(), |
| 52 | + client, |
| 53 | + 0) |
| 54 | + |
| 55 | + // Start informer and controllers |
| 56 | + stopCh := make(chan struct{}) |
| 57 | + defer close(stopCh) |
| 58 | + informers.Start(stopCh) |
| 59 | + go epController.Run(1, stopCh) |
| 60 | + |
| 61 | + // Create namespace |
| 62 | + ns := framework.CreateTestingNamespace("test-endpoints-updates", server, t) |
| 63 | + defer framework.DeleteTestingNamespace(ns, server, t) |
| 64 | + |
| 65 | + // Create a pod with labels |
| 66 | + pod := &v1.Pod{ |
| 67 | + ObjectMeta: metav1.ObjectMeta{ |
| 68 | + Name: "test-pod", |
| 69 | + Namespace: ns.Name, |
| 70 | + Labels: labelMap(), |
| 71 | + }, |
| 72 | + Spec: v1.PodSpec{ |
| 73 | + NodeName: "fakenode", |
| 74 | + Containers: []v1.Container{ |
| 75 | + { |
| 76 | + Name: "fake-name", |
| 77 | + Image: "fakeimage", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + createdPod, err := client.CoreV1().Pods(ns.Name).Create(context.TODO(), pod, metav1.CreateOptions{}) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("Failed to create pod %s: %v", pod.Name, err) |
| 86 | + } |
| 87 | + |
| 88 | + // Set pod IPs |
| 89 | + createdPod.Status = v1.PodStatus{ |
| 90 | + Phase: v1.PodRunning, |
| 91 | + PodIPs: []v1.PodIP{{IP: "1.1.1.1"}, {IP: "2001:db8::"}}, |
| 92 | + } |
| 93 | + _, err = client.CoreV1().Pods(ns.Name).UpdateStatus(context.TODO(), createdPod, metav1.UpdateOptions{}) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("Failed to update status of pod %s: %v", pod.Name, err) |
| 96 | + } |
| 97 | + |
| 98 | + // Create a service associated to the pod |
| 99 | + svc := newService(ns.Name, "foo1") |
| 100 | + svc1, err := client.CoreV1().Services(ns.Name).Create(context.TODO(), svc, metav1.CreateOptions{}) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("Failed to create service %s: %v", svc.Name, err) |
| 103 | + } |
| 104 | + |
| 105 | + // Obtain ResourceVersion of the new endpoint created |
| 106 | + var resVersion string |
| 107 | + if err := wait.PollImmediate(1*time.Second, wait.ForeverTestTimeout, func() (bool, error) { |
| 108 | + endpoints, err := client.CoreV1().Endpoints(ns.Name).Get(context.TODO(), svc.Name, metav1.GetOptions{}) |
| 109 | + if err != nil { |
| 110 | + t.Logf("error fetching endpoints: %v", err) |
| 111 | + return false, nil |
| 112 | + } |
| 113 | + resVersion = endpoints.ObjectMeta.ResourceVersion |
| 114 | + return true, nil |
| 115 | + }); err != nil { |
| 116 | + t.Fatalf("endpoints not found: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + // Force recomputation on the endpoint controller |
| 120 | + svc1.SetAnnotations(map[string]string{"foo": "bar"}) |
| 121 | + _, err = client.CoreV1().Services(ns.Name).Update(context.TODO(), svc1, metav1.UpdateOptions{}) |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("Failed to update service %s: %v", svc1.Name, err) |
| 124 | + } |
| 125 | + |
| 126 | + // Create a new service and wait until it has been processed, |
| 127 | + // this way we can be sure that the endpoint for the original service |
| 128 | + // was recomputed before asserting, since we only have 1 worker |
| 129 | + // in the endpoint controller |
| 130 | + svc2 := newService(ns.Name, "foo2") |
| 131 | + _, err = client.CoreV1().Services(ns.Name).Create(context.TODO(), svc2, metav1.CreateOptions{}) |
| 132 | + if err != nil { |
| 133 | + t.Fatalf("Failed to create service %s: %v", svc.Name, err) |
| 134 | + } |
| 135 | + |
| 136 | + if err := wait.PollImmediate(1*time.Second, wait.ForeverTestTimeout, func() (bool, error) { |
| 137 | + _, err := client.CoreV1().Endpoints(ns.Name).Get(context.TODO(), svc2.Name, metav1.GetOptions{}) |
| 138 | + if err != nil { |
| 139 | + t.Logf("error fetching endpoints: %v", err) |
| 140 | + return false, nil |
| 141 | + } |
| 142 | + return true, nil |
| 143 | + }); err != nil { |
| 144 | + t.Fatalf("endpoints not found: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + // the endpoint controller should not update the endpoint created for the original |
| 148 | + // service since nothing has changed, the resource version has to be the same |
| 149 | + endpoints, err := client.CoreV1().Endpoints(ns.Name).Get(context.TODO(), svc.Name, metav1.GetOptions{}) |
| 150 | + if err != nil { |
| 151 | + t.Fatalf("error fetching endpoints: %v", err) |
| 152 | + } |
| 153 | + if resVersion != endpoints.ObjectMeta.ResourceVersion { |
| 154 | + t.Fatalf("endpoints resource version does not match, expected %s received %s", resVersion, endpoints.ObjectMeta.ResourceVersion) |
| 155 | + } |
| 156 | + |
| 157 | +} |
| 158 | + |
| 159 | +func labelMap() map[string]string { |
| 160 | + return map[string]string{"foo": "bar"} |
| 161 | +} |
| 162 | + |
| 163 | +// newService returns a service with selector and exposing ports |
| 164 | +func newService(namespace, name string) *v1.Service { |
| 165 | + return &v1.Service{ |
| 166 | + ObjectMeta: metav1.ObjectMeta{ |
| 167 | + Name: name, |
| 168 | + Namespace: namespace, |
| 169 | + Labels: labelMap(), |
| 170 | + }, |
| 171 | + Spec: v1.ServiceSpec{ |
| 172 | + Selector: labelMap(), |
| 173 | + Ports: []v1.ServicePort{ |
| 174 | + {Name: "port-1338", Port: 1338, Protocol: "TCP", TargetPort: intstr.FromInt(1338)}, |
| 175 | + {Name: "port-1337", Port: 1337, Protocol: "TCP", TargetPort: intstr.FromInt(1337)}, |
| 176 | + }, |
| 177 | + }, |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments