Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chunter0 committed Aug 7, 2023
1 parent 7ce24cc commit 91f2377
Show file tree
Hide file tree
Showing 9 changed files with 551 additions and 32 deletions.
70 changes: 42 additions & 28 deletions codegen/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,49 @@ var _ = Describe("Cmd", func() {
Resources: []string{"leases"},
}},
Deployment: Deployment{
Sidecars: []Sidecar{{
Name: "gloo-agent",
Volumes: []v1.Volume{{
Name: "agent-volume",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "agent-volume",
Sidecars: []Sidecar{
{
Name: "gloo-agent",
Volumes: []v1.Volume{
{
Name: "agent-volume",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "agent-volume",
},
},
},
{
Name: "agent-volume-2",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "agent-volume",
},
},
},
},
}},
Rbac: []rbacv1.PolicyRule{{
Verbs: []string{"*"},
APIGroups: []string{"apiextensions.k8s.io"},
Resources: []string{"customresourcedefinitions"},
}},
Container: Container{
Image: Image{
Registry: "gcr.io/gloo-mesh",
Repository: "gloo-mesh-agent",
Tag: "0.0.1",
},
},
Service: Service{
Ports: []ServicePort{{
Name: "grpc",
DefaultPort: 9977,
Rbac: []rbacv1.PolicyRule{{
Verbs: []string{"*"},
APIGroups: []string{"apiextensions.k8s.io"},
Resources: []string{"customresourcedefinitions"},
}},
Container: Container{
Image: Image{
Registry: "gcr.io/gloo-mesh",
Repository: "gloo-mesh-agent",
Tag: "0.0.1",
},
},
Service: Service{
Ports: []ServicePort{{
Name: "grpc",
DefaultPort: 9977,
}},
},
EnableStatement: agentConditional,
ValuesPath: "$.Values.glooAgent",
},
EnableStatement: agentConditional,
}},
},
Container: Container{
Image: Image{
Registry: "gcr.io/gloo-mesh",
Expand Down Expand Up @@ -123,12 +136,12 @@ var _ = Describe("Cmd", func() {
},
},
},
ManifestRoot: "codegen/test/chart",
ManifestRoot: "codegen/test/chart/conditional-sidecar",
}

Expect(cmd.Execute()).NotTo(HaveOccurred(), "failed to execute command")

absPath, err := filepath.Abs("./test/chart/templates/deployment.yaml")
absPath, err := filepath.Abs("./test/chart/conditional-sidecar/templates/deployment.yaml")
Expect(err).NotTo(HaveOccurred(), "failed to get abs path")

