You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by zh...@apache.org on 2024/12/15 05:37:30 UTC

(dubbo-kubernetes) branch master updated: [operator] fix error logic

This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new 5d2a08f1 [operator] fix error logic
5d2a08f1 is described below

commit 5d2a08f143c7e8228b1a11e9bfac42d4fd67692b
Author: mfordjody <11...@qq.com>
AuthorDate: Sun Dec 15 13:37:20 2024 +0800

    [operator] fix error logic
---
 dubboctl/cmd/root.go                  | 10 ++++-----
 dubboctl/main.go                      |  4 ++--
 dubboctl/pkg/cli/context.go           | 17 ++++++++-------
 manifests/manifest.go                 |  1 +
 operator/cmd/cluster/install.go       | 25 +++++++++++++++-------
 operator/cmd/cluster/root.go          |  4 ++--
 operator/cmd/cluster/uninstall.go     |  2 +-
 operator/cmd/validation/validation.go |  3 +--
 operator/pkg/component/component.go   |  2 ++
 operator/pkg/install/installer.go     | 23 +++++++++++++++++++-
 operator/pkg/render/manifest.go       | 40 ++++++++++++++++++++++++++---------
 operator/pkg/render/postprocess.go    | 11 ++++++++++
 pkg/art/atr.go                        |  2 +-
 13 files changed, 103 insertions(+), 41 deletions(-)

