mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 02:09:56 +00:00
Remove hostname endpoints annotation (was beta feature)
This commit is contained in:
parent
4495af3822
commit
994250f9ac
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
@ -25,7 +24,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api/v1"
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
"k8s.io/kubernetes/pkg/api/v1/endpoints"
|
|
||||||
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
||||||
kcache "k8s.io/kubernetes/pkg/client/cache"
|
kcache "k8s.io/kubernetes/pkg/client/cache"
|
||||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
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 {
|
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()
|
subCache := treecache.NewTreeCache()
|
||||||
glog.V(4).Infof("Endpoints Annotations: %v", e.Annotations)
|
glog.V(4).Infof("Endpoints Annotations: %v", e.Annotations)
|
||||||
for idx := range e.Subsets {
|
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]
|
address := &e.Subsets[idx].Addresses[subIdx]
|
||||||
endpointIP := address.IP
|
endpointIP := address.IP
|
||||||
recordValue, endpointName := util.GetSkyMsg(endpointIP, 0)
|
recordValue, endpointName := util.GetSkyMsg(endpointIP, 0)
|
||||||
if hostLabel, exists := getHostname(address, podHostnames); exists {
|
if hostLabel, exists := getHostname(address); exists {
|
||||||
endpointName = hostLabel
|
endpointName = hostLabel
|
||||||
}
|
}
|
||||||
subCache.SetEntry(endpointName, recordValue, kd.fqdn(svc, endpointName))
|
subCache.SetEntry(endpointName, recordValue, kd.fqdn(svc, endpointName))
|
||||||
@ -427,30 +420,13 @@ func (kd *KubeDNS) generateRecordsForHeadlessService(e *v1.Endpoints, svc *v1.Se
|
|||||||
return nil
|
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 {
|
if len(address.Hostname) > 0 {
|
||||||
return address.Hostname, true
|
return address.Hostname, true
|
||||||
}
|
}
|
||||||
if hostRecord, exists := podHostnames[address.IP]; exists && len(validation.IsDNS1123Label(hostRecord.HostName)) == 0 {
|
|
||||||
return hostRecord.HostName, true
|
|
||||||
}
|
|
||||||
return "", false
|
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 {
|
func (kd *KubeDNS) generateSRVRecordValue(svc *v1.Service, portNumber int, labels ...string) *skymsg.Service {
|
||||||
host := strings.Join([]string{svc.Name, svc.Namespace, serviceSubdomain, kd.domain}, ".")
|
host := strings.Join([]string{svc.Name, svc.Namespace, serviceSubdomain, kd.domain}, ".")
|
||||||
for _, cNameLabel := range labels {
|
for _, cNameLabel := range labels {
|
||||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -33,7 +32,6 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"k8s.io/kubernetes/pkg/api/v1"
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
endpointsapi "k8s.io/kubernetes/pkg/api/v1/endpoints"
|
|
||||||
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
||||||
"k8s.io/kubernetes/pkg/client/cache"
|
"k8s.io/kubernetes/pkg/client/cache"
|
||||||
fake "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
|
fake "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
|
||||||
@ -207,21 +205,9 @@ func TestSkyPodHostnameSRVLookup(t *testing.T) {
|
|||||||
|
|
||||||
service := newHeadlessService()
|
service := newHeadlessService()
|
||||||
endpointIPs := []string{"10.0.0.1", "10.0.0.2"}
|
endpointIPs := []string{"10.0.0.1", "10.0.0.2"}
|
||||||
endpoints := newEndpoints(service, newSubsetWithOnePort("", 80, endpointIPs...))
|
endpoints := newEndpoints(
|
||||||
|
service,
|
||||||
// The format of thes annotations is:
|
newSubsetWithOnePortWithHostname("", 80, true, endpointIPs...))
|
||||||
// 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),
|
|
||||||
}
|
|
||||||
assert.NoError(t, kd.endpointsStore.Add(endpoints))
|
assert.NoError(t, kd.endpointsStore.Add(endpoints))
|
||||||
kd.newService(service)
|
kd.newService(service)
|
||||||
name := strings.Join([]string{testService, testNamespace, "svc", testDomain}, ".")
|
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 {
|
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 := newSubset()
|
||||||
subset.Ports = append(subset.Ports, v1.EndpointPort{Port: port, Name: portName, Protocol: "TCP"})
|
subset.Ports = append(subset.Ports, v1.EndpointPort{Port: port, Name: portName, Protocol: "TCP"})
|
||||||
for _, ip := range ips {
|
for i, ip := range ips {
|
||||||
subset.Addresses = append(subset.Addresses, v1.EndpointAddress{IP: ip})
|
var hostname string
|
||||||
|
if addHostname {
|
||||||
|
hostname = fmt.Sprintf("ep-%d", i)
|
||||||
|
}
|
||||||
|
subset.Addresses = append(subset.Addresses, v1.EndpointAddress{IP: ip, Hostname: hostname})
|
||||||
}
|
}
|
||||||
return subset
|
return subset
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user