Feat: enhance vela adopt and refactor (#5728)

* Feat: enhance vela adopt and refactor

Signed-off-by: Somefive <yd219913@alibaba-inc.com>

* Fix: flaky mc test

Signed-off-by: Somefive <yd219913@alibaba-inc.com>

---------

Signed-off-by: Somefive <yd219913@alibaba-inc.com>
This commit is contained in:
Somefive
2023-03-24 16:23:01 +08:00
committed by GitHub
parent 5790e57423
commit e70d2a1021
3 changed files with 32 additions and 21 deletions
+11 -9
View File
@@ -95,17 +95,17 @@ import "list"
comps: [
if len(resourceMap.crd) > 0 {
type: "k8s-objects"
name: "\(appName).crds"
name: "crds"
properties: objects: [ for r in resourceMap.crd {
apiVersion: r.apiVersion
kind: r.kind
metadata: name: r.metadata.name
}]
},
for r in resourceMap.ns {
if len(resourceMap.ns) > 0 {
type: "k8s-objects"
name: "\(appName).ns.\(r.metadata.name)"
properties: objects: [{
name: "ns"
properties: objects: [ for r in resourceMap.ns {
apiVersion: r.apiVersion
kind: r.kind
metadata: name: r.metadata.name
@@ -113,19 +113,21 @@ import "list"
},
for r in resourceMap.workload + resourceMap.service {
type: "k8s-objects"
name: "\(appName).\(r.kind).\(r.metadata.name)"
name: "\(r.kind).\(r.metadata.name)"
properties: objects: [{
apiVersion: r.apiVersion
kind: r.kind
metadata: name: r.metadata.name
metadata: namespace: r.metadata.namespace
if r.metadata.namespace != _|_ {
metadata: namespace: r.metadata.namespace
}
spec: r.spec
}]
},
for key in ["config", "sa", "operator", "storage"] if len(resourceMap[key]) > 0 {
type: "k8s-objects"
name: "\(appName).\(key)"
properties: objects: [ for r in resourceMap.config {
name: "\(key)"
properties: objects: [ for r in resourceMap[key] {
apiVersion: r.apiVersion
kind: r.kind
metadata: name: r.metadata.name
@@ -136,7 +138,7 @@ import "list"
},
for kind, rs in unknownByKinds {
type: "k8s-objects"
name: "\(appName).\(kind)"
name: "\(kind)"
properties: objects: [ for r in rs {
apiVersion: r.apiVersion
kind: r.kind
+18 -11
View File
@@ -26,7 +26,7 @@ import (
"time"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"github.com/kubevela/pkg/cue/cuex"
"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/release"
@@ -171,7 +171,7 @@ func (opt *AdoptOptions) parseResourceRef(f velacmd.Factory, cmd *cobra.Command,
}
// Init .
func (opt *AdoptOptions) Init(f velacmd.Factory, cmd *cobra.Command, args []string) error {
func (opt *AdoptOptions) Init(f velacmd.Factory, cmd *cobra.Command, args []string) (err error) {
if opt.All {
if len(args) > 0 {
for _, arg := range args {
@@ -181,7 +181,7 @@ func (opt *AdoptOptions) Init(f velacmd.Factory, cmd *cobra.Command, args []stri
}
opt.AllGVKs = append(opt.AllGVKs, gvk)
apiVersion, kind := gvk.ToAPIVersionAndKind()
fmt.Fprintf(opt.Out, "Adopt all %s/%s resources\n", apiVersion, kind)
_, _ = fmt.Fprintf(opt.Out, "Adopt all %s/%s resources\n", apiVersion, kind)
}
}
if len(opt.AllGVKs) == 0 {
@@ -212,7 +212,10 @@ func (opt *AdoptOptions) Init(f velacmd.Factory, cmd *cobra.Command, args []stri
opt.ResourceTopologyRule = defaultResourceTopologyRule
}
opt.AppNamespace = velacmd.GetNamespace(f, cmd)
opt.AdoptTemplateCUEValue = cuecontext.New().CompileString(fmt.Sprintf("%s\n\n%s: %s", opt.AdoptTemplate, adoptCUETempVal, adoptCUETempFunc))
opt.AdoptTemplateCUEValue, err = cuex.CompileString(cmd.Context(), fmt.Sprintf("%s\n\n%s: %s", opt.AdoptTemplate, adoptCUETempVal, adoptCUETempFunc))
if err != nil {
return fmt.Errorf("failed to compile template: %w", err)
}
switch opt.Type {
case adoptTypeNative:
if opt.Recycle {
@@ -351,7 +354,7 @@ func (opt *AdoptOptions) MultipleRun(f velacmd.Factory, cmd *cobra.Command) erro
}
// Complete autofill fields in opts
func (opt *AdoptOptions) Complete(f velacmd.Factory, cmd *cobra.Command, args []string) error {
func (opt *AdoptOptions) Complete(f velacmd.Factory, cmd *cobra.Command, args []string) (err error) {
opt.AppNamespace = velacmd.GetNamespace(f, cmd)
switch opt.Type {
case adoptTypeNative:
@@ -389,21 +392,25 @@ func (opt *AdoptOptions) Complete(f velacmd.Factory, cmd *cobra.Command, args []
default:
}
if opt.AppName != "" {
var ctx = context.Background()
app := &v1beta1.Application{}
err := f.Client().Get(ctx, apitypes.NamespacedName{Namespace: opt.AppNamespace, Name: opt.AppName}, app)
err := f.Client().Get(cmd.Context(), apitypes.NamespacedName{Namespace: opt.AppNamespace, Name: opt.AppName}, app)
if err == nil && app != nil {
if !opt.Yes {
if !opt.Yes && opt.Apply {
userInput := NewUserInput()
confirm := userInput.AskBool("Application '%s' already exists, apply will override the existing app with the adopted one, please confirm [Y/n]: "+opt.AppName, &UserInputOptions{AssumeYes: false})
confirm := userInput.AskBool(
fmt.Sprintf("Application '%s' already exists, apply will override the existing app with the adopted one, please confirm [Y/n]: ", opt.AppName),
&UserInputOptions{AssumeYes: false})
if !confirm {
return nil
}
}
}
}
opt.AdoptTemplateCUEValue = cuecontext.New().CompileString(fmt.Sprintf("%s\n\n%s: %s", opt.AdoptTemplate, adoptCUETempVal, adoptCUETempFunc))
return nil
opt.AdoptTemplateCUEValue, err = cuex.CompileString(cmd.Context(), fmt.Sprintf("%s\n\n%s: %s", opt.AdoptTemplate, adoptCUETempVal, adoptCUETempFunc))
if err != nil {
return fmt.Errorf("failed to compile cue template: %w", err)
}
return err
}
// Validate if opts is valid
@@ -968,7 +968,9 @@ var _ = Describe("Test multicluster scenario", func() {
}},
}}},
}
Expect(k8sClient.Create(hubCtx, app)).Should(Succeed())
Eventually(func(g Gomega) { // in case the trait definition has not been watched by vela-core
g.Expect(k8sClient.Create(hubCtx, app)).Should(Succeed())
}).WithTimeout(10 * time.Second).WithPolling(2 * time.Second).Should(Succeed())
appKey := client.ObjectKeyFromObject(app)
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(hubCtx, appKey, app)).Should(Succeed())