mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-27 16:17:34 +00:00
get cue temp from remote through URI (#287)
* get cue template of capabilities from remote by URI Signed-off-by: roy wang <seiwy2010@gmail.com> * add e2e tests Signed-off-by: roy wang <seiwy2010@gmail.com> * fix type conversion Signed-off-by: roy wang <seiwy2010@gmail.com>
This commit is contained in:
@@ -40,6 +40,7 @@ type Capability struct {
|
||||
Name string `json:"name"`
|
||||
Type CapType `json:"type"`
|
||||
CueTemplate string `json:"template,omitempty"`
|
||||
CueTemplateURI string `json:"templateURI,omitempty"`
|
||||
Parameters []Parameter `json:"parameters,omitempty"`
|
||||
DefinitionPath string `json:"definition"`
|
||||
CrdName string `json:"crdName,omitempty"`
|
||||
|
||||
+20
-3
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/oam-dev/kubevela/api/types"
|
||||
@@ -100,12 +101,28 @@ func HandleTemplate(in *runtime.RawExtension, name, syncDir string) (types.Capab
|
||||
if err != nil {
|
||||
return types.Capability{}, err
|
||||
}
|
||||
if tmp.CueTemplate == "" {
|
||||
return types.Capability{}, errors.New("template not exist in definition")
|
||||
|
||||
var cueTemplate string
|
||||
if tmp.CueTemplateURI != "" {
|
||||
res, err := http.Get(tmp.CueTemplateURI)
|
||||
if err != nil {
|
||||
return types.Capability{}, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
b, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return types.Capability{}, err
|
||||
}
|
||||
cueTemplate = string(b)
|
||||
} else {
|
||||
if tmp.CueTemplate == "" {
|
||||
return types.Capability{}, errors.New("template not exist in definition")
|
||||
}
|
||||
cueTemplate = tmp.CueTemplate
|
||||
}
|
||||
_, _ = system.CreateIfNotExist(syncDir)
|
||||
filePath := filepath.Join(syncDir, name+".cue")
|
||||
err = ioutil.WriteFile(filePath, []byte(tmp.CueTemplate), 0644)
|
||||
err = ioutil.WriteFile(filePath, []byte(cueTemplate), 0644)
|
||||
if err != nil {
|
||||
return types.Capability{}, err
|
||||
}
|
||||
|
||||
@@ -64,6 +64,37 @@ var _ = Describe("DefinitionFiles", func() {
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
websvc := types.Capability{
|
||||
Name: "webservice",
|
||||
Type: types.TypeWorkload,
|
||||
CueTemplateURI: "https://raw.githubusercontent.com/oam-dev/kubevela/master/vela-templates/web-service.cue",
|
||||
Parameters: []types.Parameter{
|
||||
{
|
||||
Name: "name",
|
||||
Required: true,
|
||||
Default: "",
|
||||
Type: cue.StringKind,
|
||||
},
|
||||
{
|
||||
Name: "image",
|
||||
Type: cue.StringKind,
|
||||
Default: "",
|
||||
Short: "i",
|
||||
Required: true,
|
||||
Usage: "specify app image",
|
||||
},
|
||||
{
|
||||
Name: "port",
|
||||
Type: cue.IntKind,
|
||||
Short: "p",
|
||||
Default: int64(6379),
|
||||
Usage: "specify port for container",
|
||||
},
|
||||
},
|
||||
CrdName: "webservice.testapps",
|
||||
}
|
||||
|
||||
req, _ := labels.NewRequirement("usecase", selection.Equals, []string{"forplugintest"})
|
||||
selector := labels.NewSelector().Add(*req)
|
||||
|
||||
@@ -89,7 +120,7 @@ var _ = Describe("DefinitionFiles", func() {
|
||||
workloadDefs[i].CueTemplate = ""
|
||||
workloadDefs[i].DefinitionPath = ""
|
||||
}
|
||||
Expect(workloadDefs).Should(Equal([]types.Capability{deployment}))
|
||||
Expect(workloadDefs).Should(Equal([]types.Capability{deployment, websvc}))
|
||||
})
|
||||
It("getall", func() {
|
||||
alldef, err := GetCapabilitiesFromCluster(context.Background(), DefinitionNamespace, k8sClient, definitionDir, selector)
|
||||
@@ -99,6 +130,6 @@ var _ = Describe("DefinitionFiles", func() {
|
||||
alldef[i].CueTemplate = ""
|
||||
alldef[i].DefinitionPath = ""
|
||||
}
|
||||
Expect(alldef).Should(Equal([]types.Capability{deployment, route}))
|
||||
Expect(alldef).Should(Equal([]types.Capability{deployment, websvc, route}))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -37,7 +37,7 @@ var k8sClient client.Client
|
||||
var testEnv *envtest.Environment
|
||||
var definitionDir string
|
||||
var td v1alpha2.TraitDefinition
|
||||
var wd v1alpha2.WorkloadDefinition
|
||||
var wd, websvcWD v1alpha2.WorkloadDefinition
|
||||
|
||||
func TestAPIs(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
@@ -138,6 +138,14 @@ var _ = BeforeSuite(func(done Done) {
|
||||
logf.Log.Info("Creating workload definition", "data", wd)
|
||||
Expect(k8sClient.Create(ctx, &wd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
|
||||
|
||||
websvcWorkloadData, err := ioutil.ReadFile("testdata/websvcWorkloadDef.yaml")
|
||||
Expect(err).Should(BeNil())
|
||||
|
||||
Expect(yaml.Unmarshal(websvcWorkloadData, &websvcWD)).Should(BeNil())
|
||||
websvcWD.Namespace = DefinitionNamespace
|
||||
logf.Log.Info("Creating workload definition whose CUE template from remote", "data", &websvcWD)
|
||||
Expect(k8sClient.Create(ctx, &websvcWD)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
|
||||
|
||||
close(done)
|
||||
}, 60)
|
||||
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
apiVersion: core.oam.dev/v1alpha2
|
||||
kind: WorkloadDefinition
|
||||
metadata:
|
||||
name: webservice.testapps
|
||||
labels:
|
||||
usecase: forplugintest
|
||||
spec:
|
||||
definitionRef:
|
||||
name: webservice.testapps
|
||||
extension:
|
||||
templateURI: "https://raw.githubusercontent.com/oam-dev/kubevela/master/vela-templates/web-service.cue"
|
||||
Reference in New Issue
Block a user