From 994250f9ace15e9a96988beba624830579543bb9 Mon Sep 17 00:00:00 2001 From: Bowei Du Date: Wed, 21 Dec 2016 14:00:37 -0800 Subject: [PATCH] Remove hostname endpoints annotation (was beta feature) --- pkg/dns/dns.go | 28 ++-------------------------- pkg/dns/dns_test.go | 32 +++++++++++++------------------- 2 files changed, 15 insertions(+), 45 deletions(-) diff --git a/pkg/dns/dns.go b/pkg/dns/dns.go index 874f7b17900..955114ef82d 100644 --- a/pkg/dns/dns.go +++ b/pkg/dns/dns.go @@ -17,7 +17,6 @@ limitations under the License. package dns import ( - "encoding/json" "fmt" "net" "strings" @@ -25,7 +24,6 @@ import ( "time" "k8s.io/kubernetes/pkg/api/v1" - "k8s.io/kubernetes/pkg/api/v1/endpoints" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" kcache "k8s.io/kubernetes/pkg/client/cache" clientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset" @@ -392,11 +390,6 @@ func (kd *KubeDNS) newPortalService(service *v1.Service) { } func (kd *KubeDNS) generateRecordsForHeadlessService(e *v1.Endpoints, svc *v1.Service) error { - // TODO: remove this after v1.4 is released and the old annotations are EOL - podHostnames, err := getPodHostnamesFromAnnotation(e.Annotations) - if err != nil { - return err - } subCache := treecache.NewTreeCache() glog.V(4).Infof("Endpoints Annotations: %v", e.Annotations) for idx := range e.Subsets { @@ -404,7 +397,7 @@ func (kd *KubeDNS) generateRecordsForHeadlessService(e *v1.Endpoints, svc *v1.Se address := &e.Subsets[idx].Addresses[subIdx] endpointIP := address.IP recordValue, endpointName := util.GetSkyMsg(endpointIP, 0) - if hostLabel, exists := getHostname(address, podHostnames); exists { + if hostLabel, exists := getHostname(address); exists { endpointName = hostLabel } subCache.SetEntry(endpointName, recordValue, kd.fqdn(svc, endpointName)) @@ -427,30 +420,13 @@ func (kd *KubeDNS) generateRecordsForHeadlessService(e *v1.Endpoints, svc *v1.Se return nil } -func getHostname(address *v1.EndpointAddress, podHostnames map[string]endpoints.HostRecord) (string, bool) { +func getHostname(address *v1.EndpointAddress) (string, bool) { if len(address.Hostname) > 0 { return address.Hostname, true } - if hostRecord, exists := podHostnames[address.IP]; exists && len(validation.IsDNS1123Label(hostRecord.HostName)) == 0 { - return hostRecord.HostName, true - } return "", false } -func getPodHostnamesFromAnnotation(annotations map[string]string) (map[string]endpoints.HostRecord, error) { - hostnames := map[string]endpoints.HostRecord{} - - if annotations != nil { - if serializedHostnames, exists := annotations[endpoints.PodHostnamesAnnotation]; exists && len(serializedHostnames) > 0 { - err := json.Unmarshal([]byte(serializedHostnames), &hostnames) - if err != nil { - return nil, err - } - } - } - return hostnames, nil -} - func (kd *KubeDNS) generateSRVRecordValue(svc *v1.Service, portNumber int, labels ...string) *skymsg.Service { host := strings.Join([]string{svc.Name, svc.Namespace, serviceSubdomain, kd.domain}, ".") for _, cNameLabel := range labels { diff --git a/pkg/dns/dns_test.go b/pkg/dns/dns_test.go index 49312b3c73a..39548e2a3e3 100644 --- a/pkg/dns/dns_test.go +++ b/pkg/dns/dns_test.go @@ -17,7 +17,6 @@ limitations under the License. package dns import ( - "encoding/json" "fmt" "net" "reflect" @@ -33,7 +32,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "k8s.io/kubernetes/pkg/api/v1" - endpointsapi "k8s.io/kubernetes/pkg/api/v1/endpoints" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" "k8s.io/kubernetes/pkg/client/cache" fake "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake" @@ -207,21 +205,9 @@ func TestSkyPodHostnameSRVLookup(t *testing.T) { service := newHeadlessService() endpointIPs := []string{"10.0.0.1", "10.0.0.2"} - endpoints := newEndpoints(service, newSubsetWithOnePort("", 80, endpointIPs...)) - - // The format of thes annotations is: - // endpoints.beta.kubernetes.io/hostnames-map: '{"ep-ip":{"HostName":"pod request hostname"}}' - epRecords := map[string]endpointsapi.HostRecord{} - for i, ep := range endpointIPs { - epRecords[ep] = endpointsapi.HostRecord{HostName: fmt.Sprintf("ep-%d", i)} - } - b, err := json.Marshal(epRecords) - if err != nil { - t.Fatalf("%v", err) - } - endpoints.Annotations = map[string]string{ - endpointsapi.PodHostnamesAnnotation: string(b), - } + endpoints := newEndpoints( + service, + newSubsetWithOnePortWithHostname("", 80, true, endpointIPs...)) assert.NoError(t, kd.endpointsStore.Add(endpoints)) kd.newService(service) name := strings.Join([]string{testService, testNamespace, "svc", testDomain}, ".") @@ -676,10 +662,18 @@ func newEndpoints(service *v1.Service, subsets ...v1.EndpointSubset) *v1.Endpoin } func newSubsetWithOnePort(portName string, port int32, ips ...string) v1.EndpointSubset { + return newSubsetWithOnePortWithHostname(portName, port, false, ips...) +} + +func newSubsetWithOnePortWithHostname(portName string, port int32, addHostname bool, ips ...string) v1.EndpointSubset { subset := newSubset() subset.Ports = append(subset.Ports, v1.EndpointPort{Port: port, Name: portName, Protocol: "TCP"}) - for _, ip := range ips { - subset.Addresses = append(subset.Addresses, v1.EndpointAddress{IP: ip}) + for i, ip := range ips { + var hostname string + if addHostname { + hostname = fmt.Sprintf("ep-%d", i) + } + subset.Addresses = append(subset.Addresses, v1.EndpointAddress{IP: ip, Hostname: hostname}) } return subset }