func (kd *KubeDNS) newPortalService(service *v1.Service) { subCache := treecache.NewTreeCache() recordValue, recordLabel := util.GetSkyMsg(service.Spec.ClusterIP, 0) subCache.SetEntry(recordLabel, recordValue, kd.fqdn(service, recordLabel)) // Generate SRV Records for i := range service.Spec.Ports { port := &service.Spec.Ports[i] if port.Name != "" && port.Protocol != "" { srvValue := kd.generateSRVRecordValue(service, int(port.Port)) l := []string{"_" + strings.ToLower(string(port.Protocol)), "_" + port.Name} glog.V(2).Infof("Added SRV record %+v", srvValue) subCache.SetEntry(recordLabel, srvValue, kd.fqdn(service, append(l, recordLabel)...), l...) } } subCachePath := append(kd.domainPath, serviceSubdomain, service.Namespace) host := getServiceFQDN(kd.domain, service) reverseRecord, _ := util.GetSkyMsg(host, 0) kd.cacheLock.Lock() defer kd.cacheLock.Unlock() kd.cache.SetSubCache(service.Name, subCache, subCachePath...) kd.reverseRecordMap[service.Spec.ClusterIP] = reverseRecord kd.clusterIPServiceMap[service.Spec.ClusterIP] = 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 { for subIdx := range e.Subsets[idx].Addresses { address := &e.Subsets[idx].Addresses[subIdx] endpointIP := address.IP recordValue, endpointName := util.GetSkyMsg(endpointIP, 0) if hostLabel, exists := getHostname(address, podHostnames); exists { endpointName = hostLabel } subCache.SetEntry(endpointName, recordValue, kd.fqdn(svc, endpointName)) for portIdx := range e.Subsets[idx].Ports { endpointPort := &e.Subsets[idx].Ports[portIdx] if endpointPort.Name != "" && endpointPort.Protocol != "" { srvValue := kd.generateSRVRecordValue(svc, int(endpointPort.Port), endpointName) glog.V(2).Infof("Added SRV record %+v", srvValue) l := []string{"_" + strings.ToLower(string(endpointPort.Protocol)), "_" + endpointPort.Name} subCache.SetEntry(endpointName, srvValue, kd.fqdn(svc, append(l, endpointName)...), l...) } } } } subCachePath := append(kd.domainPath, serviceSubdomain, svc.Namespace) kd.cacheLock.Lock() defer kd.cacheLock.Unlock() kd.cache.SetSubCache(svc.Name, subCache, subCachePath...) return 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 { host = cNameLabel + "." + host } recordValue, _ := util.GetSkyMsg(host, portNumber) return recordValue }
// Generates skydns records for an ExternalName service. func (kd *KubeDNS) newExternalNameService(service *kapi.Service) { // Create a CNAME record for the service's ExternalName. // TODO: TTL? recordValue, _ := util.GetSkyMsg(service.Spec.ExternalName, 0) cachePath := append(kd.domainPath, serviceSubdomain, service.Namespace) fqdn := kd.fqdn(service) glog.V(2).Infof("newExternalNameService: storing key %s with value %v as %s under %v", service.Name, recordValue, fqdn, cachePath) kd.cacheLock.Lock() defer kd.cacheLock.Unlock() // Store the service name directly as the leaf key kd.cache.setEntry(service.Name, recordValue, fqdn, cachePath...) }
func (kd *KubeDNS) getRecordsForPath(path []string, exact bool) ([]skymsg.Service, error) { if kd.isPodRecord(path) { ip, err := kd.getPodIP(path) if err == nil { skyMsg, _ := util.GetSkyMsg(ip, 0) return []skymsg.Service{*skyMsg}, nil } return nil, err } if exact { key := path[len(path)-1] if key == "" { return []skymsg.Service{}, nil } kd.cacheLock.RLock() defer kd.cacheLock.RUnlock() if record, ok := kd.cache.GetEntry(key, path[:len(path)-1]...); ok { glog.V(3).Infof("Exact match %v for %v received from cache", record, path[:len(path)-1]) return []skymsg.Service{*(record.(*skymsg.Service))}, nil } glog.V(3).Infof("Exact match for %v not found in cache", path) return nil, etcd.Error{Code: etcd.ErrorCodeKeyNotFound} } kd.cacheLock.RLock() defer kd.cacheLock.RUnlock() records := kd.cache.GetValuesForPathWithWildcards(path...) glog.V(3).Infof("Found %d records for %v in the cache", len(records), path) retval := []skymsg.Service{} for _, val := range records { retval = append(retval, *val) } glog.V(4).Infof("getRecordsForPath retval=%+v, path=%v", retval, path) return retval, nil }