diff --git a/changelog/v0.28.1/add-namespace-to-cluster-roles.yaml b/changelog/v0.28.1/add-namespace-to-cluster-roles.yaml new file mode 100644 index 000000000..acd5fcee5 --- /dev/null +++ b/changelog/v0.28.1/add-namespace-to-cluster-roles.yaml @@ -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 diff --git a/codegen/render/funcs.go b/codegen/render/funcs.go index 132b4c84d..1729f6036 100644 --- a/codegen/render/funcs.go +++ b/codegen/render/funcs.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "reflect" + "sort" "strings" "text/template" @@ -127,6 +128,8 @@ func makeTemplateFuncs(customFuncs template.FuncMap) template.FuncMap { }, "containerConfigs": containerConfigs, + + "opVar": opVar, } for k, v := range skv2Funcs { @@ -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 @@ -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") diff --git a/codegen/render/funcs_test.go b/codegen/render/funcs_test.go index bc9a59120..181c9f31f 100644 --- a/codegen/render/funcs_test.go +++ b/codegen/render/funcs_test.go @@ -57,8 +57,9 @@ 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"}, @@ -66,6 +67,7 @@ var _ = Describe("toYAMLWithComments", func() { 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"}, @@ -73,9 +75,17 @@ var _ = Describe("toYAMLWithComments", func() { }, }, }, &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 @@ -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: @@ -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 `))) diff --git a/codegen/templates/chart/operator-deployment.yamltmpl b/codegen/templates/chart/operator-deployment.yamltmpl index a2c393c6c..fe37ae8df 100644 --- a/codegen/templates/chart/operator-deployment.yamltmpl +++ b/codegen/templates/chart/operator-deployment.yamltmpl @@ -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"}} @@ -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: diff --git a/codegen/templates/chart/operator-rbac.yamltmpl b/codegen/templates/chart/operator-rbac.yamltmpl index b52823c3a..870d6c285 100644 --- a/codegen/templates/chart/operator-rbac.yamltmpl +++ b/codegen/templates/chart/operator-rbac.yamltmpl @@ -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 }} ---