From 833225cb13c66928eaf2cbe246637265b5fe7fe6 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Thu, 6 Jul 2023 20:22:14 +0530 Subject: [PATCH 1/9] update schema generation Signed-off-by: MUzairS15 --- go.mod | 6 ++-- go.sum | 16 +++++----- utils/component/generator.go | 61 ++++++++++++++++++++++++++++++++++++ utils/component/utils.go | 8 +++++ 4 files changed, 80 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 59e0d3a9..2ed86e7e 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ replace ( ) require ( - cuelang.org/go v0.4.3 + cuelang.org/go v0.5.0 github.com/compose-spec/compose-go v1.0.8 github.com/docker/compose/v2 v2.2.0 github.com/go-git/go-git/v5 v5.4.2 @@ -99,7 +99,7 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/chai2010/gettext-go v1.0.2 // indirect - github.com/cockroachdb/apd/v2 v2.0.1 // indirect + github.com/cockroachdb/apd/v2 v2.0.2 // indirect github.com/containerd/cgroups v1.0.4 // indirect github.com/containerd/containerd v1.6.19 // indirect github.com/cyphar/filepath-securejoin v0.2.3 // indirect @@ -177,7 +177,7 @@ require ( github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/mitchellh/go-wordwrap v1.0.0 // indirect + github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/moby/locker v1.0.1 // indirect diff --git a/go.sum b/go.sum index 71cc1e91..83910102 100644 --- a/go.sum +++ b/go.sum @@ -40,8 +40,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= -cuelang.org/go v0.4.3 h1:W3oBBjDTm7+IZfCKZAmC8uDG0eYfJL4Pp/xbbCMKaVo= -cuelang.org/go v0.4.3/go.mod h1:7805vR9H+VoBNdWFdI7jyDR3QLUPp4+naHfbcgp55HI= +cuelang.org/go v0.5.0 h1:D6N0UgTGJCOxFKU8RU+qYvavKNsVc/+ZobmifStVJzU= +cuelang.org/go v0.5.0/go.mod h1:okjJBHFQFer+a41sAe2SaGm1glWS8oEb6CmJvn5Zdws= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/AlecAivazis/survey/v2 v2.3.6 h1:NvTuVHISgTHEHeBFqt6BHOe4Ny/NwGZr7w+F8S9ziyw= github.com/AlecAivazis/survey/v2 v2.3.6/go.mod h1:4AuI9b7RjAR+G7v9+C4YSlX/YL3K3cWNXgWXOhllqvI= @@ -212,8 +212,8 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnht github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= -github.com/cockroachdb/apd/v2 v2.0.1 h1:y1Rh3tEU89D+7Tgbw+lp52T6p/GJLpDmNvr10UWqLTE= -github.com/cockroachdb/apd/v2 v2.0.1/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw= +github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E= +github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= github.com/compose-spec/compose-go v1.0.8 h1:fgT7mYYu5Sp37i2lUIAAvwJpkAHk6dP5ITHy/LlutUk= @@ -431,7 +431,7 @@ github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= github.com/emicklei/go-restful/v3 v3.9.0 h1:XwGDlfxEnQZzuopoqxwSEllNcCOM9DhhFyhFIIGKwxE= github.com/emicklei/go-restful/v3 v3.9.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= -github.com/emicklei/proto v1.6.15 h1:XbpwxmuOPrdES97FrSfpyy67SSCV/wBIKXqgJzh6hNw= +github.com/emicklei/proto v1.10.0 h1:pDGyFRVV5RvV+nkBK9iy3q67FBy9Xa7vwrOTE+g5aGw= github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -961,8 +961,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= -github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4= -github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= +github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= +github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= github.com/mitchellh/hashstructure v1.0.0/go.mod h1:QjSHrPWS+BGUVBYkbTZWEnOh3G1DutKwClXU/ABz6AQ= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= @@ -1167,7 +1167,7 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= -github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc h1:gSVONBi2HWMFXCa9jFdYvYk7IwW/mTLxWOF7rXS4LO0= +github.com/protocolbuffers/txtpbfmt v0.0.0-20220428173112-74888fd59c2b h1:zd/2RNzIRkoGGMjE+YIsZ85CnDIz672JK2F3Zl4vux4= github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 h1:MkV+77GLUNo5oJ0jf870itWm3D0Sjh7+Za9gazKc5LQ= github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= diff --git a/utils/component/generator.go b/utils/component/generator.go index 0ae123de..f4c4bac7 100644 --- a/utils/component/generator.go +++ b/utils/component/generator.go @@ -1,8 +1,10 @@ package component import ( + "encoding/json" "fmt" + "cuelang.org/go/cue" "github.com/layer5io/meshkit/models/meshmodel/core/v1alpha1" "github.com/layer5io/meshkit/utils" "github.com/layer5io/meshkit/utils/manifests" @@ -17,6 +19,7 @@ type CuePathConfig struct { VersionPath string SpecPath string ScopePath string + PropertiesPath string // identifiers are the values that uniquely identify a CRD (in most of the cases, it is the 'Name' field) IdentifierPath string } @@ -28,6 +31,7 @@ var DefaultPathConfig = CuePathConfig{ GroupPath: "spec.group", ScopePath: "spec.scope", SpecPath: "spec.versions[0].schema.openAPIV3Schema.properties.spec", + PropertiesPath: "spec.versions[0].schema.openAPIV3Schema.properties", } var DefaultPathConfig2 = CuePathConfig{ @@ -86,3 +90,60 @@ func Generate(crd string) (v1alpha1.ComponentDefinition, error) { component.DisplayName = manifests.FormatToReadableString(name) return component, nil } + +func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[string]interface{} { +// if any error occurs while updating schema properties, we return nil and skip update + fmt.Println("uzair---------", group) + rootPath := fieldVal.Path().Selectors() + + // if !strings.Contains(group, "istio") { + // return nil + // } + compProperties := fieldVal.LookupPath(cuePath) + crd, err := fieldVal.MarshalJSON() + if err != nil { + return nil + } + + modified := make(map[string]interface{}) + pathSelectors := [][]cue.Selector{} + + err = json.Unmarshal(crd, &modified) + if err != nil { + return nil + } + + compProperties.Walk(func (c cue.Value) bool { + return true + }, func (c cue.Value) { + val := c.LookupPath(cue.ParsePath(`"x-kubernetes-preserve-unknown-fields"`)) + if val.Exists() { + fmt.Println("test_______", c.Value()) + child := val.Path().Selectors() + childM := child[len(rootPath):(len(child) - 1)] + pathSelectors = append(pathSelectors, childM) + } + }) + + for _, selectors := range pathSelectors { + m := modified + + fmt.Println("length: ", len(selectors), selectors) + for _, selector := range selectors { + s := selector.String() + if selector.Type() == cue.StringLabel { + s = selector.Unquoted() + } + m, _ = m[s].(map[string]interface{}) + } + fmt.Println(m, "before: ") + delete(m, "x-kubernetes-preserve-unknown-fields") + if m == nil { + m = make(map[string]interface{}) + } + m["type"] = "string" + fmt.Println(m, "after: ") + } + + return modified +} diff --git a/utils/component/utils.go b/utils/component/utils.go index c33fea7e..db59b1f4 100644 --- a/utils/component/utils.go +++ b/utils/component/utils.go @@ -27,6 +27,14 @@ func getSchema(parsedCrd cue.Value, pathConf CuePathConfig) (string, error) { if err != nil { return "", ErrGetSchema(err) } + + if pathConf.PropertiesPath != "" { + updatedProps := UpdateProperties(parsedCrd, cue.ParsePath(pathConf.PropertiesPath), resourceId) //cahnge to use group + if updatedProps != nil { + schema = updatedProps + } + } + (schema)["title"] = manifests.FormatToReadableString(resourceId) var output []byte output, err = json.MarshalIndent(schema, "", " ") From 4558ab75d847bb1e28c78e45db026985dee1393c Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 7 Jul 2023 02:43:17 +0530 Subject: [PATCH 2/9] root selectors for walk Signed-off-by: MUzairS15 --- utils/component/generator.go | 65 ++++++++++++++++++++---------------- utils/component/utils.go | 2 +- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/utils/component/generator.go b/utils/component/generator.go index f4c4bac7..ab3a0fb1 100644 --- a/utils/component/generator.go +++ b/utils/component/generator.go @@ -14,11 +14,11 @@ const ComponentMetaNameKey = "name" // all paths should be a valid CUE expression type CuePathConfig struct { - NamePath string - GroupPath string - VersionPath string - SpecPath string - ScopePath string + NamePath string + GroupPath string + VersionPath string + SpecPath string + ScopePath string PropertiesPath string // identifiers are the values that uniquely identify a CRD (in most of the cases, it is the 'Name' field) IdentifierPath string @@ -31,7 +31,7 @@ var DefaultPathConfig = CuePathConfig{ GroupPath: "spec.group", ScopePath: "spec.scope", SpecPath: "spec.versions[0].schema.openAPIV3Schema.properties.spec", - PropertiesPath: "spec.versions[0].schema.openAPIV3Schema.properties", + PropertiesPath: "properties", } var DefaultPathConfig2 = CuePathConfig{ @@ -92,19 +92,15 @@ func Generate(crd string) (v1alpha1.ComponentDefinition, error) { } func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[string]interface{} { -// if any error occurs while updating schema properties, we return nil and skip update - fmt.Println("uzair---------", group) + // if any error occurs while updating schema properties, we return nil and skip update rootPath := fieldVal.Path().Selectors() - - // if !strings.Contains(group, "istio") { - // return nil - // } + compProperties := fieldVal.LookupPath(cuePath) crd, err := fieldVal.MarshalJSON() if err != nil { return nil } - + modified := make(map[string]interface{}) pathSelectors := [][]cue.Selector{} @@ -113,37 +109,48 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[st return nil } - compProperties.Walk(func (c cue.Value) bool { + compProperties.Walk(func(c cue.Value) bool { return true - }, func (c cue.Value) { + }, func(c cue.Value) { val := c.LookupPath(cue.ParsePath(`"x-kubernetes-preserve-unknown-fields"`)) if val.Exists() { - fmt.Println("test_______", c.Value()) child := val.Path().Selectors() - childM := child[len(rootPath):(len(child) - 1)] + childM := child[len(rootPath):(len(child) - 1)] pathSelectors = append(pathSelectors, childM) } }) for _, selectors := range pathSelectors { - m := modified - - fmt.Println("length: ", len(selectors), selectors) - for _, selector := range selectors { + var m interface{} + m = modified + index := 0 + + for index < len(selectors) { + selector := selectors[index] + selectorType := selector.Type() s := selector.String() - if selector.Type() == cue.StringLabel { - s = selector.Unquoted() + + if selectorType == cue.IndexLabel { + t := m.([]interface{}) + token := selector.Index() + m = t[token].(map[string]interface{}) + } else { + if selectorType == cue.StringLabel { + s = selector.Unquoted() + } + t := m.(map[string]interface{}) + m = t[s] } - m, _ = m[s].(map[string]interface{}) + index++ } - fmt.Println(m, "before: ") - delete(m, "x-kubernetes-preserve-unknown-fields") + + t := m.(map[string]interface{}) + delete(t, "x-kubernetes-preserve-unknown-fields") if m == nil { m = make(map[string]interface{}) } - m["type"] = "string" - fmt.Println(m, "after: ") + t["type"] = "string" + t["format"] = "textarea" } - return modified } diff --git a/utils/component/utils.go b/utils/component/utils.go index db59b1f4..f6375603 100644 --- a/utils/component/utils.go +++ b/utils/component/utils.go @@ -29,7 +29,7 @@ func getSchema(parsedCrd cue.Value, pathConf CuePathConfig) (string, error) { } if pathConf.PropertiesPath != "" { - updatedProps := UpdateProperties(parsedCrd, cue.ParsePath(pathConf.PropertiesPath), resourceId) //cahnge to use group + updatedProps := UpdateProperties(specCueVal, cue.ParsePath(pathConf.PropertiesPath), resourceId) if updatedProps != nil { schema = updatedProps } From 0915d50e0b48f2527c8449eb346f5a4363cd4011 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 7 Jul 2023 13:04:02 +0530 Subject: [PATCH 3/9] add func description Signed-off-by: MUzairS15 --- utils/component/generator.go | 9 ++++++++- utils/component/utils.go | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/utils/component/generator.go b/utils/component/generator.go index ab3a0fb1..f7349621 100644 --- a/utils/component/generator.go +++ b/utils/component/generator.go @@ -91,8 +91,12 @@ func Generate(crd string) (v1alpha1.ComponentDefinition, error) { return component, nil } +/* + We walk the entire schema, looking for specfic peroperties that requires modification and store their path. + After the walk is complete, we iterate all paths and do the modification. + If any error occurs while updating schema properties, we return nil and skip the update. +*/ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[string]interface{} { - // if any error occurs while updating schema properties, we return nil and skip update rootPath := fieldVal.Path().Selectors() compProperties := fieldVal.LookupPath(cuePath) @@ -120,6 +124,9 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[st } }) + /* + "pathSelectors" contains all the paths from root to the property which needs to be modified. + */ for _, selectors := range pathSelectors { var m interface{} m = modified diff --git a/utils/component/utils.go b/utils/component/utils.go index f6375603..208c49d7 100644 --- a/utils/component/utils.go +++ b/utils/component/utils.go @@ -28,11 +28,11 @@ func getSchema(parsedCrd cue.Value, pathConf CuePathConfig) (string, error) { return "", ErrGetSchema(err) } - if pathConf.PropertiesPath != "" { - updatedProps := UpdateProperties(specCueVal, cue.ParsePath(pathConf.PropertiesPath), resourceId) - if updatedProps != nil { - schema = updatedProps - } + updatedProps := UpdateProperties(specCueVal, cue.ParsePath(pathConf.PropertiesPath), resourceId) + // In case of any error while updating properties, nil is returned. Hence use the original schema. + + if updatedProps != nil { + schema = updatedProps } (schema)["title"] = manifests.FormatToReadableString(resourceId) From 9cc0f3c8983b10d76d26e2168f6a80b71e1e6fa2 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 7 Jul 2023 15:23:38 +0530 Subject: [PATCH 4/9] lint fixes Signed-off-by: MUzairS15 --- utils/component/error.go | 11 ++++++++--- utils/component/generator.go | 38 ++++++++++++++++++++++++------------ utils/component/utils.go | 11 ++++++----- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/utils/component/error.go b/utils/component/error.go index 2d624908..4012dd0f 100644 --- a/utils/component/error.go +++ b/utils/component/error.go @@ -3,9 +3,10 @@ package component import "github.com/layer5io/meshkit/errors" const ( - ErrCrdGenerateCode = "11088" - ErrDefinitionCode = "11090" - ErrGetSchemaCode = "11091" + ErrCrdGenerateCode = "11088" + ErrDefinitionCode = "11090" + ErrGetSchemaCode = "11091" + ErrUpdateSchemaCode = "11092" ) func ErrCrdGenerate(err error) error { @@ -19,3 +20,7 @@ func ErrGetDefinition(err error) error { func ErrGetSchema(err error) error { return errors.New(ErrGetSchemaCode, errors.Alert, []string{"Could not get schema for the given CRD"}, []string{err.Error()}, []string{""}, []string{"Make sure that the given CRD is valid"}) } + +func ErrUpdateSchema(err error, obj string) error { + return errors.New(ErrUpdateSchemaCode, errors.Alert, []string{"Could not update schema for the given CRD ", obj}, []string{err.Error()}, []string{}, []string{"Make sure that the given CRD is valid"}) +} diff --git a/utils/component/generator.go b/utils/component/generator.go index f7349621..13525cf4 100644 --- a/utils/component/generator.go +++ b/utils/component/generator.go @@ -2,6 +2,7 @@ package component import ( "encoding/json" + "errors" "fmt" "cuelang.org/go/cue" @@ -92,17 +93,17 @@ func Generate(crd string) (v1alpha1.ComponentDefinition, error) { } /* - We walk the entire schema, looking for specfic peroperties that requires modification and store their path. - After the walk is complete, we iterate all paths and do the modification. - If any error occurs while updating schema properties, we return nil and skip the update. +We walk the entire schema, looking for specific peroperties that requires modification and store their path. +After the walk is complete, we iterate all paths and do the modification. +If any error occurs while updating schema properties, we return nil and skip the update. */ -func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[string]interface{} { +func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) (map[string]interface{}, error) { rootPath := fieldVal.Path().Selectors() compProperties := fieldVal.LookupPath(cuePath) crd, err := fieldVal.MarshalJSON() if err != nil { - return nil + return nil, ErrUpdateSchema(err, group) } modified := make(map[string]interface{}) @@ -110,7 +111,7 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[st err = json.Unmarshal(crd, &modified) if err != nil { - return nil + return nil, ErrUpdateSchema(err, group) } compProperties.Walk(func(c cue.Value) bool { @@ -124,7 +125,7 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[st } }) - /* + /* "pathSelectors" contains all the paths from root to the property which needs to be modified. */ for _, selectors := range pathSelectors { @@ -136,22 +137,33 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[st selector := selectors[index] selectorType := selector.Type() s := selector.String() - if selectorType == cue.IndexLabel { - t := m.([]interface{}) + t, ok := m.([]interface{}) + if !ok { + return nil, ErrUpdateSchema(errors.New("error converting to []interface{}"), group) + } token := selector.Index() - m = t[token].(map[string]interface{}) + m, ok = t[token].(map[string]interface{}) + if !ok { + return nil, ErrUpdateSchema(errors.New("error converting to map[string]interface{}"), group) + } } else { if selectorType == cue.StringLabel { s = selector.Unquoted() } - t := m.(map[string]interface{}) + t, ok := m.(map[string]interface{}) + if !ok { + return nil, ErrUpdateSchema(errors.New("error converting to map[string]interface{}"), group) + } m = t[s] } index++ } - t := m.(map[string]interface{}) + t, ok := m.(map[string]interface{}) + if !ok { + return nil, ErrUpdateSchema(errors.New("error converting to map[string]interface{}"), group) + } delete(t, "x-kubernetes-preserve-unknown-fields") if m == nil { m = make(map[string]interface{}) @@ -159,5 +171,5 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[st t["type"] = "string" t["format"] = "textarea" } - return modified + return modified, nil } diff --git a/utils/component/utils.go b/utils/component/utils.go index 208c49d7..fe811fcc 100644 --- a/utils/component/utils.go +++ b/utils/component/utils.go @@ -28,13 +28,14 @@ func getSchema(parsedCrd cue.Value, pathConf CuePathConfig) (string, error) { return "", ErrGetSchema(err) } - updatedProps := UpdateProperties(specCueVal, cue.ParsePath(pathConf.PropertiesPath), resourceId) - // In case of any error while updating properties, nil is returned. Hence use the original schema. - - if updatedProps != nil { - schema = updatedProps + updatedProps, err := UpdateProperties(specCueVal, cue.ParsePath(pathConf.PropertiesPath), resourceId) + + if err != nil { + return "", err } + schema = updatedProps + (schema)["title"] = manifests.FormatToReadableString(resourceId) var output []byte output, err = json.MarshalIndent(schema, "", " ") From 94401b7a42807e5df471af369dc617d1198ebf86 Mon Sep 17 00:00:00 2001 From: Lee Calcote Date: Fri, 7 Jul 2023 08:38:17 -0500 Subject: [PATCH 5/9] Update utils/component/generator.go Signed-off-by: Lee Calcote --- utils/component/generator.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/utils/component/generator.go b/utils/component/generator.go index 13525cf4..d9958ba9 100644 --- a/utils/component/generator.go +++ b/utils/component/generator.go @@ -93,9 +93,11 @@ func Generate(crd string) (v1alpha1.ComponentDefinition, error) { } /* -We walk the entire schema, looking for specific peroperties that requires modification and store their path. -After the walk is complete, we iterate all paths and do the modification. -If any error occurs while updating schema properties, we return nil and skip the update. +Find and modify specific schema properties. +1. Identify interesting properties by walking entire schema. +2. Store path to interesting properties. Finish walk. +3. Iterate all paths and modify properties. +5. If error occurs, return nil and skip modifications. */ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) (map[string]interface{}, error) { rootPath := fieldVal.Path().Selectors() From 77a2b906e8384d3127dfc92247e694386192ae55 Mon Sep 17 00:00:00 2001 From: Lee Calcote Date: Fri, 7 Jul 2023 08:40:05 -0500 Subject: [PATCH 6/9] Update utils/component/error.go Signed-off-by: Lee Calcote --- utils/component/error.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/component/error.go b/utils/component/error.go index 4012dd0f..65cd854d 100644 --- a/utils/component/error.go +++ b/utils/component/error.go @@ -22,5 +22,5 @@ func ErrGetSchema(err error) error { } func ErrUpdateSchema(err error, obj string) error { - return errors.New(ErrUpdateSchemaCode, errors.Alert, []string{"Could not update schema for the given CRD ", obj}, []string{err.Error()}, []string{}, []string{"Make sure that the given CRD is valid"}) + return errors.New(ErrUpdateSchemaCode, errors.Alert, []string{"Failed to update schema properties ", obj}, []string{err.Error()}, []string{}, []string{"Verify CRD has valid schema"}) } From 000fcaa539470247696616aeba5707fadbe0343a Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 7 Jul 2023 19:24:50 +0530 Subject: [PATCH 7/9] update meshkit errors Signed-off-by: MUzairS15 --- utils/component/error.go | 10 ++++++---- utils/component/generator.go | 4 +--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/utils/component/error.go b/utils/component/error.go index 65cd854d..a79c6d61 100644 --- a/utils/component/error.go +++ b/utils/component/error.go @@ -9,18 +9,20 @@ const ( ErrUpdateSchemaCode = "11092" ) +// No reference usage found. Also check in adapters before deleting func ErrCrdGenerate(err error) error { - return errors.New(ErrCrdGenerateCode, errors.Alert, []string{"Could not generate component with the given CRD"}, []string{err.Error()}, []string{""}, []string{"Make sure that the crds provided are all valid YAML"}) + return errors.New(ErrCrdGenerateCode, errors.Alert, []string{"Could not generate component with the given CRD"}, []string{err.Error()}, []string{""}, []string{"Verify CRD has valid schema."}) } +// No reference usage found. Also check in adapters before deleting func ErrGetDefinition(err error) error { - return errors.New(ErrDefinitionCode, errors.Alert, []string{"Could not get definition for the given CRD"}, []string{err.Error()}, []string{""}, []string{"Make sure that the given CRD is valid"}) + return errors.New(ErrDefinitionCode, errors.Alert, []string{"Could not get definition for the given CRD"}, []string{err.Error()}, []string{""}, []string{"Verify CRD has valid schema."}) } func ErrGetSchema(err error) error { - return errors.New(ErrGetSchemaCode, errors.Alert, []string{"Could not get schema for the given CRD"}, []string{err.Error()}, []string{""}, []string{"Make sure that the given CRD is valid"}) + return errors.New(ErrGetSchemaCode, errors.Alert, []string{"Could not get schema for the given CRD"}, []string{err.Error()}, []string{"Unable to marshal from cue value to valid JSON", "Unable to unmarshal from JSON to Go type"}, []string{"Verify CRD has valid schema.", "Malformed JSON", "CUE path to propery doesn't exist"}) } func ErrUpdateSchema(err error, obj string) error { - return errors.New(ErrUpdateSchemaCode, errors.Alert, []string{"Failed to update schema properties ", obj}, []string{err.Error()}, []string{}, []string{"Verify CRD has valid schema"}) + return errors.New(ErrUpdateSchemaCode, errors.Alert, []string{"Failed to update schema properties for ", obj}, []string{err.Error()}, []string{"Incorrect type assertion", "Selector.Unquoted might have been invoked on non-string label", "error during conversion from cue.Selector to string"}, []string{"Ensure correct type assertion", "Perform appropriate conversion from cue.Selector to string","Verify CRD has valid schema"}) } diff --git a/utils/component/generator.go b/utils/component/generator.go index d9958ba9..7202ab60 100644 --- a/utils/component/generator.go +++ b/utils/component/generator.go @@ -127,9 +127,7 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) (map[s } }) - /* - "pathSelectors" contains all the paths from root to the property which needs to be modified. - */ + // "pathSelectors" contains all the paths from root to the property which needs to be modified. for _, selectors := range pathSelectors { var m interface{} m = modified From b10c06acd7955c92ae1e4f02b409f251dd4c7b4f Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 7 Jul 2023 19:41:36 +0530 Subject: [PATCH 8/9] lint fixes Signed-off-by: MUzairS15 --- utils/component/error.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/component/error.go b/utils/component/error.go index a79c6d61..ef211fe8 100644 --- a/utils/component/error.go +++ b/utils/component/error.go @@ -20,9 +20,9 @@ func ErrGetDefinition(err error) error { } func ErrGetSchema(err error) error { - return errors.New(ErrGetSchemaCode, errors.Alert, []string{"Could not get schema for the given CRD"}, []string{err.Error()}, []string{"Unable to marshal from cue value to valid JSON", "Unable to unmarshal from JSON to Go type"}, []string{"Verify CRD has valid schema.", "Malformed JSON", "CUE path to propery doesn't exist"}) + return errors.New(ErrGetSchemaCode, errors.Alert, []string{"Could not get schema for the given CRD"}, []string{err.Error()}, []string{"Unable to marshal from cue value to JSON", "Unable to unmarshal from JSON to Go type"}, []string{"Verify CRD has valid schema.", "Malformed JSON provided", "CUE path to propery doesn't exist"}) } func ErrUpdateSchema(err error, obj string) error { - return errors.New(ErrUpdateSchemaCode, errors.Alert, []string{"Failed to update schema properties for ", obj}, []string{err.Error()}, []string{"Incorrect type assertion", "Selector.Unquoted might have been invoked on non-string label", "error during conversion from cue.Selector to string"}, []string{"Ensure correct type assertion", "Perform appropriate conversion from cue.Selector to string","Verify CRD has valid schema"}) + return errors.New(ErrUpdateSchemaCode, errors.Alert, []string{"Failed to update schema properties for ", obj}, []string{err.Error()}, []string{"Incorrect type assertion", "Selector.Unquoted might have been invoked on non-string label", "error during conversion from cue.Selector to string"}, []string{"Ensure correct type assertion", "Perform appropriate conversion from cue.Selector to string", "Verify CRD has valid schema"}) } From 2403f3cace5dab32ad23b3c8ba917609dfb893a2 Mon Sep 17 00:00:00 2001 From: MUzairS15 Date: Fri, 7 Jul 2023 19:46:07 +0530 Subject: [PATCH 9/9] lint fixes Signed-off-by: MUzairS15 --- utils/component/generator.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/component/generator.go b/utils/component/generator.go index 7202ab60..34069227 100644 --- a/utils/component/generator.go +++ b/utils/component/generator.go @@ -93,8 +93,8 @@ func Generate(crd string) (v1alpha1.ComponentDefinition, error) { } /* -Find and modify specific schema properties. -1. Identify interesting properties by walking entire schema. +Find and modify specific schema properties. +1. Identify interesting properties by walking entire schema. 2. Store path to interesting properties. Finish walk. 3. Iterate all paths and modify properties. 5. If error occurs, return nil and skip modifications.