Code Cleanup

This commit is contained in:
Sandeep Rajan 2018-02-27 19:33:15 -05:00
parent f038d994e0
commit fbdeff5d4b
3 changed files with 63 additions and 67 deletions

View File

@ -46,7 +46,6 @@ const (
kubeDNSStubDomain = "stubDomains" kubeDNSStubDomain = "stubDomains"
kubeDNSUpstreamNameservers = "upstreamNameservers" kubeDNSUpstreamNameservers = "upstreamNameservers"
kubeDNSFederation = "federations" kubeDNSFederation = "federations"
coreDNSStanzaFormat = "\n "
) )
// EnsureDNSAddon creates the kube-dns or CoreDNS addon // EnsureDNSAddon creates the kube-dns or CoreDNS addon
@ -281,24 +280,20 @@ func createDNSService(dnsService *v1.Service, serviceBytes []byte, client client
func translateStubDomainOfKubeDNSToProxyCoreDNS(dataField string, kubeDNSConfigMap *v1.ConfigMap) (string, error) { func translateStubDomainOfKubeDNSToProxyCoreDNS(dataField string, kubeDNSConfigMap *v1.ConfigMap) (string, error) {
if proxy, ok := kubeDNSConfigMap.Data[dataField]; ok { if proxy, ok := kubeDNSConfigMap.Data[dataField]; ok {
stubDomainData := make(map[string][]string) stubDomainData := make(map[string][]string)
var proxyStanzaList string
err := json.Unmarshal([]byte(proxy), &stubDomainData) err := json.Unmarshal([]byte(proxy), &stubDomainData)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to parse JSON from 'kube-dns ConfigMap: %v", err) return "", fmt.Errorf("failed to parse JSON from 'kube-dns ConfigMap: %v", err)
} }
var proxyStanza []interface{}
var proxyStanza []interface{}
for domain, proxyIP := range stubDomainData { for domain, proxyIP := range stubDomainData {
strings.Join(proxyIP, " ")
pStanza := map[string]interface{}{} pStanza := map[string]interface{}{}
pStanza["keys"] = []string{domain + ":53"} pStanza["keys"] = []string{domain + ":53"}
pStanza["body"] = [][]string{ pStanza["body"] = [][]string{
{"errors"}, {"errors"},
{"cache", "30"}, {"cache", "30"},
{"proxy", ".", strings.Join(proxyIP, " ")}, append([]string{"proxy", "."}, proxyIP...),
} }
proxyStanza = append(proxyStanza, pStanza) proxyStanza = append(proxyStanza, pStanza)
} }
stanzasBytes, err := json.Marshal(proxyStanza) stanzasBytes, err := json.Marshal(proxyStanza)
@ -306,15 +301,12 @@ func translateStubDomainOfKubeDNSToProxyCoreDNS(dataField string, kubeDNSConfigM
return "", err return "", err
} }
outputJSON, err := caddyfile.FromJSON(stanzasBytes) corefileStanza, err := caddyfile.FromJSON(stanzasBytes)
if err != nil { if err != nil {
return "", err return "", err
} }
// This is required to format the Corefile, otherwise errors due to bad yaml format.
output := strings.NewReplacer("\n\t", "\n ", "\"", "", "\n}", "\n }", "\n\n", "\n ").Replace(string(outputJSON))
proxyStanzaList = coreDNSStanzaFormat + output + coreDNSStanzaFormat
return proxyStanzaList, nil return prepCorefileFormat(string(corefileStanza), 4), nil
} }
return "", nil return "", nil
} }
@ -342,7 +334,7 @@ func translateFederationsofKubeDNSToCoreDNS(dataField, coreDNSDomain string, kub
if federation, ok := kubeDNSConfigMap.Data[dataField]; ok { if federation, ok := kubeDNSConfigMap.Data[dataField]; ok {
var ( var (
federationStanza []interface{} federationStanza []interface{}
fData []string body [][]string
) )
federationData := make(map[string]string) federationData := make(map[string]string)
@ -350,33 +342,37 @@ func translateFederationsofKubeDNSToCoreDNS(dataField, coreDNSDomain string, kub
if err != nil { if err != nil {
return "", fmt.Errorf("failed to parse JSON from kube-dns ConfigMap: %v", err) return "", fmt.Errorf("failed to parse JSON from kube-dns ConfigMap: %v", err)
} }
fStanza := map[string]interface{}{}
for name, domain := range federationData { for name, domain := range federationData {
fData = append(fData, name+" "+domain) body = append(body, []string{name, domain})
}
fStanza := map[string]interface{}{}
fStanza["body"] = [][]string{
{strings.Join(fData, "\n ")},
} }
federationStanza = append(federationStanza, fStanza) federationStanza = append(federationStanza, fStanza)
fStanza["keys"] = []string{"federation " + coreDNSDomain} fStanza["keys"] = []string{"federation " + coreDNSDomain}
fStanza["body"] = body
stanzasBytes, err := json.Marshal(federationStanza) stanzasBytes, err := json.Marshal(federationStanza)
if err != nil { if err != nil {
return "", err return "", err
} }
outputJSON, err := caddyfile.FromJSON(stanzasBytes) corefileStanza, err := caddyfile.FromJSON(stanzasBytes)
if err != nil { if err != nil {
return "", err return "", err
} }
// This is required to format the Corefile, otherwise errors due to bad yaml format. return prepCorefileFormat(string(corefileStanza), 8), nil
output := strings.NewReplacer("\n\t", "\n ", "\"", "", "\n}", "\n }").Replace(string(outputJSON))
federationStanzaList := coreDNSStanzaFormat + output
return federationStanzaList, nil
} }
return "", nil return "", nil
} }
// prepCorefileFormat indents the output of the Corefile caddytext and replaces tabs with spaces
// to neatly format the configmap, making it readable.
func prepCorefileFormat(s string, indentation int) string {
r := []string{}
for _, line := range strings.Split(s, "\n") {
indented := strings.Repeat(" ", indentation) + line
r = append(r, indented)
}
corefile := strings.Join(r, "\n")
return "\n" + strings.Replace(corefile, "\t", " ", -1)
}

