From ee390545376e7328dc0ccf03cf3a646925041f87 Mon Sep 17 00:00:00 2001 From: Yue Wang Date: Fri, 18 Sep 2020 16:59:36 +0900 Subject: [PATCH] get cue temp from remote through URI (#287) * get cue template of capabilities from remote by URI Signed-off-by: roy wang * add e2e tests Signed-off-by: roy wang * fix type conversion Signed-off-by: roy wang --- api/types/capability.go | 1 + pkg/plugins/cluster.go | 23 ++++++++++++-- pkg/plugins/cluster_test.go | 35 +++++++++++++++++++-- pkg/plugins/suit_test.go | 10 +++++- pkg/plugins/testdata/websvcWorkloadDef.yaml | 11 +++++++ 5 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 pkg/plugins/testdata/websvcWorkloadDef.yaml diff --git a/api/types/capability.go b/api/types/capability.go index 8dca2e8a5..3b767525a 100644 --- a/api/types/capability.go +++ b/api/types/capability.go @@ -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"` diff --git a/pkg/plugins/cluster.go b/pkg/plugins/cluster.go index e97f867d9..295c4c588 100644 --- a/pkg/plugins/cluster.go +++ b/pkg/plugins/cluster.go @@ -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 } diff --git a/pkg/plugins/cluster_test.go b/pkg/plugins/cluster_test.go index a7b326fa5..92f83aaa4 100644 --- a/pkg/plugins/cluster_test.go +++ b/pkg/plugins/cluster_test.go @@ -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})) }) }) diff --git a/pkg/plugins/suit_test.go b/pkg/plugins/suit_test.go index 38fc6419d..579ef4c2b 100644 --- a/pkg/plugins/suit_test.go +++ b/pkg/plugins/suit_test.go @@ -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) diff --git a/pkg/plugins/testdata/websvcWorkloadDef.yaml b/pkg/plugins/testdata/websvcWorkloadDef.yaml new file mode 100644 index 000000000..e357506eb --- /dev/null +++ b/pkg/plugins/testdata/websvcWorkloadDef.yaml @@ -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"