Skip to content

Commit

Permalink
redpanda: convert services.nodeport.yaml to go
Browse files Browse the repository at this point in the history
This commit converts the `service.nodeport.yaml` helm template to an
equivalent go function.

There are a few notable changes to the logic that should NOT result in
any behavioral changes.

1. There used to be a for loop over all external listeners that checked
   for .Enabled and .Type on them. There was no .Type attribute on the
   external listeners therefore the entire chunk of logic was a no-op.
2. Each loop over an external listener would check the global values of
   .Values.External.Enabled. As the go function short circuits, this
   check is no longer required.

Fixes #1168.
  • Loading branch information
chrisseto authored and RafalKorepta committed Apr 12, 2024
1 parent 926afb0 commit 38a25e0
Show file tree
Hide file tree
Showing 45 changed files with 1,317 additions and 2,341 deletions.
110 changes: 110 additions & 0 deletions charts/redpanda/service.nodeport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// +gotohelm:filename=service.nodeport.go.tpl
package redpanda

import (
"fmt"

"github.com/redpanda-data/helm-charts/pkg/gotohelm/helmette"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func NodePortService(dot *helmette.Dot) *corev1.Service {
values := helmette.Unwrap[Values](dot.Values)

if !values.External.Enabled || values.External.Type != "NodePort" {
return nil
}

if values.External.Service == nil || !values.External.Service.Enabled {
return nil
}

// NB: As of writing, `mustAppend` appears to not work with nil values.
// Hence ports is defined as an empty list rather than a zero list.
ports := []corev1.ServicePort{}

for name, listener := range values.Listeners.Admin.External {
if listener.Enabled != nil && *listener.Enabled == false {
continue
}
ports = append(ports, corev1.ServicePort{
Name: fmt.Sprintf("admin-%s", name),
Protocol: corev1.ProtocolTCP,
Port: listener.Port,
NodePort: listener.AdvertisedPorts[0],
})
}

for name, listener := range values.Listeners.Kafka.External {
if listener.Enabled != nil && *listener.Enabled == false {
continue
}
ports = append(ports, corev1.ServicePort{
Name: fmt.Sprintf("kafka-%s", name),
Protocol: corev1.ProtocolTCP,
Port: listener.Port,
NodePort: listener.AdvertisedPorts[0],
})
}

for name, listener := range values.Listeners.HTTP.External {
if listener.Enabled != nil && *listener.Enabled == false {
continue
}
ports = append(ports, corev1.ServicePort{
Name: fmt.Sprintf("http-%s", name),
Protocol: corev1.ProtocolTCP,
Port: listener.Port,
NodePort: listener.AdvertisedPorts[0],
})
}

for name, listener := range values.Listeners.SchemaRegistry.External {
if listener.Enabled != nil && *listener.Enabled == false {
continue
}
ports = append(ports, corev1.ServicePort{
Name: fmt.Sprintf("schema-%s", name),
Protocol: corev1.ProtocolTCP,
Port: listener.Port,
NodePort: listener.AdvertisedPorts[0],
})
}

return &corev1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Service",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-external", ServiceName(dot)),
Namespace: dot.Release.Namespace,
Labels: FullLabels(dot),
Annotations: helmette.Default(map[string]string{}, values.External.Annotations).(map[string]string),
},
Spec: corev1.ServiceSpec{
ExternalTrafficPolicy: corev1.ServiceExternalTrafficPolicyLocal,
Ports: ports,
PublishNotReadyAddresses: true,
Selector: StatefulSetPodLabelsSelector(dot, nil /* TODO this probably needs to be filled out */),
SessionAffinity: corev1.ServiceAffinityNone,
Type: corev1.ServiceTypeNodePort,
},
}
}
13 changes: 0 additions & 13 deletions charts/redpanda/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,6 @@ Use AppVersion if image.tag is not set
{{- $result -}}
{{- end -}}

{{- define "external-nodeport-enabled" -}}
{{- $values := .Values -}}
{{- $enabled := and .Values.external.enabled (eq .Values.external.type "NodePort") -}}
{{- range $listener := .Values.listeners -}}
{{- range $external := $listener.external -}}
{{- if and (dig "enabled" false $external) (eq (dig "type" $values.external.type $external) "NodePort") -}}
{{- $enabled = true -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- toJson (dict "bool" $enabled) -}}
{{- end -}}

{{- define "external-loadbalancer-enabled" -}}
{{- $values := .Values -}}
{{- $enabled := and .Values.external.enabled (eq .Values.external.type "LoadBalancer") -}}
Expand Down
44 changes: 44 additions & 0 deletions charts/redpanda/templates/service.nodeport.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{{- /* Generated from "service.nodeport.go" */ -}}

