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/02 04:02:49 UTC

(dubbo-kubernetes) branch master updated: [operator] Updating components and wrapping maps (#514)

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 f9a3c159 [operator] Updating components and wrapping maps (#514)
f9a3c159 is described below

commit f9a3c15902fe78c5283235c9629f23ac05c443ea
Author: mfordjody <11...@qq.com>
AuthorDate: Mon Dec 2 12:02:44 2024 +0800

    [operator] Updating components and wrapping maps (#514)
---
 operator/pkg/comp/comp.go  | 37 +++++++++++++++++++++++++++++++++++--
 operator/pkg/values/map.go | 13 +++++++++++--
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/operator/pkg/comp/comp.go b/operator/pkg/comp/comp.go
index 3e35109f..2e9aae9f 100644
--- a/operator/pkg/comp/comp.go
+++ b/operator/pkg/comp/comp.go
@@ -1,6 +1,7 @@
 package comp
 
 import (
+	"fmt"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/apis"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/values"
 )
@@ -47,6 +48,38 @@ func UserFacingCompName(name Name) string {
 }
 
 func (c Comp) Get(merged values.Map) ([]apis.MetadataCompSpec, error) {
-	defaultNamespace := merged
-	return nil, nil
+	defaultNamespace := merged.GetPathString("metadata.namespace")
+	var defaultResp []apis.MetadataCompSpec
+	def := c.Default
+	if def {
+		defaultResp = []apis.MetadataCompSpec{{
+			ComponentSpec: apis.ComponentSpec{
+				Namespace: defaultNamespace,
+			}},
+		}
+	}
+	buildSpec := func(m values.Map) (apis.MetadataCompSpec, error) {
+		spec, err := values.ConvertMap[apis.MetadataCompSpec](m)
+		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"
+		}
+		spec.Raw = m
+		return spec, nil
+	}
+	s, ok := merged.GetPathMap("spec.components." + c.SpecName)
+	if ok {
+		return defaultResp, nil
+	}
+	spec, err := buildSpec(s)
+	if err != nil {
+		return nil, err
+	}
+	if !(spec.Enabled.GetValueOrTrue()) {
+		return nil, nil
+	}
+	return []apis.MetadataCompSpec{spec}, nil
 }
diff --git a/operator/pkg/values/map.go b/operator/pkg/values/map.go
index 85b0221b..4232ff99 100644
--- a/operator/pkg/values/map.go
+++ b/operator/pkg/values/map.go
@@ -133,11 +133,16 @@ func splitPath(path string) []string {
 }
 
 func (m Map) GetPathString(s string) string {
-	return ""
+	return GetPathHelper[string](m, s)
 }
 
 func GetPathHelper[T any](m Map, name string) T {
-	return nil
+	v, ok := m.GetPath(name)
+	if !ok {
+		return pointer.Empty[T]()
+	}
+	t, _ := v.(T)
+	return t
 }
 
 func (m Map) GetPath(name string) (any, bool) {
@@ -209,6 +214,10 @@ func CastAsMap(current any) (Map, bool) {
 	return nil, false
 }
 
+func ConvertMap[T any](m Map) (T, error) {
+	return fromJSON[T]([]byte(m.JSON()))
+}
+
 func extractIndex(seg string) (int, bool) {
 	if !strings.HasPrefix(seg, "[") || !strings.HasSuffix(seg, "]") {
 		return 0, false