-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
redpanda: convert services.nodeport.yaml to go
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
1 parent
926afb0
commit 8ceb2aa
Showing
45 changed files
with
1,317 additions
and
2,341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -}} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.