Skip to content

Commit 43bf657

Browse files
committed
charts/console: remove all helmette.Dot references
1 parent b91b220 commit 43bf657

39 files changed

+771
-612
lines changed

charts/console/chart.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

charts/console/chart/chart.go

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ package chart
1111

1212
import (
1313
"embed"
14+
"fmt"
15+
"strings"
1416

1517
"github.com/redpanda-data/redpanda-operator/charts/console/v3"
1618
"github.com/redpanda-data/redpanda-operator/gotohelm"
@@ -31,30 +33,118 @@ var (
3133
//go:embed values.schema.json
3234
ValuesSchemaJSON []byte
3335

34-
// ChartLabel is the go version of the console helm chart.
35-
Chart = gotohelm.MustLoad(chartFiles, render)
36+
// Chart is the go version of the console helm chart.
37+
Chart = gotohelm.MustLoad(chartFiles, Render)
3638
)
3739

38-
type Values struct {
39-
console.Values `json:",inline"`
40-
41-
Enabled *bool `json:"enabled,omitempty"`
42-
Tests Enableable `json:"tests"`
43-
}
44-
45-
type Enableable struct {
46-
Enabled bool `json:"enabled"`
47-
}
48-
49-
// render is the entrypoint to both the go and helm versions of the console
40+
// Render is the entrypoint to both the go and helm versions of the console
5041
// helm chart.
5142
// In helm, _shims.render-manifest is used to call and filter the output of
5243
// this function.
5344
// In go, this function should be call by executing [ChartLabel.Render], which will
5445
// handle construction of [helmette.Dot], subcharting, and output filtering.
55-
func render(dot *helmette.Dot) []kube.Object {
46+
func Render(dot *helmette.Dot) []kube.Object {
47+
state := DotToState(dot)
48+
5649
// NB: This slice may contain nil interfaces!
5750
// Filtering happens elsewhere, don't call this function directly if you
5851
// can avoid it.
59-
return console.Render(dot)
52+
return console.Render(state)
53+
}
54+
55+
func DotToState(dot *helmette.Dot) *console.RenderState {
56+
values := helmette.Unwrap[Values](dot.Values)
57+
templater := &templater{Dot: dot, FauxDot: newFauxDot(dot)}
58+
59+
return &console.RenderState{
60+
ReleaseName: dot.Release.Name,
61+
Namespace: dot.Release.Namespace,
62+
Values: values.RenderValues,
63+
Template: templater.Template,
64+
CommonLabels: map[string]string{
65+
"helm.sh/chart": ChartLabel(dot),
66+
"app.kubernetes.io/managed-by": dot.Release.Service,
67+
"app.kubernetes.io/version": dot.Chart.AppVersion,
68+
},
69+
}
70+
}
71+
72+
// templater is a fairly hacky yet effective way to abstract out the global
73+
// `tpl` function that's plagued our rendering packages.
74+
//
75+
// It works around a very niche issue in gotohelm/helm:
76+
// 1. The inability to return a [helmette.Chart]. Helm's internal version
77+
// of `Chart` has JSON tags. Returning it in gotohelm results in all
78+
// fields being downcased and therefore inaccessible. To work around this, we
79+
// construct a [FauxChart] which explicitly keeps the Chart fields upper cased.
80+
// 2. The inability to return a `helmette.Values`. Values is a wrapper type
81+
// around a map[string]any. It comes with an AsMap function that's used by
82+
// gotohelm. We hack in interoperability by assigning a shallow clone of the
83+
// values to itself in the AsMap key.
84+
type templater struct {
85+
// Dot is present purely to satisfy the go requirements of helmette.Tpl. DO
86+
// NOT REFERENCE IT IN HELM IT WILL NOT WORK.
87+
Dot *helmette.Dot
88+
FauxDot *FauxDot
89+
}
90+
91+
func (t *templater) Template(tpl string) string {
92+
return helmette.Tpl(t.Dot, tpl, t.FauxDot)
93+
}
94+
95+
type FauxChart struct {
96+
Name string
97+
Version string
98+
AppVersion string
99+
}
100+
101+
type FauxDot struct {
102+
Values map[string]any
103+
Chart FauxChart
104+
Release helmette.Release
105+
Template helmette.Template
106+
}
107+
108+
func newFauxDot(dot *helmette.Dot) *FauxDot {
109+
clone := map[string]any{}
110+
for key, value := range dot.Values.AsMap() {
111+
clone[key] = value
112+
}
113+
// NB: A shallow clone MUST be used here. If there's any recursion, the
114+
// JSON serialization in sprig "gives up" and returns an empty string.
115+
clone["AsMap"] = dot.Values.AsMap()
116+
return &FauxDot{
117+
Values: clone,
118+
Release: dot.Release,
119+
Template: dot.Template,
120+
Chart: FauxChart{
121+
Name: dot.Chart.Name,
122+
AppVersion: dot.Chart.AppVersion,
123+
Version: dot.Chart.Version,
124+
},
125+
}
126+
}
127+
128+
// Functions below this line are included for backwards compatibility purposes.
129+
// They're re-namespaced in compat.tpl
130+
131+
func Name(dot *helmette.Dot) string {
132+
return DotToState(dot).ChartName()
133+
}
134+
135+
// Create a default fully qualified app name.
136+
// We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
137+
// If release name contains chart name it will be used as a full name.
138+
func Fullname(dot *helmette.Dot) string {
139+
return DotToState(dot).FullName()
140+
}
141+
142+
// Create chart name and version as used by the chart label.
143+
func ChartLabel(dot *helmette.Dot) string {
144+
chart := fmt.Sprintf("%s-%s", dot.Chart.Name, dot.Chart.Version)
145+
return cleanForK8s(strings.ReplaceAll(chart, "+", "_"))
146+
}
147+
148+
func cleanForK8s(s string) string {
149+
return helmette.TrimSuffix("-", helmette.Trunc(63, s))
60150
}

charts/console/chart/chart_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ import (
3939
"github.com/redpanda-data/redpanda-operator/pkg/testutil"
4040
)
4141

42-
func TestArtifactHubImages(t *testing.T) {
42+
func TestChartYAML(t *testing.T) {
4343
var images []map[string]any
4444
require.NoError(t, yaml.Unmarshal([]byte(Chart.Metadata().Annotations["artifacthub.io/images"]), &images))
4545

46-
require.Equal(t, []map[string]any{
46+
assert.Equal(t, []map[string]any{
4747
{
4848
"name": "console",
4949
"image": "docker.redpanda.com/redpandadata/console:" + Chart.Metadata().AppVersion,
5050
},
5151
}, images)
52+
53+
assert.Equal(t, console.ChartName, Chart.Metadata().Name)
54+
assert.Equal(t, console.AppVersion, Chart.Metadata().AppVersion)
5255
}
5356

5457
// TestValues asserts that the chart's values.yaml file can be losslessly
@@ -206,7 +209,7 @@ func TestGoHelmEquivalence(t *testing.T) {
206209
Tests: &PartialEnableable{
207210
Enabled: ptr.To(false),
208211
},
209-
PartialValues: console.PartialValues{
212+
PartialRenderValues: console.PartialRenderValues{
210213
Secret: &console.PartialSecretConfig{
211214
Authentication: &console.PartialAuthenticationSecrets{
212215
JWTSigningKey: ptr.To("SECRET"),
@@ -259,7 +262,7 @@ func TestGoHelmEquivalence(t *testing.T) {
259262

260263
func TestSecrets(t *testing.T) {
261264
values := PartialValues{
262-
PartialValues: console.PartialValues{
265+
PartialRenderValues: console.PartialRenderValues{
263266
Secret: &console.PartialSecretConfig{
264267
Create: ptr.To(true),
265268
Kafka: &console.PartialKafkaSecrets{
@@ -357,7 +360,7 @@ func TestIntegrationChart(t *testing.T) {
357360
env := h.Namespaced(t)
358361

359362
partial := PartialValues{
360-
PartialValues: console.PartialValues{
363+
PartialRenderValues: console.PartialRenderValues{
361364
Image: &console.PartialImage{
362365
Repository: ptr.To("redpandadata/console-unstable"),
363366
Tag: ptr.To("master-a4cf9be"),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// the Business Source License, use of this software will be governed
88
// by the Apache License, Version 2.0
99

10-
package console
10+
package chart
1111

1212
import (
1313
"fmt"

charts/console/chart/templates/NOTES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/}}
17-
{{- $notes := (get ((include "console.Notes" (dict "a" (list .))) | fromJson) "r") -}}
17+
{{- $notes := (get ((include "chart.Notes" (dict "a" (list .))) | fromJson) "r") -}}
1818
{{- range $_, $note := $notes }}
1919
{{ $note }}
2020
{{- end }}

charts/console/chart/templates/_chart.chart.tpl

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
11
{{- /* GENERATED FILE DO NOT EDIT */ -}}
22
{{- /* Transpiled by gotohelm from "github.com/redpanda-data/redpanda-operator/charts/console/v3/chart/chart.go" */ -}}
33

4-
{{- define "chart.render" -}}
4+
{{- define "chart.Render" -}}
55
{{- $dot := (index .a 0) -}}
66
{{- range $_ := (list 1) -}}
77
{{- $_is_returning := false -}}
8+
{{- $state := (get (fromJson (include "chart.DotToState" (dict "a" (list $dot)))) "r") -}}
89
{{- $_is_returning = true -}}
9-
{{- (dict "r" (get (fromJson (include "console.Render" (dict "a" (list $dot)))) "r")) | toJson -}}
10+
{{- (dict "r" (get (fromJson (include "console.Render" (dict "a" (list $state)))) "r")) | toJson -}}
11+
{{- break -}}
12+
{{- end -}}
13+
{{- end -}}
14+
15+
{{- define "chart.DotToState" -}}
16+
{{- $dot := (index .a 0) -}}
17+
{{- range $_ := (list 1) -}}
18+
{{- $_is_returning := false -}}
19+
{{- $values := $dot.Values.AsMap -}}
20+
{{- $templater := (mustMergeOverwrite (dict "Dot" (coalesce nil) "FauxDot" (coalesce nil)) (dict "Dot" $dot "FauxDot" (get (fromJson (include "chart.newFauxDot" (dict "a" (list $dot)))) "r"))) -}}
21+
{{- $_is_returning = true -}}
22+
{{- (dict "r" (mustMergeOverwrite (dict "ReleaseName" "" "Namespace" "" "Template" (coalesce nil) "CommonLabels" (coalesce nil) "Values" (dict "replicaCount" 0 "nameOverride" "" "commonLabels" (coalesce nil) "fullnameOverride" "" "image" (dict "registry" "" "repository" "" "pullPolicy" "" "tag" "") "imagePullSecrets" (coalesce nil) "automountServiceAccountToken" false "serviceAccount" (dict "create" false "automountServiceAccountToken" false "annotations" (coalesce nil) "name" "") "annotations" (coalesce nil) "podAnnotations" (coalesce nil) "podLabels" (coalesce nil) "podSecurityContext" (dict) "securityContext" (dict) "service" (dict "type" "" "port" 0 "annotations" (coalesce nil)) "ingress" (dict "enabled" false "annotations" (coalesce nil) "hosts" (coalesce nil) "tls" (coalesce nil)) "resources" (dict) "autoscaling" (dict "enabled" false "minReplicas" 0 "maxReplicas" 0 "targetCPUUtilizationPercentage" (coalesce nil)) "nodeSelector" (coalesce nil) "tolerations" (coalesce nil) "affinity" (dict) "topologySpreadConstraints" (coalesce nil) "priorityClassName" "" "config" (coalesce nil) "extraEnv" (coalesce nil) "extraEnvFrom" (coalesce nil) "extraVolumes" (coalesce nil) "extraVolumeMounts" (coalesce nil) "extraContainers" (coalesce nil) "initContainers" (dict "extraInitContainers" (coalesce nil)) "secretMounts" (coalesce nil) "secret" (dict "create" false "kafka" (dict) "authentication" (dict "jwtSigningKey" "" "oidc" (dict)) "license" "" "redpanda" (dict "adminApi" (dict)) "serde" (dict) "schemaRegistry" (dict)) "livenessProbe" (dict) "readinessProbe" (dict) "configmap" (dict "create" false) "deployment" (dict "create" false) "strategy" (dict))) (dict "ReleaseName" $dot.Release.Name "Namespace" $dot.Release.Namespace "Values" $values "Template" (list "chart.templater.Template" $templater) "CommonLabels" (dict "helm.sh/chart" (get (fromJson (include "chart.ChartLabel" (dict "a" (list $dot)))) "r") "app.kubernetes.io/managed-by" $dot.Release.Service "app.kubernetes.io/version" $dot.Chart.AppVersion)))) | toJson -}}
23+
{{- break -}}
24+
{{- end -}}
25+
{{- end -}}
26+
27+
{{- define "chart.templater.Template" -}}
28+
{{- $t := (index .a 0) -}}
29+
{{- $tpl := (index .a 1) -}}
30+
{{- range $_ := (list 1) -}}
31+
{{- $_is_returning := false -}}
32+
{{- $_is_returning = true -}}
33+
{{- (dict "r" (tpl $tpl $t.FauxDot)) | toJson -}}
34+
{{- break -}}
35+
{{- end -}}
36+
{{- end -}}
37+
38+
{{- define "chart.newFauxDot" -}}
39+
{{- $dot := (index .a 0) -}}
40+
{{- range $_ := (list 1) -}}
41+
{{- $_is_returning := false -}}
42+
{{- $clone := (dict) -}}
43+
{{- range $key, $value := $dot.Values.AsMap -}}
44+
{{- $_ := (set $clone $key $value) -}}
45+
{{- end -}}
46+
{{- if $_is_returning -}}
47+
{{- break -}}
48+
{{- end -}}
49+
{{- $_ := (set $clone "AsMap" $dot.Values.AsMap) -}}
50+
{{- $_is_returning = true -}}
51+
{{- (dict "r" (mustMergeOverwrite (dict "Values" (coalesce nil) "Chart" (dict "Name" "" "Version" "" "AppVersion" "") "Release" (dict "Name" "" "Namespace" "" "Service" "" "IsUpgrade" false "IsInstall" false) "Template" (dict "Name" "" "BasePath" "")) (dict "Values" $clone "Release" $dot.Release "Template" $dot.Template "Chart" (mustMergeOverwrite (dict "Name" "" "Version" "" "AppVersion" "") (dict "Name" $dot.Chart.Name "AppVersion" $dot.Chart.AppVersion "Version" $dot.Chart.Version))))) | toJson -}}
1052
{{- break -}}
1153
{{- end -}}
1254
{{- end -}}

charts/console/chart/templates/_console.notes.tpl renamed to charts/console/chart/templates/_chart.notes.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{- /* GENERATED FILE DO NOT EDIT */ -}}
2-
{{- /* Transpiled by gotohelm from "github.com/redpanda-data/redpanda-operator/charts/console/v3/notes.go" */ -}}
2+
{{- /* Transpiled by gotohelm from "github.com/redpanda-data/redpanda-operator/charts/console/v3/chart/notes.go" */ -}}
33

4-
{{- define "console.Notes" -}}
4+
{{- define "chart.Notes" -}}
55
{{- $dot := (index .a 0) -}}
66
{{- range $_ := (list 1) -}}
77
{{- $_is_returning := false -}}
@@ -24,11 +24,11 @@
2424
{{- break -}}
2525
{{- end -}}
2626
{{- else -}}{{- if (contains "NodePort" (toString $values.service.type)) -}}
27-
{{- $commands = (concat (default (list) $commands) (list (printf ` export NODE_PORT=$(kubectl get --namespace %s -o jsonpath="{.spec.ports[0].nodePort}" services %s)` $dot.Release.Namespace (get (fromJson (include "console.Fullname" (dict "a" (list $dot)))) "r")) (printf ` export NODE_IP=$(kubectl get nodes --namespace %s -o jsonpath="{.items[0].status.addresses[0].address}")` $dot.Release.Namespace) " echo http://$NODE_IP:$NODE_PORT")) -}}
27+
{{- $commands = (concat (default (list) $commands) (list (printf ` export NODE_PORT=$(kubectl get --namespace %s -o jsonpath="{.spec.ports[0].nodePort}" services %s)` $dot.Release.Namespace (get (fromJson (include "chart.Fullname" (dict "a" (list $dot)))) "r")) (printf ` export NODE_IP=$(kubectl get nodes --namespace %s -o jsonpath="{.items[0].status.addresses[0].address}")` $dot.Release.Namespace) " echo http://$NODE_IP:$NODE_PORT")) -}}
2828
{{- else -}}{{- if (contains "NodePort" (toString $values.service.type)) -}}
29-
{{- $commands = (concat (default (list) $commands) (list ` NOTE: It may take a few minutes for the LoadBalancer IP to be available.` (printf ` You can watch the status of by running 'kubectl get --namespace %s svc -w %s'` $dot.Release.Namespace (get (fromJson (include "console.Fullname" (dict "a" (list $dot)))) "r")) (printf ` export SERVICE_IP=$(kubectl get svc --namespace %s %s --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")` $dot.Release.Namespace (get (fromJson (include "console.Fullname" (dict "a" (list $dot)))) "r")) (printf ` echo http://$SERVICE_IP:%d` ($values.service.port | int)))) -}}
29+
{{- $commands = (concat (default (list) $commands) (list ` NOTE: It may take a few minutes for the LoadBalancer IP to be available.` (printf ` You can watch the status of by running 'kubectl get --namespace %s svc -w %s'` $dot.Release.Namespace (get (fromJson (include "chart.Fullname" (dict "a" (list $dot)))) "r")) (printf ` export SERVICE_IP=$(kubectl get svc --namespace %s %s --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")` $dot.Release.Namespace (get (fromJson (include "chart.Fullname" (dict "a" (list $dot)))) "r")) (printf ` echo http://$SERVICE_IP:%d` ($values.service.port | int)))) -}}
3030
{{- else -}}{{- if (contains "ClusterIP" (toString $values.service.type)) -}}
31-
{{- $commands = (concat (default (list) $commands) (list (printf ` export POD_NAME=$(kubectl get pods --namespace %s -l "app.kubernetes.io/name=%s,app.kubernetes.io/instance=%s" -o jsonpath="{.items[0].metadata.name}")` $dot.Release.Namespace (get (fromJson (include "console.Name" (dict "a" (list $dot)))) "r") $dot.Release.Name) (printf ` export CONTAINER_PORT=$(kubectl get pod --namespace %s $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")` $dot.Release.Namespace) ` echo "Visit http://127.0.0.1:8080 to use your application"` (printf ` kubectl --namespace %s port-forward $POD_NAME 8080:$CONTAINER_PORT` $dot.Release.Namespace))) -}}
31+
{{- $commands = (concat (default (list) $commands) (list (printf ` export POD_NAME=$(kubectl get pods --namespace %s -l "app.kubernetes.io/name=%s,app.kubernetes.io/instance=%s" -o jsonpath="{.items[0].metadata.name}")` $dot.Release.Namespace (get (fromJson (include "chart.Name" (dict "a" (list $dot)))) "r") $dot.Release.Name) (printf ` export CONTAINER_PORT=$(kubectl get pod --namespace %s $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")` $dot.Release.Namespace) ` echo "Visit http://127.0.0.1:8080 to use your application"` (printf ` kubectl --namespace %s port-forward $POD_NAME 8080:$CONTAINER_PORT` $dot.Release.Namespace))) -}}
3232
{{- end -}}
3333
{{- end -}}
3434
{{- end -}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{- /* GENERATED FILE DO NOT EDIT */ -}}
2+
{{- /* Transpiled by gotohelm from "github.com/redpanda-data/redpanda-operator/charts/console/v3/chart/values.go" */ -}}
3+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{- define "console.Name" -}}
2+
{{- (dict "r" (get (fromJson (include "chart.Name" .)) "r")) | toJson -}}
3+
{{- end -}}
4+
5+
{{- define "console.Fullname" -}}
6+
{{- (dict "r" (get (fromJson (include "chart.Fullname" .)) "r")) | toJson -}}
7+
{{- end -}}
8+

charts/console/chart/templates/_console.configmap.tpl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
{{- /* Transpiled by gotohelm from "github.com/redpanda-data/redpanda-operator/charts/console/v3/configmap.go" */ -}}
33

44
{{- define "console.ConfigMap" -}}
5-
{{- $dot := (index .a 0) -}}
5+
{{- $state := (index .a 0) -}}
66
{{- range $_ := (list 1) -}}
77
{{- $_is_returning := false -}}
8-
{{- $values := $dot.Values.AsMap -}}
9-
{{- if (not $values.configmap.create) -}}
8+
{{- if (not $state.Values.configmap.create) -}}
109
{{- $_is_returning = true -}}
1110
{{- (dict "r" (coalesce nil)) | toJson -}}
1211
{{- break -}}
1312
{{- end -}}
14-
{{- $data := (dict "config.yaml" (printf "# from .Values.config\n%s\n" (tpl (toYaml $values.config) $dot))) -}}
13+
{{- $data := (dict "config.yaml" (printf "# from .Values.config\n%s\n" (get (fromJson (include (first $state.Template) (dict "a" (concat (rest $state.Template) (list (toYaml $state.Values.config)))))) "r"))) -}}
1514
{{- $_is_returning = true -}}
16-
{{- (dict "r" (mustMergeOverwrite (dict "metadata" (dict "creationTimestamp" (coalesce nil))) (mustMergeOverwrite (dict) (dict "apiVersion" "v1" "kind" "ConfigMap")) (dict "metadata" (mustMergeOverwrite (dict "creationTimestamp" (coalesce nil)) (dict "labels" (get (fromJson (include "console.Labels" (dict "a" (list $dot)))) "r") "name" (get (fromJson (include "console.Fullname" (dict "a" (list $dot)))) "r") "namespace" $dot.Release.Namespace)) "data" $data))) | toJson -}}
15+
{{- (dict "r" (mustMergeOverwrite (dict "metadata" (dict "creationTimestamp" (coalesce nil))) (mustMergeOverwrite (dict) (dict "apiVersion" "v1" "kind" "ConfigMap")) (dict "metadata" (mustMergeOverwrite (dict "creationTimestamp" (coalesce nil)) (dict "labels" (get (fromJson (include "console.RenderState.Labels" (dict "a" (list $state (coalesce nil))))) "r") "name" (get (fromJson (include "console.RenderState.FullName" (dict "a" (list $state)))) "r") "namespace" $state.Namespace)) "data" $data))) | toJson -}}
1716
{{- break -}}
1817
{{- end -}}
1918
{{- end -}}

0 commit comments

Comments
 (0)