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

View File

@@ -95,17 +95,17 @@ import "list"
comps: [ comps: [
if len(resourceMap.crd) > 0 { if len(resourceMap.crd) > 0 {
type: "k8s-objects" type: "k8s-objects"
name: "\(appName).crds" name: "crds"
properties: objects: [ for r in resourceMap.crd { properties: objects: [ for r in resourceMap.crd {
apiVersion: r.apiVersion apiVersion: r.apiVersion
kind: r.kind kind: r.kind
metadata: name: r.metadata.name metadata: name: r.metadata.name
}] }]
}, },
for r in resourceMap.ns { if len(resourceMap.ns) > 0 {
type: "k8s-objects" type: "k8s-objects"
name: "\(appName).ns.\(r.metadata.name)" name: "ns"
properties: objects: [{ properties: objects: [ for r in resourceMap.ns {
apiVersion: r.apiVersion apiVersion: r.apiVersion
kind: r.kind kind: r.kind
metadata: name: r.metadata.name metadata: name: r.metadata.name
@@ -113,19 +113,21 @@ import "list"
}, },
for r in resourceMap.workload + resourceMap.service { for r in resourceMap.workload + resourceMap.service {
type: "k8s-objects" type: "k8s-objects"
name: "\(appName).\(r.kind).\(r.metadata.name)" name: "\(r.kind).\(r.metadata.name)"
properties: objects: [{ properties: objects: [{
apiVersion: r.apiVersion apiVersion: r.apiVersion
kind: r.kind kind: r.kind
metadata: name: r.metadata.name metadata: name: r.metadata.name
metadata: namespace: r.metadata.namespace if r.metadata.namespace != _|_ {
metadata: namespace: r.metadata.namespace
}
spec: r.spec spec: r.spec
}] }]
}, },
for key in ["config", "sa", "operator", "storage"] if len(resourceMap[key]) > 0 { for key in ["config", "sa", "operator", "storage"] if len(resourceMap[key]) > 0 {
type: "k8s-objects" type: "k8s-objects"
name: "\(appName).\(key)" name: "\(key)"
properties: objects: [ for r in resourceMap.config { properties: objects: [ for r in resourceMap[key] {
apiVersion: r.apiVersion apiVersion: r.apiVersion
kind: r.kind kind: r.kind
metadata: name: r.metadata.name metadata: name: r.metadata.name
@@ -136,7 +138,7 @@ import "list"
}, },
for kind, rs in unknownByKinds { for kind, rs in unknownByKinds {
type: "k8s-objects" type: "k8s-objects"
name: "\(appName).\(kind)" name: "\(kind)"
properties: objects: [ for r in rs { properties: objects: [ for r in rs {
apiVersion: r.apiVersion apiVersion: r.apiVersion
kind: r.kind kind: r.kind

View File

@@ -26,7 +26,7 @@ import (
"time" "time"
"cuelang.org/go/cue" "cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext" "github.com/kubevela/pkg/cue/cuex"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
@@ -171,7 +171,7 @@ func (opt *AdoptOptions) parseResourceRef(f velacmd.Factory, cmd *cobra.Command,
} }
// Init . // 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 opt.All {
if len(args) > 0 { if len(args) > 0 {
for _, arg := range args { 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) opt.AllGVKs = append(opt.AllGVKs, gvk)
apiVersion, kind := gvk.ToAPIVersionAndKind() 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 { 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.ResourceTopologyRule = defaultResourceTopologyRule
} }
opt.AppNamespace = velacmd.GetNamespace(f, cmd) 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 { switch opt.Type {
case adoptTypeNative: case adoptTypeNative:
if opt.Recycle { if opt.Recycle {
@@ -351,7 +354,7 @@ func (opt *AdoptOptions) MultipleRun(f velacmd.Factory, cmd *cobra.Command) erro
} }
// Complete autofill fields in opts // 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) opt.AppNamespace = velacmd.GetNamespace(f, cmd)
switch opt.Type { switch opt.Type {
case adoptTypeNative: case adoptTypeNative:
@@ -389,21 +392,25 @@ func (opt *AdoptOptions) Complete(f velacmd.Factory, cmd *cobra.Command, args []
default: default:
} }
if opt.AppName != "" { if opt.AppName != "" {
var ctx = context.Background()
app := &v1beta1.Application{} 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 err == nil && app != nil {
if !opt.Yes { if !opt.Yes && opt.Apply {
userInput := NewUserInput() 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 { if !confirm {
return nil return nil
} }
} }
} }
} }
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))
return nil if err != nil {
return fmt.Errorf("failed to compile cue template: %w", err)
}
return err
} }
// Validate if opts is valid // Validate if opts is valid

View File

@@ -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) appKey := client.ObjectKeyFromObject(app)
Eventually(func(g Gomega) { Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(hubCtx, appKey, app)).Should(Succeed()) g.Expect(k8sClient.Get(hubCtx, appKey, app)).Should(Succeed())