fix label not int

This commit is contained in:
天元
2020-10-22 12:22:50 +08:00
parent 6675b8a806
commit 3082b7b9dd
3 changed files with 59 additions and 4 deletions
+9 -4
View File
@@ -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
+1
View File
@@ -0,0 +1 @@
package appfile
+49
View File
@@ -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)
}
}