Skip to content

Commit

Permalink
Make operator nesting work & sort yaml alphabetically (#425)
Browse files Browse the repository at this point in the history
* fix some issues with operator nesting

* sort generated yaml

* changelog

* add some sorting to the tests
  • Loading branch information
josh-pritchard authored Mar 1, 2023
1 parent 0711957 commit 3325366
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 22 deletions.
5 changes: 5 additions & 0 deletions changelog/v0.28.1/add-namespace-to-cluster-roles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

changelog:
- type: NON_USER_FACING
description: Sort yaml alphabetically for helm values
issueLink: https://github.com/solo-io/skv2/issues/426
62 changes: 54 additions & 8 deletions codegen/render/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"reflect"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -127,6 +128,8 @@ func makeTemplateFuncs(customFuncs template.FuncMap) template.FuncMap {
},

"containerConfigs": containerConfigs,

"opVar": opVar,
}

for k, v := range skv2Funcs {
Expand All @@ -147,28 +150,33 @@ type containerConfig struct {
}

func containerConfigs(op model.Operator) []containerConfig {
opVar := fmt.Sprintf("$.Values.%s", strcase.ToLowerCamel(op.Name))
if op.ValuePath != "" {
opVar = fmt.Sprintf("$.Values.%s.%s", op.ValuePath, strcase.ToLowerCamel(op.Name))

}
valuesVar := opVar(op)
configs := []containerConfig{{
Container: op.Deployment.Container,
Name: op.Name,
ValuesVar: opVar,
ValuesVar: valuesVar,
}}

for _, sidecar := range op.Deployment.Sidecars {
configs = append(configs, containerConfig{
Container: sidecar.Container,
Name: sidecar.Name,
ValuesVar: opVar + ".sidecars." + strcase.ToLowerCamel(sidecar.Name),
ValuesVar: valuesVar + ".sidecars." + strcase.ToLowerCamel(sidecar.Name),
})
}

return configs
}

func opVar(op model.Operator) string {
opVar := fmt.Sprintf("$.Values.%s", strcase.ToLowerCamel(op.Name))
if op.ValuePath != "" {
opVar = fmt.Sprintf("$.Values.%s.%s", op.ValuePath, strcase.ToLowerCamel(op.Name))

}
return opVar
}

/*
Find the proto messages for a given set of descriptors which need proto_deepcopoy funcs and whose types are not in
the API root package
Expand Down Expand Up @@ -464,13 +472,51 @@ func wrapWords(s string, limit int) string {
}

func fromNode(n goyaml.Node) string {
b, err := goyaml.Marshal(&n)
b, err := goyaml.Marshal(sortYAML(&n))
if err != nil {
panic(err)
}
return string(b)
}

// Implement sorting for prettier yaml
type nodes []*goyaml.Node

func (i nodes) Len() int { return len(i) / 2 }

func (i nodes) Swap(x, y int) {
x *= 2
y *= 2
i[x], i[y] = i[y], i[x] // keys
i[x+1], i[y+1] = i[y+1], i[x+1] // values
}

func (i nodes) Less(x, y int) bool {
x *= 2
y *= 2
return i[x].Value < i[y].Value
}

func sortYAML(node *goyaml.Node) *goyaml.Node {
if node.Kind == goyaml.DocumentNode {
for i, n := range node.Content {
node.Content[i] = sortYAML(n)
}
}
if node.Kind == goyaml.SequenceNode {
for i, n := range node.Content {
node.Content[i] = sortYAML(n)
}
}
if node.Kind == goyaml.MappingNode {
for i, n := range node.Content {
node.Content[i] = sortYAML(n)
}
sort.Sort(nodes(node.Content))
}
return node
}

func mergeNodes(nodes ...goyaml.Node) goyaml.Node {
if len(nodes) <= 1 {
panic("at least two nodes required for merge")
Expand Down
25 changes: 14 additions & 11 deletions codegen/render/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,35 @@ var _ = Describe("toYAMLWithComments", func() {
Field5: "hello field 5",
Field6: []string{"hello", "field", "six"},
Field7: map[string]string{
"hello": "field seven",
"field 7": "hello",
// This will sort
"hello2": "hello 2",
"hello1": "hello 1",
},
Field8: map[string]NestedType{
"hello": {FieldN1: "field 8"},
},
Field9: map[string]*NestedType{
"hello": {FieldN1: "field 9"},
},
// We will expect sorting to actually bring this to the top
Field10: map[string][]NestedType{
"hello": {
{FieldN1: "field"},
{FieldN1: "ten"},
},
},
}, &values.UserValuesInlineDocs{})
fmt.Println(render.FromNode(node))
Expect(render.FromNode(node)).To(Equal(prepareExpected(`
# field descripting comment
Field1: Hello, comments
# a map to a list of structs
Field10:
hello:
- # nested field 1
FieldN1: field
- # nested field 1
FieldN1: ten
# list of field 2
Field2:
- # nested field 1
Expand All @@ -98,8 +108,8 @@ var _ = Describe("toYAMLWithComments", func() {
- six
# a map of scalars to scalars
Field7:
field 7: hello
hello: field seven
hello1: hello 1
hello2: hello 2
# a map to a struct
Field8:
hello:
Expand All @@ -110,13 +120,6 @@ var _ = Describe("toYAMLWithComments", func() {
hello:
# nested field 1
FieldN1: field 9
# a map to a list of structs
Field10:
hello:
- # nested field 1
FieldN1: field
- # nested field 1
FieldN1: ten
# non standard field name
fieldfive: hello field 5
`)))
Expand Down
4 changes: 2 additions & 2 deletions codegen/templates/chart/operator-deployment.yamltmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Expressions evaluating SKv2 Config use "[[" and "]]"
[[- $customServiceLabels := $operator.Service.CustomLabels -]]
[[- $customServiceAnnotations := $operator.Service.CustomAnnotations ]]

{{- $[[ $operatorVar ]] := $.Values.[[ $operatorVar ]]}}
{{- $[[ $operatorVar ]] := [[ (opVar $operator) ]]}}
---

{{- define "[[ $operator.Name ]].deploymentSpec"}}
Expand Down Expand Up @@ -171,7 +171,7 @@ metadata:
[[ if gt (len $operator.Service.Ports) 0 ]]
# Service for [[ $operator.Name ]]
{{/* Define variables in function scope */}}
{{- $[[ $operatorVar ]] := $.Values.[[ $operatorVar ]]}}
{{- $[[ $operatorVar ]] := [[ (opVar $operator) ]]}}
apiVersion: v1
kind: Service
metadata:
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/chart/operator-rbac.yamltmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Expressions evaluating SKv2 Config use [[ "[[" ]] and [[ "]]" ]]
[[- if $operator.Rbac ]]
# Rbac manifests for [[ $operator.Name ]]

{{- $[[ $operatorVar ]] := $.Values.[[ $operatorVar ]]}}
{{- $[[ $operatorVar ]] := [[ (opVar $operator)]]}}
{{- if $[[ $operatorVar ]].enabled }}
---

Expand Down

0 comments on commit 3325366

Please sign in to comment.