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 9cc251d2 [dubboctl] Complete the profile list command (#561)
9cc251d2 is described below
commit 9cc251d29a11370c223865c149a2b8babe5fa95d
Author: Jian Zhong <[email protected]>
AuthorDate: Sun Jan 26 02:20:24 2025 +0800
[dubboctl] Complete the profile list command (#561)
---
operator/cmd/cluster/profile.go | 44 +++++++++++++++++++++++++++++++++++++++++
operator/pkg/helm/helm.go | 35 ++++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+)
diff --git a/operator/cmd/cluster/profile.go b/operator/cmd/cluster/profile.go
index d377b7c5..167a13b7 100644
--- a/operator/cmd/cluster/profile.go
+++ b/operator/cmd/cluster/profile.go
@@ -2,11 +2,23 @@ package cluster
import (
"github.com/apache/dubbo-kubernetes/dubboctl/pkg/cli"
+ "github.com/apache/dubbo-kubernetes/operator/pkg/helm"
"github.com/spf13/cobra"
+ "sort"
)
+type profileListArgs struct {
+ 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 ProfileCmd(ctx cli.Context) *cobra.Command {
rootArgs := &RootArgs{}
+ plArgs := &profileListArgs{}
+ plc := profileListCmd(rootArgs, plArgs)
pc := &cobra.Command{
Use: "profile",
Short: "Commands related to Dubbo configuration profiles",
@@ -15,5 +27,37 @@ func ProfileCmd(ctx cli.Context) *cobra.Command {
"dubboctl install --set profile=demo # Use a profile
from the list",
}
AddFlags(pc, rootArgs)
+ pc.AddCommand(plc)
return pc
}
+
+func profileListCmd(rootArgs *RootArgs, plArgs *profileListArgs)
*cobra.Command {
+ return &cobra.Command{
+ Use: "list",
+ Short: "Lists available Dubbo configuration profiles",
+ Long: "The list subcommand lists the available Dubbo
configuration profiles.",
+ Args: cobra.ExactArgs(0),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return profileList(cmd, rootArgs, plArgs)
+ },
+ }
+}
+
+func profileList(cmd *cobra.Command, args *RootArgs, plArgs *profileListArgs)
error {
+ profiles, err := helm.ListProfiles(plArgs.manifestsPath)
+ if err != nil {
+ return err
+ }
+
+ if len(profiles) == 0 {
+ cmd.Println("No profiles available.")
+ } else {
+ cmd.Println("Dubbo configuration profiles:")
+ sort.Strings(profiles)
+ for _, profile := range profiles {
+ cmd.Printf(" %s\n", profile)
+ }
+ }
+
+ return nil
+}
diff --git a/operator/pkg/helm/helm.go b/operator/pkg/helm/helm.go
index 0d848b13..8cd4cfb2 100644
--- a/operator/pkg/helm/helm.go
+++ b/operator/pkg/helm/helm.go
@@ -24,6 +24,7 @@ const (
// see https://helm.sh/docs/chart_template_guide/notes_files/
NotesFileNameSuffix = ".txt"
BaseChartName = "base"
+ profilesDirName = "profiles"
)
type Warnings = util.Errors
@@ -129,3 +130,37 @@ func getFilesRecursive(f fs.FS, root string) ([]string,
error) {
})
return result, err
}
+
+func readProfiles(chartsDir string) (map[string]bool, error) {
+ profiles := map[string]bool{}
+ f := manifests.BuiltinDir(chartsDir)
+ dir, err := fs.ReadDir(f, profilesDirName)
+ if err != nil {
+ return nil, err
+ }
+ for _, f := range dir {
+ trimmedString := strings.TrimSuffix(f.Name(), ".yaml")
+ if f.Name() != trimmedString {
+ profiles[trimmedString] = true
+ }
+ }
+ return profiles, nil
+}
+
+func ListProfiles(charts string) ([]string, error) {
+ profiles, err := readProfiles(charts)
+ if err != nil {
+ return nil, err
+ }
+ return stringBoolMapToSlice(profiles), nil
+}
+
+func stringBoolMapToSlice(m map[string]bool) []string {
+ s := make([]string, 0, len(m))
+ for k, v := range m {
+ if v {
+ s = append(s, k)
+ }
+ }
+ return s
+}