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)
This commit is contained in:
parent
446eaa957d
commit
d04abdf422
14 changed files with 1560 additions and 1 deletions
110
middleware/kubernetes/k8sclient/dataobjects.go
Normal file
110
middleware/kubernetes/k8sclient/dataobjects.go
Normal file
|
@ -0,0 +1,110 @@
|
|||
package k8sclient
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
func getJson(url string, target interface{}) error {
|
||||
r, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
return json.NewDecoder(r.Body).Decode(target)
|
||||
}
|
||||
|
||||
|
||||
// Kubernetes Resource List
|
||||
type ResourceList struct {
|
||||
Kind string `json:"kind"`
|
||||
GroupVersion string `json:"groupVersion"`
|
||||
Resources []resource `json:"resources"`
|
||||
}
|
||||
|
||||
type resource struct {
|
||||
Name string `json:"name"`
|
||||
Namespaced bool `json:"namespaced"`
|
||||
Kind string `json:"kind"`
|
||||
}
|
||||
|
||||
|
||||
// Kubernetes NamespaceList
|
||||
type NamespaceList struct {
|
||||
Kind string `json:"kind"`
|
||||
APIVersion string `json:"apiVersion"`
|
||||
Metadata apiListMetadata `json:"metadata"`
|
||||
Items []nsItems `json:"items"`
|
||||
}
|
||||
|
||||
type apiListMetadata struct {
|
||||
SelfLink string `json:"selfLink"`
|
||||
resourceVersion string `json:"resourceVersion"`
|
||||
}
|
||||
|
||||
type nsItems struct {
|
||||
Metadata nsMetadata `json:"metadata"`
|
||||
Spec nsSpec `json:"spec"`
|
||||
Status nsStatus `json:"status"`
|
||||
}
|
||||
|
||||
type nsMetadata struct {
|
||||
Name string `json:"name"`
|
||||
SelfLink string `json:"selfLink"`
|
||||
Uid string `json:"uid"`
|
||||
ResourceVersion string `json:"resourceVersion"`
|
||||
CreationTimestamp string `json:"creationTimestamp"`
|
||||
}
|
||||
|
||||
type nsSpec struct {
|
||||
Finalizers []string `json:"finalizers"`
|
||||
}
|
||||
|
||||
type nsStatus struct {
|
||||
Phase string `json:"phase"`
|
||||
}
|
||||
|
||||
|
||||
// Kubernetes ServiceList
|
||||
type ServiceList struct {
|
||||
Kind string `json:"kind"`
|
||||
APIVersion string `json:"apiVersion"`
|
||||
Metadata apiListMetadata `json:"metadata"`
|
||||
Items []ServiceItem `json:"items"`
|
||||
}
|
||||
|
||||
type ServiceItem struct {
|
||||
Metadata serviceMetadata `json:"metadata"`
|
||||
Spec serviceSpec `json:"spec"`
|
||||
// Status serviceStatus `json:"status"`
|
||||
}
|
||||
|
||||
type serviceMetadata struct {
|
||||
Name string `json:"name"`
|
||||
Namespace string `json:"namespace"`
|
||||
SelfLink string `json:"selfLink"`
|
||||
Uid string `json:"uid"`
|
||||
ResourceVersion string `json:"resourceVersion"`
|
||||
CreationTimestamp string `json:"creationTimestamp"`
|
||||
// labels
|
||||
}
|
||||
|
||||
type serviceSpec struct {
|
||||
Ports []servicePort `json:"ports"`
|
||||
ClusterIP string `json:"clusterIP"`
|
||||
Type string `json:"type"`
|
||||
SessionAffinity string `json:"sessionAffinity"`
|
||||
}
|
||||
|
||||
type servicePort struct {
|
||||
Name string `json:"name"`
|
||||
Protocol string `json:"protocol"`
|
||||
Port int `json:"port"`
|
||||
TargetPort int `json:"targetPort"`
|
||||
}
|
||||
|
||||
type serviceStatus struct {
|
||||
LoadBalancer string `json:"loadBalancer"`
|
||||
}
|
117
middleware/kubernetes/k8sclient/k8sclient.go
Normal file
117
middleware/kubernetes/k8sclient/k8sclient.go
Normal file
|
@ -0,0 +1,117 @@
|
|||
package k8sclient
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// API strings
|
||||
const (
|
||||
apiBase = "/api/v1"
|
||||
apiNamespaces = "/namespaces"
|
||||
apiServices = "/services"
|
||||
)
|
||||
|
||||
// Defaults
|
||||
const (
|
||||
defaultBaseUrl = "http://localhost:8080"
|
||||
)
|
||||
|
||||
|
||||
type K8sConnector struct {
|
||||
baseUrl string
|
||||
}
|
||||
|
||||
func (c *K8sConnector) SetBaseUrl(u string) error {
|
||||
validUrl, error := url.Parse(u)
|
||||
|
||||
if error != nil {
|
||||
return error
|
||||
}
|
||||
c.baseUrl = validUrl.String()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *K8sConnector) GetBaseUrl() string {
|
||||
return c.baseUrl
|
||||
}
|
||||
|
||||
|
||||
func (c *K8sConnector) GetResourceList() *ResourceList {
|
||||
resources := new(ResourceList)
|
||||
|
||||
error := getJson((c.baseUrl + apiBase), resources)
|
||||
if error != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return resources
|
||||
}
|
||||
|
||||
|
||||
func (c *K8sConnector) GetNamespaceList() *NamespaceList {
|
||||
namespaces := new(NamespaceList)
|
||||
|
||||
error := getJson((c.baseUrl + apiBase + apiNamespaces), namespaces)
|
||||
if error != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return namespaces
|
||||
}
|
||||
|
||||
|
||||
func (c *K8sConnector) GetServiceList() *ServiceList {
|
||||
services := new(ServiceList)
|
||||
|
||||
error := getJson((c.baseUrl + apiBase + apiServices), services)
|
||||
if error != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return services
|
||||
}
|
||||
|
||||
|
||||
func (c *K8sConnector) GetServicesByNamespace() map[string][]ServiceItem {
|
||||
// GetServicesByNamespace returns a map of namespacename :: [ kubernetesServiceItem ]
|
||||
|
||||
items := make(map[string][]ServiceItem)
|
||||
|
||||
k8sServiceList := c.GetServiceList()
|
||||
k8sItemList := k8sServiceList.Items
|
||||
|
||||
for _, i := range k8sItemList {
|
||||
namespace := i.Metadata.Namespace
|
||||
items[namespace] = append(items[namespace], i)
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
|
||||
func (c *K8sConnector) GetServiceItemInNamespace(namespace string, servicename string) *ServiceItem {
|
||||
// GetServiceItemInNamespace returns the ServiceItem that matches servicename in the namespace
|
||||
|
||||
itemMap := c.GetServicesByNamespace()
|
||||
|
||||
// TODO: Handle case where namesapce == nil
|
||||
|
||||
for _, x := range itemMap[namespace] {
|
||||
if x.Metadata.Name == servicename {
|
||||
return &x
|
||||
}
|
||||
}
|
||||
|
||||
// No matching item found in namespace
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func NewK8sConnector(baseurl string) *K8sConnector {
|
||||
k := new(K8sConnector)
|
||||
k.SetBaseUrl(baseurl)
|
||||
|
||||
return k
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue