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

add oam default labels for trait #197

Merged
merged 1 commit into from
Sep 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (r *OAMApplicationReconciler) updateStatus(ctx context.Context, ac *v1alpha
var ul unstructured.UnstructuredList
ul.SetKind(w.Workload.GetKind())
ul.SetAPIVersion(w.Workload.GetAPIVersion())
if err := r.client.List(ctx, &ul, client.MatchingLabels{oam.LabelAppName: ac.Name, oam.LabelAppComponent: w.ComponentName}); err != nil {
if err := r.client.List(ctx, &ul, client.MatchingLabels{oam.LabelAppName: ac.Name, oam.LabelAppComponent: w.ComponentName, oam.LabelOAMResourceType: oam.ResourceTypeWorkload}); err != nil {
continue
}
for _, v := range ul.Items {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/v1alpha2/applicationconfiguration/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (r *components) renderComponent(ctx context.Context, acc v1alpha2.Applicati
oam.LabelAppName: ac.Name,
oam.LabelAppComponent: acc.ComponentName,
oam.LabelAppComponentRevision: componentRevisionName,
oam.LabelOAMResourceType: oam.ResourceTypeWorkload,
}
util.AddLabels(w, compInfoLabels)

Expand All @@ -147,12 +148,13 @@ func (r *components) renderComponent(ctx context.Context, acc v1alpha2.Applicati

traits := make([]*Trait, 0, len(acc.Traits))
traitDefs := make([]v1alpha2.TraitDefinition, 0, len(acc.Traits))
compInfoLabels[oam.LabelOAMResourceType] = oam.ResourceTypeTrait
for _, ct := range acc.Traits {
t, traitDef, err := r.renderTrait(ctx, ct, ac, acc.ComponentName, ref, dag)
if err != nil {
return nil, err
}

util.AddLabels(t, compInfoLabels)
// pass through labels and annotation from app-config to trait
util.PassLabelAndAnnotation(ac, t)
traits = append(traits, &Trait{Object: *t})
Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/v1alpha2/applicationconfiguration/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func TestRenderComponents(t *testing.T) {
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: "",
oam.LabelOAMResourceType: oam.ResourceTypeWorkload,
})
return w
}(),
Expand All @@ -208,6 +209,12 @@ func TestRenderComponents(t *testing.T) {
t.SetNamespace(namespace)
t.SetName(traitName)
t.SetOwnerReferences([]metav1.OwnerReference{*ref})
t.SetLabels(map[string]string{
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: "",
oam.LabelOAMResourceType: oam.ResourceTypeTrait,
})
return &Trait{Object: *t}
}(),
},
Expand Down Expand Up @@ -267,6 +274,7 @@ func TestRenderComponents(t *testing.T) {
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: revisionName,
oam.LabelOAMResourceType: oam.ResourceTypeWorkload,
})
return w
}(),
Expand All @@ -276,6 +284,12 @@ func TestRenderComponents(t *testing.T) {
t.SetNamespace(namespace)
t.SetName(traitName)
t.SetOwnerReferences([]metav1.OwnerReference{*ref})
t.SetLabels(map[string]string{
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: revisionName,
oam.LabelOAMResourceType: oam.ResourceTypeTrait,
})
return &Trait{Object: *t}
}(),
},
Expand Down Expand Up @@ -326,6 +340,7 @@ func TestRenderComponents(t *testing.T) {
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: revisionName2,
oam.LabelOAMResourceType: oam.ResourceTypeWorkload,
})
return w
}(),
Expand All @@ -335,6 +350,12 @@ func TestRenderComponents(t *testing.T) {
t.SetNamespace(namespace)
t.SetName(traitName)
t.SetOwnerReferences([]metav1.OwnerReference{*ref})
t.SetLabels(map[string]string{
oam.LabelAppComponent: componentName,
oam.LabelAppName: acName,
oam.LabelAppComponentRevision: revisionName2,
oam.LabelOAMResourceType: oam.ResourceTypeTrait,
})
return &Trait{Object: *t}
}(),
},
Expand Down
9 changes: 9 additions & 0 deletions pkg/oam/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ const (
LabelAppComponent = "app.oam.dev/component"
// LabelAppComponentRevision records the revision name of Component
LabelAppComponentRevision = "app.oam.dev/revision"
// LabelOAMResourceType whether a CR is workload or trait
LabelOAMResourceType = "app.oam.dev/resourceType"
)

const (
// ResourceTypeTrait mark this K8s Custom Resource is an OAM trait
ResourceTypeTrait = "TRAIT"
// ResourceTypeWorkload mark this K8s Custom Resource is an OAM workload
ResourceTypeWorkload = "WORKLOAD"
)
18 changes: 16 additions & 2 deletions test/e2e-test/containerized_workload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,27 @@ var _ = Describe("ContainerizedWorkload", func() {
return k8sClient.Get(ctx, client.ObjectKey{Name: workloadInstanceName, Namespace: namespace}, cw)
}, time.Second*15, time.Millisecond*500).Should(BeNil())

By("Checking lables")
By("Checking ManuelScalerTrait is created")
Eventually(func() error {
return k8sClient.Get(ctx, client.ObjectKey{Name: mts.Name, Namespace: namespace}, &mts)
}, time.Second*15, time.Millisecond*500).Should(BeNil())

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

cwLabels = mts.GetLabels()
Expect(cwLabels).Should(SatisfyAll(
HaveKey(fakeLabelKey), // propogated from appConfig
HaveKey(oam.LabelAppComponent),
HaveKey(oam.LabelAppComponentRevision),
HaveKey(oam.LabelAppName),
HaveKey(oam.LabelOAMResourceType)))

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