Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Fix: Overriding user defined labels
Browse files Browse the repository at this point in the history
This PR fixes an issue where helm's postrenderer
used to override the user defined object labels instead
of merging them.

Signed-off-by: Varsha Prasad Narsing <[email protected]>
  • Loading branch information
varshaprasad96 authored and ncdc committed Aug 1, 2023
1 parent 19f2876 commit eac7920
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/controllers/bundledeployment/bundledeployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (p *postrenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, erro
if err != nil {
return nil, err
}
obj.SetLabels(p.labels)
obj.SetLabels(util.MergeMaps(obj.GetLabels(), p.labels))
b, err := obj.MarshalJSON()
if err != nil {
return nil, err
Expand Down
120 changes: 120 additions & 0 deletions internal/controllers/bundledeployment/bundledeployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package bundledeployment

import (
"bytes"
"encoding/json"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"helm.sh/helm/v3/pkg/postrender"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"
"github.com/operator-framework/rukpak/internal/util"
)

func TestBundleDeploymentController(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "BundleDeployment Controller Suite")
}

var _ = Describe("BundleDeployment", func() {
var _ = Describe("PostRenderer", func() {
var (
postren postrender.PostRenderer
pod corev1.Pod
inBuf bytes.Buffer
)

Context("should add labels defined in the postrenderer", func() {
BeforeEach(func() {
postren = &postrenderer{
labels: map[string]string{
util.CoreOwnerKindKey: rukpakv1alpha1.BundleDeploymentKind,
util.CoreOwnerNameKey: "test-owner",
},
}

pod = corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "testPod",
Labels: map[string]string{
"testKey": "testValue",
},
},
}
pod.SetGroupVersionKind(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Pod"})

})

It("when object has no labels", func() {
pod.SetLabels(nil)

By("encoding pod")
Expect(json.NewEncoder(&inBuf).Encode(pod)).NotTo(HaveOccurred())

By("verifying if postrender ran successfully")
outBuf, err := postren.Run(&inBuf)
Expect(err).NotTo(HaveOccurred())
Expect(outBuf.String()).NotTo(BeEmpty())

By("converting output string back to pod")
renderedPod := &corev1.Pod{}
Expect(json.Unmarshal(outBuf.Bytes(), renderedPod)).NotTo(HaveOccurred())
Expect(renderedPod).NotTo(BeNil())

labels := renderedPod.GetLabels()
Expect(len(labels)).To(BeEquivalentTo(2))
Expect(labels).Should(HaveKeyWithValue("core.rukpak.io/owner-kind", "BundleDeployment"))
Expect(labels).Should(HaveKeyWithValue("core.rukpak.io/owner-name", "test-owner"))
})

It("when object's label has an empty map", func() {
pod.SetLabels(map[string]string{})

By("encoding pod")
Expect(json.NewEncoder(&inBuf).Encode(pod)).NotTo(HaveOccurred())

By("verifying if postrender ran successfully")
outBuf, err := postren.Run(&inBuf)
Expect(err).NotTo(HaveOccurred())
Expect(outBuf.String()).NotTo(BeEmpty())

By("converting output string back to pod")
renderedPod := &corev1.Pod{}
Expect(json.Unmarshal(outBuf.Bytes(), renderedPod)).NotTo(HaveOccurred())
Expect(renderedPod).NotTo(BeNil())

labels := renderedPod.GetLabels()
Expect(len(labels)).To(BeEquivalentTo(2))
Expect(labels).Should(HaveKeyWithValue("core.rukpak.io/owner-kind", "BundleDeployment"))
Expect(labels).Should(HaveKeyWithValue("core.rukpak.io/owner-name", "test-owner"))
})

It("when object has custom labels", func() {
By("encoding pod")
Expect(json.NewEncoder(&inBuf).Encode(pod)).NotTo(HaveOccurred())

By("verifying if postrender ran successfully")
outBuf, err := postren.Run(&inBuf)
Expect(err).NotTo(HaveOccurred())
Expect(outBuf.String()).NotTo(BeEmpty())

By("converting output string back to pod")
renderedPod := &corev1.Pod{}
Expect(json.Unmarshal(outBuf.Bytes(), renderedPod)).NotTo(HaveOccurred())
Expect(renderedPod).NotTo(BeNil())

labels := renderedPod.GetLabels()
Expect(len(labels)).To(BeEquivalentTo(3))
Expect(labels).Should(HaveKeyWithValue("testKey", "testValue"))
Expect(labels).Should(HaveKeyWithValue("core.rukpak.io/owner-kind", "BundleDeployment"))
Expect(labels).Should(HaveKeyWithValue("core.rukpak.io/owner-name", "test-owner"))
})

})
})
})

0 comments on commit eac7920

Please sign in to comment.