mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-27 16:17:34 +00:00
fix vela traits not compatible with installed capability (#1033)
* fix issue 1029 * add test case * fix check diff Co-authored-by: 王易可 <wangyike@B-V1QHML7L-1909.local>
This commit is contained in:
@@ -2,6 +2,7 @@ package serverlib
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@@ -114,6 +115,10 @@ func InstallCapability(client client.Client, mapper discoverymapper.DiscoveryMap
|
||||
APIVersion: gvk.GroupVersion().String(),
|
||||
Kind: gvk.Kind,
|
||||
}
|
||||
err = addSourceIntoExtension(wd.Spec.Extension, tp.Source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = client.Create(context.Background(), &wd); err != nil && !apierrors.IsAlreadyExists(err) {
|
||||
return err
|
||||
}
|
||||
@@ -145,6 +150,10 @@ func InstallCapability(client client.Client, mapper discoverymapper.DiscoveryMap
|
||||
APIVersion: gvk.GroupVersion().String(),
|
||||
Kind: gvk.Kind,
|
||||
}
|
||||
err = addSourceIntoExtension(td.Spec.Extension, tp.Source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = client.Create(context.Background(), &td); err != nil && !apierrors.IsAlreadyExists(err) {
|
||||
return err
|
||||
}
|
||||
@@ -421,3 +430,18 @@ func checkInstallStatus(repoName string, tmp types.Capability) string {
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
func addSourceIntoExtension(in *runtime.RawExtension, source *types.Source) error {
|
||||
var extension map[string]interface{}
|
||||
err := json.Unmarshal(in.Raw, &extension)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
extension["source"] = source
|
||||
data, err := json.Marshal(extension)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
in.Raw = data
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package serverlib
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
"github.com/oam-dev/kubevela/apis/types"
|
||||
)
|
||||
|
||||
func TestAddSourceIntoDefinition(t *testing.T) {
|
||||
caseJson := []byte(`{"template":""}`)
|
||||
wantJson := []byte(`{"source":{"repoName":"foo"},"template":""}`)
|
||||
source := types.Source{RepoName: "foo"}
|
||||
testcase := runtime.RawExtension{Raw: caseJson}
|
||||
err := addSourceIntoExtension(&testcase, &source)
|
||||
if err != nil {
|
||||
t.Error("meet an error ", err)
|
||||
return
|
||||
}
|
||||
var result, want map[string]interface{}
|
||||
err = json.Unmarshal(testcase.Raw, &result)
|
||||
if err != nil {
|
||||
t.Error("marshaling object meet an error ", err)
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(wantJson, &want)
|
||||
if err != nil {
|
||||
t.Error("marshaling object meet an error ", err)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(result, want) {
|
||||
t.Errorf("error result want %s, got %s", result, testcase)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user