Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into trait_name
Browse files Browse the repository at this point in the history
Signed-off-by: zhuhuijun <[email protected]>
  • Loading branch information
Ghostbaby committed Jul 9, 2020
2 parents 8bd01be + b79367b commit 46dd69b
Show file tree
Hide file tree
Showing 2 changed files with 103 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 @@ -120,6 +120,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 @@ -135,6 +138,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 @@ -272,6 +278,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
74 changes: 74 additions & 0 deletions pkg/controller/v1alpha2/applicationconfiguration/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package applicationconfiguration
import (
"context"
"encoding/json"
"reflect"
"testing"

"github.com/crossplane/crossplane-runtime/pkg/test"
Expand Down Expand Up @@ -909,6 +910,7 @@ func TestGetComponent(t *testing.T) {
}, c)
}

<<<<<<< HEAD
var scheme = runtime.NewScheme()

func TestRenderTraitName(t *testing.T) {
Expand Down Expand Up @@ -1016,4 +1018,76 @@ func TestRenderTraitName(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, traitName, "component-trait-11111111")

=======
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)
}
})
>>>>>>> upstream/master
}

0 comments on commit 46dd69b

Please sign in to comment.