Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
fixes bug so that configmap volumemounts are merged in deployment (#1093
Browse files Browse the repository at this point in the history
)
  • Loading branch information
anaik-zam authored and Andres Martinez Gotor committed Nov 8, 2019
1 parent 673b738 commit e701fe1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/utils/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions pkg/utils/k8sutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -142,6 +161,7 @@ func TestMergeDeployments(t *testing.T) {
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Template: template,
},
}

Expand All @@ -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])
Expand All @@ -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)
}

}

Expand Down

0 comments on commit e701fe1

Please sign in to comment.