{{- define "redpanda.NodePortService" -}}
{{- $dot := (index .a 0) -}}
{{- range $_ := (list 1) -}}
{{- $values := $dot.Values.AsMap -}}
{{- if (or (not $values.external.enabled) (ne $values.external.type "NodePort")) -}}
{{- (dict "r" (coalesce nil)) | toJson -}}
{{- break -}}
{{- end -}}
{{- if (or (eq $values.external.service (coalesce nil)) (not $values.external.service.enabled)) -}}
{{- (dict "r" (coalesce nil)) | toJson -}}
{{- break -}}
{{- end -}}
{{- $ports := (list ) -}}
{{- range $name, $listener := $values.listeners.admin.external -}}
{{- if (and (ne $listener.enabled (coalesce nil)) (eq $listener.enabled false)) -}}
{{- continue -}}
{{- end -}}
{{- $ports = (mustAppend $ports (mustMergeOverwrite (dict "port" 0 "targetPort" 0 ) (dict "name" (printf "admin-%s" $name) "protocol" "TCP" "port" $listener.port "nodePort" (index $listener.advertisedPorts 0) ))) -}}
{{- end -}}
{{- range $name, $listener := $values.listeners.kafka.external -}}
{{- if (and (ne $listener.enabled (coalesce nil)) (eq $listener.enabled false)) -}}
{{- continue -}}
{{- end -}}
{{- $ports = (mustAppend $ports (mustMergeOverwrite (dict "port" 0 "targetPort" 0 ) (dict "name" (printf "kafka-%s" $name) "protocol" "TCP" "port" $listener.port "nodePort" (index $listener.advertisedPorts 0) ))) -}}
{{- end -}}
{{- range $name, $listener := $values.listeners.http.external -}}
{{- if (and (ne $listener.enabled (coalesce nil)) (eq $listener.enabled false)) -}}
{{- continue -}}
{{- end -}}
{{- $ports = (mustAppend $ports (mustMergeOverwrite (dict "port" 0 "targetPort" 0 ) (dict "name" (printf "http-%s" $name) "protocol" "TCP" "port" $listener.port "nodePort" (index $listener.advertisedPorts 0) ))) -}}
{{- end -}}
{{- range $name, $listener := $values.listeners.schemaRegistry.external -}}
{{- if (and (ne $listener.enabled (coalesce nil)) (eq $listener.enabled false)) -}}
{{- continue -}}
{{- end -}}
{{- $ports = (mustAppend $ports (mustMergeOverwrite (dict "port" 0 "targetPort" 0 ) (dict "name" (printf "schema-%s" $name) "protocol" "TCP" "port" $listener.port "nodePort" (index $listener.advertisedPorts 0) ))) -}}
{{- end -}}
{{- (dict "r" (mustMergeOverwrite (mustMergeOverwrite (dict ) (dict "metadata" (dict "creationTimestamp" (coalesce nil) ) "spec" (dict ) "status" (dict "loadBalancer" (dict ) ) )) (mustMergeOverwrite (dict ) (dict "apiVersion" "v1" "kind" "Service" )) (dict "metadata" (mustMergeOverwrite (dict "creationTimestamp" (coalesce nil) ) (dict "name" (printf "%s-external" (get (fromJson (include "redpanda.ServiceName" (dict "a" (list $dot) ))) "r")) "namespace" $dot.Release.Namespace "labels" (get (fromJson (include "redpanda.FullLabels" (dict "a" (list $dot) ))) "r") "annotations" (default (dict ) $values.external.annotations) )) "spec" (mustMergeOverwrite (dict ) (dict "externalTrafficPolicy" "Local" "ports" $ports "publishNotReadyAddresses" true "selector" (get (fromJson (include "redpanda.StatefulSetPodLabelsSelector" (dict "a" (list $dot (coalesce nil)) ))) "r") "sessionAffinity" "None" "type" "NodePort" )) ))) | toJson -}}
{{- break -}}
{{- end -}}
{{- end -}}

21 changes: 21 additions & 0 deletions charts/redpanda/templates/service.nodeport.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
{{- $svc := (get ((include "redpanda.NodePortService" (dict "a" (list .))) | fromJson) "r") -}}
{{- if ne $svc nil -}}
---
{{toYaml $svc}}
{{- end -}}
76 changes: 0 additions & 76 deletions charts/redpanda/templates/services.nodeport.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions charts/redpanda/templates/values.yaml

This file was deleted.

54 changes: 31 additions & 23 deletions charts/redpanda/testdata/01-default-values.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -567,44 +567,52 @@ spec:
port: 8081
targetPort: 8081
---
# Source: redpanda/templates/services.nodeport.yaml
# Source: redpanda/templates/service.nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: redpanda-external
namespace: "default"
annotations: {}
creationTimestamp: null
labels:
app.kubernetes.io/component: redpanda
app.kubernetes.io/instance: redpanda
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: redpanda
helm.sh/chart: redpanda-5.7.39
name: redpanda-external
namespace: default
spec:
type: NodePort
publishNotReadyAddresses: true
externalTrafficPolicy: Local
sessionAffinity: None
ports:
- name: admin-default
protocol: TCP
port: 9645
nodePort: 31644
- name: kafka-default
protocol: TCP
port: 9094
nodePort: 31092
- name: http-default
protocol: TCP
port: 8083
nodePort: 30082
- name: schema-default
protocol: TCP
port: 8084
nodePort: 30081
selector:
- name: admin-default
nodePort: 31644
port: 9645
protocol: TCP
targetPort: 0
- name: kafka-default
nodePort: 31092
port: 9094
protocol: TCP
targetPort: 0
- name: http-default
nodePort: 30082
port: 8083
protocol: TCP
targetPort: 0
- name: schema-default
nodePort: 30081
port: 8084
protocol: TCP
targetPort: 0
publishNotReadyAddresses: true
selector:
app.kubernetes.io/component: redpanda-statefulset
app.kubernetes.io/instance: redpanda
app.kubernetes.io/name: redpanda
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
---
# Source: redpanda/templates/console/configmap-and-deployment.yaml
apiVersion: apps/v1
Expand Down
Loading

0 comments on commit 38a25e0

Please sign in to comment.