From d04abdf422ae4e472b386ebbef466977eace968a Mon Sep 17 00:00:00 2001 From: Michael Richmond Date: Mon, 6 Jun 2016 12:49:53 -0700 Subject: BK8s datasource middleware -- PoC for A records (#153) * Laying down kubernetes middleware foundation * Duplicated a bunch of code form etcd middleware * Duplicated code hacked to compile and load as a separate middleware * Adding verbose build option to Makefile * Removing stubzone and tls support tls and stubzone support was carried over from base etcd middleware code. Removing to simplify the kube middleware implementation. (For now.) * Adding conf directory for sample conf files * Removing stubzone support from query handler * Remove upstream and proxy from k8s corefile. Not sure that upstream or proxy makes sense for a k8s backed zone. * Comment out use of singleflight serialization * Removing parsing support for "upstream" directive from k8s * Removing upstream directive parsing code * Removing CNAME and TXT lookup implementation * Create README.md Brain-dump of DNS record name assembly and open work items. * Adding notes about wildcard handling * Adding basic k8s API client * Fleshing out methods on k8s connector * Remove PathPrefix from middleware init * Removing incorrect plural * Adding brute-force k8s service lookup functions * Initializing k8s API connector during startup * Hacking around to call k8s connector * Parsing incoming domain name into serviceName and namespace * Improving and simplifying k8s zone matching and label segmentation * Removing unused functions carried over from etcd middleware * Adding basic return of k8s data to DNS client * updated debugging println statements to flag with "[debug]" * removed code in kubernetes.go::Records that was a hold-over from etcd middleware. * Removed some random exploratory hacking. * Minior README.md updates * Updating with demo instructions * Updating README.md with CoreFile and removing completed TODO items * Updating conf file and README to reflect DNS response cache works * Disabling DNS response caching * Adding debug statement on entry to Records() * Changing port number in exampes to port 53. * Misc style and clarity changes * Removing empty function definitions * Adding comment to track future cleanup * Refactoring README to follow style of other middleware * Exposing dataobject field (typo) --- core/setup/kubernetes.go | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 core/setup/kubernetes.go (limited to 'core/setup/kubernetes.go') diff --git a/core/setup/kubernetes.go b/core/setup/kubernetes.go new file mode 100644 index 000000000..8f21286f5 --- /dev/null +++ b/core/setup/kubernetes.go @@ -0,0 +1,93 @@ +package setup + +import ( +// "crypto/tls" +// "crypto/x509" + "fmt" +// "io/ioutil" +// "net" +// "net/http" +// "time" + + "github.com/miekg/coredns/middleware" + "github.com/miekg/coredns/middleware/kubernetes" + k8sc "github.com/miekg/coredns/middleware/kubernetes/k8sclient" + "github.com/miekg/coredns/middleware/proxy" +// "github.com/miekg/coredns/middleware/singleflight" + + "golang.org/x/net/context" +) + +const defaultK8sEndpoint = "http://localhost:8080" + +// Kubernetes sets up the kubernetes middleware. +func Kubernetes(c *Controller) (middleware.Middleware, error) { + fmt.Println("controller %v", c) + // TODO: Determine if subzone support required + + kubernetes, err := kubernetesParse(c) + + if err != nil { + return nil, err + } + + return func(next middleware.Handler) middleware.Handler { + kubernetes.Next = next + return kubernetes + }, nil +} + +func kubernetesParse(c *Controller) (kubernetes.Kubernetes, error) { + + /* + * TODO: Remove unused state and simplify. + * Inflight and Ctx might not be needed. Leaving in place until + * we take a pass at API caching and optimizing connector to the + * k8s API. Single flight (or limited upper-bound) for inflight + * API calls may be desirable. + */ + + k8s := kubernetes.Kubernetes{ + Proxy: proxy.New([]string{}), + Ctx: context.Background(), +// Inflight: &singleflight.Group{}, + APIConn: nil, + } + var ( + endpoints = []string{defaultK8sEndpoint} + ) + for c.Next() { + if c.Val() == "kubernetes" { + k8s.Zones = c.RemainingArgs() + if len(k8s.Zones) == 0 { + k8s.Zones = c.ServerBlockHosts + } + middleware.Zones(k8s.Zones).FullyQualify() + if c.NextBlock() { + // TODO(miek): 2 switches? + switch c.Val() { + case "endpoint": + args := c.RemainingArgs() + if len(args) == 0 { + return kubernetes.Kubernetes{}, c.ArgErr() + } + endpoints = args + k8s.APIConn = k8sc.NewK8sConnector(endpoints[0]) + } + for c.Next() { + switch c.Val() { + case "endpoint": + args := c.RemainingArgs() + if len(args) == 0 { + return kubernetes.Kubernetes{}, c.ArgErr() + } + endpoints = args + } + } + } + return k8s, nil + } + fmt.Println("endpoints='%v'", endpoints) + } + return kubernetes.Kubernetes{}, nil +} -- cgit v1.2.3