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 committed Aug 1, 2023
1 parent 802fe8a commit 3aa0dc6
Show file tree
Hide file tree
Showing 2 changed files with 96 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
95 changes: 95 additions & 0 deletions internal/controllers/bundledeployment/bundledeployment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package bundledeployment

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

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rukpakv1alpha1 "github.com/operator-framework/rukpak/api/v1alpha1"

Check failure on line 10 in internal/controllers/bundledeployment/bundledeployment_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/operator-framework/rukpak (goimports)
"github.com/operator-framework/rukpak/internal/util"
"helm.sh/helm/v3/pkg/postrender"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Check failure on line 14 in internal/controllers/bundledeployment/bundledeployment_test.go

View workflow job for this annotation

GitHub Actions / lint

import "k8s.io/apimachinery/pkg/apis/meta/v1" imported as "v1" but must be "metav1" according to config (importas)
"k8s.io/apimachinery/pkg/runtime/schema"
)

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
)

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

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

})

It("should preserve existing labels present in the pod", 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"))
})

It("should add default labels to the pod", 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"))
})

})
})

0 comments on commit 3aa0dc6

Please sign in to comment.