deployment, err := os.ReadFile(absPath)
Expand All @@ -137,6 +150,7 @@ var _ = Describe("Cmd", func() {
Expect(deployment).To(ContainSubstring(fmt.Sprintf("{{- if %s -}}", agentConditional)))
Expect(deployment).To(ContainSubstring(fmt.Sprintf("{{- if %s }}", "and ($.Values.glooAgent.enabled) (not $.Values.glooAgent.runAsSidecar)")))
Expect(deployment).To(ContainSubstring("name: agent-volume"))
Expect(deployment).To(ContainSubstring("{{ $.Values.glooAgent.ports.grpc }}"))
})
It("generates controller code and manifests for a proto file", func() {
cmd := &Command{
Expand Down
8 changes: 7 additions & 1 deletion codegen/model/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ type Sidecar struct {
Rbac []rbacv1.PolicyRule
Volumes []corev1.Volume
Name string
EnableStatement string `json:"enableStatement,omitempty" yaml:"enableStatement,omitempty"`
EnableStatement string `json:"enableStatement,omitempty" yaml:"enableStatement,omitempty"` // Optional: if specified, the operator resources will be abled based on the condition specified in the enable statement.
ValuesPath string `json:"valuesPath,omitempty" yaml:"valuesPath,omitempty"` // Override for values path in generated yaml.
}

// values for struct template
Expand Down Expand Up @@ -197,6 +198,11 @@ func (c Chart) BuildChartValues() values.UserHelmValues {
}
sidecars := map[string]values.UserContainerValues{}
for _, sidecar := range operator.Deployment.Sidecars {
// Note: We don't want to render docs for conditional sidecars
if sidecar.ValuesPath != "" {
continue
}

sidecars[strcase.ToLowerCamel(sidecar.Name)] = makeContainerDocs(sidecar.Container)
}

Expand Down
4 changes: 2 additions & 2 deletions codegen/render/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func containerConfigs(op model.Operator) []containerConfig {
ValuesVar: valuesVar + ".sidecars." + strcase.ToLowerCamel(sidecar.Name),
}

if sidecar.EnableStatement != "" {
config.ValuesVar = fmt.Sprintf("$.Values.%s", strcase.ToLowerCamel(sidecar.Name))
if sidecar.ValuesPath != "" {
config.ValuesVar = sidecar.ValuesPath
}

configs = append(configs, config)
Expand Down
2 changes: 1 addition & 1 deletion codegen/templates/chart/operator-deployment.yamltmpl
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ spec:
{{- end }}

[[- range $container := $containers -]]
[[- if and (gt (len $container.EnableStatement) 0) (gt (len $operator.Service.Ports) 0) ]]
[[- if and (gt (len $container.EnableStatement) 0) (gt (len $container.Service.Ports) 0) ]]
[[ printf "\n---" ]]
[[ $containerVar := printf "$.Values.%s" (lower_camel $container.Name) ]]
[[- printf "\n{{- if %s -}}" $container.EnableStatement ]]
Expand Down
3 changes: 3 additions & 0 deletions codegen/test/chart/conditional-sidecar/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Code generated by skv2. DO NOT EDIT.

{}
54 changes: 54 additions & 0 deletions codegen/test/chart/conditional-sidecar/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Code generated by skv2. DO NOT EDIT.



{{/* Below are library functions provided by skv2 */}}

{{- /*
"skv2.utils.merge" takes an array of three values:
- the top context
- the yaml block that will be merged in (override)
- the name of the base template (source)
note: the source must be a named template (helm partial). This is necessary for the merging logic.
The behaviour is as follows, to align with already existing helm behaviour:
- If no source is found (template is empty), the merged output will be empty
- If no overrides are specified, the source is rendered as is
- If overrides are specified and source is not empty, overrides will be merged in to the source.
Overrides can replace / add to deeply nested dictionaries, but will completely replace lists.
Examples:
┌─────────────────────┬───────────────────────┬────────────────────────┐
│ Source (template) │ Overrides │ Result │
├─────────────────────┼───────────────────────┼────────────────────────┤
│ metadata: │ metadata: │ metadata: │
│ labels: │ labels: │ labels: │
│ app: gloo │ app: gloo1 │ app: gloo1 │
│ cluster: useast │ author: infra-team │ author: infra-team │
│ │ │ cluster: useast │
├─────────────────────┼───────────────────────┼────────────────────────┤
│ lists: │ lists: │ lists: │
│ groceries: │ groceries: │ groceries: │
│ - apple │ - grapes │ - grapes │
│ - banana │ │ │
└─────────────────────┴───────────────────────┴────────────────────────┘
skv2.utils.merge is a fork of a helm library chart function (https://github.com/helm/charts/blob/master/incubator/common/templates/_util.tpl).
This includes some optimizations to speed up chart rendering time, and merges in a value (overrides) with a named template, unlike the upstream
version, which merges two named templates.
*/ -}}
{{- define "skv2.utils.merge" -}}
{{- $top := first . -}}
{{- $overrides := (index . 1) -}}
{{- $tpl := fromYaml (include (index . 2) $top) -}}
{{- if or (empty $overrides) (empty $tpl) -}}
{{ include (index . 2) $top }} {{/* render source as is */}}
{{- else -}}
{{- $merged := merge $overrides $tpl -}}
{{- toYaml $merged -}} {{/* render source with overrides as YAML */}}
{{- end -}}
{{- end -}}
Loading

0 comments on commit 91f2377

Please sign in to comment.