add federations translation
This commit is contained in:
parent
6d8459166d
commit
d2e83a2b07
@ -46,6 +46,7 @@ const (
|
||||
KubeDNSServiceAccountName = "kube-dns"
|
||||
kubeDNSStubDomain = "stubDomains"
|
||||
kubeDNSUpstreamNameservers = "upstreamNameservers"
|
||||
kubeDNSFederation = "federations"
|
||||
)
|
||||
|
||||
// EnsureDNSAddon creates the kube-dns or CoreDNS addon
|
||||
@ -160,12 +161,17 @@ func coreDNSAddon(cfg *kubeadmapi.MasterConfiguration, client clientset.Interfac
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
coreDNSDomain := cfg.Networking.DNSDomain
|
||||
federations, err := translateFederationsofKubeDNSToCoreDNS(kubeDNSFederation, coreDNSDomain, kubeDNSConfigMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get the config file for CoreDNS
|
||||
coreDNSConfigMapBytes, err := kubeadmutil.ParseTemplate(CoreDNSConfigMap, struct{ DNSDomain, ServiceCIDR, UpstreamNameserver, StubDomain string }{
|
||||
ServiceCIDR: cfg.Networking.ServiceSubnet,
|
||||
DNSDomain: cfg.Networking.DNSDomain,
|
||||
coreDNSConfigMapBytes, err := kubeadmutil.ParseTemplate(CoreDNSConfigMap, struct{ DNSDomain, UpstreamNameserver, Federation, StubDomain string }{
|
||||
DNSDomain: coreDNSDomain,
|
||||
UpstreamNameserver: upstreamNameserver,
|
||||
Federation: federations,
|
||||
StubDomain: stubDomain,
|
||||
})
|
||||
if err != nil {
|
||||
@ -273,7 +279,7 @@ func createDNSService(dnsService *v1.Service, serviceBytes []byte, client client
|
||||
func translateStubDomainOfKubeDNSToProxyCoreDNS(dataField string, kubeDNSConfigMap *v1.ConfigMap) (string, error) {
|
||||
if proxy, ok := kubeDNSConfigMap.Data[dataField]; ok {
|
||||
stubDomainData := make(map[string][]string)
|
||||
proxyStanzaList := coreDNSProxyStanzaPrefix
|
||||
proxyStanzaList := coreDNSStanzaPrefix
|
||||
|
||||
err := json.Unmarshal([]byte(proxy), &stubDomainData)
|
||||
if err != nil {
|
||||
@ -304,3 +310,24 @@ func translateUpstreamNameServerOfKubeDNSToUpstreamProxyCoreDNS(dataField string
|
||||
}
|
||||
return kubetypes.ResolvConfDefault, nil
|
||||
}
|
||||
|
||||
// translateFederationofKubeDNSToCoreDNS translates Federations Data in kube-dns ConfigMap
|
||||
// to Federation for CoreDNS Corefile.
|
||||
func translateFederationsofKubeDNSToCoreDNS(dataField, coreDNSDomain string, kubeDNSConfigMap *v1.ConfigMap) (string, error) {
|
||||
if federation, ok := kubeDNSConfigMap.Data[dataField]; ok {
|
||||
federationData := make(map[string]string)
|
||||
|
||||
err := json.Unmarshal([]byte(federation), &federationData)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse JSON from kube-dns ConfigMap: %v", err)
|
||||
}
|
||||
federationStanzaList := coreDNSStanzaPrefix + coreDNSFederation + coreDNSDomain + coreDNSFederationStanzaPrefix
|
||||
var federationStanzaContents string
|
||||
for name, domain := range federationData {
|
||||
federationStanzaContents = federationStanzaContents + coreDNSFederationStanzaMid + name + " " + domain
|
||||
}
|
||||
federationStanzaList = federationStanzaList + federationStanzaContents + coreDNSFederationStanzaSuffix
|
||||
return federationStanzaList, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
@ -130,9 +130,9 @@ func TestCompileManifests(t *testing.T) {
|
||||
},
|
||||
{
|
||||
manifest: CoreDNSConfigMap,
|
||||
data: struct{ DNSDomain, ServiceCIDR, UpstreamNameserver, StubDomain string }{
|
||||
data: struct{ DNSDomain, Federation, UpstreamNameserver, StubDomain string }{
|
||||
DNSDomain: "foo",
|
||||
ServiceCIDR: "foo",
|
||||
Federation: "foo",
|
||||
UpstreamNameserver: "foo",
|
||||
StubDomain: "foo",
|
||||
},
|
||||
@ -311,3 +311,63 @@ func TestTranslateUpstreamKubeDNSToCoreDNS(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslateFederationKubeDNSToCoreDNS(t *testing.T) {
|
||||
testCases := []struct {
|
||||
configMap *v1.ConfigMap
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
configMap: &v1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "kube-dns",
|
||||
Namespace: "kube-system",
|
||||
},
|
||||
Data: map[string]string{
|
||||
"federations": `{"foo" : "foo.feddomain.com", "bar" : "bar.feddomain.com"}`,
|
||||
"stubDomains": `{"foo.com" : ["1.2.3.4:5300","3.3.3.3"], "my.cluster.local" : ["2.3.4.5"]}`,
|
||||
"upstreamNameservers": `["8.8.8.8", "8.8.4.4"]`,
|
||||
},
|
||||
},
|
||||
|
||||
expect: `
|
||||
federation cluster.local {
|
||||
foo foo.feddomain.com
|
||||
bar bar.feddomain.com
|
||||
}`,
|
||||
},
|
||||
{
|
||||
configMap: &v1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "kubedns",
|
||||
Namespace: "kube-system",
|
||||
},
|
||||
},
|
||||
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
configMap: &v1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "kube-dns",
|
||||
Namespace: "kube-system",
|
||||
},
|
||||
Data: map[string]string{
|
||||
"stubDomains": `{"foo.com" : ["1.2.3.4:5300"], "my.cluster.local" : ["2.3.4.5"]}`,
|
||||
"upstreamNameservers": `["8.8.8.8", "8.8.4.4"]`,
|
||||
},
|
||||
},
|
||||
|
||||
expect: "",
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
out, err := translateFederationsofKubeDNSToCoreDNS(kubeDNSFederation, "cluster.local", testCase.configMap)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !strings.Contains(out, testCase.expect) {
|
||||
t.Errorf("expected to find %q in output: %q", testCase.expect, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -307,11 +307,11 @@ data:
|
||||
.:53 {
|
||||
errors
|
||||
health
|
||||
kubernetes {{ .DNSDomain }} {{ .ServiceCIDR }} {
|
||||
kubernetes {{ .DNSDomain }} in-addr.arpa ip6.arpa {
|
||||
pods insecure
|
||||
upstream {{ .UpstreamNameserver }}
|
||||
fallthrough in-addr.arpa ip6.arpa
|
||||
}
|
||||
}{{ .Federation }}
|
||||
prometheus :9153
|
||||
proxy . {{ .UpstreamNameserver }}
|
||||
cache 30
|
||||
@ -359,14 +359,15 @@ metadata:
|
||||
namespace: kube-system
|
||||
`
|
||||
// coreDNSProxyDefaultPort, coreDNSCorefileDefaultData, coreDNSProxyStanzaSuffix is used in the translation of configMap of kube-dns to CoreDNS.
|
||||
coreDNSProxyStanzaPrefix = `
|
||||
`
|
||||
coreDNSStanzaPrefix = "\n "
|
||||
coreDNSProxyDefaultPort = `:53`
|
||||
coreDNSCorefileDefaultData = ` {
|
||||
errors
|
||||
cache 30
|
||||
proxy . `
|
||||
coreDNSProxyStanzaSuffix = `
|
||||
}
|
||||
`
|
||||
coreDNSProxyStanzaSuffix = "\n }\n "
|
||||
coreDNSFederation = "federation "
|
||||
coreDNSFederationStanzaPrefix = " {"
|
||||
coreDNSFederationStanzaMid = "\n "
|
||||
coreDNSFederationStanzaSuffix = "\n }"
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user