diff --git a/pkg/appfile/service.go b/pkg/appfile/service.go index fea5f33ec..ceef65ed0 100644 --- a/pkg/appfile/service.go +++ b/pkg/appfile/service.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" + "github.com/crossplane/oam-kubernetes-runtime/pkg/oam" + "cuelang.org/go/cue" cueJson "cuelang.org/go/pkg/encoding/json" "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2" @@ -97,12 +99,16 @@ func (s Service) RenderService(tm template.Manager, name, ns, image string) ( component.Spec.Workload.Object = u // render traits - traits := []v1alpha2.ComponentTrait{} - for k, v := range traitKeys { - ts, err := evalTraits(tm.LoadTemplate(k), ctxData, intifyValues(v)) + traits := make([]v1alpha2.ComponentTrait, 0) + for traitType, traitData := range traitKeys { + ts, err := evalTraits(tm.LoadTemplate(traitType), ctxData, intifyValues(traitData)) if err != nil { return nil, nil, fmt.Errorf("eval traits failed: %w", err) } + // one capability corresponds to one trait only + if len(ts) == 1 { + ts[0].SetLabels(map[string]string{oam.TraitTypeLabel: traitType}) + } for _, t := range ts { traits = append(traits, v1alpha2.ComponentTrait{ Trait: runtime.RawExtension{ @@ -244,7 +250,6 @@ func evalTraits(raw string, ctxValues, userValues interface{}) ([]*unstructured. } return renderAllOutputs(outputField) } - u, err := renderOneOutput(appValue) if err != nil { return nil, err diff --git a/pkg/appfile/service_test.go b/pkg/appfile/service_test.go new file mode 100644 index 000000000..c1c55bbb4 --- /dev/null +++ b/pkg/appfile/service_test.go @@ -0,0 +1 @@ +package appfile diff --git a/pkg/application/app_test.go b/pkg/application/app_test.go index b5e33dcd5..ea1faa669 100644 --- a/pkg/application/app_test.go +++ b/pkg/application/app_test.go @@ -5,6 +5,13 @@ import ( "fmt" "testing" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + + "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2" + "github.com/oam-dev/kubevela/pkg/appfile" + "github.com/ghodss/yaml" "github.com/stretchr/testify/assert" @@ -136,3 +143,45 @@ services: assert.Equal(t, c.ExpTraits, traits, caseName) } } + +func TestAddWorkloadTypeLabel(t *testing.T) { + tests := map[string]struct { + comps []*v1alpha2.Component + services map[string]appfile.Service + expect []*v1alpha2.Component + }{ + "empty case": { + comps: []*v1alpha2.Component{}, + services: map[string]appfile.Service{}, + expect: []*v1alpha2.Component{}, + }, + "add type to labels normal case": { + comps: []*v1alpha2.Component{ + { + ObjectMeta: v1.ObjectMeta{Name: "mycomp"}, + Spec: v1alpha2.ComponentSpec{Workload: runtime.RawExtension{Object: &unstructured.Unstructured{Object: map[string]interface{}{}}}}, + }, + }, + services: map[string]appfile.Service{ + "mycomp": {"type": "kubewatch"}, + }, + expect: []*v1alpha2.Component{ + { + ObjectMeta: v1.ObjectMeta{Name: "mycomp"}, + Spec: v1alpha2.ComponentSpec{ + Workload: runtime.RawExtension{ + Object: &unstructured.Unstructured{Object: map[string]interface{}{ + "metadata": map[string]interface{}{ + "labels": map[string]interface{}{ + "workload.oam.dev/type": "kubewatch", + }}}}}, + }, + }, + }, + }, + } + for key, ca := range tests { + addWorkloadTypeLabel(ca.comps, ca.services) + assert.Equal(t, ca.expect, ca.comps, key) + } +}