Skip to content

Commit

Permalink
Merge pull request #513 from fluxcd/set-common-metadata
Browse files Browse the repository at this point in the history
ssa: Set common labels and annotations
  • Loading branch information
stefanprodan authored Mar 21, 2023
2 parents 5da79e1 + 1ac829d commit eb40b22
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions ssa/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/google/go-cmp v0.5.9
github.com/onsi/gomega v1.24.1
k8s.io/api v0.26.2
k8s.io/apimachinery v0.26.2
sigs.k8s.io/cli-utils v0.34.0
Expand Down
1 change: 1 addition & 0 deletions ssa/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWb
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/onsi/ginkgo/v2 v2.6.0 h1:9t9b9vRUbFq3C4qKFCGkVuq/fIHji802N1nrtkh1mNc=
github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E=
github.com/onsi/gomega v1.24.1/go.mod h1:3AOiACssS3/MajrniINInwbfOOtfZvplPzuRSmvt1jM=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
32 changes: 32 additions & 0 deletions ssa/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ import (

const fmtSeparator = "/"

// SetCommonMetadata adds the specified labels and annotations to all objects.
// Existing keys will have their values overridden.
func SetCommonMetadata(objects []*unstructured.Unstructured, labels map[string]string, annotations map[string]string) {
for _, object := range objects {
lbs := object.GetLabels()
if lbs == nil {
lbs = make(map[string]string)
}

for k, v := range labels {
lbs[k] = v
}

if len(lbs) > 0 {
object.SetLabels(lbs)
}

ans := object.GetAnnotations()
if ans == nil {
ans = make(map[string]string)
}

for k, v := range annotations {
ans[k] = v
}

if len(ans) > 0 {
object.SetAnnotations(ans)
}
}
}

// FmtObjMetadata returns the object ID in the format <kind>/<namespace>/<name>.
func FmtObjMetadata(obj object.ObjMetadata) string {
var builder strings.Builder
Expand Down
78 changes: 78 additions & 0 deletions ssa/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
)

func TestCmpMaskData(t *testing.T) {
Expand Down Expand Up @@ -161,3 +162,80 @@ stringData:
})
}
}

func TestSetCommonMetadata(t *testing.T) {
testCases := []struct {
name string
resources string
labels map[string]string
annotations map[string]string
}{
{
name: "adds metadata",
resources: `
---
apiVersion: v1
kind: Secret
metadata:
name: test
namespace: default
stringData:
key: "private-key"
`,
labels: map[string]string{
"test1": "lb1",
"test2": "lb2",
},
annotations: map[string]string{
"test1": "a1",
"test2": "a2",
},
},
{
name: "overrides metadata",
resources: `
---
apiVersion: v1
kind: Secret
metadata:
name: test
namespace: default
labels:
test1: over
annotations:
test2: over
stringData:
key: "private-key"
`,
labels: map[string]string{
"test1": "lb1",
},
annotations: map[string]string{
"test1": "an1",
"test2": "an2",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)

objects, err := ReadObjects(strings.NewReader(tc.resources))
if err != nil {
t.Errorf("unexpected error: %v", err)
}

SetCommonMetadata(objects, tc.labels, tc.annotations)

for _, object := range objects {
for k, v := range tc.labels {
g.Expect(object.GetLabels()).To(HaveKeyWithValue(k, v))
}
for k, v := range tc.annotations {
g.Expect(object.GetAnnotations()).To(HaveKeyWithValue(k, v))
}
}
})
}
}

0 comments on commit eb40b22

Please sign in to comment.