Skip to content

Commit

Permalink
added parser for loadBalancerSourceRanges field
Browse files Browse the repository at this point in the history
  • Loading branch information
Greendor1234 authored and arttor committed Nov 18, 2024
1 parent ad4eef7 commit 369f0b3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/app/templates/myapp-lb-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "app.fullname" . }}-myapp-lb-service
labels:
app: myapp
{{- include "app.labels" . | nindent 4 }}
spec:
type: {{ .Values.myappLbService.type }}
selector:
app: myapp
{{- include "app.selectorLabels" . | nindent 4 }}
ports:
{{- .Values.myappLbService.ports | toYaml | nindent 2 }}
loadBalancerSourceRanges:
{{ - .Values.myappLbService.loadBalancerSourceRanges | toYaml | nindent 2 }}
8 changes: 8 additions & 0 deletions examples/app/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ myapp:
tag: v0.8.0
replicas: 3
revisionHistoryLimit: 5
myappLbService:
loadBalancerSourceRanges:
- 10.0.0.0/8
ports:
- name: https
port: 8443
targetPort: https
type: LoadBalancer
myappPdb:
minAvailable: 2
myappService:
Expand Down
21 changes: 21 additions & 0 deletions pkg/processor/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ spec:
{{- .Values.%[1]s.ports | toYaml | nindent 2 }}`
)

const (
lbSourceRangesTempSpec = `
loadBalancerSourceRanges:
{{ - .Values.%[1]s.loadBalancerSourceRanges | toYaml | nindent 2 }}`
)

var svcGVC = schema.GroupVersionKind{
Group: "",
Version: "v1",
Expand Down Expand Up @@ -97,6 +103,9 @@ func (r svc) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured

_ = unstructured.SetNestedSlice(values, ports, shortNameCamel, "ports")
res := meta + fmt.Sprintf(svcTempSpec, shortNameCamel, selector, appMeta.ChartName())

res += parseLoadBalancerSourceRanges(values, service, shortNameCamel)

if shortNameCamel == "webhookService" && appMeta.Config().AddWebhookOption {
res = fmt.Sprintf("{{- if .Values.webhook.enabled }}\n%s\n{{- end }}", res)
}
Expand All @@ -107,6 +116,18 @@ func (r svc) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
}, nil
}

func parseLoadBalancerSourceRanges(values helmify.Values, service corev1.Service, shortNameCamel string) string {
if len(service.Spec.LoadBalancerSourceRanges) < 1 {
return ""
}
lbSourceRanges := make([]interface{}, len(service.Spec.LoadBalancerSourceRanges))
for i, ip := range service.Spec.LoadBalancerSourceRanges {
lbSourceRanges[i] = ip
}
_ = unstructured.SetNestedSlice(values, lbSourceRanges, shortNameCamel, "loadBalancerSourceRanges")
return fmt.Sprintf(lbSourceRangesTempSpec, shortNameCamel)
}

type result struct {
name string
data string
Expand Down
18 changes: 18 additions & 0 deletions test_data/sample-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ spec:
selector:
app: myapp
---
apiVersion: v1
kind: Service
metadata:
labels:
app: myapp
name: myapp-lb-service
namespace: my-ns
spec:
ports:
- name: https
port: 8443
targetPort: https
selector:
app: myapp
type: LoadBalancer
loadBalancerSourceRanges:
- 10.0.0.0/8
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
Expand Down

0 comments on commit 369f0b3

Please sign in to comment.