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 e2199e5e [dubboctl] add profile show logic (#567)
e2199e5e is described below

commit e2199e5e4416d712f1e8d031bdc93049258a7341
Author: Jian Zhong <[email protected]>
AuthorDate: Thu Jan 30 13:50:59 2025 +0800

    [dubboctl] add profile show logic (#567)
---
 operator/cmd/cluster/install.go  |   4 +-
 operator/cmd/cluster/manifest.go |   2 +-
 operator/cmd/cluster/profile.go  |  79 ++++++++++++++++++++++++++++--
 operator/cmd/cluster/shared.go   | 102 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 181 insertions(+), 6 deletions(-)

diff --git a/operator/cmd/cluster/install.go b/operator/cmd/cluster/install.go
index 0fd4b932..c96640b7 100644
--- a/operator/cmd/cluster/install.go
+++ b/operator/cmd/cluster/install.go
@@ -30,14 +30,14 @@ type installArgs struct {
 
 func (i *installArgs) String() string {
        var b strings.Builder
-       b.WriteString("files:    " + (fmt.Sprint(i.files) + "\n"))
+       b.WriteString("filenames:    " + (fmt.Sprint(i.files) + "\n"))
        b.WriteString("sets:    " + (fmt.Sprint(i.sets) + "\n"))
        b.WriteString("waitTimeout: " + fmt.Sprint(i.waitTimeout) + "\n")
        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().StringSliceVarP(&args.files, "filenames", "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.`)
        cmd.PersistentFlags().BoolVarP(&args.skipConfirmation, 
"skip-confirmation", "y", false, `The skipConfirmation determines whether the 
user is prompted for confirmation.`)
        cmd.PersistentFlags().DurationVar(&args.waitTimeout, "wait-timeout", 
300*time.Second, "Maximum time to wait for Dubbo resources in each component to 
be ready.")
diff --git a/operator/cmd/cluster/manifest.go b/operator/cmd/cluster/manifest.go
index 7e513066..84581101 100644
--- a/operator/cmd/cluster/manifest.go
+++ b/operator/cmd/cluster/manifest.go
@@ -22,7 +22,7 @@ type manifestGenerateArgs struct {
 
 func (a *manifestGenerateArgs) String() string {
        var b strings.Builder
-       b.WriteString("files:   " + fmt.Sprint(a.files) + "\n")
+       b.WriteString("filenames:   " + fmt.Sprint(a.files) + "\n")
        b.WriteString("sets:           " + fmt.Sprint(a.sets) + "\n")
        b.WriteString("manifestPath: " + a.manifestPath + "\n")
        return b.String()
diff --git a/operator/cmd/cluster/profile.go b/operator/cmd/cluster/profile.go
index b06eb072..23ecd77d 100644
--- a/operator/cmd/cluster/profile.go
+++ b/operator/cmd/cluster/profile.go
@@ -1,24 +1,61 @@
 package cluster
 
 import (
+       "fmt"
        "github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
        "github.com/apache/dubbo-kubernetes/operator/pkg/helm"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
        "github.com/spf13/cobra"
        "sort"
 )
 
+const (
+       jsonOutput  = "json"
+       yamlOutput  = "yaml"
+       flagsOutput = "flags"
+)
+
+const (
+       dubboOperatorTreeString = `
+apiVersion: install.dubbo.io/v1alpha1
+kind: DubboOperator
+`
+)
+
+const (
+       DefaultProfileName = "default"
+)
+
 type profileListArgs struct {
        manifestsPath string
 }
 
+type profileShowArgs struct {
+       filenames     []string
+       configPath    string
+       outputFormat  string
+       manifestsPath string
+}
+
 func addProfileListFlags(cmd *cobra.Command, args *profileListArgs) {
        cmd.PersistentFlags().StringVarP(&args.manifestsPath, "manifests", "d", 
"", "Specify a path to a directory of charts and profiles")
 }
 
+func addProfileShowFlags(cmd *cobra.Command, args *profileShowArgs) {
+       cmd.PersistentFlags().StringSliceVarP(&args.filenames, "filenames", 
"f", nil, "")
+       cmd.PersistentFlags().StringVarP(&args.configPath, "config-path", "p", 
"",
+               "The path the root of the configuration subtree to dump e.g. 
components.pilot. By default, dump whole tree")
+       cmd.PersistentFlags().StringVarP(&args.outputFormat, "output", "o", 
yamlOutput,
+               "Output format: one of json|yaml|flags")
+       cmd.PersistentFlags().StringVarP(&args.manifestsPath, "manifests", "d", 
"", "")
+}
+
 func ProfileCmd(ctx cli.Context) *cobra.Command {
        rootArgs := &RootArgs{}
        plArgs := &profileListArgs{}
+       psArgs := &profileShowArgs{}
        plc := profileListCmd(rootArgs, plArgs)
+       psc := profileShowCmd(rootArgs, psArgs)
        pc := &cobra.Command{
                Use:   "profile",
                Short: "Commands related to Dubbo configuration profiles",
@@ -27,17 +64,16 @@ func ProfileCmd(ctx cli.Context) *cobra.Command {
                        "  dubboctl profile list\n" +
                        "  dubboctl install --set profile=demo",
        }
-
        pc.AddCommand(plc)
        addProfileListFlags(plc, plArgs)
-
+       addProfileShowFlags(psc, psArgs)
        AddFlags(pc, rootArgs)
        return pc
 }
 
 func profileListCmd(rootArgs *RootArgs, plArgs *profileListArgs) 
*cobra.Command {
        return &cobra.Command{
-               Use:   "list",
+               Use:   "list [<profile>]",
                Short: "Lists available Dubbo configuration profiles",
                Long:  "The list subcommand lists the available Dubbo 
configuration profiles.",
                Args:  cobra.ExactArgs(0),
@@ -47,6 +83,24 @@ func profileListCmd(rootArgs *RootArgs, plArgs 
*profileListArgs) *cobra.Command
        }
 }
 
+func profileShowCmd(rootArgs *RootArgs, pdArgs *profileShowArgs) 
*cobra.Command {
+       return &cobra.Command{
+               Use:   "show [<profile>]",
+               Short: "Show an Dubbo configuration profile",
+               Long:  "The show subcommand show the values in an Dubbo 
configuration profile.",
+               Args: func(cmd *cobra.Command, args []string) error {
+                       if len(args) > 1 {
+                               return fmt.Errorf("too many positional 
arguments")
+                       }
+                       return nil
+               },
+               RunE: func(cmd *cobra.Command, args []string) error {
+                       l := clog.NewConsoleLogger(cmd.OutOrStdout(), 
cmd.ErrOrStderr(), InstallerScope)
+                       return profileShow(args, rootArgs, pdArgs, l)
+               },
+       }
+}
+
 func profileList(cmd *cobra.Command, args *RootArgs, plArgs *profileListArgs) 
error {
        profiles, err := helm.ListProfiles(plArgs.manifestsPath)
        if err != nil {
@@ -64,3 +118,22 @@ func profileList(cmd *cobra.Command, args *RootArgs, plArgs 
*profileListArgs) er
        }
        return nil
 }
+
+func profileShow(args []string, rootArgs *RootArgs, pdArgs *profileShowArgs, l 
clog.Logger) error {
+       if len(args) == 1 && pdArgs.filenames != nil {
+               return fmt.Errorf("cannot specify both profile name and 
filename flag")
+       }
+
+       switch pdArgs.outputFormat {
+       case jsonOutput, yamlOutput, flagsOutput:
+       default:
+               return fmt.Errorf("unknown output format: %v", 
pdArgs.outputFormat)
+       }
+
+       setFlags := applyFlagAliases(make([]string, 0), pdArgs.manifestsPath)
+       if len(args) == 1 {
+               setFlags = append(setFlags, "profile="+args[0])
+       }
+
+       return nil
+}
diff --git a/operator/cmd/cluster/shared.go b/operator/cmd/cluster/shared.go
index 2cde9b49..c63c30dd 100644
--- a/operator/cmd/cluster/shared.go
+++ b/operator/cmd/cluster/shared.go
@@ -2,7 +2,12 @@ package cluster
 
 import (
        "fmt"
+       dopv1alpha1 "github.com/apache/dubbo-kubernetes/operator/pkg/apis"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
        "io"
+       "io/ioutil"
+       "k8s.io/client-go/rest"
+       "os"
        "strings"
 )
 
@@ -47,3 +52,100 @@ func OptionDeterminate(msg string, writer io.Writer) bool {
 func NewPrinterForWriter(w io.Writer) Printer {
        return &writerPrinter{writer: w}
 }
+
+func GenerateConfig(filenames []string, setFlags []string, force bool, 
kubeConfig *rest.Config,
+       l clog.Logger) (string, *dopv1alpha1.DubboOperator, error) {
+       if err := validateSetFlags(setFlags); err != nil {
+               return "", nil, err
+       }
+
+       _, _, err := readYamlProfile(filenames, setFlags, force, l)
+       if err != nil {
+               return "", nil, err
+       }
+
+       return "", nil, err
+}
+
+func validateSetFlags(setFlags []string) error {
+       for _, sf := range setFlags {
+               pv := strings.Split(sf, "=")
+               if len(pv) != 2 {
+                       return fmt.Errorf("set flag %s has incorrect format, 
must be path=value", sf)
+               }
+       }
+       return nil
+}
+
+func readYamlProfile(filenames []string, setFlags []string, force bool, l 
clog.Logger) (string, string, error) {
+       profile := DefaultProfileName
+       fy, fp, err := ParseYAMLfilenames(filenames, force, l)
+       if err != nil {
+               return "", "", err
+       }
+       if fp != "" {
+               profile = fp
+       }
+       psf := GetValueForSetFlag(setFlags, "profile")
+       if psf != "" {
+               profile = psf
+       }
+       return fy, profile, nil
+}
+
+func ParseYAMLfilenames(filenames []string, force bool, l clog.Logger) 
(overlayYAML string, profile string, err error) {
+       if filenames == nil {
+               return "", "", nil
+       }
+       y, err := ReadLayeredYAMLs(filenames)
+       if err != nil {
+               return "", "", err
+       }
+       return y, profile, nil
+}
+
+func ReadLayeredYAMLs(filenames []string) (string, error) {
+       return readLayeredYAMLs(filenames, os.Stdin)
+}
+
+func readLayeredYAMLs(filenames []string, stdinReader io.Reader) (string, 
error) {
+       var ly string
+       var stdin bool
+       for _, fn := range filenames {
+               var _ []byte
+               var err error
+               if fn == "-" {
+                       if stdin {
+                               continue
+                       }
+                       stdin = true
+                       _, err = ioutil.ReadAll(stdinReader)
+               } else {
+                       _, err = ioutil.ReadFile(strings.TrimSpace(fn))
+               }
+               if err != nil {
+                       return "", err
+               }
+       }
+       return ly, nil
+}
+
+func GetValueForSetFlag(setFlags []string, path string) string {
+       ret := ""
+       for _, sf := range setFlags {
+               p, v := getPV(sf)
+               if p == path {
+                       ret = v
+               }
+       }
+       return ret
+}
+
+func getPV(setFlag string) (path string, value string) {
+       pv := strings.Split(setFlag, "=")
+       if len(pv) != 2 {
+               return setFlag, ""
+       }
+       path, value = strings.TrimSpace(pv[0]), strings.TrimSpace(pv[1])
+       return
+}

Reply via email to