diff --git a/operator/cmd/validation/validation.go b/operator/cmd/validation/validation.go new file mode 100644 index 00000000..7e6fbefc --- /dev/null +++ b/operator/cmd/validation/validation.go @@ -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) { + 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)) + } + var warnings Warnings + var errors util.Errors + vw, ve := validateValues(dop) + warnings = util.AppendErrs(warnings, vw) + errors = util.AppendErrs(errors, ve) + 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 +} diff --git a/operator/manifest/render/manifest.go b/operator/manifest/render/manifest.go index f4f337cb..d407bb74 100644 --- a/operator/manifest/render/manifest.go +++ b/operator/manifest/render/manifest.go @@ -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 () { - -} \ No newline at end of file +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 +}