Skip to content

Commit

Permalink
Merge pull request crossplane#86 from allenhaozi/pass-labels-annotation
Browse files Browse the repository at this point in the history
pass through labels and annotation from app-config to workload  or trait
  • Loading branch information
ryanzhang-oss authored Jul 8, 2020
2 parents d9a7a18 + e942d85 commit b79367b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/controller/v1alpha2/applicationconfiguration/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func (r *components) renderComponent(ctx context.Context, acc v1alpha2.Applicati
return nil, errors.Wrapf(err, errFmtRenderWorkload, acc.ComponentName)
}

// pass through labels and annotation from app-config to workload
r.passThroughObjMeta(ac.ObjectMeta, w)

ref := metav1.NewControllerRef(ac, v1alpha2.ApplicationConfigurationGroupVersionKind)
w.SetOwnerReferences([]metav1.OwnerReference{*ref})
w.SetNamespace(ac.GetNamespace())
Expand All @@ -134,6 +137,9 @@ func (r *components) renderComponent(ctx context.Context, acc v1alpha2.Applicati
if t == nil { // Depends on other resources. Not creating it now.
continue
}

// pass through labels and annotation from app-config to trait
r.passThroughObjMeta(ac.ObjectMeta, t)
traits = append(traits, *t)
traitDefs = append(traitDefs, *traitDef)
}
Expand Down Expand Up @@ -266,6 +272,29 @@ func (r *components) getComponent(ctx context.Context, acc v1alpha2.ApplicationC
return c, revisionName, nil
}

// pass through labels and annotation from app-config to workload or trait
func (r *components) passThroughObjMeta(oMeta metav1.ObjectMeta, u *unstructured.Unstructured) {
mergeMap := func(src, dst map[string]string) map[string]string {
if len(src) == 0 {
return dst
}
// make sure dst is initialized
if dst == nil {
dst = map[string]string{}
}
for k, v := range src {
if _, exist := dst[k]; !exist {
dst[k] = v
}
}
return dst
}
// pass app-config labels
u.SetLabels(mergeMap(oMeta.GetLabels(), u.GetLabels()))
// pass app-config annotation
u.SetAnnotations(mergeMap(oMeta.GetAnnotations(), u.GetAnnotations()))
}

// A ResourceRenderer renders a Kubernetes-compliant YAML resource into an
// Unstructured object, optionally setting the supplied parameters.
type ResourceRenderer interface {
Expand Down
73 changes: 73 additions & 0 deletions pkg/controller/v1alpha2/applicationconfiguration/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package applicationconfiguration

import (
"context"
"reflect"
"testing"

"github.com/crossplane/crossplane-runtime/pkg/test"
Expand Down Expand Up @@ -905,3 +906,75 @@ func TestGetComponent(t *testing.T) {
},
}, c)
}

func TestPassThroughObjMeta(t *testing.T) {
c := components{}
ac := &v1alpha2.ApplicationConfiguration{}

labels := map[string]string{
"core.oam.dev/ns": "oam-system",
"core.oam.dev/controller": "oam-kubernetes-runtime",
}

annotation := map[string]string{
"key1": "value1",
"key2": "value2",
}

ac.SetLabels(labels)
ac.SetAnnotations(annotation)

t.Run("workload and trait have no labels and annotation", func(t *testing.T) {
var u unstructured.Unstructured
c.passThroughObjMeta(ac.ObjectMeta, &u)
got := u.GetLabels()
want := labels
if !reflect.DeepEqual(want, got) {
t.Errorf("labels want:%v,got:%v", want, got)
}
gotAnnotation := u.GetAnnotations()
wantAnnotation := annotation
if !reflect.DeepEqual(want, got) {
t.Errorf("annotation want:%v,got:%v", wantAnnotation, gotAnnotation)
}
})

t.Run("workload and trait contains overlapping keys", func(t *testing.T) {
var u unstructured.Unstructured
existAnnotation := map[string]string{
"key1": "exist value1",
"key3": "value3",
}
existLabels := map[string]string{
"core.oam.dev/ns": "kube-system",
"core.oam.dev/kube-native": "deployment",
}
u.SetLabels(existLabels)
u.SetAnnotations(existAnnotation)

c.passThroughObjMeta(ac.ObjectMeta, &u)

gotAnnotation := u.GetAnnotations()
wantAnnotation := map[string]string{
"key1": "exist value1",
"key2": "value2",
"key3": "value3",
}
if !reflect.DeepEqual(gotAnnotation, wantAnnotation) {
t.Errorf("annotation got:%v,want:%v", gotAnnotation, wantAnnotation)
}

gotLabels := u.GetLabels()
wantLabels := map[string]string{
"core.oam.dev/ns": "kube-system",
"core.oam.dev/kube-native": "deployment",
"core.oam.dev/controller": "oam-kubernetes-runtime",
}
if !reflect.DeepEqual(gotLabels, wantLabels) {
t.Errorf("labels got:%v,want:%v", gotLabels, wantLabels)
}
if !reflect.DeepEqual(gotLabels, wantLabels) {
t.Errorf("labels got:%v,want:%v", gotLabels, wantLabels)
}
})
}

0 comments on commit b79367b

Please sign in to comment.