Skip to content

Commit

Permalink
Merge pull request #112 from Revolyssup/master
Browse files Browse the repository at this point in the history
Enhanced filters for k8s support
  • Loading branch information
leecalcote authored Sep 29, 2021
2 parents 894730b + b2bcfde commit 3e29d3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
20 changes: 9 additions & 11 deletions utils/manifests/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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.
Expand All @@ -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.
*/
Expand All @@ -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
Expand Down
29 changes: 24 additions & 5 deletions utils/manifests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e29d3d

Please sign in to comment.