Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

add labels for workload to record component info #189

Merged
merged 3 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/controller/v1alpha2/applicationconfiguration/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam/util"
)

Expand Down Expand Up @@ -130,6 +131,13 @@ func (r *components) renderComponent(ctx context.Context, acc v1alpha2.Applicati
return nil, errors.Wrapf(err, errFmtRenderWorkload, acc.ComponentName)
}

compInfoLabels := map[string]string{
oam.LabelAppName: ac.Name,
oam.LabelAppComponent: acc.ComponentName,
oam.LabelAppComponentRevision: componentRevisionName,
}
util.AddLabels(w, compInfoLabels)

// pass through labels and annotation from app-config to workload
util.PassLabelAndAnnotation(ac, w)

Expand Down
16 changes: 16 additions & 0 deletions pkg/controller/v1alpha2/applicationconfiguration/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

"github.com/crossplane/oam-kubernetes-runtime/apis/core"
"github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam/util"
)

Expand Down Expand Up @@ -194,6 +195,11 @@ func TestRenderComponents(t *testing.T) {
w.SetNamespace(namespace)
w.SetName(workloadName)
w.SetOwnerReferences([]metav1.OwnerReference{*ref})
w.SetLabels(map[string]string{
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: "",
})
return w
}(),
Traits: []*Trait{
Expand Down Expand Up @@ -257,6 +263,11 @@ func TestRenderComponents(t *testing.T) {
w.SetNamespace(namespace)
w.SetName(componentName)
w.SetOwnerReferences([]metav1.OwnerReference{*ref})
w.SetLabels(map[string]string{
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: revisionName,
})
return w
}(),
Traits: []*Trait{
Expand Down Expand Up @@ -311,6 +322,11 @@ func TestRenderComponents(t *testing.T) {
w.SetNamespace(namespace)
w.SetName(revisionName2)
w.SetOwnerReferences([]metav1.OwnerReference{*ref})
w.SetLabels(map[string]string{
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: revisionName2,
})
return w
}(),
Traits: []*Trait{
Expand Down
28 changes: 28 additions & 0 deletions pkg/oam/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright 2019 The Crossplane Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package oam

// Label key strings.
// AppConfig controller will add these labels into workloads.
const (
// LabelAppName records the name of AppConfig
LabelAppName = "app.oam.dev/name"
// LabelAppComponent records the name of Component
LabelAppComponent = "app.oam.dev/component"
// LabelAppComponentRevision records the revision name of Component
LabelAppComponentRevision = "app.oam.dev/revision"
)
36 changes: 21 additions & 15 deletions pkg/oam/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,6 @@ type labelAnnotationObject interface {

// PassLabelAndAnnotation passes through labels and annotation objectMeta from the parent to the child object
func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject) {
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
childObj.SetLabels(mergeMap(parentObj.GetLabels(), childObj.GetLabels()))
// pass app-config annotation
Expand Down Expand Up @@ -356,3 +341,24 @@ func UnpackRevisionData(rev *appsv1.ControllerRevision) (*v1alpha2.Component, er
err = json.Unmarshal(rev.Data.Raw, &comp)
return &comp, err
}

// AddLabels will merge labels with existing labels
func AddLabels(o *unstructured.Unstructured, labels map[string]string) {
o.SetLabels(mergeMap(o.GetLabels(), labels))
}

func mergeMap(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
}
48 changes: 48 additions & 0 deletions pkg/oam/util/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,51 @@ var _ = Describe("TestPassThroughObjMeta", func() {
Expect(gotLabels).Should(BeEquivalentTo(wantLabels))
})
})

var _ = Describe("TestAddLabels", func() {
It("Test AddLabels", func() {
obj1 := new(unstructured.Unstructured)
wantObj1 := new(unstructured.Unstructured)
wantObj1.SetLabels(map[string]string{
"newKey": "newValue",
})
obj2 := new(unstructured.Unstructured)
wantObj2 := new(unstructured.Unstructured)
obj2.SetLabels(map[string]string{
"key": "value",
})
wantObj2.SetLabels(map[string]string{
"key": "value",
"newKey": "newValue",
})

cases := map[string]struct {
obj *unstructured.Unstructured
newLabels map[string]string
want *unstructured.Unstructured
}{
"add labels to workload without labels": {
obj1,
map[string]string{
"newKey": "newValue",
},
wantObj1,
},
"add labels to workload with labels": {
obj2,
map[string]string{
"newKey": "newValue",
},
wantObj2,
},
}

for name, tc := range cases {
By("Running test case: " + name)
obj := tc.obj
wantObj := tc.want
util.AddLabels(obj, tc.newLabels)
Expect(obj.GetLabels()).Should(Equal(wantObj.GetLabels()))
}
})
})
2 changes: 1 addition & 1 deletion test/e2e-test/component_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ var _ = Describe("Versioning mechanism of components", func() {
Eventually(func() string {
k8sClient.Get(ctx, client.ObjectKey{Namespace: namespace, Name: componentName}, cwWlV2)
return cwWlV2.Spec.Containers[0].Image
}, time.Second*30, time.Microsecond*500).Should(Equal(imageV2))
}, time.Second*60, time.Microsecond*500).Should(Equal(imageV2))
})
})

Expand Down
18 changes: 17 additions & 1 deletion test/e2e-test/containerized_workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam/util"
)

Expand Down Expand Up @@ -60,7 +61,8 @@ var _ = Describe("ContainerizedWorkload", func() {
})

It("apply an application config", func() {
label := map[string]string{"workload": "containerized-workload"}
fakeLabelKey := "workload"
label := map[string]string{fakeLabelKey: "containerized-workload"}
// create a workload definition
wd := v1alpha2.WorkloadDefinition{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -192,6 +194,20 @@ var _ = Describe("ContainerizedWorkload", func() {
logf.Log.Info("Creating application config", "Name", appConfig.Name, "Namespace", appConfig.Namespace)
Expect(k8sClient.Create(ctx, &appConfig)).Should(BeNil())
// Verification
By("Checking containerizedworkload is created")
cw := &v1alpha2.ContainerizedWorkload{}
Eventually(func() error {
return k8sClient.Get(ctx, client.ObjectKey{Name: workloadInstanceName, Namespace: namespace}, cw)
}, time.Second*15, time.Millisecond*500).Should(BeNil())

By("Checking lables")
cwLabels := cw.GetLabels()
Expect(cwLabels).Should(SatisfyAll(
HaveKey(fakeLabelKey), // propogated from appConfig
HaveKey(oam.LabelAppComponent),
HaveKey(oam.LabelAppComponentRevision),
HaveKey(oam.LabelAppName)))

By("Checking deployment is created")
objectKey := client.ObjectKey{
Name: workloadInstanceName,
Expand Down