From b2bcfde7a9e94fdd147578215bbbeb13e4ed8dbe Mon Sep 17 00:00:00 2001 From: ashish Date: Thu, 30 Sep 2021 03:10:04 +0530 Subject: [PATCH] Enhanced filters for k8s support Signed-off-by: ashish --- utils/manifests/definitions.go | 20 +++++++++----------- utils/manifests/utils.go | 29 ++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/utils/manifests/definitions.go b/utils/manifests/definitions.go index 41891c07..fc3e6d84 100644 --- a/utils/manifests/definitions.go +++ b/utils/manifests/definitions.go @@ -18,9 +18,10 @@ type Component struct { } type Config struct { - Name string // Name of the service mesh,or k8 or meshery - MeshVersion string + Name string // Name of the service mesh,or k8 or meshery + MeshVersion string // For service meshes Filter CrdFilter //json path filters + K8sVersion string //For K8ss ModifyDefSchema func(*string, *string) //takes in definition and schema, does some manipulation on them and returns the new def and schema } @@ -33,12 +34,8 @@ The filters described below are an abstraction over those filters. (b) [this will be discussed after ItrFilter] 2. NameFilter- As explained above, it is used as an --o-filter to extract only the names after RootFilter(1(a)) has been applied. -3. ItrFilter- This is an incomplete filter, intentionally left incomplete. Before getting version and group with VersionFilter and GroupFilter, we want to obtain only the - object we are interested in, in a given iteration. Crdnames or ApiResource names which are obtained by NameFilter are iterated over and used within,lets call it X. - ItrFilter filters out the object which has some given field set to X. A complete filter might look something like "$.a.b[?(@.c==X)]". - Since X is obtained at runtime, we pass ItrFilter such that it can be later appended with "==X)]". So you can use this filter to find objects where we can - get versions and groups based on X. Hence ItrFilter in this example can be passed as "$.a.b[?(@.c". - All filters except this and ItrSpecFilter are complete. +3. ItrFilter- The first element of the array is an incomplete filter and expected to end with "=='crdname')]". So it needs to be configured just before the double + equals sign. The rest of the elements of the array are directly appended afterwards to form the complete filter. 4. GroupFilter- After ItrFilter gives us the object with the group and version of the crd/resource we are interested in with this iteration, GroupFilter is used as output filter to only extract the object with group in one of the fields. 5. VersionFilter- After ItrFilter gives us the object with the group and version of the crd/resource we are interested in with this iteration, GroupFilter is used as output filter to only extract the object with version in one of the fields. @@ -48,7 +45,7 @@ The filters described below are an abstraction over those filters. 9. OnlyRes- In some cases we dont want to compute crdnames/api-resources at runtime as we already have them. Pass those names as an array here to skip that step. 10. IsJson- The file on which to apply all these filters is by default expected to be YAML. Set this to true if a JSON is passed instead. (These are the only two supported formats) 11. SpecFilter- When SpecFilter is passed, it is applied as output filter after ItrSpec filter. - +12. ResolveFilter- If passed, this filter will be applied after all the filter for generating schema. This filter is used to resolve $ref in json schema 1(b) If SpecFilter is not passed, then before the ItrSpecFilter the rootfilter will be applied by default and then the ItrSpec filter will be applied as output filter. */ @@ -58,8 +55,9 @@ type CrdFilter struct { GroupFilter JsonPath VersionFilter JsonPath SpecFilter JsonPath - ItrFilter string - ItrSpecFilter string + ItrFilter JsonPath + ItrSpecFilter JsonPath + ResolveFilter JsonPath VField string GField string IsJson bool diff --git a/utils/manifests/utils.go b/utils/manifests/utils.go index eb7e1e71..e8e63fd8 100644 --- a/utils/manifests/utils.go +++ b/utils/manifests/utils.go @@ -46,6 +46,7 @@ func getDefinitions(crd string, resource int, cfg Config, filepath string, binPa "@type": "pattern.meshery.io/k8s", "k8sAPIVersion": apiGroup + "/" + apiVersion, "k8skind": crd, + "version": cfg.K8sVersion, } def.ObjectMeta.Name += ".K8s" def.Spec.DefinitionRef.Name = strings.ToLower(crd) + ".k8s.meshery.layer5.io" @@ -73,7 +74,11 @@ func getSchema(crd string, fp string, binPath string, cfg Config) (string, error er bytes.Buffer ) if len(cfg.Filter.SpecFilter) != 0 { //If SpecFilter is passed then it will evaluated in output filter. [currently this case is for service meshes] - getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", cfg.Filter.ItrSpecFilter + "=='" + crd + "')]", "--o-filter"} + itr := cfg.Filter.ItrSpecFilter[0] + "=='" + crd + "')]" + for _, f := range cfg.Filter.ItrSpecFilter[1:] { + itr += f + } + getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", itr, "--o-filter"} getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.SpecFilter...) schemaCmd := exec.Command(binPath, getAPIvCmdArgs...) schemaCmd.Stdout = &out @@ -83,9 +88,16 @@ func getSchema(crd string, fp string, binPath string, cfg Config) (string, error return er.String(), err } } else { //If no specfilter is passed then root filter is applied and iterator filter is used in output filter + itr := cfg.Filter.ItrSpecFilter[0] + "=='" + crd + "')]" + for _, f := range cfg.Filter.ItrSpecFilter[1:] { + itr += f + } getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter"} getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.RootFilter...) - getAPIvCmdArgs = append(getAPIvCmdArgs, "--o-filter", cfg.Filter.ItrSpecFilter+"=='"+crd+"')]") + getAPIvCmdArgs = append(getAPIvCmdArgs, "--o-filter", itr) + if len(cfg.Filter.ResolveFilter) != 0 { + getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.ResolveFilter...) + } schemaCmd := exec.Command(binPath, getAPIvCmdArgs...) schemaCmd.Stdout = &out schemaCmd.Stderr = &er @@ -157,8 +169,11 @@ func getApiVersion(binPath string, fp string, crd string, inputFormat string, cf out bytes.Buffer er bytes.Buffer ) - - getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", cfg.Filter.ItrFilter + "=='" + crd + "')]", "--o-filter"} + itr := cfg.Filter.ItrFilter[0] + "=='" + crd + "')]" + for _, f := range cfg.Filter.ItrFilter[1:] { + itr += f + } + getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", itr, "--o-filter"} getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.VersionFilter...) schemaCmd := exec.Command(binPath, getAPIvCmdArgs...) @@ -188,7 +203,11 @@ func getApiGrp(binPath string, fp string, crd string, inputFormat string, cfg Co out bytes.Buffer er bytes.Buffer ) - getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", cfg.Filter.ItrFilter + "=='" + crd + "')]", "--o-filter"} + itr := cfg.Filter.ItrFilter[0] + "=='" + crd + "')]" + for _, f := range cfg.Filter.ItrFilter[1:] { + itr += f + } + getAPIvCmdArgs := []string{"--location", fp, "-t", inputFormat, "--filter", itr, "--o-filter"} getAPIvCmdArgs = append(getAPIvCmdArgs, cfg.Filter.GroupFilter...) schemaCmd := exec.Command(binPath, getAPIvCmdArgs...) schemaCmd.Stdout = &out