From e701fe197e97090cd069b637c763352f733f4914 Mon Sep 17 00:00:00 2001 From: Aditya Naik Date: Fri, 8 Nov 2019 04:36:45 -0800 Subject: [PATCH] fixes bug so that configmap volumemounts are merged in deployment (#1093) --- pkg/utils/k8sutil.go | 10 +++++++++- pkg/utils/k8sutil_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/utils/k8sutil.go b/pkg/utils/k8sutil.go index a82bdf5a4..7ad6b3e89 100644 --- a/pkg/utils/k8sutil.go +++ b/pkg/utils/k8sutil.go @@ -373,7 +373,15 @@ func MergeDeployments(destinationDeployment *appsv1.Deployment, sourceDeployment // Initializing nil maps in deployment objects else github.com/imdario/mergo panics initializeEmptyMapsInDeployment(destinationDeployment) initializeEmptyMapsInDeployment(sourceDeployment) - return mergo.Merge(destinationDeployment, sourceDeployment) + err := mergo.Merge(destinationDeployment, sourceDeployment) + if err == nil && len(sourceDeployment.Spec.Template.Spec.Containers) > 0 && len(sourceDeployment.Spec.Template.Spec.Containers[0].VolumeMounts) > 0 { + // cannot use sourceDeployment.Spec.Template.Spec.Containers[0].VolumeMounts (type []"k8s.io/api/core/v1".VolumeMount) as type "k8s.io/api/core/v1".VolumeMount in append + // so iterate over and append + for _, volumeMount := range sourceDeployment.Spec.Template.Spec.Containers[0].VolumeMounts { + destinationDeployment.Spec.Template.Spec.Containers[0].VolumeMounts = append(destinationDeployment.Spec.Template.Spec.Containers[0].VolumeMounts, volumeMount) + } + } + return err } // FunctionObjAddFinalizer add specified finalizer string to function object diff --git a/pkg/utils/k8sutil_test.go b/pkg/utils/k8sutil_test.go index 80d89c71d..d0585541b 100644 --- a/pkg/utils/k8sutil_test.go +++ b/pkg/utils/k8sutil_test.go @@ -9,6 +9,7 @@ import ( appsv1 "k8s.io/api/apps/v1" v2beta1 "k8s.io/api/autoscaling/v2beta1" + corev1 "k8s.io/api/core/v1" extensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" fakeextensionsapi "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -126,6 +127,24 @@ func TestInitializeEmptyMapsInDeployment(t *testing.T) { func TestMergeDeployments(t *testing.T) { var replicas int32 replicas = 10 + volumeMount := corev1.VolumeMount{ + Name: "foo", + MountPath: "/bar", + } + container := corev1.Container{ + VolumeMounts: []corev1.VolumeMount{ + volumeMount, + }, + } + podSpec := corev1.PodSpec{ + Containers: []corev1.Container{ + container, + }, + } + template := corev1.PodTemplateSpec{ + Spec: podSpec, + } + destinationDeployment := appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{ @@ -142,6 +161,7 @@ func TestMergeDeployments(t *testing.T) { }, Spec: appsv1.DeploymentSpec{ Replicas: &replicas, + Template: template, }, } @@ -150,6 +170,8 @@ func TestMergeDeployments(t *testing.T) { "foo1-deploy": "bar", "foo2-deploy": "bar", } + expectedVolumneMounts := len(destinationDeployment.Spec.Template.Spec.Containers[0].VolumeMounts) + mergedVolumneMounts := len(sourceDeployment.Spec.Template.Spec.Containers[0].VolumeMounts) for i := range expectedAnnotations { if destinationDeployment.ObjectMeta.Annotations[i] != expectedAnnotations[i] { t.Fatalf("Expecting annotation %s but received %s", destinationDeployment.ObjectMeta.Annotations[i], expectedAnnotations[i]) @@ -158,6 +180,9 @@ func TestMergeDeployments(t *testing.T) { if *destinationDeployment.Spec.Replicas != replicas { t.Fatalf("Expecting replicas as 10 but received %v", *destinationDeployment.Spec.Replicas) } + if mergedVolumneMounts != expectedVolumneMounts { + t.Fatalf("Expecting %v volumeMounts but received %v", expectedVolumneMounts, mergedVolumneMounts) + } }