View File

@ -201,28 +201,28 @@ func TestTranslateStubDomainKubeDNSToCoreDNS(t *testing.T) {
expectOne: ` expectOne: `
foo.com:53 { foo.com:53 {
errors errors
cache 30 cache 30
proxy . 1.2.3.4:5300 3.3.3.3 proxy . 1.2.3.4:5300 3.3.3.3
} }
my.cluster.local:53 { my.cluster.local:53 {
errors errors
cache 30 cache 30
proxy . 2.3.4.5 proxy . 2.3.4.5
} }`,
`,
expectTwo: ` expectTwo: `
my.cluster.local:53 { my.cluster.local:53 {
errors errors
cache 30 cache 30
proxy . 2.3.4.5 proxy . 2.3.4.5
} }
foo.com:53 { foo.com:53 {
errors errors
cache 30 cache 30
proxy . 1.2.3.4:5300 3.3.3.3 proxy . 1.2.3.4:5300 3.3.3.3
} }`,
`,
}, },
{ {
configMap: &v1.ConfigMap{ configMap: &v1.ConfigMap{
@ -248,28 +248,28 @@ func TestTranslateStubDomainKubeDNSToCoreDNS(t *testing.T) {
expectOne: ` expectOne: `
foo.com:53 { foo.com:53 {
errors errors
cache 30 cache 30
proxy . 1.2.3.4:5300 proxy . 1.2.3.4:5300
} }
my.cluster.local:53 { my.cluster.local:53 {
errors errors
cache 30 cache 30
proxy . 2.3.4.5 proxy . 2.3.4.5
} }`,
`,
expectTwo: ` expectTwo: `
my.cluster.local:53 { my.cluster.local:53 {
errors errors
cache 30 cache 30
proxy . 2.3.4.5 proxy . 2.3.4.5
} }
foo.com:53 { foo.com:53 {
errors errors
cache 30 cache 30
proxy . 1.2.3.4:5300 proxy . 1.2.3.4:5300
} }`,
`,
}, },
{ {
configMap: &v1.ConfigMap{ configMap: &v1.ConfigMap{
@ -370,15 +370,15 @@ func TestTranslateFederationKubeDNSToCoreDNS(t *testing.T) {
}, },
expectOne: ` expectOne: `
federation cluster.local { federation cluster.local {
foo foo.feddomain.com foo foo.feddomain.com
bar bar.feddomain.com bar bar.feddomain.com
}`, }`,
expectTwo: ` expectTwo: `
federation cluster.local { federation cluster.local {
bar bar.feddomain.com bar bar.feddomain.com
foo foo.feddomain.com foo foo.feddomain.com
}`, }`,
}, },
{ {
configMap: &v1.ConfigMap{ configMap: &v1.ConfigMap{

View File

@ -309,7 +309,7 @@ data:
health health
kubernetes {{ .DNSDomain }} in-addr.arpa ip6.arpa { kubernetes {{ .DNSDomain }} in-addr.arpa ip6.arpa {
pods insecure pods insecure
upstream upstream
fallthrough in-addr.arpa ip6.arpa fallthrough in-addr.arpa ip6.arpa
}{{ .Federation }} }{{ .Federation }}
prometheus :9153 prometheus :9153