func testOne(t *testing.T, client *kclient.Client, namespace, addrType string, success bool) *kapi.Endpoints { testEndpoint := &kapi.Endpoints{} testEndpoint.GenerateName = "test" testEndpoint.Subsets = []kapi.EndpointSubset{ { Addresses: []kapi.EndpointAddress{ { IP: exampleAddresses[addrType], }, }, Ports: []kapi.EndpointPort{ { Port: 9999, Protocol: kapi.ProtocolTCP, }, }, }, } ep, err := client.Endpoints(namespace).Create(testEndpoint) if err != nil && success { t.Fatalf("unexpected error creating %s network endpoint: %v", addrType, err) } else if err == nil && !success { t.Fatalf("unexpected success creating %s network endpoint", addrType) } return ep }
func createEndpoint(client *kubeclient.Client, meta kube.ObjectMeta, dnsIP string, dnsPort int32) error { endpoints := &kube.Endpoints{ ObjectMeta: meta, Subsets: []kube.EndpointSubset{ { Addresses: []kube.EndpointAddress{ {IP: dnsIP}, }, Ports: []kube.EndpointPort{ { Name: "dns", Port: dnsPort, }, { Name: "dns-tcp", Port: dnsPort, }, }, }, }, } _, err := client.Endpoints(meta.Namespace).Create(endpoints) if err != nil { return err } return nil }
func runMasterServiceTest(client *client.Client) { time.Sleep(12 * time.Second) svcList, err := client.Services(api.NamespaceDefault).List(labels.Everything()) if err != nil { glog.Fatalf("unexpected error listing services: %v", err) } var foundRW bool found := util.StringSet{} for i := range svcList.Items { found.Insert(svcList.Items[i].Name) if svcList.Items[i].Name == "kubernetes" { foundRW = true } } if foundRW { ep, err := client.Endpoints(api.NamespaceDefault).Get("kubernetes") if err != nil { glog.Fatalf("unexpected error listing endpoints for kubernetes service: %v", err) } if countEndpoints(ep) == 0 { glog.Fatalf("no endpoints for kubernetes service: %v", ep) } } else { glog.Errorf("no RW service found: %v", found) glog.Fatal("Kubernetes service test failed") } glog.Infof("Master service test passed.") }
func endpointsSet(c *client.Client, serviceNamespace, serviceID string, endpointCount int) wait.ConditionFunc { return func() (bool, error) { endpoints, err := c.Endpoints(serviceNamespace).Get(serviceID) if err != nil { glog.Infof("Error getting endpoints: %v", err) return false, nil } count := 0 for _, ss := range endpoints.Subsets { for _, addr := range ss.Addresses { for _, port := range ss.Ports { count++ glog.Infof("%s/%s endpoint: %s:%d %#v", serviceNamespace, serviceID, addr.IP, port.Port, addr.TargetRef) } } } return count == endpointCount, nil } }
// CheckService waits for the specified service to be ready by returning an error until the service is up // The check is done by polling the endpoint associated with the service and when the endpoint exists, returning no error->service-online // Credits: https://github.com/kubernetes/minikube/blob/v0.9.0/cmd/minikube/cmd/service.go#L89 func CheckService(ns string, service string, c *k8sclient.Client) error { svc, err := c.Services(ns).Get(service) if err != nil { return err } url := svc.ObjectMeta.Annotations[exposeURLAnnotation] if url == "" { util.Info(".") return errors.New("") } endpoints := c.Endpoints(ns) if endpoints == nil { util.Errorf("No endpoints found in namespace %s\n", ns) } endpoint, err := endpoints.Get(service) if err != nil { util.Errorf("No endpoints found for service %s\n", service) return err } return CheckEndpointReady(endpoint) }
func copyEndpoints(c *k8sclient.Client, fromNs string, toNs string, names []string) error { var answer error for _, name := range names { item, err := c.Endpoints(fromNs).Get(name) if err != nil { util.Warnf("No endpoint called %s found in namespace %s", name, fromNs) answer = err } name := item.Name objectMeta := item.ObjectMeta current, err := c.Endpoints(toNs).Get(name) if current == nil || err != nil { // lets create the endpoint newEndpoints := &api.Endpoints{ ObjectMeta: api.ObjectMeta{ Name: name, Labels: objectMeta.Labels, Annotations: objectMeta.Annotations, }, Subsets: item.Subsets, } _, err = c.Endpoints(toNs).Create(newEndpoints) if err != nil { return err } util.Infof("Created endpoint %s in namespace %s\n", name, toNs) } } return answer }
func getMaster(c *client.Client) string { master := "" switch framework.TestContext.Provider { case "gce": eps, err := c.Endpoints(api.NamespaceDefault).Get("kubernetes") if err != nil { framework.Failf("Fail to get kubernetes endpoinds: %v", err) } if len(eps.Subsets) != 1 || len(eps.Subsets[0].Addresses) != 1 { framework.Failf("There are more than 1 endpoints for kubernetes service: %+v", eps) } master = eps.Subsets[0].Addresses[0].IP case "gke": master = strings.TrimPrefix(framework.TestContext.Host, "https://") case "aws": // TODO(justinsb): Avoid hardcoding this. master = "172.20.0.9" default: framework.Failf("This test is not supported for provider %s and should be disabled", framework.TestContext.Provider) } return master }
func validateEndpointsOrFail(c *client.Client, namespace, serviceName string, expectedEndpoints PortsByPodName) { By(fmt.Sprintf("waiting up to %v for service %s in namespace %s to expose endpoints %v", serviceStartTimeout, serviceName, namespace, expectedEndpoints)) i := 1 for start := time.Now(); time.Since(start) < serviceStartTimeout; time.Sleep(1 * time.Second) { endpoints, err := c.Endpoints(namespace).Get(serviceName) if err != nil { Logf("Get endpoints failed (%v elapsed, ignoring for 5s): %v", time.Since(start), err) continue } // Logf("Found endpoints %v", endpoints) portsByPodUID := getContainerPortsByPodUID(endpoints) // Logf("Found port by pod UID %v", portsByPodUID) expectedPortsByPodUID := translatePodNameToUIDOrFail(c, namespace, expectedEndpoints) if len(portsByPodUID) == len(expectedEndpoints) { validatePortsOrFail(portsByPodUID, expectedPortsByPodUID) Logf("successfully validated that service %s in namespace %s exposes endpoints %v (%v elapsed)", serviceName, namespace, expectedEndpoints, time.Since(start)) return } if i%5 == 0 { Logf("Unexpected endpoints: found %v, expected %v (%v elapsed, will retry)", portsByPodUID, expectedEndpoints, time.Since(start)) } i++ } if pods, err := c.Pods(api.NamespaceAll).List(labels.Everything(), fields.Everything()); err == nil { for _, pod := range pods.Items { Logf("Pod %s\t%s\t%s\t%s", pod.Namespace, pod.Name, pod.Spec.NodeName, pod.DeletionTimestamp) } } else { Logf("Can't list pod debug info: %v", err) } Failf("Timed out waiting for service %s in namespace %s to expose endpoints %v (%v elapsed)", serviceName, namespace, expectedEndpoints, serviceStartTimeout) }
//getWebserverEndpoints returns the webserver endpoints as a set of String, each in the format like "http://{ip}:{port}" func getWebserverEndpoints(client *client.Client) sets.String { endpoints, err := client.Endpoints(*namespace).Get(*service) eps := sets.String{} if err != nil { state.Logf("Unable to read the endpoints for %v/%v: %v.", *namespace, *service, err) return eps } for _, ss := range endpoints.Subsets { for _, a := range ss.Addresses { for _, p := range ss.Ports { eps.Insert(fmt.Sprintf("http://%s:%d", a.IP, p.Port)) } } } return eps }
mkpath := func(file string) string { return filepath.Join(testContext.RepoRoot, "examples/guestbook-go", file) } controllerJson := mkpath("redis-master-controller.json") nsFlag := fmt.Sprintf("--namespace=%v", ns) redisPort := 6379 By("creating Redis RC") runKubectl("create", "-f", controllerJson, nsFlag) forEachPod(c, ns, "app", "redis", func(pod api.Pod) { lookForStringInLog(ns, pod.Name, "redis-master", "The server is now ready to accept connections", podStartTimeout) }) validateService := func(name string, servicePort int, timeout time.Duration) { err := wait.Poll(poll, timeout, func() (bool, error) { endpoints, err := c.Endpoints(ns).Get(name) if err != nil { if apierrs.IsNotFound(err) { err = nil } Logf("Get endpoints failed (interval %v): %v", poll, err) return false, err } uidToPort := getContainerPortsByPodUID(endpoints) if len(uidToPort) == 0 { Logf("No endpoint found, retrying") return false, nil } if len(uidToPort) > 1 { Fail("Too many endpoints found")
{ IP: serverIP, }, }, Ports: []api.EndpointPort{ { Name: "gluster", Port: 24007, Protocol: api.ProtocolTCP, }, }, }, }, } endClient := c.Endpoints(config.namespace) defer func() { if clean { endClient.Delete(config.prefix + "-server") } }() if _, err := endClient.Create(&endpoints); err != nil { framework.Failf("Failed to create endpoints for Gluster server: %v", err) } volume := api.VolumeSource{ Glusterfs: &api.GlusterfsVolumeSource{ EndpointsName: config.prefix + "-server", // 'test_vol' comes from test/images/volumes-tester/gluster/run_gluster.sh
func endpointsWatchFunc(c *client.Client, ns string) func(options api.ListOptions) (watch.Interface, error) { return func(options api.ListOptions) (watch.Interface, error) { return c.Endpoints(ns).Watch(options) } }
func endpointsListFunc(c *client.Client, ns string) func(api.ListOptions) (runtime.Object, error) { return func(opts api.ListOptions) (runtime.Object, error) { return c.Endpoints(ns).List(opts) } }
func runPatchTest(c *client.Client) { name := "patchservice" resource := "services" svcBody := api.Service{ TypeMeta: unversioned.TypeMeta{ APIVersion: c.APIVersion().String(), }, ObjectMeta: api.ObjectMeta{ Name: name, Labels: map[string]string{}, }, Spec: api.ServiceSpec{ // This is here because validation requires it. Selector: map[string]string{ "foo": "bar", }, Ports: []api.ServicePort{{ Port: 12345, Protocol: "TCP", }}, SessionAffinity: "None", }, } services := c.Services(api.NamespaceDefault) svc, err := services.Create(&svcBody) if err != nil { glog.Fatalf("Failed creating patchservice: %v", err) } patchBodies := map[unversioned.GroupVersion]map[api.PatchType]struct { AddLabelBody []byte RemoveLabelBody []byte RemoveAllLabelsBody []byte }{ v1.SchemeGroupVersion: { api.JSONPatchType: { []byte(`[{"op":"add","path":"/metadata/labels","value":{"foo":"bar","baz":"qux"}}]`), []byte(`[{"op":"remove","path":"/metadata/labels/foo"}]`), []byte(`[{"op":"remove","path":"/metadata/labels"}]`), }, api.MergePatchType: { []byte(`{"metadata":{"labels":{"foo":"bar","baz":"qux"}}}`), []byte(`{"metadata":{"labels":{"foo":null}}}`), []byte(`{"metadata":{"labels":null}}`), }, api.StrategicMergePatchType: { []byte(`{"metadata":{"labels":{"foo":"bar","baz":"qux"}}}`), []byte(`{"metadata":{"labels":{"foo":null}}}`), []byte(`{"metadata":{"labels":{"$patch":"replace"}}}`), }, }, } pb := patchBodies[c.APIVersion()] execPatch := func(pt api.PatchType, body []byte) error { return c.Patch(pt). Resource(resource). Namespace(api.NamespaceDefault). Name(name). Body(body). Do(). Error() } for k, v := range pb { // add label err := execPatch(k, v.AddLabelBody) if err != nil { glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err) } svc, err = services.Get(name) if err != nil { glog.Fatalf("Failed getting patchservice: %v", err) } if len(svc.Labels) != 2 || svc.Labels["foo"] != "bar" || svc.Labels["baz"] != "qux" { glog.Fatalf("Failed updating patchservice with patch type %s: labels are: %v", k, svc.Labels) } // remove one label err = execPatch(k, v.RemoveLabelBody) if err != nil { glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err) } svc, err = services.Get(name) if err != nil { glog.Fatalf("Failed getting patchservice: %v", err) } if len(svc.Labels) != 1 || svc.Labels["baz"] != "qux" { glog.Fatalf("Failed updating patchservice with patch type %s: labels are: %v", k, svc.Labels) } // remove all labels err = execPatch(k, v.RemoveAllLabelsBody) if err != nil { glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err) } svc, err = services.Get(name) if err != nil { glog.Fatalf("Failed getting patchservice: %v", err) } if svc.Labels != nil { glog.Fatalf("Failed remove all labels from patchservice with patch type %s: %v", k, svc.Labels) } } // Test patch with a resource that allows create on update endpointTemplate := &api.Endpoints{ ObjectMeta: api.ObjectMeta{Name: "patchendpoint"}, Subsets: []api.EndpointSubset{ { Addresses: []api.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []api.EndpointPort{{Port: 80, Protocol: api.ProtocolTCP}}, }, }, } patchEndpoint := func(json []byte) (runtime.Object, error) { return c.Patch(api.MergePatchType).Resource("endpoints").Namespace(api.NamespaceDefault).Name("patchendpoint").Body(json).Do().Get() } // Make sure patch doesn't get to CreateOnUpdate { endpointJSON, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), endpointTemplate) if err != nil { glog.Fatalf("Failed creating endpoint JSON: %v", err) } if obj, err := patchEndpoint(endpointJSON); !apierrors.IsNotFound(err) { glog.Fatalf("Expected notfound creating from patch, got error=%v and object: %#v", err, obj) } } // Create the endpoint (endpoints set AllowCreateOnUpdate=true) to get a UID and resource version createdEndpoint, err := c.Endpoints(api.NamespaceDefault).Update(endpointTemplate) if err != nil { glog.Fatalf("Failed creating endpoint: %v", err) } // Make sure identity patch is accepted { endpointJSON, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), createdEndpoint) if err != nil { glog.Fatalf("Failed creating endpoint JSON: %v", err) } if _, err := patchEndpoint(endpointJSON); err != nil { glog.Fatalf("Failed patching endpoint: %v", err) } } // Make sure patch complains about a mismatched resourceVersion { endpointTemplate.Name = "" endpointTemplate.UID = "" endpointTemplate.ResourceVersion = "1" endpointJSON, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), endpointTemplate) if err != nil { glog.Fatalf("Failed creating endpoint JSON: %v", err) } if _, err := patchEndpoint(endpointJSON); !apierrors.IsConflict(err) { glog.Fatalf("Expected error, got %#v", err) } } // Make sure patch complains about mutating the UID { endpointTemplate.Name = "" endpointTemplate.UID = "abc" endpointTemplate.ResourceVersion = "" endpointJSON, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), endpointTemplate) if err != nil { glog.Fatalf("Failed creating endpoint JSON: %v", err) } if _, err := patchEndpoint(endpointJSON); !apierrors.IsInvalid(err) { glog.Fatalf("Expected error, got %#v", err) } } // Make sure patch complains about a mismatched name { endpointTemplate.Name = "changedname" endpointTemplate.UID = "" endpointTemplate.ResourceVersion = "" endpointJSON, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), endpointTemplate) if err != nil { glog.Fatalf("Failed creating endpoint JSON: %v", err) } if _, err := patchEndpoint(endpointJSON); !apierrors.IsBadRequest(err) { glog.Fatalf("Expected error, got %#v", err) } } // Make sure patch containing originally submitted JSON is accepted { endpointTemplate.Name = "" endpointTemplate.UID = "" endpointTemplate.ResourceVersion = "" endpointJSON, err := runtime.Encode(api.Codecs.LegacyCodec(v1.SchemeGroupVersion), endpointTemplate) if err != nil { glog.Fatalf("Failed creating endpoint JSON: %v", err) } if _, err := patchEndpoint(endpointJSON); err != nil { glog.Fatalf("Failed patching endpoint: %v", err) } } glog.Info("PATCHs work.") }