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 2 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
19 changes: 19 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,
}
addWorkloadLabels(w, compInfoLabels)

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

Expand Down Expand Up @@ -469,6 +477,17 @@ func fillValue(obj *unstructured.Unstructured, fs []string, val interface{}) err
return nil
}

func addWorkloadLabels(w *unstructured.Unstructured, labels map[string]string) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this to util and change the name from "addWorkloadLabels" to "addLabels" as this is very generic. Also, I am not sure if this already exists somewhere in the library

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will check and move it.

workloadLabels := w.GetLabels()
if workloadLabels == nil {
workloadLabels = map[string]string{}
}
for k, v := range labels {
workloadLabels[k] = v
}
w.SetLabels(workloadLabels)
}

func (r *components) getDataInput(ctx context.Context, s *dagSource) (interface{}, bool, error) {
obj := s.ObjectRef
key := types.NamespacedName{
Expand Down
65 changes: 65 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 Expand Up @@ -1040,3 +1056,52 @@ func TestMatchValue(t *testing.T) {
})
}
}

func TestAddWorkloadLabels(t *testing.T) {
workload1 := new(unstructured.Unstructured)
wantWorkload1 := new(unstructured.Unstructured)
wantWorkload1.SetLabels(map[string]string{
"newKey": "newValue",
})
workload2 := new(unstructured.Unstructured)
wantWorkload2 := new(unstructured.Unstructured)
workload2.SetLabels(map[string]string{
"key": "value",
})
wantWorkload2.SetLabels(map[string]string{
"key": "value",
"newKey": "newValue",
})

cases := map[string]struct {
workload *unstructured.Unstructured
newLabels map[string]string
want *unstructured.Unstructured
}{
"add labels to workload without labels": {
workload1,
map[string]string{
"newKey": "newValue",
},
wantWorkload1,
},
"add labels to workload with labels": {
workload2,
map[string]string{
"newKey": "newValue",
},
wantWorkload2,
},
}

for name, tc := range cases {
workload := tc.workload
wantWorkload := tc.want
t.Run(name, func(t *testing.T) {
addWorkloadLabels(tc.workload, tc.newLabels)
if diff := cmp.Diff(wantWorkload, workload); diff != "" {
t.Error(diff)
}
})
}
}
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"
)
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