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 aa933507 [dubboctl] read profile info logic (#569)
aa933507 is described below

commit aa933507711070569ea792a432ebe5f9bc185d22
Author: Jian Zhong <[email protected]>
AuthorDate: Thu Jan 30 16:57:27 2025 +0800

    [dubboctl] read profile info logic (#569)
---
 operator/cmd/cluster/profile.go |  2 +-
 operator/cmd/cluster/shared.go  | 37 +++++++++++++++++------
 operator/pkg/apis/types.go      | 12 +++++---
 operator/pkg/helm/helm.go       | 66 +++++++++++++++++++++++++++++++++++++++--
 operator/pkg/util/common.go     |  7 +++++
 operator/pkg/util/yaml.go       | 37 +++++++++++++++++++++++
 6 files changed, 144 insertions(+), 17 deletions(-)

diff --git a/operator/cmd/cluster/profile.go b/operator/cmd/cluster/profile.go
index 524e77cc..3b6f175a 100644
--- a/operator/cmd/cluster/profile.go
+++ b/operator/cmd/cluster/profile.go
@@ -139,7 +139,7 @@ func profileShow(args []string, rootArgs *RootArgs, pdArgs 
*profileShowArgs, l c
                setFlags = append(setFlags, "profile="+args[0])
        }
 
-       y, _, err := GenerateConfig(pdArgs.filenames, setFlags, nil, l)
+       y, _, err := GenerateConfig(pdArgs.filenames, setFlags, l)
        if err != nil {
                return err
        }
diff --git a/operator/cmd/cluster/shared.go b/operator/cmd/cluster/shared.go
index 299e90c1..83d5d768 100644
--- a/operator/cmd/cluster/shared.go
+++ b/operator/cmd/cluster/shared.go
@@ -3,10 +3,11 @@ package cluster
 import (
        "fmt"
        dopv1alpha1 "github.com/apache/dubbo-kubernetes/operator/pkg/apis"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/helm"
+       "github.com/apache/dubbo-kubernetes/operator/pkg/util"
        "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
        "io"
        "io/ioutil"
-       "k8s.io/client-go/rest"
        "os"
        "strings"
 )
@@ -53,8 +54,7 @@ func NewPrinterForWriter(w io.Writer) Printer {
        return &writerPrinter{writer: w}
 }
 
-func GenerateConfig(filenames []string, setFlags []string,
-       kubeConfig *rest.Config, l clog.Logger) (string, 
*dopv1alpha1.DubboOperator, error) {
+func GenerateConfig(filenames []string, setFlags []string, l clog.Logger) 
(string, *dopv1alpha1.DubboOperator, error) {
        if err := validateSetFlags(setFlags); err != nil {
                return "", nil, err
        }
@@ -63,7 +63,7 @@ func GenerateConfig(filenames []string, setFlags []string,
        if err != nil {
                return "", nil, err
        }
-       return overlayYAMLStrings(profile, fy, setFlags, kubeConfig, l)
+       return overlayYAMLStrings(profile, fy, setFlags, l)
 }
 
 func validateSetFlags(setFlags []string) error {
@@ -78,18 +78,37 @@ func validateSetFlags(setFlags []string) error {
 
 func overlayYAMLStrings(
        profile string, fy string,
-       setFlags []string, kubeConfig *rest.Config, l clog.Logger,
+       setFlags []string, l clog.Logger,
 ) (string, *dopv1alpha1.DubboOperator, error) {
-       dopsString, dops, err := GenDOPFromProfile(profile, fy, setFlags, 
kubeConfig, l)
+       dopsString, dops, err := GenDOPFromProfile(profile, fy, setFlags, l)
        if err != nil {
                return "", nil, err
        }
-
        return dopsString, dops, nil
 }
 
-func GenDOPFromProfile(profileOrPath, fileOverlayYAML string, setFlags 
[]string, kubeConfig *rest.Config, l clog.Logger) (string, 
*dopv1alpha1.DubboOperator, error) {
-       return "", nil, nil
+func GenDOPFromProfile(profileOrPath, fileOverlayYAML string, setFlags 
[]string, l clog.Logger) (string, *dopv1alpha1.DubboOperator, error) {
+       outYAML, err := helm.GetProfileYAML("", profileOrPath)
+       if err != nil {
+               return "", nil, err
+       }
+       finalIOP, err := unmarshalDOP(outYAML, false, l)
+       if err != nil {
+               return "", nil, err
+       }
+
+       if finalIOP.Spec.Profile == "" {
+               finalIOP.Spec.Profile = DefaultProfileName
+       }
+       return util.ToYAMLWithJSONPB(finalIOP), finalIOP, nil
+}
+
+func unmarshalDOP(dopsYAML string, allowUnknownField bool, l clog.Logger) 
(*dopv1alpha1.DubboOperator, error) {
+       dop := &dopv1alpha1.DubboOperator{}
+       if err := util.UnmarshalWithJSONPB(dopsYAML, dop, allowUnknownField); 
err != nil {
+               return nil, fmt.Errorf("could not unmarshal merged YAML: 
%s\n\nYAML:\n%s", err, dopsYAML)
+       }
+       return dop, nil
 }
 
 func readYamlProfile(filenames []string, setFlags []string, l clog.Logger) 
(string, string, error) {
diff --git a/operator/pkg/apis/types.go b/operator/pkg/apis/types.go
index 14424451..2eda62a3 100644
--- a/operator/pkg/apis/types.go
+++ b/operator/pkg/apis/types.go
@@ -2,7 +2,7 @@ package apis
 
 import (
        "encoding/json"
-
+       proto "github.com/gogo/protobuf/proto"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
@@ -47,9 +47,13 @@ type BoolValue struct {
        bool
 }
 
-func (b *BoolValue) MarshalJSON() ([]byte, error) {
-       return json.Marshal(b.GetValueOrFalse())
-}
+func (d *DubboOperator) Reset() { *d = DubboOperator{} }
+
+func (d *DubboOperator) String() string { return proto.CompactTextString(d) }
+
+func (*DubboOperator) ProtoMessage() {}
+
+func (b *BoolValue) MarshalJSON() ([]byte, error) { return 
json.Marshal(b.GetValueOrFalse()) }
 
 func (b *BoolValue) UnmarshalJSON(bytes []byte) error {
        bb := false
diff --git a/operator/pkg/helm/helm.go b/operator/pkg/helm/helm.go
index 8cd4cfb2..146a2d43 100644
--- a/operator/pkg/helm/helm.go
+++ b/operator/pkg/helm/helm.go
@@ -13,6 +13,7 @@ import (
        "helm.sh/helm/v3/pkg/chartutil"
        "helm.sh/helm/v3/pkg/engine"
        "io/fs"
+       "io/ioutil"
        "os"
        "path/filepath"
        "sort"
@@ -22,9 +23,10 @@ import (
 const (
        // NotesFileNameSuffix is the file name suffix for helm notes.
        // see https://helm.sh/docs/chart_template_guide/notes_files/
-       NotesFileNameSuffix = ".txt"
-       BaseChartName       = "base"
-       profilesDirName     = "profiles"
+       NotesFileNameSuffix    = ".txt"
+       BaseChartName          = "base"
+       profilesDirName        = "profiles"
+       DefaultProfileFilename = "default.yaml"
 )
 
 type Warnings = util.Errors
@@ -164,3 +166,61 @@ func stringBoolMapToSlice(m map[string]bool) []string {
        }
        return s
 }
+
+func GetProfileYAML(installPackagePath, profileOrPath string) (string, error) {
+       if profileOrPath == "" {
+               profileOrPath = "default"
+       }
+       profiles, err := readProfiles(installPackagePath)
+       if err != nil {
+               return "", fmt.Errorf("failed to read profiles: %v", err)
+       }
+       if profiles[profileOrPath] && installPackagePath != "" {
+               profileOrPath = filepath.Join(installPackagePath, "profiles", 
profileOrPath+".yaml")
+       }
+       baseCRYAML, err := ReadProfileYAML(profileOrPath, installPackagePath)
+       if err != nil {
+               return "", err
+       }
+
+       return baseCRYAML, nil
+}
+
+func readFile(path string) (string, error) {
+       b, err := ioutil.ReadFile(path)
+       return string(b), err
+}
+
+func ReadProfileYAML(profile, manifestsPath string) (string, error) {
+       var err error
+       var globalValues string
+
+       switch {
+       case util.IsFilePath(profile):
+               if globalValues, err = readFile(profile); err != nil {
+                       return "", err
+               }
+       default:
+               if globalValues, err = LoadValues(profile, manifestsPath); err 
!= nil {
+                       return "", fmt.Errorf("failed to read profile %v from 
%v: %v", profile, manifestsPath, err)
+               }
+       }
+
+       return globalValues, nil
+}
+
+func LoadValues(profileName string, chartsDir string) (string, error) {
+       path := strings.Join([]string{profilesDirName, 
builtinProfileToFilename(profileName)}, "/")
+       by, err := fs.ReadFile(manifests.BuiltinDir(chartsDir), path)
+       if err != nil {
+               return "", err
+       }
+       return string(by), nil
+}
+
+func builtinProfileToFilename(name string) string {
+       if name == "" {
+               return DefaultProfileFilename
+       }
+       return name + ".yaml"
+}
diff --git a/operator/pkg/util/common.go b/operator/pkg/util/common.go
new file mode 100644
index 00000000..e9a38104
--- /dev/null
+++ b/operator/pkg/util/common.go
@@ -0,0 +1,7 @@
+package util
+
+import "strings"
+
+func IsFilePath(path string) bool {
+       return strings.Contains(path, "/") || strings.Contains(path, ".")
+}
diff --git a/operator/pkg/util/yaml.go b/operator/pkg/util/yaml.go
new file mode 100644
index 00000000..63b43217
--- /dev/null
+++ b/operator/pkg/util/yaml.go
@@ -0,0 +1,37 @@
+package util
+
+import (
+       "bytes"
+       "github.com/gogo/protobuf/jsonpb"
+       "github.com/gogo/protobuf/proto"
+       "sigs.k8s.io/yaml"
+)
+
+func UnmarshalWithJSONPB(y string, out proto.Message, allowUnknownField bool) 
error {
+       if y == "" {
+               return nil
+       }
+       jb, err := yaml.YAMLToJSON([]byte(y))
+       if err != nil {
+               return err
+       }
+       u := jsonpb.Unmarshaler{AllowUnknownFields: allowUnknownField}
+       err = u.Unmarshal(bytes.NewReader(jb), out)
+       if err != nil {
+               return err
+       }
+       return nil
+}
+
+func ToYAMLWithJSONPB(val proto.Message) string {
+       m := jsonpb.Marshaler{EnumsAsInts: true}
+       js, err := m.MarshalToString(val)
+       if err != nil {
+               return err.Error()
+       }
+       yb, err := yaml.JSONToYAML([]byte(js))
+       if err != nil {
+               return err.Error()
+       }
+       return string(yb)
+}

Reply via email to