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 1 commit
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)
}

labels := map[string]string{
oam.LabelAppName: ac.Name,
oam.LabelAppComponent: acc.ComponentName,
oam.LabelAppComponentRevision: componentRevisionName,
}
w.SetLabels(labels)
Copy link
Member

Choose a reason for hiding this comment

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

please make sure we won't override the existing labels.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, I will use merge instead of override here.


// 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"
)
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