diff --git a/controlplane/kubeadm/internal/workload_cluster_coredns_test.go b/controlplane/kubeadm/internal/workload_cluster_coredns_test.go index 4d71a87f2ab8..9323c3e72dde 100644 --- a/controlplane/kubeadm/internal/workload_cluster_coredns_test.go +++ b/controlplane/kubeadm/internal/workload_cluster_coredns_test.go @@ -28,6 +28,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4" controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4" + "sigs.k8s.io/cluster-api/util/yaml" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" ) @@ -111,13 +112,14 @@ func TestUpdateCoreDNS(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - "ClusterConfiguration": `apiServer: -apiVersion: kubeadm.k8s.io/v1beta2 -dns: - type: CoreDNS -imageRepository: k8s.gcr.io -kind: ClusterConfiguration -`, + "ClusterConfiguration": yaml.Raw(` + apiServer: + apiVersion: kubeadm.k8s.io/v1beta2 + dns: + type: CoreDNS + imageRepository: k8s.gcr.io + kind: ClusterConfiguration + `), }, } @@ -886,24 +888,28 @@ func TestUpdateCoreDNSImageInfoInKubeadmConfigMap(t *testing.T) { }{ { name: "it should set the DNS image config", - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + `), newDNS: bootstrapv1.DNS{ ImageMeta: bootstrapv1.ImageMeta{ ImageRepository: "example.com/k8s", ImageTag: "v1.2.3", }, }, - wantClusterConfiguration: "apiServer: {}\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "controllerManager: {}\n" + - "dns:\n" + - " imageRepository: example.com/k8s\n" + - " imageTag: v1.2.3\n" + - "etcd: {}\n" + - "kind: ClusterConfiguration\n" + - "networking: {}\n" + - "scheduler: {}\n", + wantClusterConfiguration: yaml.Raw(` + apiServer: {} + apiVersion: kubeadm.k8s.io/v1beta2 + controllerManager: {} + dns: + imageRepository: example.com/k8s + imageTag: v1.2.3 + etcd: {} + kind: ClusterConfiguration + networking: {} + scheduler: {} + `), }, } for _, tt := range tests { diff --git a/controlplane/kubeadm/internal/workload_cluster_etcd_test.go b/controlplane/kubeadm/internal/workload_cluster_etcd_test.go index 1b980e36b629..8c7f6c18bd38 100644 --- a/controlplane/kubeadm/internal/workload_cluster_etcd_test.go +++ b/controlplane/kubeadm/internal/workload_cluster_etcd_test.go @@ -31,6 +31,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" "sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd" fake2 "sigs.k8s.io/cluster-api/controlplane/kubeadm/internal/etcd/fake" + "sigs.k8s.io/cluster-api/util/yaml" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" ) @@ -45,36 +46,44 @@ func TestUpdateEtcdVersionInKubeadmConfigMap(t *testing.T) { }{ { name: "it should set etcd version when local etcd", - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n" + - "etcd:\n" + - " local: {}\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + etcd: + local: {} + `), newImageRepository: "example.com/k8s", newImageTag: "v1.6.0", - wantClusterConfiguration: "apiServer: {}\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "controllerManager: {}\n" + - "dns: {}\n" + - "etcd:\n" + - " local:\n" + - " imageRepository: example.com/k8s\n" + - " imageTag: v1.6.0\n" + - "kind: ClusterConfiguration\n" + - "networking: {}\n" + - "scheduler: {}\n", + wantClusterConfiguration: yaml.Raw(` + apiServer: {} + apiVersion: kubeadm.k8s.io/v1beta2 + controllerManager: {} + dns: {} + etcd: + local: + imageRepository: example.com/k8s + imageTag: v1.6.0 + kind: ClusterConfiguration + networking: {} + scheduler: {} + `), }, { name: "no op when external etcd", - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n" + - "etcd:\n" + - " external: {}\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + etcd: + external: {} + `), newImageRepository: "example.com/k8s", newImageTag: "v1.6.0", - wantClusterConfiguration: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n" + - "etcd:\n" + - " external: {}\n", + wantClusterConfiguration: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + etcd: + external: {} + `), }, } @@ -442,19 +451,21 @@ func TestReconcileEtcdMembers(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterStatusKey: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - " ip-10-0-0-2.ec2.internal:\n" + - " advertiseAddress: 10.0.0.2\n" + - " bindPort: 6443\n" + - " someFieldThatIsAddedInTheFuture: bar\n" + - " ip-10-0-0-3.ec2.internal:\n" + - " advertiseAddress: 10.0.0.3\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusKey: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + ip-10-0-0-2.ec2.internal: + advertiseAddress: 10.0.0.2 + bindPort: 6443 + someFieldThatIsAddedInTheFuture: bar + ip-10-0-0-3.ec2.internal: + advertiseAddress: 10.0.0.3 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, } kubeadmConfigWithoutClusterStatus := kubeadmConfig.DeepCopy() @@ -516,16 +527,18 @@ func TestReconcileEtcdMembers(t *testing.T) { client.ObjectKey{Name: kubeadmConfigKey, Namespace: metav1.NamespaceSystem}, &actualConfig, )).To(Succeed()) - - g.Expect(actualConfig.Data[clusterStatusKey]).To(Equal("apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - " ip-10-0-0-2.ec2.internal:\n" + - " advertiseAddress: 10.0.0.2\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n")) + expectedOutput := yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + ip-10-0-0-2.ec2.internal: + advertiseAddress: 10.0.0.2 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `) + g.Expect(actualConfig.Data[clusterStatusKey]).To(Equal(expectedOutput)) }, }, { @@ -606,37 +619,45 @@ func TestRemoveNodeFromKubeadmConfigMap(t *testing.T) { { name: "removes the api endpoint", apiEndpoint: "ip-10-0-0-2.ec2.internal", - clusterStatusData: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - " ip-10-0-0-2.ec2.internal:\n" + - " advertiseAddress: 10.0.0.2\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", - wantClusterStatus: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusData: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + ip-10-0-0-2.ec2.internal: + advertiseAddress: 10.0.0.2 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), + wantClusterStatus: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, { name: "no op if the api endpoint does not exists", apiEndpoint: "ip-10-0-0-2.ec2.internal", - clusterStatusData: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", - wantClusterStatus: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusData: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), + wantClusterStatus: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, } for _, tt := range tests { diff --git a/controlplane/kubeadm/internal/workload_cluster_test.go b/controlplane/kubeadm/internal/workload_cluster_test.go index 680aefd06479..56423d16e625 100644 --- a/controlplane/kubeadm/internal/workload_cluster_test.go +++ b/controlplane/kubeadm/internal/workload_cluster_test.go @@ -32,6 +32,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4" "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4" + "sigs.k8s.io/cluster-api/util/yaml" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" ) @@ -175,15 +176,17 @@ func TestRemoveMachineFromKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterStatusKey: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - " ip-10-0-0-2.ec2.internal:\n" + - " advertiseAddress: 10.0.0.2\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusKey: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + ip-10-0-0-2.ec2.internal: + advertiseAddress: 10.0.0.2 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, BinaryData: map[string][]byte{ "": nil, @@ -238,12 +241,14 @@ func TestRemoveMachineFromKubeadmConfigMap(t *testing.T) { machine: machine, objs: []client.Object{kubeadmConfig}, expectErr: false, - expectedEndpoints: "apiEndpoints:\n" + - " ip-10-0-0-2.ec2.internal:\n" + - " advertiseAddress: 10.0.0.2\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + expectedEndpoints: yaml.Raw(` + apiEndpoints: + ip-10-0-0-2.ec2.internal: + advertiseAddress: 10.0.0.2 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, { name: "no op for Kubernetes version >= 1.22.0", @@ -298,8 +303,10 @@ func TestUpdateKubeletConfigMap(t *testing.T) { ResourceVersion: "some-resource-version", }, Data: map[string]string{ - kubeletConfigKey: "apiVersion: kubelet.config.k8s.io/v1beta1\n" + - "kind: KubeletConfiguration\n", + kubeletConfigKey: yaml.Raw(` + apiVersion: kubelet.config.k8s.io/v1beta1 + kind: KubeletConfiguration + `), }, }}, expectErr: false, @@ -315,8 +322,9 @@ func TestUpdateKubeletConfigMap(t *testing.T) { ResourceVersion: "some-resource-version", }, Data: map[string]string{ - kubeletConfigKey: "apiVersion: kubelet.config.k8s.io/v1beta1\n" + - "kind: KubeletConfiguration\n", + kubeletConfigKey: yaml.Raw(` + apiVersion: kubelet.config.k8s.io/v1beta1 + kind: KubeletConfiguration`), }, }}, expectErr: false, @@ -332,9 +340,10 @@ func TestUpdateKubeletConfigMap(t *testing.T) { ResourceVersion: "some-resource-version", }, Data: map[string]string{ - kubeletConfigKey: "apiVersion: kubelet.config.k8s.io/v1beta1\n" + - "kind: KubeletConfiguration\n" + - "cgroupDriver: foo\n", + kubeletConfigKey: yaml.Raw(` + apiVersion: kubelet.config.k8s.io/v1beta1 + kind: KubeletConfiguration + cgroupDriver: foo`), }, }}, expectErr: false, @@ -424,9 +433,11 @@ func TestUpdateUpdateClusterConfigurationInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterConfigurationKey: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n" + - "kubernetesVersion: v1.16.1\n", + clusterConfigurationKey: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + kubernetesVersion: v1.16.1 + `), }, }}, mutator: func(c *bootstrapv1.ClusterConfiguration) {}, @@ -436,9 +447,11 @@ func TestUpdateUpdateClusterConfigurationInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterConfigurationKey: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n" + - "kubernetesVersion: v1.16.1\n", + clusterConfigurationKey: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + kubernetesVersion: v1.16.1 + `), }, }, }, @@ -451,9 +464,11 @@ func TestUpdateUpdateClusterConfigurationInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterConfigurationKey: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n" + - "kubernetesVersion: v1.16.1\n", + clusterConfigurationKey: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + kubernetesVersion: v1.16.1 + `), }, }}, mutator: func(c *bootstrapv1.ClusterConfiguration) { @@ -465,15 +480,17 @@ func TestUpdateUpdateClusterConfigurationInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterConfigurationKey: "apiServer: {}\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "controllerManager: {}\n" + - "dns: {}\n" + - "etcd: {}\n" + - "kind: ClusterConfiguration\n" + - "kubernetesVersion: v1.17.2\n" + - "networking: {}\n" + - "scheduler: {}\n", + clusterConfigurationKey: yaml.Raw(` + apiServer: {} + apiVersion: kubeadm.k8s.io/v1beta2 + controllerManager: {} + dns: {} + etcd: {} + kind: ClusterConfiguration + kubernetesVersion: v1.17.2 + networking: {} + scheduler: {} + `), }, }, }, @@ -486,9 +503,11 @@ func TestUpdateUpdateClusterConfigurationInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterConfigurationKey: "apiVersion: kubeadm.k8s.io/v1beta1\n" + - "kind: ClusterConfiguration\n" + - "kubernetesVersion: v1.16.1\n", + clusterConfigurationKey: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta1 + kind: ClusterConfiguration + kubernetesVersion: v1.16.1 + `), }, }}, mutator: func(c *bootstrapv1.ClusterConfiguration) { @@ -500,15 +519,17 @@ func TestUpdateUpdateClusterConfigurationInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterConfigurationKey: "apiServer: {}\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "controllerManager: {}\n" + - "dns: {}\n" + - "etcd: {}\n" + - "kind: ClusterConfiguration\n" + - "kubernetesVersion: v1.17.2\n" + - "networking: {}\n" + - "scheduler: {}\n", + clusterConfigurationKey: yaml.Raw(` + apiServer: {} + apiVersion: kubeadm.k8s.io/v1beta2 + controllerManager: {} + dns: {} + etcd: {} + kind: ClusterConfiguration + kubernetesVersion: v1.17.2 + networking: {} + scheduler: {} + `), }, }, }, @@ -590,12 +611,14 @@ func TestUpdateUpdateClusterStatusInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterStatusKey: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusKey: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, }}, mutator: func(status *bootstrapv1.ClusterStatus) {}, @@ -605,12 +628,14 @@ func TestUpdateUpdateClusterStatusInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterStatusKey: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusKey: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, }, }, @@ -623,12 +648,14 @@ func TestUpdateUpdateClusterStatusInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterStatusKey: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusKey: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, }}, mutator: func(status *bootstrapv1.ClusterStatus) { @@ -640,13 +667,15 @@ func TestUpdateUpdateClusterStatusInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterStatusKey: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - " ip-10-0-0-2.ec2.internal: {}\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusKey: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + ip-10-0-0-2.ec2.internal: {} + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, }, }, @@ -659,12 +688,14 @@ func TestUpdateUpdateClusterStatusInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterStatusKey: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - "apiVersion: kubeadm.k8s.io/v1beta1\n" + - "kind: ClusterStatus\n", + clusterStatusKey: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + apiVersion: kubeadm.k8s.io/v1beta1 + kind: ClusterStatus + `), }, }}, mutator: func(status *bootstrapv1.ClusterStatus) { @@ -676,13 +707,15 @@ func TestUpdateUpdateClusterStatusInKubeadmConfigMap(t *testing.T) { Namespace: metav1.NamespaceSystem, }, Data: map[string]string{ - clusterStatusKey: "apiEndpoints:\n" + - " ip-10-0-0-1.ec2.internal:\n" + - " advertiseAddress: 10.0.0.1\n" + - " bindPort: 6443\n" + - " ip-10-0-0-2.ec2.internal: {}\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterStatus\n", + clusterStatusKey: yaml.Raw(` + apiEndpoints: + ip-10-0-0-1.ec2.internal: + advertiseAddress: 10.0.0.1 + bindPort: 6443 + ip-10-0-0-2.ec2.internal: {} + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterStatus + `), }, }, }, @@ -723,9 +756,10 @@ func TestUpdateKubernetesVersionInKubeadmConfigMap(t *testing.T) { { name: "updates the config map and changes the kubeadm API version", version: semver.MustParse("1.17.2"), - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta1\n" + - "kind: ClusterConfiguration\n" + - "kubernetesVersion: v1.16.1\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta1 + kind: ClusterConfiguration + kubernetesVersion: v1.16.1`), }, } @@ -768,16 +802,18 @@ func TestUpdateImageRepositoryInKubeadmConfigMap(t *testing.T) { }{ { name: "it should set the image repository", - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration`), newImageRepository: "example.com/k8s", wantImageRepository: "example.com/k8s", }, { name: "it should preserve the existing image repository if then new value is empty", - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n" + - "imageRepository: foo.bar/baz.io\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + imageRepository: foo.bar/baz.io`), newImageRepository: "", wantImageRepository: "foo.bar/baz.io", }, @@ -822,8 +858,10 @@ func TestUpdateApiServerInKubeadmConfigMap(t *testing.T) { }{ { name: "it should set the api server config", - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + `), newAPIServer: bootstrapv1.APIServer{ ControlPlaneComponent: bootstrapv1.ControlPlaneComponent{ ExtraArgs: map[string]string{ @@ -839,21 +877,23 @@ func TestUpdateApiServerInKubeadmConfigMap(t *testing.T) { }, }, }, - wantClusterConfiguration: "apiServer:\n" + - " extraArgs:\n" + - " bar: baz\n" + - " someKey: someVal\n" + - " extraVolumes:\n" + - " - hostPath: /bar/baz\n" + - " mountPath: /foo/bar\n" + - " name: mount2\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "controllerManager: {}\n" + - "dns: {}\n" + - "etcd: {}\n" + - "kind: ClusterConfiguration\n" + - "networking: {}\n" + - "scheduler: {}\n", + wantClusterConfiguration: yaml.Raw(` + apiServer: + extraArgs: + bar: baz + someKey: someVal + extraVolumes: + - hostPath: /bar/baz + mountPath: /foo/bar + name: mount2 + apiVersion: kubeadm.k8s.io/v1beta2 + controllerManager: {} + dns: {} + etcd: {} + kind: ClusterConfiguration + networking: {} + scheduler: {} + `), }, } @@ -896,8 +936,10 @@ func TestUpdateControllerManagerInKubeadmConfigMap(t *testing.T) { }{ { name: "it should set the controller manager config", - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + `), newControllerManager: bootstrapv1.ControlPlaneComponent{ ExtraArgs: map[string]string{ "bar": "baz", @@ -911,21 +953,23 @@ func TestUpdateControllerManagerInKubeadmConfigMap(t *testing.T) { }, }, }, - wantClusterConfiguration: "apiServer: {}\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "controllerManager:\n" + - " extraArgs:\n" + - " bar: baz\n" + - " someKey: someVal\n" + - " extraVolumes:\n" + - " - hostPath: /bar/baz\n" + - " mountPath: /foo/bar\n" + - " name: mount2\n" + - "dns: {}\n" + - "etcd: {}\n" + - "kind: ClusterConfiguration\n" + - "networking: {}\n" + - "scheduler: {}\n", + wantClusterConfiguration: yaml.Raw(` + apiServer: {} + apiVersion: kubeadm.k8s.io/v1beta2 + controllerManager: + extraArgs: + bar: baz + someKey: someVal + extraVolumes: + - hostPath: /bar/baz + mountPath: /foo/bar + name: mount2 + dns: {} + etcd: {} + kind: ClusterConfiguration + networking: {} + scheduler: {} + `), }, } @@ -968,8 +1012,10 @@ func TestUpdateSchedulerInKubeadmConfigMap(t *testing.T) { }{ { name: "it should set the scheduler config", - clusterConfigurationData: "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "kind: ClusterConfiguration\n", + clusterConfigurationData: yaml.Raw(` + apiVersion: kubeadm.k8s.io/v1beta2 + kind: ClusterConfiguration + `), newScheduler: bootstrapv1.ControlPlaneComponent{ ExtraArgs: map[string]string{ "bar": "baz", @@ -983,21 +1029,23 @@ func TestUpdateSchedulerInKubeadmConfigMap(t *testing.T) { }, }, }, - wantClusterConfiguration: "apiServer: {}\n" + - "apiVersion: kubeadm.k8s.io/v1beta2\n" + - "controllerManager: {}\n" + - "dns: {}\n" + - "etcd: {}\n" + - "kind: ClusterConfiguration\n" + - "networking: {}\n" + - "scheduler:\n" + - " extraArgs:\n" + - " bar: baz\n" + - " someKey: someVal\n" + - " extraVolumes:\n" + - " - hostPath: /bar/baz\n" + - " mountPath: /foo/bar\n" + - " name: mount2\n", + wantClusterConfiguration: yaml.Raw(` + apiServer: {} + apiVersion: kubeadm.k8s.io/v1beta2 + controllerManager: {} + dns: {} + etcd: {} + kind: ClusterConfiguration + networking: {} + scheduler: + extraArgs: + bar: baz + someKey: someVal + extraVolumes: + - hostPath: /bar/baz + mountPath: /foo/bar + name: mount2 + `), }, } for _, tt := range tests { diff --git a/test/go.sum b/test/go.sum index 234707929952..0c6ea71380f6 100644 --- a/test/go.sum +++ b/test/go.sum @@ -42,6 +42,7 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E= +github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= diff --git a/util/yaml/yaml.go b/util/yaml/yaml.go index af2b5a3ffa3b..8ee41b94423e 100644 --- a/util/yaml/yaml.go +++ b/util/yaml/yaml.go @@ -22,7 +22,9 @@ import ( "bytes" "io" "os" + "strings" + "github.com/MakeNowJust/heredoc" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -268,3 +270,9 @@ func FromUnstructured(objs []unstructured.Unstructured) ([]byte, error) { return JoinYaml(ret...), nil } + +// Raw returns un-indented yaml string; it also remove the first empty line, if any. +// While writing yaml, always use space instead of tabs for indentation. +func Raw(raw string) string { + return strings.TrimPrefix(heredoc.Doc(raw), "\n") +} diff --git a/util/yaml/yaml_test.go b/util/yaml/yaml_test.go index d20c2a4889b7..ad9bffdc90fc 100644 --- a/util/yaml/yaml_test.go +++ b/util/yaml/yaml_test.go @@ -466,3 +466,17 @@ func TestFromUnstructured(t *testing.T) { g.Expect(err).ToNot(HaveOccurred()) g.Expect(string(rawyaml)).To(Equal(string(convertedyaml))) } + +func TestRaw(t *testing.T) { + g := NewWithT(t) + + input := ` + apiVersion:v1 + kind:newKind + spec: + param: abc + ` + output := "apiVersion:v1\nkind:newKind\nspec:\n\tparam: abc\n" + result := Raw(input) + g.Expect(result).To(Equal(output)) +}