Move federation record response logic into its own method

This introduces some decoupling between the mainline logic and the
federation logic.
This commit is contained in:
Bowei Du 2016-10-20 16:11:54 -07:00
parent 2cf7f7bfc5
commit ffed9c6f93

View File

@ -462,12 +462,15 @@ func (kd *KubeDNS) newExternalNameService(service *kapi.Service) {
// the subtree matching the name are returned. // the subtree matching the name are returned.
func (kd *KubeDNS) Records(name string, exact bool) (retval []skymsg.Service, err error) { func (kd *KubeDNS) Records(name string, exact bool) (retval []skymsg.Service, err error) {
glog.V(2).Infof("Received DNS Request:%s, exact:%v", name, exact) glog.V(2).Infof("Received DNS Request:%s, exact:%v", name, exact)
trimmed := strings.TrimRight(name, ".") trimmed := strings.TrimRight(name, ".")
segments := strings.Split(trimmed, ".") segments := strings.Split(trimmed, ".")
isFederationQuery := false isFederationQuery := false
federationSegments := []string{} federationSegments := []string{}
if !exact && kd.isFederationQuery(segments) { if !exact && kd.isFederationQuery(segments) {
glog.V(2).Infof("federation service query: Received federation query. Going to try to find local service first") glog.V(2).Infof(
"federation service query: Received federation query. Going to try to find local service first")
// Try quering the non-federation (local) service first. // Try quering the non-federation (local) service first.
// Will try the federation one later, if this fails. // Will try the federation one later, if this fails.
isFederationQuery = true isFederationQuery = true
@ -476,18 +479,24 @@ func (kd *KubeDNS) Records(name string, exact bool) (retval []skymsg.Service, er
// Federation name is 3rd in the segment (after service name and namespace). // Federation name is 3rd in the segment (after service name and namespace).
segments = append(segments[:2], segments[3:]...) segments = append(segments[:2], segments[3:]...)
} }
path := reverseArray(segments) path := reverseArray(segments)
records, err := kd.getRecordsForPath(path, exact) records, err := kd.getRecordsForPath(path, exact)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !isFederationQuery {
if len(records) > 0 { if isFederationQuery {
return records, nil return kd.recordsForFederation(records, path, exact, federationSegments)
} } else if len(records) > 0 {
return nil, etcd.Error{Code: etcd.ErrorCodeKeyNotFound} return records, nil
} }
return nil, etcd.Error{Code: etcd.ErrorCodeKeyNotFound}
}
func (kd *KubeDNS) recordsForFederation(records []skymsg.Service, path []string, exact bool, federationSegments []string) (retval []skymsg.Service, err error) {
// For federation query, verify that the local service has endpoints. // For federation query, verify that the local service has endpoints.
validRecord := false validRecord := false
for _, val := range records { for _, val := range records {
@ -496,7 +505,8 @@ func (kd *KubeDNS) Records(name string, exact bool) (retval []skymsg.Service, er
if !kd.isHeadlessServiceRecord(&val) { if !kd.isHeadlessServiceRecord(&val) {
ok, err := kd.serviceWithClusterIPHasEndpoints(&val) ok, err := kd.serviceWithClusterIPHasEndpoints(&val)
if err != nil { if err != nil {
glog.V(2).Infof("federation service query: unexpected error while trying to find if service has endpoint: %v", err) glog.V(2).Infof(
"federation service query: unexpected error while trying to find if service has endpoint: %v", err)
continue continue
} }
if !ok { if !ok {
@ -507,6 +517,7 @@ func (kd *KubeDNS) Records(name string, exact bool) (retval []skymsg.Service, er
validRecord = true validRecord = true
break break
} }
if validRecord { if validRecord {
// There is a local service with valid endpoints, return its CNAME. // There is a local service with valid endpoints, return its CNAME.
name := strings.Join(reverseArray(path), ".") name := strings.Join(reverseArray(path), ".")
@ -516,14 +527,15 @@ func (kd *KubeDNS) Records(name string, exact bool) (retval []skymsg.Service, er
if !strings.HasSuffix(name, ".") { if !strings.HasSuffix(name, ".") {
name = name + "." name = name + "."
} }
glog.Infof("federation service query: Returning CNAME for local service : %s", name) glog.V(2).Infof("federation service query: Returning CNAME for local service : %s", name)
return []skymsg.Service{{Host: name}}, nil return []skymsg.Service{{Host: name}}, nil
} }
// If the name query is not an exact query and does not match any records in the local store, // If the name query is not an exact query and does not match any records in the local store,
// attempt to send a federation redirect (CNAME) response. // attempt to send a federation redirect (CNAME) response.
if !exact { if !exact {
glog.V(2).Infof("federation service query: Did not find a local service. Trying federation redirect (CNAME) response") glog.V(2).Infof(
"federation service query: Did not find a local service. Trying federation redirect (CNAME) response")
return kd.federationRecords(reverseArray(federationSegments)) return kd.federationRecords(reverseArray(federationSegments))
} }