Skip to content

Commit 7b792c3

Browse files
authored
Merge pull request kubernetes#85837 from pickledrick/coredns-replicas
kubeadm: persist dns replica count on upgrade
2 parents cfdfd04 + 0e4469c commit 7b792c3

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

cmd/kubeadm/app/phases/addons/dns/dns.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ import (
2525
"github.com/caddyserver/caddy/caddyfile"
2626
"github.com/coredns/corefile-migration/migration"
2727
"github.com/pkg/errors"
28-
2928
apps "k8s.io/api/apps/v1"
30-
"k8s.io/api/core/v1"
29+
v1 "k8s.io/api/core/v1"
3130
rbac "k8s.io/api/rbac/v1"
3231
apierrors "k8s.io/apimachinery/pkg/api/errors"
3332
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -52,6 +51,8 @@ const (
5251
kubeDNSUpstreamNameservers = "upstreamNameservers"
5352
kubeDNSFederation = "federations"
5453
unableToDecodeCoreDNS = "unable to decode CoreDNS"
54+
coreDNSReplicas = 2
55+
kubeDNSReplicas = 1
5556
)
5657

5758
// DeployedDNSAddon returns the type of DNS addon currently deployed
@@ -80,15 +81,40 @@ func DeployedDNSAddon(client clientset.Interface) (kubeadmapi.DNSAddOnType, stri
8081
}
8182
}
8283

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+
83101
// EnsureDNSAddon creates the kube-dns or CoreDNS addon
84102
func EnsureDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface) error {
85103
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
87113
}
88-
return kubeDNSAddon(cfg, client)
114+
return kubeDNSAddon(cfg, client, replicas)
89115
}
90116

91-
func kubeDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface) error {
117+
func kubeDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface, replicas *int32) error {
92118
if err := CreateServiceAccount(client); err != nil {
93119
return err
94120
}
@@ -108,7 +134,10 @@ func kubeDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interfa
108134
}
109135

