@@ -25,9 +25,8 @@ import (
25
25
"github.com/caddyserver/caddy/caddyfile"
26
26
"github.com/coredns/corefile-migration/migration"
27
27
"github.com/pkg/errors"
28
-
29
28
apps "k8s.io/api/apps/v1"
30
- "k8s.io/api/core/v1"
29
+ v1 "k8s.io/api/core/v1"
31
30
rbac "k8s.io/api/rbac/v1"
32
31
apierrors "k8s.io/apimachinery/pkg/api/errors"
33
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -52,6 +51,8 @@ const (
52
51
kubeDNSUpstreamNameservers = "upstreamNameservers"
53
52
kubeDNSFederation = "federations"
54
53
unableToDecodeCoreDNS = "unable to decode CoreDNS"
54
+ coreDNSReplicas = 2
55
+ kubeDNSReplicas = 1
55
56
)
56
57
57
58
// DeployedDNSAddon returns the type of DNS addon currently deployed
@@ -80,15 +81,40 @@ func DeployedDNSAddon(client clientset.Interface) (kubeadmapi.DNSAddOnType, stri
80
81
}
81
82
}
82
83
84
+ // deployedDNSReplicas returns the replica count for the current DNS deployment
85
+ func deployedDNSReplicas (client clientset.Interface , replicas int32 ) (* int32 , error ) {
86
+ deploymentsClient := client .AppsV1 ().Deployments (metav1 .NamespaceSystem )
87
+ deployments , err := deploymentsClient .List (metav1.ListOptions {LabelSelector : "k8s-app=kube-dns" })
88
+ if err != nil {
89
+ return & replicas , errors .Wrap (err , "couldn't retrieve DNS addon deployments" )
90
+ }
91
+ switch len (deployments .Items ) {
92
+ case 0 :
93
+ return & replicas , nil
94
+ case 1 :
95
+ return deployments .Items [0 ].Spec .Replicas , nil
96
+ default :
97
+ return & replicas , errors .Errorf ("multiple DNS addon deployments found: %v" , deployments .Items )
98
+ }
99
+ }
100
+
83
101
// EnsureDNSAddon creates the kube-dns or CoreDNS addon
84
102
func EnsureDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface ) error {
85
103
if cfg .DNS .Type == kubeadmapi .CoreDNS {
86
- return coreDNSAddon (cfg , client )
104
+ replicas , err := deployedDNSReplicas (client , coreDNSReplicas )
105
+ if err != nil {
106
+ return err
107
+ }
108
+ return coreDNSAddon (cfg , client , replicas )
109
+ }
110
+ replicas , err := deployedDNSReplicas (client , kubeDNSReplicas )
111
+ if err != nil {
112
+ return err
87
113
}
88
- return kubeDNSAddon (cfg , client )
114
+ return kubeDNSAddon (cfg , client , replicas )
89
115
}
90
116
91
- func kubeDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface ) error {
117
+ func kubeDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface , replicas * int32 ) error {
92
118
if err := CreateServiceAccount (client ); err != nil {
93
119
return err
94
120
}
@@ -108,7 +134,10 @@ func kubeDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interfa
108
134
}
109
135
110
136
dnsDeploymentBytes , err := kubeadmutil .ParseTemplate (KubeDNSDeployment ,
111
- struct { DeploymentName , KubeDNSImage , DNSMasqImage , SidecarImage , DNSBindAddr , DNSProbeAddr , DNSDomain , ControlPlaneTaintKey string }{
137
+ struct {
138
+ DeploymentName , KubeDNSImage , DNSMasqImage , SidecarImage , DNSBindAddr , DNSProbeAddr , DNSDomain , ControlPlaneTaintKey string
139
+ Replicas * int32
140
+ }{
112
141
DeploymentName : kubeadmconstants .KubeDNSDeploymentName ,
113
142
KubeDNSImage : images .GetDNSImage (cfg , kubeadmconstants .KubeDNSKubeDNSImageName ),
114
143
DNSMasqImage : images .GetDNSImage (cfg , kubeadmconstants .KubeDNSDnsMasqNannyImageName ),
@@ -117,6 +146,7 @@ func kubeDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interfa
117
146
DNSProbeAddr : dnsProbeAddr ,
118
147
DNSDomain : cfg .Networking .DNSDomain ,
119
148
ControlPlaneTaintKey : kubeadmconstants .LabelNodeRoleMaster ,
149
+ Replicas : replicas ,
120
150
})
121
151
if err != nil {
122
152
return errors .Wrap (err , "error when parsing kube-dns deployment template" )
@@ -162,12 +192,16 @@ func createKubeDNSAddon(deploymentBytes, serviceBytes []byte, client clientset.I
162
192
return createDNSService (kubednsService , serviceBytes , client )
163
193
}
164
194
165
- func coreDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface ) error {
195
+ func coreDNSAddon (cfg * kubeadmapi.ClusterConfiguration , client clientset.Interface , replicas * int32 ) error {
166
196
// Get the YAML manifest
167
- coreDNSDeploymentBytes , err := kubeadmutil .ParseTemplate (CoreDNSDeployment , struct { DeploymentName , Image , ControlPlaneTaintKey string }{
197
+ coreDNSDeploymentBytes , err := kubeadmutil .ParseTemplate (CoreDNSDeployment , struct {
198
+ DeploymentName , Image , ControlPlaneTaintKey string
199
+ Replicas * int32
200
+ }{
168
201
DeploymentName : kubeadmconstants .CoreDNSDeploymentName ,
169
202
Image : images .GetDNSImage (cfg , kubeadmconstants .CoreDNSImageName ),
170
203
ControlPlaneTaintKey : kubeadmconstants .LabelNodeRoleMaster ,
204
+ Replicas : replicas ,
171
205
})
172
206
if err != nil {
173
207
return errors .Wrap (err , "error when parsing CoreDNS deployment template" )
0 commit comments