diff --git a/dubboctl/cmd/root.go b/dubboctl/cmd/root.go
index efcf5b9d..113bf07f 100644
--- a/dubboctl/cmd/root.go
+++ b/dubboctl/cmd/root.go
@@ -27,20 +27,20 @@ func AddFlags(cmd *cobra.Command) {
 }
 
 func GetRootCmd(args []string) *cobra.Command {
-
 	rootCmd := &cobra.Command{
 		Use:   "dubboctl",
 		Short: "Dubbo command line utilities",
 		Long:  `Dubbo configuration command line utility for debug and use dubbo applications.`,
 	}
+	AddFlags(rootCmd)
 	rootCmd.SetArgs(args)
 	flags := rootCmd.PersistentFlags()
-
 	rootOptions := cli.AddRootFlags(flags)
 	ctx := cli.NewCLIContext(rootOptions)
 
 	installCmd := cluster.InstallCmd(ctx)
 	rootCmd.AddCommand(installCmd)
+	hideFlags(installCmd, cli.NamespaceFlag, cli.DubboNamespaceFlag, cli.ChartFlag)
 
 	uninstallCmd := cluster.UninstallCmd(ctx)
 	rootCmd.AddCommand(uninstallCmd)
@@ -48,17 +48,15 @@ func GetRootCmd(args []string) *cobra.Command {
 	upgradeCmd := cluster.UpgradeCmd(ctx)
 	rootCmd.AddCommand(upgradeCmd)
 
-	AddFlags(installCmd)
-	hideFlags(installCmd, cli.NamespaceFlag, cli.DubboNamespaceFlag, cli.ChartFlag)
 	return rootCmd
 }
 
 func hideFlags(origin *cobra.Command, hide ...string) {
-	origin.SetHelpFunc(func(command *cobra.Command, strings []string) {
+	origin.SetHelpFunc(func(command *cobra.Command, args []string) {
 		for _, hf := range hide {
 			_ = command.Flags().MarkHidden(hf)
 		}
 		origin.SetHelpFunc(nil)
-		origin.HelpFunc()(command, strings)
+		origin.HelpFunc()(command, args)
 	})
 }
diff --git a/dubboctl/main.go b/dubboctl/main.go
index c82e578a..ec5eb018 100644
--- a/dubboctl/main.go
+++ b/dubboctl/main.go
@@ -18,7 +18,7 @@
 package main
 
 import (
-	"k8s.io/klog/v2"
+	"fmt"
 	"os"
 )
 
@@ -29,7 +29,7 @@ import (
 func main() {
 	rootCmd := cmd.GetRootCmd(os.Args[1:])
 	if err := rootCmd.Execute(); err != nil {
-		klog.Errorf("GetRootCmd Execute err:%v", err)
+		fmt.Errorf("GetRootCmd Execute err:%v", err)
 		os.Exit(1)
 	}
 }
diff --git a/dubboctl/pkg/cli/context.go b/dubboctl/pkg/cli/context.go
index 86e52028..bb210026 100644
--- a/dubboctl/pkg/cli/context.go
+++ b/dubboctl/pkg/cli/context.go
@@ -13,7 +13,6 @@ type instance struct {
 
 type Context interface {
 	CLIClient() (kube.CLIClient, error)
-	CLIClientWithRevision(rev string) (kube.CLIClient, error)
 }
 
 func NewCLIContext(rootFlags *RootFlags) Context {
@@ -38,19 +37,21 @@ func (i *instance) CLIClientWithRevision(rev string) (kube.CLIClient, error) {
 	if i.clients == nil {
 		i.clients = make(map[string]kube.CLIClient)
 	}
-	impersonationConfig := rest.ImpersonationConfig{}
-	client, err := newKubeClientWithRevision(*i.kubeconfig, *i.Context, rev, impersonationConfig)
-	if err != nil {
-		return nil, err
+	if i.clients[rev] == nil {
+		impersonationConfig := rest.ImpersonationConfig{}
+		client, err := newKubeClientWithRevision(*i.kubeconfig, *i.Context, rev, impersonationConfig)
+		if err != nil {
+			return nil, err
+		}
+		i.clients[rev] = client
 	}
-	i.clients[rev] = client
 	return i.clients[rev], nil
 }
 
 func newKubeClientWithRevision(kubeconfig, context, revision string, impersonationConfig rest.ImpersonationConfig) (kube.CLIClient, error) {
 	drc, err := kube.DefaultRestConfig(kubeconfig, context, func(config *rest.Config) {
-		config.QPS = 50
-		config.Burst = 100
+		config.QPS = 55
+		config.Burst = 95
 		config.Impersonate = impersonationConfig
 	})
 	if err != nil {
diff --git a/manifests/manifest.go b/manifests/manifest.go
index 8ea61bce..b747cd66 100644
--- a/manifests/manifest.go
+++ b/manifests/manifest.go
@@ -21,6 +21,7 @@ import (
 	"os"
 )
 
+//go:embed all:charts/* profiles/*
 var FS embed.FS
 
 func BuiltinDir(dir string) fs.FS {
diff --git a/operator/cmd/cluster/install.go b/operator/cmd/cluster/install.go
index 641a346d..cdc8241f 100644
--- a/operator/cmd/cluster/install.go
+++ b/operator/cmd/cluster/install.go
@@ -37,6 +37,12 @@ func (i *installArgs) String() string {
 	return b.String()
 }
 
+func addInstallFlags(cmd *cobra.Command, args *installArgs) {
+	cmd.PersistentFlags().StringSliceVarP(&args.Files, "files", "f", nil, `Path to the file containing the dubboOperator's custom resources`)
+	cmd.PersistentFlags().StringArrayVarP(&args.Sets, "set", "s", nil, `Override dubboOperator values, such as selecting profiles, etc`)
+
+}
+
 func InstallCmd(ctx cli.Context) *cobra.Command {
 	return InstallCmdWithArgs(ctx, &RootArgs{}, &installArgs{})
 }
@@ -46,13 +52,14 @@ func InstallCmdWithArgs(ctx cli.Context, rootArgs *RootArgs, iArgs *installArgs)
 		Use:   "install",
 		Short: "Applies an Dubbo manifest, installing or reconfiguring Dubbo on a cluster",
 		Long:  "The install command generates an Dubbo install manifest and applies it to a cluster",
-		Example: `
-        # Apply a default dubboctl installation.
-		dubboctl install
-		# Apply a default profile.
-		dubboctl install --profile=default
-		# Apply a config file
-		dubboctl install -f my-config.yaml
+		Example: `# Apply a default dubboctl installation.
+dubboctl install
+
+# Apply a default profile.
+dubboctl install --profile=default
+
+# Apply a config file.
+dubboctl install -f my-config.yaml
 		`,
 		Aliases: []string{"apply"},
 		Args:    cobra.ExactArgs(0),
@@ -63,10 +70,12 @@ func InstallCmdWithArgs(ctx cli.Context, rootArgs *RootArgs, iArgs *installArgs)
 			}
 			p := NewPrinterForWriter(cmd.OutOrStderr())
 			cl := clog.NewConsoleLogger(cmd.OutOrStdout(), cmd.ErrOrStderr(), installerScope)
-			p.Printf("%v\n", art.DubboArt())
+			p.Printf("%v\n", art.DubboColoredArt())
 			return Install(kubeClient, rootArgs, iArgs, cl, cmd.OutOrStdout(), p)
 		},
 	}
+	addFlags(ic, rootArgs)
+	addInstallFlags(ic, iArgs)
 	return ic
 }
 
diff --git a/operator/cmd/cluster/root.go b/operator/cmd/cluster/root.go
index 23d7ff87..69bf9320 100644
--- a/operator/cmd/cluster/root.go
+++ b/operator/cmd/cluster/root.go
@@ -22,6 +22,6 @@ func AddRootFlags(cmd *cobra.Command) *RootFlags {
 	return rootFlags
 }
 
-func AddFlags(cmd *cobra.Command, rootArgs *RootArgs) {
-	cmd.Flags().BoolVar(&rootArgs.DryRun, "dry-run", false, "")
+func addFlags(cmd *cobra.Command, rootArgs *RootArgs) {
+	cmd.Flags().BoolVar(&rootArgs.DryRun, "dry-run", false, `Outputs only the console/log without making any changes`)
 }
diff --git a/operator/cmd/cluster/uninstall.go b/operator/cmd/cluster/uninstall.go
index 5133d6a5..00794f18 100644
--- a/operator/cmd/cluster/uninstall.go
+++ b/operator/cmd/cluster/uninstall.go
@@ -24,7 +24,7 @@ func UninstallCmd(ctx cli.Context) *cobra.Command {
 			return uninstall(cmd, ctx, rootArgs, uiArgs)
 		},
 	}
-	AddFlags(uicmd, rootArgs)
+	addFlags(uicmd, rootArgs)
 	addUninstallFlags()
 	return uicmd
 }
diff --git a/operator/cmd/validation/validation.go b/operator/cmd/validation/validation.go
index c59ea24b..e9d9848a 100644
--- a/operator/cmd/validation/validation.go
+++ b/operator/cmd/validation/validation.go
@@ -7,13 +7,12 @@ import (
 	"github.com/apache/dubbo-kubernetes/operator/pkg/apis"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/util"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/values"
-	"github.com/apache/dubbo-kubernetes/pkg/kube"
 	"sigs.k8s.io/yaml"
 )
 
 type Warnings = util.Errors
 
-func ParseAndValidateDubboOperator(dopMap values.Map, client kube.Client) (Warnings, util.Errors) {
+func ParseAndValidateDubboOperator(dopMap values.Map) (Warnings, util.Errors) {
 	dop := &apis.DubboOperator{}
 	dec := json.NewDecoder(bytes.NewBufferString(dopMap.JSON()))
 	dec.DisallowUnknownFields()
diff --git a/operator/pkg/component/component.go b/operator/pkg/component/component.go
index ce91380c..b31b24f0 100644
--- a/operator/pkg/component/component.go
+++ b/operator/pkg/component/component.go
@@ -18,6 +18,7 @@ type Component struct {
 	Default        bool
 	HelmSubDir     string
 	HelmTreeRoot   string
+	FlattenValues  bool
 }
 
 var AllComponents = []Component{
@@ -63,6 +64,7 @@ func (c Component) Get(merged values.Map) ([]apis.MetadataCompSpec, error) {
 		if err != nil {
 			return apis.MetadataCompSpec{}, fmt.Errorf("fail to convert %v: %v", c.SpecName, err)
 		}
+
 		if spec.Namespace == "" {
 			spec.Namespace = defaultNamespace
 			spec.Namespace = "dubbo-system"
diff --git a/operator/pkg/install/installer.go b/operator/pkg/install/installer.go
index 36fd9689..8ddb4ead 100644
--- a/operator/pkg/install/installer.go
+++ b/operator/pkg/install/installer.go
@@ -1,15 +1,20 @@
 package install
 
 import (
+	"context"
+	"fmt"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/component"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/manifest"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/util/dmultierr"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/util/progress"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/values"
 	"github.com/apache/dubbo-kubernetes/pkg/kube"
+	"github.com/apache/dubbo-kubernetes/pkg/pointer"
 	"github.com/apache/dubbo-kubernetes/pkg/util/sets"
 	"github.com/apache/dubbo-kubernetes/pkg/util/slices"
 	"github.com/hashicorp/go-multierror"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/types"
 	"sync"
 )
 
@@ -94,7 +99,23 @@ func (i Installer) applyManifestSet(manifestSet manifest.ManifestSet) error {
 }
 
 func (i Installer) serverSideApply(obj manifest.Manifest) error {
-	const fieldOwner = "dubbo-operator"
+	var dryRun []string
+	const operatorFieldOwner = "dubbo-operator"
+	dc, err := i.Kube.DynamicClientFor(obj.GroupVersionKind(), obj.Unstructured, "")
+	if err != nil {
+		return err
+	}
+	objStr := fmt.Sprintf("%s/%s/%s", obj.GetKind(), obj.GetNamespace(), obj.GetName())
+	if i.DryRun {
+		return nil
+	}
+	if _, err := dc.Patch(context.TODO(), obj.GetName(), types.ApplyPatchType, []byte(obj.Content), metav1.PatchOptions{
+		DryRun:       dryRun,
+		Force:        pointer.Of(true),
+		FieldManager: operatorFieldOwner,
+	}); err != nil {
+		return fmt.Errorf("failed to update resource with server-side apply for obj %v: %v", objStr, err)
+	}
 	return nil
 }
 
diff --git a/operator/pkg/render/manifest.go b/operator/pkg/render/manifest.go
index 54897fe6..ccccab57 100644
--- a/operator/pkg/render/manifest.go
+++ b/operator/pkg/render/manifest.go
@@ -17,7 +17,7 @@ import (
 	"strings"
 )
 
-func MergeInputs(filenames []string, flags []string, client kube.Client) (values.Map, error) {
+func MergeInputs(filenames []string, flags []string) (values.Map, error) {
 	ConfigBase, err := values.MapFromJSON([]byte(`{
 	  "apiVersion": "install.dubbo.io/v1alpha1",
 	  "kind": "DubboOperator",
@@ -39,9 +39,11 @@ func MergeInputs(filenames []string, flags []string, client kube.Client) (values
 		} else {
 			b, err = os.ReadFile(strings.TrimSpace(fn))
 		}
+
 		if err := checkDops(string(b)); err != nil {
 			return nil, fmt.Errorf("checkDops err:%v", err)
 		}
+
 		m, err := values.MapFromYAML(b)
 		if err != nil {
 			return nil, fmt.Errorf("yaml Unmarshal err:%v", err)
@@ -110,17 +112,19 @@ func checkDops(s string) error {
 		return fmt.Errorf("unable to parse file: %v", err)
 	}
 	if len(mfs) > 1 {
-		return fmt.Errorf("")
+		return fmt.Errorf("contains multiple DubboOperator CRs, only one per file is supported")
 	}
 	return nil
 }
+
 func GenerateManifest(files []string, setFlags []string, logger clog.Logger, client kube.Client) ([]manifest.ManifestSet, values.Map, error) {
 	var chartWarnings util.Errors
-	merged, err := MergeInputs(files, setFlags, client)
+	merged, err := MergeInputs(files, setFlags)
 	if err != nil {
 		return nil, nil, fmt.Errorf("merge inputs: %v", err)
 	}
-	if err := validateDubboOperator(merged, client, logger); err != nil {
+
+	if err := validateDubboOperator(merged, logger); err != nil {
 		return nil, nil, fmt.Errorf("validateDubboOperator err:%v", err)
 	}
 	allManifests := map[component.Name]manifest.ManifestSet{}
@@ -165,8 +169,8 @@ func GenerateManifest(files []string, setFlags []string, logger clog.Logger, cli
 	return val, merged, nil
 }
 
-func validateDubboOperator(dop values.Map, client kube.Client, logger clog.Logger) error {
-	warnings, errs := validation.ParseAndValidateDubboOperator(dop, client)
+func validateDubboOperator(dop values.Map, logger clog.Logger) error {
+	warnings, errs := validation.ParseAndValidateDubboOperator(dop)
 	if err := errs.ToErrors(); err != nil {
 		return err
 	}
@@ -179,12 +183,28 @@ func validateDubboOperator(dop values.Map, client kube.Client, logger clog.Logge
 }
 
 func applyComponentValuesToHelmValues(comp component.Component, spec apis.MetadataCompSpec, merged values.Map) values.Map {
+	root := comp.HelmTreeRoot
 	if spec.Namespace != "" {
 		spec.Namespace = "dubbo-system"
 	}
+	if comp.FlattenValues {
+		cv, f := merged.GetPathMap("spec.values." + root)
+		if f {
+			vals, _ := merged.GetPathMap("spec.values")
+			nv := values.Map{
+				"global": vals["global"],
+			}
+			for k, v := range vals {
+				_, isMap := v.(map[string]any)
+				if !isMap {
+					nv[k] = v
+				}
+			}
+			for k, v := range cv {
+				nv[k] = v
+			}
+			merged["spec"].(map[string]any)["values"] = nv
+		}
+	}
 	return merged
 }
-
-func postProcess(comp component.Component, manifests []manifest.Manifest, vals values.Map) ([]manifest.Manifest, error) {
-	return manifests, nil
-}
diff --git a/operator/pkg/render/postprocess.go b/operator/pkg/render/postprocess.go
new file mode 100644
index 00000000..ab156df2
--- /dev/null
+++ b/operator/pkg/render/postprocess.go
@@ -0,0 +1,11 @@
+package render
+
+import (
+	"github.com/apache/dubbo-kubernetes/operator/pkg/component"
+	"github.com/apache/dubbo-kubernetes/operator/pkg/manifest"
+	"github.com/apache/dubbo-kubernetes/operator/pkg/values"
+)
+
+func postProcess(comp component.Component, manifests []manifest.Manifest, vals values.Map) ([]manifest.Manifest, error) {
+	return manifests, nil
+}
diff --git a/pkg/art/atr.go b/pkg/art/atr.go
index 2e41ef25..8ae9d4cd 100644
--- a/pkg/art/atr.go
+++ b/pkg/art/atr.go
@@ -8,7 +8,7 @@ import (
 //go:embed dubbo-ascii.txt
 var dubboASCIIArt string
 
-func DubboArt() string {
+func dubboArt() string {
 	return dubboASCIIArt
 }