110136
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+
}{
112141
DeploymentName: kubeadmconstants.KubeDNSDeploymentName,
113142
KubeDNSImage: images.GetDNSImage(cfg, kubeadmconstants.KubeDNSKubeDNSImageName),
114143
DNSMasqImage: images.GetDNSImage(cfg, kubeadmconstants.KubeDNSDnsMasqNannyImageName),
@@ -117,6 +146,7 @@ func kubeDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interfa
117146
DNSProbeAddr: dnsProbeAddr,
118147
DNSDomain: cfg.Networking.DNSDomain,
119148
ControlPlaneTaintKey: kubeadmconstants.LabelNodeRoleMaster,
149+
Replicas: replicas,
120150
})
121151
if err != nil {
122152
return errors.Wrap(err, "error when parsing kube-dns deployment template")
@@ -162,12 +192,16 @@ func createKubeDNSAddon(deploymentBytes, serviceBytes []byte, client clientset.I
162192
return createDNSService(kubednsService, serviceBytes, client)
163193
}
164194

165-
func coreDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface) error {
195+
func coreDNSAddon(cfg *kubeadmapi.ClusterConfiguration, client clientset.Interface, replicas *int32) error {
166196
// 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+
}{
168201
DeploymentName: kubeadmconstants.CoreDNSDeploymentName,
169202
Image: images.GetDNSImage(cfg, kubeadmconstants.CoreDNSImageName),
170203
ControlPlaneTaintKey: kubeadmconstants.LabelNodeRoleMaster,
204+
Replicas: replicas,
171205
})
172206
if err != nil {
173207
return errors.Wrap(err, "error when parsing CoreDNS deployment template")

cmd/kubeadm/app/phases/addons/dns/dns_test.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
apps "k8s.io/api/apps/v1"
24-
"k8s.io/api/core/v1"
24+
v1 "k8s.io/api/core/v1"
2525
apierrors "k8s.io/apimachinery/pkg/api/errors"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/runtime"
@@ -91,6 +91,7 @@ func TestCreateServiceAccount(t *testing.T) {
9191
}
9292

9393
func TestCompileManifests(t *testing.T) {
94+
replicas := int32(coreDNSReplicas)
9495
var tests = []struct {
9596
name string
9697
manifest string
@@ -99,7 +100,10 @@ func TestCompileManifests(t *testing.T) {
99100
{
100101
name: "KubeDNSDeployment manifest",
101102
manifest: KubeDNSDeployment,
102-
data: struct{ DeploymentName, KubeDNSImage, DNSMasqImage, SidecarImage, DNSBindAddr, DNSProbeAddr, DNSDomain, ControlPlaneTaintKey string }{
103+
data: struct {
104+
DeploymentName, KubeDNSImage, DNSMasqImage, SidecarImage, DNSBindAddr, DNSProbeAddr, DNSDomain, ControlPlaneTaintKey string
105+
Replicas *int32
106+
}{
103107
DeploymentName: "foo",
104108
KubeDNSImage: "foo",
105109
DNSMasqImage: "foo",
@@ -108,6 +112,7 @@ func TestCompileManifests(t *testing.T) {
108112
DNSProbeAddr: "foo",
109113
DNSDomain: "foo",
110114
ControlPlaneTaintKey: "foo",
115+
Replicas: &replicas,
111116
},
112117
},
113118
{
@@ -120,10 +125,14 @@ func TestCompileManifests(t *testing.T) {
120125
{
121126
name: "CoreDNSDeployment manifest",
122127
manifest: CoreDNSDeployment,
123-
data: struct{ DeploymentName, Image, ControlPlaneTaintKey string }{
128+
data: struct {
129+
DeploymentName, Image, ControlPlaneTaintKey string
130+
Replicas *int32
131+
}{
124132
DeploymentName: "foo",
125133
Image: "foo",
126134
ControlPlaneTaintKey: "foo",
135+
Replicas: &replicas,
127136
},
128137
},
129138
{
@@ -561,6 +570,7 @@ func TestTranslateFederationKubeDNSToCoreDNS(t *testing.T) {
561570
}
562571

563572
func TestDeploymentsHaveSystemClusterCriticalPriorityClassName(t *testing.T) {
573+
replicas := int32(coreDNSReplicas)
564574
testCases := []struct {
565575
name string
566576
manifest string
@@ -569,7 +579,10 @@ func TestDeploymentsHaveSystemClusterCriticalPriorityClassName(t *testing.T) {
569579
{
570580
name: "KubeDNSDeployment",
571581
manifest: KubeDNSDeployment,
572-
data: struct{ DeploymentName, KubeDNSImage, DNSMasqImage, SidecarImage, DNSBindAddr, DNSProbeAddr, DNSDomain, ControlPlaneTaintKey string }{
582+
data: struct {
583+
DeploymentName, KubeDNSImage, DNSMasqImage, SidecarImage, DNSBindAddr, DNSProbeAddr, DNSDomain, ControlPlaneTaintKey string
584+
Replicas *int32
585+
}{
573586
DeploymentName: "foo",
574587
KubeDNSImage: "foo",
575588
DNSMasqImage: "foo",
@@ -578,16 +591,21 @@ func TestDeploymentsHaveSystemClusterCriticalPriorityClassName(t *testing.T) {
578591
DNSProbeAddr: "foo",
579592
DNSDomain: "foo",
580593
ControlPlaneTaintKey: "foo",
594+
Replicas: &replicas,
581595
},
582596
},
583597
{
584598
name: "CoreDNSDeployment",
585599
manifest: CoreDNSDeployment,
586-
data: struct{ DeploymentName, Image, ControlPlaneTaintKey, CoreDNSConfigMapName string }{
600+
data: struct {
601+
DeploymentName, Image, ControlPlaneTaintKey, CoreDNSConfigMapName string
602+
Replicas *int32
603+
}{
587604
DeploymentName: "foo",
588605
Image: "foo",
589606
ControlPlaneTaintKey: "foo",
590607
CoreDNSConfigMapName: "foo",
608+
Replicas: &replicas,
591609
},
592610
},
593611
}

cmd/kubeadm/app/phases/addons/dns/manifests.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ metadata:
2727
labels:
2828
k8s-app: kube-dns
2929
spec:
30-
# replicas: not specified here:
31-
# 1. In order to make Addon Manager do not reconcile this replicas parameter.
32-
# 2. Default is 1.
33-
# 3. Will be tuned in real time if DNS horizontal auto-scaling is turned on.
30+
replicas: {{ .Replicas }}
3431
strategy:
3532
rollingUpdate:
3633
maxSurge: 10%
@@ -223,7 +220,7 @@ metadata:
223220
labels:
224221
k8s-app: kube-dns
225222
spec:
226-
replicas: 2
223+
replicas: {{ .Replicas }}
227224
strategy:
228225
type: RollingUpdate
229226
rollingUpdate:

0 commit comments

Comments
 (0)