Skip to content

Commit

Permalink
[dubboctl] add profile show logic (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfordjody authored Jan 30, 2025
1 parent 824babb commit e2199e5
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 6 deletions.
4 changes: 2 additions & 2 deletions operator/cmd/cluster/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion operator/cmd/cluster/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
79 changes: 76 additions & 3 deletions operator/cmd/cluster/profile.go
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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),
Expand All @@ -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 {
Expand All @@ -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
}
102 changes: 102 additions & 0 deletions operator/cmd/cluster/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

0 comments on commit e2199e5

Please sign in to comment.