// Records looks up services in kubernetes. // If exact is true, it will lookup just // this name. This is used when find matches when completing SRV lookups // for instance. func (g Kubernetes) Records(name string, exact bool) ([]msg.Service, error) { var ( serviceName string namespace string typeName string ) log.Printf("[debug] enter Records('%v', '%v')\n", name, exact) zone, serviceSegments := g.getZoneForName(name) // TODO: Implementation above globbed together segments for the serviceName if // multiple segments remained. Determine how to do similar globbing using // the template-based implementation. namespace = g.NameTemplate.GetNamespaceFromSegmentArray(serviceSegments) serviceName = g.NameTemplate.GetServiceFromSegmentArray(serviceSegments) typeName = g.NameTemplate.GetTypeFromSegmentArray(serviceSegments) if namespace == "" { err := errors.New("Parsing query string did not produce a namespace value. Assuming wildcard namespace.") log.Printf("[WARN] %v\n", err) namespace = util.WildcardStar } if serviceName == "" { err := errors.New("Parsing query string did not produce a serviceName value. Assuming wildcard serviceName.") log.Printf("[WARN] %v\n", err) serviceName = util.WildcardStar } log.Printf("[debug] exact: %v\n", exact) log.Printf("[debug] zone: %v\n", zone) log.Printf("[debug] servicename: %v\n", serviceName) log.Printf("[debug] namespace: %v\n", namespace) log.Printf("[debug] typeName: %v\n", typeName) log.Printf("[debug] APIconn: %v\n", g.APIConn) nsWildcard := util.SymbolContainsWildcard(namespace) serviceWildcard := util.SymbolContainsWildcard(serviceName) // Abort if the namespace does not contain a wildcard, and namespace is not published per CoreFile // Case where namespace contains a wildcard is handled in Get(...) method. if (!nsWildcard) && (g.Namespaces != nil && !util.StringInSlice(namespace, *g.Namespaces)) { log.Printf("[debug] Namespace '%v' is not published by Corefile\n", namespace) return nil, nil } k8sItems, err := g.Get(namespace, nsWildcard, serviceName, serviceWildcard) log.Printf("[debug] k8s items: %v\n", k8sItems) if err != nil { log.Printf("[ERROR] Got error while looking up ServiceItems. Error is: %v\n", err) return nil, err } if k8sItems == nil { // Did not find item in k8s return nil, nil } records := g.getRecordsForServiceItems(k8sItems, nametemplate.NameValues{TypeName: typeName, ServiceName: serviceName, Namespace: namespace, Zone: zone}) return records, nil }
func (t *NameTemplate) GetTypeFromSegmentArray(segments []string) string { typeSegment := t.GetSymbolFromSegmentArray("type", segments) // Limit type to known types symbols if util.StringInSlice(typeSegment, types) { return "" } return typeSegment }
// Get performs the call to the Kubernetes http API. func (g Kubernetes) Get(namespace string, nsWildcard bool, servicename string, serviceWildcard bool) ([]k8sc.ServiceItem, error) { serviceList, err := g.APIConn.GetServiceList() if err != nil { log.Printf("[ERROR] Getting service list produced error: %v", err) return nil, err } var resultItems []k8sc.ServiceItem for _, item := range serviceList.Items { if symbolMatches(namespace, item.Metadata.Namespace, nsWildcard) && symbolMatches(servicename, item.Metadata.Name, serviceWildcard) { // If namespace has a wildcard, filter results against Corefile namespace list. // (Namespaces without a wildcard were filtered before the call to this function.) if nsWildcard && (g.Namespaces != nil && !util.StringInSlice(item.Metadata.Namespace, *g.Namespaces)) { log.Printf("[debug] Namespace '%v' is not published by Corefile\n", item.Metadata.Namespace) continue } resultItems = append(resultItems, item) } } return resultItems, nil }