Skip to content

Commit

Permalink
[operator] add validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mfordjody committed Dec 12, 2024
1 parent 19d054a commit 0484e29
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
32 changes: 32 additions & 0 deletions operator/cmd/validation/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package validation

import (
"bytes"
"encoding/json"
"github.com/apache/dubbo-kubernetes/operator/pkg/apis"
"github.com/apache/dubbo-kubernetes/operator/pkg/values"
)

func ParseAndValidateDubboOperator(dopMap values.Map) (Warnings, util.Errors) {

Check failure on line 10 in operator/cmd/validation/validation.go

View workflow job for this annotation

GitHub Actions / Go Test

undefined: Warnings

Check failure on line 10 in operator/cmd/validation/validation.go

View workflow job for this annotation

GitHub Actions / Go Test

undefined: util
dop := &apis.DubboOperator{}
dec := json.NewDecoder(bytes.NewBufferString(dopMap.JSON()))
dec.DisallowUnknownFields()
if err := dec.Decode(dop); err != nil {
return nil, util.NewErrs(fmt.Errorf("could not unmarshal: %v", err))

Check failure on line 15 in operator/cmd/validation/validation.go

View workflow job for this annotation

GitHub Actions / Go Test

undefined: util

Check failure on line 15 in operator/cmd/validation/validation.go

View workflow job for this annotation

GitHub Actions / Go Test

undefined: fmt
}
var warnings Warnings

Check failure on line 17 in operator/cmd/validation/validation.go

View workflow job for this annotation

GitHub Actions / Go Test

undefined: Warnings
var errors util.Errors

Check failure on line 18 in operator/cmd/validation/validation.go

View workflow job for this annotation

GitHub Actions / Go Test

undefined: util
vw, ve := validateValues(dop)
warnings = util.AppendErrs(warnings, vw)

Check failure on line 20 in operator/cmd/validation/validation.go

View workflow job for this annotation

GitHub Actions / Go Test

undefined: util
errors = util.AppendErrs(errors, ve)

Check failure on line 21 in operator/cmd/validation/validation.go

View workflow job for this annotation

GitHub Actions / Go Test

undefined: util
errors = util.AppendErr(errors, validateComponentNames(dop.Spec.Components))
return warnings, errors
}

func validateValues(raw *apis.DubboOperator) (Warnings, util.Errors) {
v := &apis.Values{}
if err := yaml.Unmarshal(raw.Spec.Values, v); err != nil {
return nil, util.NewErrs(fmt.Errorf("could not unmarshal: %v", err))
}
return nil, nil
}
62 changes: 59 additions & 3 deletions operator/manifest/render/manifest.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
package render

import (
"fmt"
"github.com/apache/dubbo-kubernetes/operator/manifest"
"github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
"github.com/apache/dubbo-kubernetes/operator/pkg/values"
"io"
"os"
"strings"
)

func () {

}
func MergeInputs(filenames []string, flags []string) ([]values.Map, error) {
ConfigBase, err := values.MapFromJSON([]byte(`{
"apiVersion": "install.dubbo.io/v1alpha1",
"kind": "DubboOperator",
"metadata": {},
"spec": {}
}`))
if err != nil {
return nil, err
}
for i, fn := range filenames {
var b []byte
var err error
if fn == "-" {
if i != len(filenames)-1 {
return nil, fmt.Errorf("stdin is only allowed as the last filename")
}
b, err = io.ReadAll(os.Stdin)
} else {
b, err = os.ReadFile(strings.TrimSpace(fn))
}
}
return nil, nil
}
func checkDops(s string) error {
mfs, err := manifest.ParseMultiple(s)
if err != nil {
return fmt.Errorf("unable to parse file: %v", err)
}
if len(mfs) > 1 {
return fmt.Errorf("")
}
return nil
}
func GenerateManifest(files []string, setFlags []string, logger clog.Logger) ([]manifest.ManifestSet, values.Map, error) {
merged, err := MergeInputs(files, setFlags)
if err != nil {
return nil, nil, fmt.Errorf("merge inputs: %v", err)
}
}

func validateDubboOperator(dop values.Map, logger clog.Logger) error {
warnings, errs := validation.ParseAndValidateDubboOperator(dop)
if err := errs.ToError(); err != nil {
return err
}
if logger != nil {
for _, w := range warnings {
logger.LogAndErrorf("%s %v", "❗", w)
}
}
return nil
}

0 comments on commit 0484e29

Please sign in to comment.