k8s middleware cleanup, testcases, basic SRV (#181)
* Removing unnecessary gitignore pattern * Updating Makefile to run unittests for subpackages * Adding Corefile validation to ignore overlapping zones * Fixing SRV query handling * Updating README.md now that SRV works * Fixing debug message, adding code comment * Clarifying implementation of zone normalization * "Overlapping zones" is ill-defined. Reimplemented zone overlap/subzone checking to contain these functions in k8s middleware and provide better code comments explaining the normalization. * Separate build verbosity from test verbosity * Cleaning up comments to match repo code style * Merging warning messages into single message * Moving function docs to before function declaration * Adding test cases for k8sclient connector * Tests cover connector create and setting base url * Fixed bugs in connector create and setting base url functions * Updaing README to group and order development work * Priority focused on achieving functional parity with SkyDNS. * Adding work items to README and cleaning up formatting * More README format cleaning * List formating * Refactoring k8s API call to allow dependency injection * Add test cases for data parsing from k8s into dataobject structures * URL is dependency-injected to allow replacement with a mock http server during test execution * Adding more data validation for JSON parsing tests * Adding test case for GetResourceList() * Adding notes about SkyDNS embedded IP and port record names * Marked test case implemented. * Fixing formatting for example command. * Fixing formatting * Adding notes about Docker image building. * Adding SkyDNS work item * Updating TODO list * Adding name template to Corefile to specify how k8s record names are assembled * Adding template support for multi-segment zones * Updating example CoreFile for k8s with template comment * Misc whitespace cleanup * Adding SkyDNS naming notes * Adding namespace filtering to CoreFile config * Updating example k8sCoreFile to specify namespaces * Removing unused codepath * Adding check for valid namespace * More README TODO restructuring to focus effort * Adding template validation while parsing CoreFile * Record name template is considered invalid if it contains a symbol of the form ${bar} where the symbol "${bar}" is not an accepted template symbol. * Refactoring generation of answer records * Parse typeName out of query string * Refactor answer record creation as operation over list of ServiceItems * Moving k8s API caching into SkyDNS equivalency segment * Adding function to assemble record names from template * Warning: This commit may be broken. Syncing to get laptop code over to dev machine. * More todo notes * Adding comment describing sample test data. * Update k8sCorefile * Adding comment * Adding filtering support for kubernetes "type" * Required refactoring to support reuse of the StringInSlice function. * Cleaning up formatting * Adding note about SkyDNS supporting word "any". * baseUrl -> baseURL * Also removed debug statement from core/setup/kubernetes.go * Fixing test breaking from Url -> URL naming changes * Changing record name template language ${...} -> {...} * Fix formatting with go fmt * Updating all k8sclient data getters to return error value * Adding error message to k8sclient data accessors * Cleaning up setup for kubernetes * Removed verbose nils in initial k8s middleware instance * Set reasonable defaults if CoreFile has no parameters in the kubernetes block. (k8s endpoint, and name template) * Formatting cleanup -- go fmt
This commit is contained in:
parent
558c34a23e
commit
289f53d386
19 changed files with 1610 additions and 304 deletions
|
@ -2,111 +2,148 @@
|
|||
package kubernetes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
"github.com/miekg/coredns/middleware/kubernetes/msg"
|
||||
k8sc "github.com/miekg/coredns/middleware/kubernetes/k8sclient"
|
||||
"github.com/miekg/coredns/middleware/kubernetes/msg"
|
||||
"github.com/miekg/coredns/middleware/kubernetes/nametemplate"
|
||||
"github.com/miekg/coredns/middleware/kubernetes/util"
|
||||
"github.com/miekg/coredns/middleware/proxy"
|
||||
// "github.com/miekg/coredns/middleware/singleflight"
|
||||
// "github.com/miekg/coredns/middleware/singleflight"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Kubernetes struct {
|
||||
Next middleware.Handler
|
||||
Zones []string
|
||||
Proxy proxy.Proxy // Proxy for looking up names during the resolution process
|
||||
Ctx context.Context
|
||||
// Inflight *singleflight.Group
|
||||
APIConn *k8sc.K8sConnector
|
||||
Next middleware.Handler
|
||||
Zones []string
|
||||
Proxy proxy.Proxy // Proxy for looking up names during the resolution process
|
||||
Ctx context.Context
|
||||
// Inflight *singleflight.Group
|
||||
APIConn *k8sc.K8sConnector
|
||||
NameTemplate *nametemplate.NameTemplate
|
||||
Namespaces *[]string
|
||||
}
|
||||
|
||||
|
||||
// getZoneForName returns the zone string that matches the name and a
|
||||
// list of the DNS labels from name that are within the zone.
|
||||
// For example, if "coredns.local" is a zone configured for the
|
||||
// Kubernetes middleware, then getZoneForName("a.b.coredns.local")
|
||||
// will return ("coredns.local", ["a", "b"]).
|
||||
func (g Kubernetes) getZoneForName(name string) (string, []string) {
|
||||
/*
|
||||
* getZoneForName returns the zone string that matches the name and a
|
||||
* list of the DNS labels from name that are within the zone.
|
||||
* For example, if "coredns.local" is a zone configured for the
|
||||
* Kubernetes middleware, then getZoneForName("a.b.coredns.local")
|
||||
* will return ("coredns.local", ["a", "b"]).
|
||||
*/
|
||||
var zone string
|
||||
var serviceSegments []string
|
||||
var zone string
|
||||
var serviceSegments []string
|
||||
|
||||
for _, z := range g.Zones {
|
||||
if dns.IsSubDomain(z, name) {
|
||||
zone = z
|
||||
|
||||
serviceSegments = dns.SplitDomainName(name)
|
||||
serviceSegments = serviceSegments[:len(serviceSegments) - dns.CountLabel(zone)]
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, z := range g.Zones {
|
||||
if dns.IsSubDomain(z, name) {
|
||||
zone = z
|
||||
|
||||
return zone, serviceSegments
|
||||
}
|
||||
serviceSegments = dns.SplitDomainName(name)
|
||||
serviceSegments = serviceSegments[:len(serviceSegments)-dns.CountLabel(zone)]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return zone, serviceSegments
|
||||
}
|
||||
|
||||
// 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
|
||||
)
|
||||
|
||||
fmt.Println("enter Records('", name, "', ", exact, ")")
|
||||
fmt.Println("enter Records('", name, "', ", exact, ")")
|
||||
zone, serviceSegments := g.getZoneForName(name)
|
||||
|
||||
zone, serviceSegments := g.getZoneForName(name)
|
||||
/*
|
||||
// For initial implementation, assume namespace is first serviceSegment
|
||||
// and service name is remaining segments.
|
||||
serviceSegLen := len(serviceSegments)
|
||||
if serviceSegLen >= 2 {
|
||||
namespace = serviceSegments[serviceSegLen-1]
|
||||
serviceName = strings.Join(serviceSegments[:serviceSegLen-1], ".")
|
||||
}
|
||||
// else we are looking up the zone. So handle the NS, SOA records etc.
|
||||
*/
|
||||
|
||||
var serviceName string
|
||||
var namespace string
|
||||
// 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)
|
||||
|
||||
// For initial implementation, assume namespace is first serviceSegment
|
||||
// and service name is remaining segments.
|
||||
serviceSegLen := len(serviceSegments)
|
||||
if serviceSegLen >= 2 {
|
||||
namespace = serviceSegments[serviceSegLen-1]
|
||||
serviceName = strings.Join(serviceSegments[:serviceSegLen-1], ".")
|
||||
}
|
||||
// else we are looking up the zone. So handle the NS, SOA records etc.
|
||||
fmt.Println("[debug] exact: ", exact)
|
||||
fmt.Println("[debug] zone: ", zone)
|
||||
fmt.Println("[debug] servicename: ", serviceName)
|
||||
fmt.Println("[debug] namespace: ", namespace)
|
||||
fmt.Println("[debug] typeName: ", typeName)
|
||||
fmt.Println("[debug] APIconn: ", g.APIConn)
|
||||
|
||||
fmt.Println("[debug] zone: ", zone)
|
||||
fmt.Println("[debug] servicename: ", serviceName)
|
||||
fmt.Println("[debug] namespace: ", namespace)
|
||||
fmt.Println("[debug] APIconn: ", g.APIConn)
|
||||
// TODO: Implement wildcard support to allow blank namespace value
|
||||
if namespace == "" {
|
||||
err := errors.New("Parsing query string did not produce a namespace value")
|
||||
fmt.Printf("[ERROR] %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
k8sItem := g.APIConn.GetServiceItemInNamespace(namespace, serviceName)
|
||||
fmt.Println("[debug] k8s item:", k8sItem)
|
||||
// Abort if the namespace is not published per CoreFile
|
||||
if g.Namespaces != nil && !util.StringInSlice(namespace, *g.Namespaces) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
switch {
|
||||
case exact && k8sItem == nil:
|
||||
fmt.Println("here2")
|
||||
return nil, nil
|
||||
}
|
||||
k8sItems, err := g.APIConn.GetServiceItemsInNamespace(namespace, serviceName)
|
||||
fmt.Println("[debug] k8s items:", k8sItems)
|
||||
|
||||
if k8sItem == nil {
|
||||
// Did not find item in k8s
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
fmt.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
|
||||
}
|
||||
|
||||
fmt.Println("[debug] clusterIP:", k8sItem.Spec.ClusterIP)
|
||||
// test := g.NameTemplate.GetRecordNameFromNameValues(nametemplate.NameValues{ServiceName: serviceName, TypeName: typeName, Namespace: namespace, Zone: zone})
|
||||
// fmt.Printf("[debug] got recordname %v\n", test)
|
||||
|
||||
for _, p := range k8sItem.Spec.Ports {
|
||||
fmt.Println("[debug] host:", name)
|
||||
fmt.Println("[debug] port:", p.Port)
|
||||
}
|
||||
records := g.getRecordsForServiceItems(k8sItems, name)
|
||||
|
||||
clusterIP := k8sItem.Spec.ClusterIP
|
||||
var records []msg.Service
|
||||
for _, p := range k8sItem.Spec.Ports{
|
||||
s := msg.Service{Host: clusterIP, Port: p.Port}
|
||||
records = append(records, s)
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
return records, nil
|
||||
// TODO: assemble name from parts found in k8s data based on name template rather than reusing query string
|
||||
func (g Kubernetes) getRecordsForServiceItems(serviceItems []*k8sc.ServiceItem, name string) []msg.Service {
|
||||
var records []msg.Service
|
||||
|
||||
for _, item := range serviceItems {
|
||||
fmt.Println("[debug] clusterIP:", item.Spec.ClusterIP)
|
||||
for _, p := range item.Spec.Ports {
|
||||
fmt.Println("[debug] port:", p.Port)
|
||||
}
|
||||
|
||||
clusterIP := item.Spec.ClusterIP
|
||||
|
||||
s := msg.Service{Host: name}
|
||||
records = append(records, s)
|
||||
for _, p := range item.Spec.Ports {
|
||||
s := msg.Service{Host: clusterIP, Port: p.Port}
|
||||
records = append(records, s)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("[debug] records from getRecordsForServiceItems(): %v\n", records)
|
||||
return records
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -121,13 +158,13 @@ func (g Kubernetes) Get(path string, recursive bool) (bool, error) {
|
|||
*/
|
||||
|
||||
func (g Kubernetes) splitDNSName(name string) []string {
|
||||
l := dns.SplitDomainName(name)
|
||||
l := dns.SplitDomainName(name)
|
||||
|
||||
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
|
||||
l[i], l[j] = l[j], l[i]
|
||||
}
|
||||
for i, j := 0, len(l)-1; i < j; i, j = i+1, j-1 {
|
||||
l[i], l[j] = l[j], l[i]
|
||||
}
|
||||
|
||||
return l
|
||||
return l
|
||||
}
|
||||
|
||||
// skydns/local/skydns/east/staging/web
|
||||
|
@ -215,9 +252,9 @@ func isKubernetesNameError(err error) bool {
|
|||
}
|
||||
|
||||
const (
|
||||
priority = 10 // default priority when nothing is set
|
||||
ttl = 300 // default ttl when nothing is set
|
||||
minTtl = 60
|
||||
hostmaster = "hostmaster"
|
||||
priority = 10 // default priority when nothing is set
|
||||
ttl = 300 // default ttl when nothing is set
|
||||
minTtl = 60
|
||||
hostmaster = "hostmaster"
|
||||
k8sTimeout = 5 * time.Second
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue