Skip to content

Commit 5177a47

Browse files
authored
Merge pull request kubernetes-sigs#4809 from shivi28/capi_4676_final
🌱 Refactor multiline yaml const
2 parents 20756a8 + 028ac54 commit 5177a47

File tree

6 files changed

+339
-241
lines changed

6 files changed

+339
-241
lines changed

controlplane/kubeadm/internal/workload_cluster_coredns_test.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
3030
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4"
31+
"sigs.k8s.io/cluster-api/util/yaml"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
3233
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3334
)
@@ -111,13 +112,14 @@ func TestUpdateCoreDNS(t *testing.T) {
111112
Namespace: metav1.NamespaceSystem,
112113
},
113114
Data: map[string]string{
114-
"ClusterConfiguration": `apiServer:
115-
apiVersion: kubeadm.k8s.io/v1beta2
116-
dns:
117-
type: CoreDNS
118-
imageRepository: k8s.gcr.io
119-
kind: ClusterConfiguration
120-
`,
115+
"ClusterConfiguration": yaml.Raw(`
116+
apiServer:
117+
apiVersion: kubeadm.k8s.io/v1beta2
118+
dns:
119+
type: CoreDNS
120+
imageRepository: k8s.gcr.io
121+
kind: ClusterConfiguration
122+
`),
121123
},
122124
}
123125

@@ -886,24 +888,28 @@ func TestUpdateCoreDNSImageInfoInKubeadmConfigMap(t *testing.T) {
886888
}{
887889
{
888890
name: "it should set the DNS image config",
889-
clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" +
890-
"kind: ClusterConfiguration\n",
891+
clusterConfigurationData: yaml.Raw(`
892+
apiVersion: kubeadm.k8s.io/v1beta2
893+
kind: ClusterConfiguration
894+
`),
891895
newDNS: bootstrapv1.DNS{
892896
ImageMeta: bootstrapv1.ImageMeta{
893897
ImageRepository: "example.com/k8s",
894898
ImageTag: "v1.2.3",
895899
},
896900
},
897-
wantClusterConfiguration: "apiServer: {}\n" +
898-
"apiVersion: kubeadm.k8s.io/v1beta2\n" +
899-
"controllerManager: {}\n" +
900-
"dns:\n" +
901-
" imageRepository: example.com/k8s\n" +
902-
" imageTag: v1.2.3\n" +
903-
"etcd: {}\n" +
904-
"kind: ClusterConfiguration\n" +
905-
"networking: {}\n" +
906-
"scheduler: {}\n",
901+
wantClusterConfiguration: yaml.Raw(`
902+
apiServer: {}
903+
apiVersion: kubeadm.k8s.io/v1beta2
904+
controllerManager: {}
905+
dns:
906+
imageRepository: example.com/k8s
907+
imageTag: v1.2.3
908+
etcd: {}
909+
kind: ClusterConfiguration
910+
networking: {}
911+
scheduler: {}
912+
`),
907913
},
908914
}
909915
for _, tt := range tests {

controlplane/kubeadm/internal/workload_cluster_etcd_test.go

Lines changed: 94 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
3232
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd"
3333
fake2 "sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd/fake"
34+
"sigs.k8s.io/cluster-api/util/yaml"
3435
"sigs.k8s.io/controller-runtime/pkg/client"
3536
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3637
)
@@ -45,36 +46,44 @@ func TestUpdateEtcdVersionInKubeadmConfigMap(t *testing.T) {
4546
}{
4647
{
4748
name: "it should set etcd version when local etcd",
48-
clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" +
49-
"kind: ClusterConfiguration\n" +
50-
"etcd:\n" +
51-
" local: {}\n",
49+
clusterConfigurationData: yaml.Raw(`
50+
apiVersion: kubeadm.k8s.io/v1beta2
51+
kind: ClusterConfiguration
52+
etcd:
53+
local: {}
54+
`),
5255
newImageRepository: "example.com/k8s",
5356
newImageTag: "v1.6.0",
54-
wantClusterConfiguration: "apiServer: {}\n" +
55-
"apiVersion: kubeadm.k8s.io/v1beta2\n" +
56-
"controllerManager: {}\n" +
57-
"dns: {}\n" +
58-
"etcd:\n" +
59-
" local:\n" +
60-
" imageRepository: example.com/k8s\n" +
61-
" imageTag: v1.6.0\n" +
62-
"kind: ClusterConfiguration\n" +
63-
"networking: {}\n" +
64-
"scheduler: {}\n",
57+
wantClusterConfiguration: yaml.Raw(`
58+
apiServer: {}
59+
apiVersion: kubeadm.k8s.io/v1beta2
60+
controllerManager: {}
61+
dns: {}
62+
etcd:
63+
local:
64+
imageRepository: example.com/k8s
65+
imageTag: v1.6.0
66+
kind: ClusterConfiguration
67+
networking: {}
68+
scheduler: {}
69+
`),
6570
},
6671
{
6772
name: "no op when external etcd",
68-
clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" +
69-
"kind: ClusterConfiguration\n" +
70-
"etcd:\n" +
71-
" external: {}\n",
73+
clusterConfigurationData: yaml.Raw(`
74+
apiVersion: kubeadm.k8s.io/v1beta2
75+
kind: ClusterConfiguration
76+
etcd:
77+
external: {}
78+
`),
7279
newImageRepository: "example.com/k8s",
7380
newImageTag: "v1.6.0",
74-
wantClusterConfiguration: "apiVersion: kubeadm.k8s.io/v1beta2\n" +
75-
"kind: ClusterConfiguration\n" +
76-
"etcd:\n" +
77-
" external: {}\n",
81+
wantClusterConfiguration: yaml.Raw(`
82+
apiVersion: kubeadm.k8s.io/v1beta2
83+
kind: ClusterConfiguration
84+
etcd:
85+
external: {}
86+
`),
7887
},
7988
}
8089

@@ -442,19 +451,21 @@ func TestReconcileEtcdMembers(t *testing.T) {
442451
Namespace: metav1.NamespaceSystem,
443452
},
444453
Data: map[string]string{
445-
clusterStatusKey: "apiEndpoints:\n" +
446-
" ip-10-0-0-1.ec2.internal:\n" +
447-
" advertiseAddress: 10.0.0.1\n" +
448-
" bindPort: 6443\n" +
449-
" ip-10-0-0-2.ec2.internal:\n" +
450-
" advertiseAddress: 10.0.0.2\n" +
451-
" bindPort: 6443\n" +
452-
" someFieldThatIsAddedInTheFuture: bar\n" +
453-
" ip-10-0-0-3.ec2.internal:\n" +
454-
" advertiseAddress: 10.0.0.3\n" +
455-
" bindPort: 6443\n" +
456-
"apiVersion: kubeadm.k8s.io/v1beta2\n" +
457-
"kind: ClusterStatus\n",
454+
clusterStatusKey: yaml.Raw(`
455+
apiEndpoints:
456+
ip-10-0-0-1.ec2.internal:
457+
advertiseAddress: 10.0.0.1
458+
bindPort: 6443
459+
ip-10-0-0-2.ec2.internal:
460+
advertiseAddress: 10.0.0.2
461+
bindPort: 6443
462+
someFieldThatIsAddedInTheFuture: bar
463+
ip-10-0-0-3.ec2.internal:
464+
advertiseAddress: 10.0.0.3
465+
bindPort: 6443
466+
apiVersion: kubeadm.k8s.io/v1beta2
467+
kind: ClusterStatus
468+
`),
458469
},
459470
}
460471
kubeadmConfigWithoutClusterStatus := kubeadmConfig.DeepCopy()
@@ -516,16 +527,18 @@ func TestReconcileEtcdMembers(t *testing.T) {
516527
client.ObjectKey{Name: kubeadmConfigKey, Namespace: metav1.NamespaceSystem},
517528
&actualConfig,
518529
)).To(Succeed())
519-
520-
g.Expect(actualConfig.Data[clusterStatusKey]).To(Equal("apiEndpoints:\n" +
521-
" ip-10-0-0-1.ec2.internal:\n" +
522-
" advertiseAddress: 10.0.0.1\n" +
523-
" bindPort: 6443\n" +
524-
" ip-10-0-0-2.ec2.internal:\n" +
525-
" advertiseAddress: 10.0.0.2\n" +
526-
" bindPort: 6443\n" +
527-
"apiVersion: kubeadm.k8s.io/v1beta2\n" +
528-
"kind: ClusterStatus\n"))
530+
expectedOutput := yaml.Raw(`
531+
apiEndpoints:
532+
ip-10-0-0-1.ec2.internal:
533+
advertiseAddress: 10.0.0.1
534+
bindPort: 6443
535+
ip-10-0-0-2.ec2.internal:
536+
advertiseAddress: 10.0.0.2
537+
bindPort: 6443
538+
apiVersion: kubeadm.k8s.io/v1beta2
539+
kind: ClusterStatus
540+
`)
541+
g.Expect(actualConfig.Data[clusterStatusKey]).To(Equal(expectedOutput))
529542
},
530543
},
531544
{
@@ -606,37 +619,45 @@ func TestRemoveNodeFromKubeadmConfigMap(t *testing.T) {
606619
{
607620
name: "removes the api endpoint",
608621
apiEndpoint: "ip-10-0-0-2.ec2.internal",
609-
clusterStatusData: "apiEndpoints:\n" +
610-
" ip-10-0-0-1.ec2.internal:\n" +
611-
" advertiseAddress: 10.0.0.1\n" +
612-
" bindPort: 6443\n" +
613-
" ip-10-0-0-2.ec2.internal:\n" +
614-
" advertiseAddress: 10.0.0.2\n" +
615-
" bindPort: 6443\n" +
616-
"apiVersion: kubeadm.k8s.io/v1beta2\n" +
617-
"kind: ClusterStatus\n",
618-
wantClusterStatus: "apiEndpoints:\n" +
619-
" ip-10-0-0-1.ec2.internal:\n" +
620-
" advertiseAddress: 10.0.0.1\n" +
621-
" bindPort: 6443\n" +
622-
"apiVersion: kubeadm.k8s.io/v1beta2\n" +
623-
"kind: ClusterStatus\n",
622+
clusterStatusData: yaml.Raw(`
623+
apiEndpoints:
624+
ip-10-0-0-1.ec2.internal:
625+
advertiseAddress: 10.0.0.1
626+
bindPort: 6443
627+
ip-10-0-0-2.ec2.internal:
628+
advertiseAddress: 10.0.0.2
629+
bindPort: 6443
630+
apiVersion: kubeadm.k8s.io/v1beta2
631+
kind: ClusterStatus
632+
`),
633+
wantClusterStatus: yaml.Raw(`
634+
apiEndpoints:
635+
ip-10-0-0-1.ec2.internal:
636+
advertiseAddress: 10.0.0.1
637+
bindPort: 6443
638+
apiVersion: kubeadm.k8s.io/v1beta2
639+
kind: ClusterStatus
640+
`),
624641
},
625642
{
626643
name: "no op if the api endpoint does not exists",
627644
apiEndpoint: "ip-10-0-0-2.ec2.internal",
628-
clusterStatusData: "apiEndpoints:\n" +
629-
" ip-10-0-0-1.ec2.internal:\n" +
630-
" advertiseAddress: 10.0.0.1\n" +
631-
" bindPort: 6443\n" +
632-
"apiVersion: kubeadm.k8s.io/v1beta2\n" +
633-
"kind: ClusterStatus\n",
634-
wantClusterStatus: "apiEndpoints:\n" +
635-
" ip-10-0-0-1.ec2.internal:\n" +
636-
" advertiseAddress: 10.0.0.1\n" +
637-
" bindPort: 6443\n" +
638-
"apiVersion: kubeadm.k8s.io/v1beta2\n" +
639-
"kind: ClusterStatus\n",
645+
clusterStatusData: yaml.Raw(`
646+
apiEndpoints:
647+
ip-10-0-0-1.ec2.internal:
648+
advertiseAddress: 10.0.0.1
649+
bindPort: 6443
650+
apiVersion: kubeadm.k8s.io/v1beta2
651+
kind: ClusterStatus
652+
`),
653+
wantClusterStatus: yaml.Raw(`
654+
apiEndpoints:
655+
ip-10-0-0-1.ec2.internal:
656+
advertiseAddress: 10.0.0.1
657+
bindPort: 6443
658+
apiVersion: kubeadm.k8s.io/v1beta2
659+
kind: ClusterStatus
660+
`),
640661
},
641662
}
642663
for _, tt := range tests {

0 commit comments

Comments
 (0)