Skip to content

Commit

Permalink
[operator] add yaml split logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mfordjody committed Dec 12, 2024
1 parent d7702f1 commit 19d054a
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions operator/cmd/cluster/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
63 changes: 63 additions & 0 deletions operator/manifest/manifest.go
Original file line number Diff line number Diff line change
@@ -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()

Check failure on line 62 in operator/manifest/manifest.go

View workflow job for this annotation

GitHub Actions / Go Test

not enough arguments in call to Parse
}
9 changes: 9 additions & 0 deletions operator/manifest/render/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package render

import (
"github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
)

func () {

Check failure on line 7 in operator/manifest/render/manifest.go

View workflow job for this annotation

GitHub Actions / Go fmt

expected 'IDENT', found '{'

}

Check failure on line 9 in operator/manifest/render/manifest.go

View workflow job for this annotation

GitHub Actions / Go fmt

expected '(', found '}'
33 changes: 33 additions & 0 deletions operator/pkg/yml/parts.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 19d054a

Please sign in to comment.