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/12 09:35:06 UTC

(dubbo-kubernetes) branch master updated: [operator] add yaml split logic

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 19d054a2 [operator] add yaml split logic
19d054a2 is described below

commit 19d054a23217f9d1e04250e993a4445ea55288db
Author: mfordjody <11...@qq.com>
AuthorDate: Thu Dec 12 17:34:38 2024 +0800

    [operator] add yaml split logic
---
 operator/cmd/cluster/install.go      |  1 +
 operator/manifest/manifest.go        | 63 ++++++++++++++++++++++++++++++++++++
 operator/manifest/render/manifest.go |  9 ++++++
 operator/pkg/yml/parts.go            | 33 +++++++++++++++++++
 4 files changed, 106 insertions(+)

diff --git a/operator/cmd/cluster/install.go b/operator/cmd/cluster/install.go
index 383c328c..0efeb988 100644
--- a/operator/cmd/cluster/install.go
+++ b/operator/cmd/cluster/install.go
@@ -53,5 +53,6 @@ func InstallCmdWithArgs(ctx cli.Context, rootArgs *RootArgs, iArgs *installArgs)
 }
 
 func install(kubeClient kube.CLIClient, rootArgs *RootArgs, iArgs *installArgs) error {
+
 	return nil
 }
diff --git a/operator/manifest/manifest.go b/operator/manifest/manifest.go
new file mode 100644
index 00000000..8d3fde4b
--- /dev/null
+++ b/operator/manifest/manifest.go
@@ -0,0 +1,63 @@
+package manifest
+
+import (
+	"encoding/json"
+	"github.com/apache/dubbo-kubernetes/operator/pkg/component"
+	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
+	"sigs.k8s.io/yaml"
+)
+
+type Manifest struct {
+	*unstructured.Unstructured
+	Content string
+}
+
+type ManifestSet struct {
+	Comps     component.Name
+	Manifests []Manifest
+}
+
+func FromJSON(j []byte) (Manifest, error) {
+	us := &unstructured.Unstructured{}
+	if err := json.Unmarshal(j, us); err != nil {
+		return Manifest{}, err
+	}
+	yml, err := yaml.Marshal(us)
+	if err != nil {
+		return Manifest{}, err
+	}
+	return Manifest{
+		Unstructured: us,
+		Content:      string(yml),
+	}, nil
+}
+
+func FromYAML(y []byte) (Manifest, error) {
+	us := &unstructured.Unstructured{}
+	if err := yaml.Unmarshal(y, us); err != nil {
+		return Manifest{}, err
+	}
+	return Manifest{
+		Unstructured: us,
+		Content:      string(y),
+	}, nil
+}
+
+func Parse(output []string) ([]Manifest, error) {
+	result := make([]Manifest, 0, len(output))
+	for _, m := range output {
+		mf, err := FromYAML([]byte(m))
+		if err != nil {
+			return nil, err
+		}
+		if mf.GetObjectKind().GroupVersionKind().Kind == "" {
+			continue
+		}
+		result = append(result, mf)
+	}
+	return result, nil
+}
+
+func ParseMultiple(output string) ([]Manifest, error) {
+	return Parse()
+}
diff --git a/operator/manifest/render/manifest.go b/operator/manifest/render/manifest.go
new file mode 100644
index 00000000..f4f337cb
--- /dev/null
+++ b/operator/manifest/render/manifest.go
@@ -0,0 +1,9 @@
+package render
+
+import (
+	"github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
+)
+
+func ()  {
+	
+}
\ No newline at end of file
diff --git a/operator/pkg/yml/parts.go b/operator/pkg/yml/parts.go
new file mode 100644
index 00000000..d3545fc4
--- /dev/null
+++ b/operator/pkg/yml/parts.go
@@ -0,0 +1,33 @@
+package yml
+
+import (
+	"bufio"
+	"strings"
+)
+
+func SplitString(yamlText string) []string {
+	out := make([]string, 0)
+	scanner := bufio.NewScanner(strings.NewReader(yamlText))
+	parts := []string{}
+	active := strings.Builder{}
+	for scanner.Scan() {
+		line := scanner.Text()
+		if strings.HasPrefix(line, "---") {
+			parts = append(parts, active.String())
+			active = strings.Builder{}
+		} else {
+			active.WriteString(line)
+			active.WriteString("\n")
+		}
+	}
+	if active.Len() > 0 {
+		parts = append(parts, active.String())
+	}
+	for _, part := range parts {
+		part := strings.TrimSpace(part)
+		if len(part) > 0 {
+			out = append(out, part)
+		}
+	}
+	return out
+}