Commit 5e8b22fd authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #36013 from bowei/kubedns-logging

Automatic merge from submit-queue Kubedns logging fixes https://github.com/kubernetes/kubernetes/issues/29053 may resolve https://github.com/kubernetes/kubernetes/issues/29054, but depends on what the specific ask is
parents 4b1e36f9 d9557d4e
...@@ -20,8 +20,10 @@ go_binary( ...@@ -20,8 +20,10 @@ go_binary(
"//pkg/client/metrics/prometheus:go_default_library", "//pkg/client/metrics/prometheus:go_default_library",
"//pkg/util/flag:go_default_library", "//pkg/util/flag:go_default_library",
"//pkg/util/logs:go_default_library", "//pkg/util/logs:go_default_library",
"//pkg/version:go_default_library",
"//pkg/version/prometheus:go_default_library", "//pkg/version/prometheus:go_default_library",
"//pkg/version/verflag:go_default_library", "//pkg/version/verflag:go_default_library",
"//vendor:github.com/golang/glog",
"//vendor:github.com/spf13/pflag", "//vendor:github.com/spf13/pflag",
], ],
) )
...@@ -21,7 +21,6 @@ go_library( ...@@ -21,7 +21,6 @@ go_library(
"//pkg/client/restclient:go_default_library", "//pkg/client/restclient:go_default_library",
"//pkg/client/unversioned/clientcmd:go_default_library", "//pkg/client/unversioned/clientcmd:go_default_library",
"//pkg/dns:go_default_library", "//pkg/dns:go_default_library",
"//pkg/version:go_default_library",
"//vendor:github.com/golang/glog", "//vendor:github.com/golang/glog",
"//vendor:github.com/skynetservices/skydns/metrics", "//vendor:github.com/skynetservices/skydns/metrics",
"//vendor:github.com/skynetservices/skydns/server", "//vendor:github.com/skynetservices/skydns/server",
......
...@@ -34,7 +34,6 @@ import ( ...@@ -34,7 +34,6 @@ import (
"k8s.io/kubernetes/pkg/client/restclient" "k8s.io/kubernetes/pkg/client/restclient"
kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd" kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
kdns "k8s.io/kubernetes/pkg/dns" kdns "k8s.io/kubernetes/pkg/dns"
"k8s.io/kubernetes/pkg/version"
) )
type KubeDNSServer struct { type KubeDNSServer struct {
...@@ -47,9 +46,7 @@ type KubeDNSServer struct { ...@@ -47,9 +46,7 @@ type KubeDNSServer struct {
} }
func NewKubeDNSServerDefault(config *options.KubeDNSConfig) *KubeDNSServer { func NewKubeDNSServerDefault(config *options.KubeDNSConfig) *KubeDNSServer {
ks := KubeDNSServer{ ks := KubeDNSServer{domain: config.ClusterDomain}
domain: config.ClusterDomain,
}
kubeClient, err := newKubeClient(config) kubeClient, err := newKubeClient(config)
if err != nil { if err != nil {
...@@ -93,28 +90,32 @@ func newKubeClient(dnsConfig *options.KubeDNSConfig) (clientset.Interface, error ...@@ -93,28 +90,32 @@ func newKubeClient(dnsConfig *options.KubeDNSConfig) (clientset.Interface, error
} }
} }
glog.Infof("Using %s for kubernetes master, kubernetes API: %v", config.Host, config.GroupVersion) glog.V(0).Infof("Using %v for kubernetes master, kubernetes API: %v",
config.Host, config.GroupVersion)
return clientset.NewForConfig(config) return clientset.NewForConfig(config)
} }
func (server *KubeDNSServer) Run() { func (server *KubeDNSServer) Run() {
glog.Infof("%+v", version.Get())
pflag.VisitAll(func(flag *pflag.Flag) { pflag.VisitAll(func(flag *pflag.Flag) {
glog.Infof("FLAG: --%s=%q", flag.Name, flag.Value) glog.V(0).Infof("FLAG: --%s=%q", flag.Name, flag.Value)
}) })
setupSignalHandlers() setupSignalHandlers()
server.startSkyDNSServer() server.startSkyDNSServer()
server.kd.Start() server.kd.Start()
server.setupHealthzHandlers() server.setupHandlers()
glog.Infof("Setting up Healthz Handler(/readiness, /cache) on port :%d", server.healthzPort)
glog.V(0).Infof("Status HTTP port %v", server.healthzPort)
glog.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", server.healthzPort), nil)) glog.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", server.healthzPort), nil))
} }
// setupHealthzHandlers sets up a readiness and liveness endpoint for kube2sky. // setupHealthzHandlers sets up a readiness and liveness endpoint for kube2sky.
func (server *KubeDNSServer) setupHealthzHandlers() { func (server *KubeDNSServer) setupHandlers() {
glog.V(0).Infof("Setting up Healthz Handler (/readiness)")
http.HandleFunc("/readiness", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/readiness", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "ok\n") fmt.Fprintf(w, "ok\n")
}) })
glog.V(0).Infof("Setting up cache handler (/cache)")
http.HandleFunc("/cache", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/cache", func(w http.ResponseWriter, req *http.Request) {
serializedJSON, err := server.kd.GetCacheAsJSON() serializedJSON, err := server.kd.GetCacheAsJSON()
if err == nil { if err == nil {
...@@ -126,25 +127,32 @@ func (server *KubeDNSServer) setupHealthzHandlers() { ...@@ -126,25 +127,32 @@ func (server *KubeDNSServer) setupHealthzHandlers() {
}) })
} }
// setupSignalHandlers runs a goroutine that waits on SIGINT or SIGTERM and logs it // setupSignalHandlers installs signal handler to ignore SIGINT and
// program will be terminated by SIGKILL when grace period ends. // SIGTERM. This daemon will be killed by SIGKILL after the grace
// period to allow for some manner of graceful shutdown.
func setupSignalHandlers() { func setupSignalHandlers() {
sigChan := make(chan os.Signal) sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
go func() { go func() {
glog.Infof("Received signal: %s, will exit when the grace period ends", <-sigChan) glog.V(0).Infof("Ignoring signal %v (can only be terminated by SIGKILL)", <-sigChan)
}() }()
} }
func (d *KubeDNSServer) startSkyDNSServer() { func (d *KubeDNSServer) startSkyDNSServer() {
glog.Infof("Starting SkyDNS server. Listening on %s:%d", d.dnsBindAddress, d.dnsPort) glog.V(0).Infof("Starting SkyDNS server (%v:%v)", d.dnsBindAddress, d.dnsPort)
skydnsConfig := &server.Config{Domain: d.domain, DnsAddr: fmt.Sprintf("%s:%d", d.dnsBindAddress, d.dnsPort)} skydnsConfig := &server.Config{
Domain: d.domain,
DnsAddr: fmt.Sprintf("%s:%d", d.dnsBindAddress, d.dnsPort),
}
server.SetDefaults(skydnsConfig) server.SetDefaults(skydnsConfig)
s := server.New(d.kd, skydnsConfig) s := server.New(d.kd, skydnsConfig)
if err := metrics.Metrics(); err != nil { if err := metrics.Metrics(); err != nil {
glog.Fatalf("skydns: %s", err) glog.Fatalf("Skydns metrics error: %s", err)
} else if metrics.Port != "" {
glog.V(0).Infof("Skydns metrics enabled (%v:%v)", metrics.Path, metrics.Port)
} else {
glog.V(0).Infof("Skydns metrics not enabled")
} }
glog.Infof("skydns: metrics enabled on : %s:%s", metrics.Path, metrics.Port)
go s.Run() go s.Run()
} }
...@@ -17,12 +17,14 @@ limitations under the License. ...@@ -17,12 +17,14 @@ limitations under the License.
package main package main
import ( import (
"github.com/golang/glog"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"k8s.io/kubernetes/cmd/kube-dns/app" "k8s.io/kubernetes/cmd/kube-dns/app"
"k8s.io/kubernetes/cmd/kube-dns/app/options" "k8s.io/kubernetes/cmd/kube-dns/app/options"
_ "k8s.io/kubernetes/pkg/client/metrics/prometheus" // for client metric registration _ "k8s.io/kubernetes/pkg/client/metrics/prometheus" // for client metric registration
"k8s.io/kubernetes/pkg/util/flag" "k8s.io/kubernetes/pkg/util/flag"
"k8s.io/kubernetes/pkg/util/logs" "k8s.io/kubernetes/pkg/util/logs"
"k8s.io/kubernetes/pkg/version"
_ "k8s.io/kubernetes/pkg/version/prometheus" // for version metric registration _ "k8s.io/kubernetes/pkg/version/prometheus" // for version metric registration
"k8s.io/kubernetes/pkg/version/verflag" "k8s.io/kubernetes/pkg/version/verflag"
) )
...@@ -36,6 +38,9 @@ func main() { ...@@ -36,6 +38,9 @@ func main() {
defer logs.FlushLogs() defer logs.FlushLogs()
verflag.PrintAndExitIfRequested() verflag.PrintAndExitIfRequested()
glog.V(0).Infof("version: %+v", version.Get())
server := app.NewKubeDNSServerDefault(config) server := app.NewKubeDNSServerDefault(config)
server.Run() server.Run()
} }
...@@ -15,7 +15,6 @@ go_library( ...@@ -15,7 +15,6 @@ go_library(
srcs = [ srcs = [
"dns.go", "dns.go",
"doc.go", "doc.go",
"treecache.go",
], ],
tags = ["automanaged"], tags = ["automanaged"],
deps = [ deps = [
...@@ -24,6 +23,7 @@ go_library( ...@@ -24,6 +23,7 @@ go_library(
"//pkg/api/unversioned:go_default_library", "//pkg/api/unversioned:go_default_library",
"//pkg/client/cache:go_default_library", "//pkg/client/cache:go_default_library",
"//pkg/client/clientset_generated/internalclientset:go_default_library", "//pkg/client/clientset_generated/internalclientset:go_default_library",
"//pkg/dns/treecache:go_default_library",
"//pkg/dns/util:go_default_library", "//pkg/dns/util:go_default_library",
"//pkg/runtime:go_default_library", "//pkg/runtime:go_default_library",
"//pkg/util/validation:go_default_library", "//pkg/util/validation:go_default_library",
...@@ -47,6 +47,7 @@ go_test( ...@@ -47,6 +47,7 @@ go_test(
"//pkg/api/unversioned:go_default_library", "//pkg/api/unversioned:go_default_library",
"//pkg/client/cache:go_default_library", "//pkg/client/cache:go_default_library",
"//pkg/client/clientset_generated/internalclientset/fake:go_default_library", "//pkg/client/clientset_generated/internalclientset/fake:go_default_library",
"//pkg/dns/treecache:go_default_library",
"//pkg/dns/util:go_default_library", "//pkg/dns/util:go_default_library",
"//pkg/util/sets:go_default_library", "//pkg/util/sets:go_default_library",
"//vendor:github.com/coreos/etcd/client", "//vendor:github.com/coreos/etcd/client",
......
...@@ -35,6 +35,7 @@ import ( ...@@ -35,6 +35,7 @@ import (
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/client/cache" "k8s.io/kubernetes/pkg/client/cache"
fake "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" fake "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"k8s.io/kubernetes/pkg/dns/treecache"
"k8s.io/kubernetes/pkg/dns/util" "k8s.io/kubernetes/pkg/dns/util"
"k8s.io/kubernetes/pkg/util/sets" "k8s.io/kubernetes/pkg/util/sets"
) )
...@@ -51,7 +52,7 @@ func newKubeDNS() *KubeDNS { ...@@ -51,7 +52,7 @@ func newKubeDNS() *KubeDNS {
domain: testDomain, domain: testDomain,
endpointsStore: cache.NewStore(cache.MetaNamespaceKeyFunc), endpointsStore: cache.NewStore(cache.MetaNamespaceKeyFunc),
servicesStore: cache.NewStore(cache.MetaNamespaceKeyFunc), servicesStore: cache.NewStore(cache.MetaNamespaceKeyFunc),
cache: NewTreeCache(), cache: treecache.NewTreeCache(),
reverseRecordMap: make(map[string]*skymsg.Service), reverseRecordMap: make(map[string]*skymsg.Service),
clusterIPServiceMap: make(map[string]*kapi.Service), clusterIPServiceMap: make(map[string]*kapi.Service),
cacheLock: sync.RWMutex{}, cacheLock: sync.RWMutex{},
......
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_library",
"go_test",
"cgo_library",
)
go_library(
name = "go_default_library",
srcs = ["treecache.go"],
tags = ["automanaged"],
deps = ["//vendor:github.com/skynetservices/skydns/msg"],
)
go_test(
name = "go_default_test",
srcs = ["treecache_test.go"],
library = "go_default_library",
tags = ["automanaged"],
deps = ["//vendor:github.com/skynetservices/skydns/msg"],
)
...@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and ...@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package dns package treecache
import ( import (
"encoding/json" "encoding/json"
...@@ -23,19 +23,48 @@ import ( ...@@ -23,19 +23,48 @@ import (
skymsg "github.com/skynetservices/skydns/msg" skymsg "github.com/skynetservices/skydns/msg"
) )
type TreeCache struct { type TreeCache interface {
ChildNodes map[string]*TreeCache // GetEntry with the given key for the given path.
GetEntry(key string, path ...string) (interface{}, bool)
// Get a list of values including wildcards labels (e.g. "*").
GetValuesForPathWithWildcards(path ...string) []*skymsg.Service
// SetEntry creates the entire path if it doesn't already exist in
// the cache, then sets the given service record under the given
// key. The path this entry would have occupied in an etcd datastore
// is computed from the given fqdn and stored as the "Key" of the
// skydns service; this is only required because skydns expects the
// service record to contain a key in a specific format (presumably
// for legacy compatibility). Note that the fqnd string typically
// contains both the key and all elements in the path.
SetEntry(key string, val *skymsg.Service, fqdn string, path ...string)
// SetSubCache inserts the given subtree under the given
// path:key. Usually the key is the name of a Kubernetes Service,
// and the path maps to the cluster subdomains matching the Service.
SetSubCache(key string, subCache TreeCache, path ...string)
// DeletePath removes all entries associated with a given path.
DeletePath(path ...string) bool
// Serialize dumps a JSON representation of the cache.
Serialize() (string, error)
}
type treeCache struct {
ChildNodes map[string]*treeCache
Entries map[string]interface{} Entries map[string]interface{}
} }
func NewTreeCache() *TreeCache { func NewTreeCache() TreeCache {
return &TreeCache{ return &treeCache{
ChildNodes: make(map[string]*TreeCache), ChildNodes: make(map[string]*treeCache),
Entries: make(map[string]interface{}), Entries: make(map[string]interface{}),
} }
} }
func (cache *TreeCache) Serialize() (string, error) { func (cache *treeCache) Serialize() (string, error) {
prettyJSON, err := json.MarshalIndent(cache, "", "\t") prettyJSON, err := json.MarshalIndent(cache, "", "\t")
if err != nil { if err != nil {
return "", err return "", err
...@@ -43,14 +72,7 @@ func (cache *TreeCache) Serialize() (string, error) { ...@@ -43,14 +72,7 @@ func (cache *TreeCache) Serialize() (string, error) {
return string(prettyJSON), nil return string(prettyJSON), nil
} }
// setEntry creates the entire path if it doesn't already exist in the cache, func (cache *treeCache) SetEntry(key string, val *skymsg.Service, fqdn string, path ...string) {
// then sets the given service record under the given key. The path this entry
// would have occupied in an etcd datastore is computed from the given fqdn and
// stored as the "Key" of the skydns service; this is only required because
// skydns expects the service record to contain a key in a specific format
// (presumably for legacy compatibility). Note that the fqnd string typically
// contains both the key and all elements in the path.
func (cache *TreeCache) setEntry(key string, val *skymsg.Service, fqdn string, path ...string) {
// TODO: Consolidate setEntry and setSubCache into a single method with a // TODO: Consolidate setEntry and setSubCache into a single method with a
// type switch. // type switch.
// TODO: Instead of passing the fqdn as an argument, we can reconstruct // TODO: Instead of passing the fqdn as an argument, we can reconstruct
...@@ -70,7 +92,7 @@ func (cache *TreeCache) setEntry(key string, val *skymsg.Service, fqdn string, p ...@@ -70,7 +92,7 @@ func (cache *TreeCache) setEntry(key string, val *skymsg.Service, fqdn string, p
node.Entries[key] = val node.Entries[key] = val
} }
func (cache *TreeCache) getSubCache(path ...string) *TreeCache { func (cache *treeCache) getSubCache(path ...string) *treeCache {
childCache := cache childCache := cache
for _, subpath := range path { for _, subpath := range path {
childCache = childCache.ChildNodes[subpath] childCache = childCache.ChildNodes[subpath]
...@@ -81,15 +103,12 @@ func (cache *TreeCache) getSubCache(path ...string) *TreeCache { ...@@ -81,15 +103,12 @@ func (cache *TreeCache) getSubCache(path ...string) *TreeCache {
return childCache return childCache
} }
// setSubCache inserts the given subtree under the given path:key. Usually the func (cache *treeCache) SetSubCache(key string, subCache TreeCache, path ...string) {
// key is the name of a Kubernetes Service, and the path maps to the cluster
// subdomains matching the Service.
func (cache *TreeCache) setSubCache(key string, subCache *TreeCache, path ...string) {
node := cache.ensureChildNode(path...) node := cache.ensureChildNode(path...)
node.ChildNodes[key] = subCache node.ChildNodes[key] = subCache.(*treeCache)
} }
func (cache *TreeCache) getEntry(key string, path ...string) (interface{}, bool) { func (cache *treeCache) GetEntry(key string, path ...string) (interface{}, bool) {
childNode := cache.getSubCache(path...) childNode := cache.getSubCache(path...)
if childNode == nil { if childNode == nil {
return nil, false return nil, false
...@@ -98,11 +117,11 @@ func (cache *TreeCache) getEntry(key string, path ...string) (interface{}, bool) ...@@ -98,11 +117,11 @@ func (cache *TreeCache) getEntry(key string, path ...string) (interface{}, bool)
return val, ok return val, ok
} }
func (cache *TreeCache) getValuesForPathWithWildcards(path ...string) []*skymsg.Service { func (cache *treeCache) GetValuesForPathWithWildcards(path ...string) []*skymsg.Service {
retval := []*skymsg.Service{} retval := []*skymsg.Service{}
nodesToExplore := []*TreeCache{cache} nodesToExplore := []*treeCache{cache}
for idx, subpath := range path { for idx, subpath := range path {
nextNodesToExplore := []*TreeCache{} nextNodesToExplore := []*treeCache{}
if idx == len(path)-1 { if idx == len(path)-1 {
// if path ends on an entry, instead of a child node, add the entry // if path ends on an entry, instead of a child node, add the entry
for _, node := range nodesToExplore { for _, node := range nodesToExplore {
...@@ -150,7 +169,7 @@ func (cache *TreeCache) getValuesForPathWithWildcards(path ...string) []*skymsg. ...@@ -150,7 +169,7 @@ func (cache *TreeCache) getValuesForPathWithWildcards(path ...string) []*skymsg.
return retval return retval
} }
func (cache *TreeCache) deletePath(path ...string) bool { func (cache *treeCache) DeletePath(path ...string) bool {
if len(path) == 0 { if len(path) == 0 {
return false return false
} }
...@@ -169,19 +188,7 @@ func (cache *TreeCache) deletePath(path ...string) bool { ...@@ -169,19 +188,7 @@ func (cache *TreeCache) deletePath(path ...string) bool {
return false return false
} }
func (cache *TreeCache) deleteEntry(key string, path ...string) bool { func (cache *treeCache) appendValues(recursive bool, ref [][]interface{}) {
childNode := cache.getSubCache(path...)
if childNode == nil {
return false
}
if _, ok := childNode.Entries[key]; ok {
delete(childNode.Entries, key)
return true
}
return false
}
func (cache *TreeCache) appendValues(recursive bool, ref [][]interface{}) {
for _, value := range cache.Entries { for _, value := range cache.Entries {
ref[0] = append(ref[0], value) ref[0] = append(ref[0], value)
} }
...@@ -192,83 +199,15 @@ func (cache *TreeCache) appendValues(recursive bool, ref [][]interface{}) { ...@@ -192,83 +199,15 @@ func (cache *TreeCache) appendValues(recursive bool, ref [][]interface{}) {
} }
} }
func (cache *TreeCache) ensureChildNode(path ...string) *TreeCache { func (cache *treeCache) ensureChildNode(path ...string) *treeCache {
childNode := cache childNode := cache
for _, subpath := range path { for _, subpath := range path {
newNode, ok := childNode.ChildNodes[subpath] newNode, ok := childNode.ChildNodes[subpath]
if !ok { if !ok {
newNode = NewTreeCache() newNode = NewTreeCache().(*treeCache)
childNode.ChildNodes[subpath] = newNode childNode.ChildNodes[subpath] = newNode
} }
childNode = newNode childNode = newNode
} }
return childNode return childNode
} }
// unused function. keeping it around in commented-fashion
// in the future, we might need some form of this function so that
// we can serialize to a file in a mounted empty dir..
//const (
// dataFile = "data.dat"
// crcFile = "data.crc"
//)
//func (cache *TreeCache) Serialize(dir string) (string, error) {
// cache.m.RLock()
// defer cache.m.RUnlock()
// b, err := json.Marshal(cache)
// if err != nil {
// return "", err
// }
//
// if err := ensureDir(dir, os.FileMode(0755)); err != nil {
// return "", err
// }
// if err := ioutil.WriteFile(path.Join(dir, dataFile), b, 0644); err != nil {
// return "", err
// }
// if err := ioutil.WriteFile(path.Join(dir, crcFile), getMD5(b), 0644); err != nil {
// return "", err
// }
// return string(b), nil
//}
//func ensureDir(path string, perm os.FileMode) error {
// s, err := os.Stat(path)
// if err != nil || !s.IsDir() {
// return os.Mkdir(path, perm)
// }
// return nil
//}
//func getMD5(b []byte) []byte {
// h := md5.New()
// h.Write(b)
// return []byte(fmt.Sprintf("%x", h.Sum(nil)))
//}
// unused function. keeping it around in commented-fashion
// in the future, we might need some form of this function so that
// we can restart kube-dns, deserialize the tree and have a cache
// without having to wait for kube-dns to reach out to API server.
//func Deserialize(dir string) (*TreeCache, error) {
// b, err := ioutil.ReadFile(path.Join(dir, dataFile))
// if err != nil {
// return nil, err
// }
//
// hash, err := ioutil.ReadFile(path.Join(dir, crcFile))
// if err != nil {
// return nil, err
// }
// if !reflect.DeepEqual(hash, getMD5(b)) {
// return nil, fmt.Errorf("Checksum failed")
// }
//
// var cache TreeCache
// err = json.Unmarshal(b, &cache)
// if err != nil {
// return nil, err
// }
// cache.m = &sync.RWMutex{}
// return &cache, nil
//}
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package treecache
import (
"testing"
"github.com/skynetservices/skydns/msg"
)
func TestTreeCache(t *testing.T) {
tc := NewTreeCache()
{
_, ok := tc.GetEntry("key1", "p1", "p2")
if ok {
t.Errorf("key should not exist")
}
}
checkExists := func(key string, expectedSvc *msg.Service, path ...string) {
svc, ok := tc.GetEntry(key, path...)
if !ok {
t.Fatalf("key %v should exist", key)
}
if svc := svc.(*msg.Service); svc != nil {
if svc != expectedSvc {
t.Errorf("value is not correct (%v != %v)", svc, expectedSvc)
}
} else {
t.Errorf("entry is not of the right type: %T", svc)
}
}
setEntryTC := []struct {
key string
svc *msg.Service
fqdn string
path []string
}{
{"key1", &msg.Service{}, "key1.p2.p1.", []string{"p1", "p2"}},
{"key2", &msg.Service{}, "key2.p2.p1.", []string{"p1", "p2"}},
{"key3", &msg.Service{}, "key3.p2.p1.", []string{"p1", "p3"}},
}
for _, testCase := range setEntryTC {
tc.SetEntry(testCase.key, testCase.svc, testCase.fqdn, testCase.path...)
checkExists(testCase.key, testCase.svc, testCase.path...)
}
wildcardTC := []struct {
path []string
count int
}{
{[]string{"p1"}, 0},
{[]string{"p1", "p2"}, 2},
{[]string{"p1", "p3"}, 1},
{[]string{"p1", "p2", "key1"}, 1},
{[]string{"p1", "p2", "key2"}, 1},
{[]string{"p1", "p2", "key3"}, 0},
{[]string{"p1", "p3", "key3"}, 1},
{[]string{"p1", "p2", "*"}, 2},
{[]string{"p1", "*", "*"}, 3},
}
for _, testCase := range wildcardTC {
services := tc.GetValuesForPathWithWildcards(testCase.path...)
if len(services) != testCase.count {
t.Fatalf("Expected %v services for path %v, got %v",
testCase.count, testCase.path, len(services))
}
}
// Delete some paths
if !tc.DeletePath("p1", "p2") {
t.Fatal("should delete path p2.p1.")
}
if _, ok := tc.GetEntry("key3", "p1", "p3"); !ok {
t.Error("should not affect p3.p1.")
}
if tc.DeletePath("p1", "p2") {
t.Fatalf("should not be able to delete p2.p1")
}
if !tc.DeletePath("p1", "p3") {
t.Fatalf("should be able to delete p3.p1")
}
if tc.DeletePath("p1", "p3") {
t.Fatalf("should not be able to delete p3.t1")
}
for _, testCase := range []struct {
k string
p []string
}{
{"key1", []string{"p1", "p2"}},
{"key2", []string{"p1", "p2"}},
{"key3", []string{"p1", "p3"}},
} {
if _, ok := tc.GetEntry(testCase.k, testCase.p...); ok {
t.Error()
}
}
}
func TestTreeCacheSetSubCache(t *testing.T) {
tc := NewTreeCache()
m := &msg.Service{}
branch := NewTreeCache()
branch.SetEntry("key1", m, "key", "p2")
tc.SetSubCache("p1", branch, "p0")
if _, ok := tc.GetEntry("key1", "p0", "p1", "p2"); !ok {
t.Errorf("should be able to get entry p0.p1.p2.key1")
}
}
func TestTreeCacheSerialize(t *testing.T) {
tc := NewTreeCache()
tc.SetEntry("key1", &msg.Service{}, "key1.p2.p1.", "p1", "p2")
const expected = `{
"ChildNodes": {
"p1": {
"ChildNodes": {
"p2": {
"ChildNodes": {},
"Entries": {
"key1": {}
}
}
},
"Entries": {}
}
},
"Entries": {}
}`
actual, err := tc.Serialize()
if err != nil {
}
if actual != expected {
t.Errorf("expected %q, got %q", expected, actual)
}
}
...@@ -63,7 +63,8 @@ func ReverseArray(arr []string) []string { ...@@ -63,7 +63,8 @@ func ReverseArray(arr []string) []string {
func GetSkyMsg(ip string, port int) (*msg.Service, string) { func GetSkyMsg(ip string, port int) (*msg.Service, string) {
msg := NewServiceRecord(ip, port) msg := NewServiceRecord(ip, port)
hash := HashServiceRecord(msg) hash := HashServiceRecord(msg)
glog.V(2).Infof("DNS Record:%s, hash:%s", fmt.Sprintf("%v", msg), hash) glog.V(5).Infof("Constructed new DNS record: %s, hash:%s",
fmt.Sprintf("%v", msg), hash)
return msg, fmt.Sprintf("%x", hash) return msg, fmt.Sprintf("%x", hash)
} }
......
...@@ -598,6 +598,7 @@ k8s.io/kubernetes/pkg/credentialprovider,justinsb,1 ...@@ -598,6 +598,7 @@ k8s.io/kubernetes/pkg/credentialprovider,justinsb,1
k8s.io/kubernetes/pkg/credentialprovider/aws,zmerlynn,1 k8s.io/kubernetes/pkg/credentialprovider/aws,zmerlynn,1
k8s.io/kubernetes/pkg/credentialprovider/gcp,mml,1 k8s.io/kubernetes/pkg/credentialprovider/gcp,mml,1
k8s.io/kubernetes/pkg/dns,jdef,1 k8s.io/kubernetes/pkg/dns,jdef,1
k8s.io/kubernetes/pkg/dns/treecache,bowei,0
k8s.io/kubernetes/pkg/fieldpath,childsb,1 k8s.io/kubernetes/pkg/fieldpath,childsb,1
k8s.io/kubernetes/pkg/fields,jsafrane,1 k8s.io/kubernetes/pkg/fields,jsafrane,1
k8s.io/kubernetes/pkg/genericapiserver,nikhiljindal,0 k8s.io/kubernetes/pkg/genericapiserver,nikhiljindal,0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment