initial service work

This commit is contained in:
Stavros kois
2022-11-23 19:01:57 +02:00
parent 1bb921f3fb
commit 71aa458e8f
3 changed files with 117 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
{{/* Template for service object, can only be called by the spawner */}}
{{/* An "svc" object and "root" is passed from the spawner */}}
{{- define "ix.v1.common.class.service" -}}
{{- $svcValues := .svc -}}
{{- $root := .root -}}
{{- $svcName := include "ix.v1.common.names.fullname" $root -}}
{{- if and (hasKey $svcValues "nameOverride") $svcValues.nameOverride -}}
{{- $svcName = (printf "%v-%v" $svcName $svcValues.nameOverride) -}}
{{- end -}}
{{- $svcType := $svcValues.type | default "ClusterIP" -}}
{{- $primaryPort := get $svcValues.port (include "ix.v1.common.lib.util.service.ports.primary" (dict "values" $svcValues)) -}}
---
apiVersion: {{ include "ix.v1.common.capabilities.service.apiVersion" $root }}
kind: Service
metadata:
name: {{ $svcName }}
{{- $labels := (mustMerge ($svcValues.labels | default dict) (include "ix.v1.common.labels" $root | fromYaml)) -}}
{{- with (include "ix.v1.common.util.labels.render" (dict "root" $root "labels" $labels) | trim) }}
labels:
{{- . | nindent 4 }}
{{- end }}
{{- $annotations := (mustMerge ($svcValues.annotations | default dict) (include "ix.v1.common.annotations" $root | fromYaml)) -}}
{{- with (include "ix.v1.common.util.annotations.render" (dict "root" $root "annotations" $annotations) | trim) }}
annotations:
{{- . | nindent 4 }}
{{- end }}
{{- if and $root.addAnnotations.traefik (eq ($primaryPort.protocol | default "") "HTTPS") }}
traefik.ingress.kubernetes.io/service.serversscheme: https
{{- end -}}
{{- if and $root.addAnnotations.metallb (eq $svcType "LoadBalancer") }}
metallb.universe.tf/allow-shared-ip: {{ include "ix.v1.common.names.fullname" . }}
{{- end -}}
spec:
{{- if eq $svcType "ClusterIP" }} {{/* ClusterIP */}}
type: {{ $svcType }}
{{- if $svcValues.clusterIP }}
clusterIP: {{ $svcValues.clusterIP }}
{{- end }}
{{- else if eq $svcType "NodePort" -}} {{/* NodePort */}}
type: {{ $svcType }}
{{- else if eq $svcType "ExternalName" -}} {{/* ExternalName */}}
type: {{ $svcType }}
externalName: {{ $svcValues.externalName }}
{{- else if eq $svcType "LoadBalancer" -}} {{/* LoadBalancer */}}
type: {{ $svcType }}
{{- with $svcValues.loadBalancerIP }}
loadBalancerIP: {{ . }}
{{- end }}
{{- with $svcValues.loadBalancerSourceRanges }}
loadBalancerSourceRanges:
{{- range . }}
- {{ tpl . $root }}
{{- end }}
{{- end }}
{{- else if eq $svcType "ExternalIP" -}} {{/* ExternalIP */}}
{{/*TODO: */}}
{{- end -}}
ports:
{{- range $name, $port := $svcValues.ports }}
{{- if $port.enabled }}
- port: {{ $port.port }}
targetPort: {{ $port.targetPort | default $name }}
{{- if $port.protocol -}}
{{- if or (eq $port.protocol "HTTP") (eq $port.protocol "HTTPS") (eq $port.protocol "TCP") }}
protocol: TCP
{{- else }}
protocol: {{ $port.protocol }}
{{- end }}
{{- else }}
protocol: TCP
{{- end }}
name: {{ $name }}
{{- if (and (eq $svcType "NodePort") (not (empty $port.nodePort))) }}
nodePort: {{ $port.nodePort }}
{{- end }}
{{- end -}}
{{- end -}}
{{- if and (ne $svcType "ExternalName") (ne $svcType "ExternalIP") }}
selector:
{{- with $svcValues.selector }}
{{/*TODO: */}}
{{- else }}
{{- include "ix.v1.common.labels.selectorLabels" $root | nindent 4 }}
{{- end }}
{{- end -}}
{{- end -}}

View File

@@ -0,0 +1,23 @@
{{/* Renders the service object(s) */}}
{{- define "ix.v1.common.spawner.service" -}}
{{- range $name, $svc := .Values.service -}}
{{- if $svc.enabled -}}
{{- $svcValues := $svc -}}
{{/*
If it's not the primary service, and no name override is defined, make sure
we have a unique name passed to the service class.
Primary service cannot have it's nameOverride
*/}}
{{- if and (not $svcValues.nameOverride) (ne $name (include "ix.v1.common.lib.util.service.primary" $)) -}}
{{- $_ := set $svcValues "nameOverride" $name -}}
{{- end -}}
{{/*
Pass a service object containing this single service to the class,
in order to create the object. Also pass "root" for includes to work.
*/}}
{{- include "ix.v1.common.class.service" (dict "svc" $svcValues "root" $) -}}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@@ -190,6 +190,10 @@ service:
# [[ref]](https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport)
nodePort:
addAnnotations:
traefik: true
metallb: true
probes:
liveness:
enabled: true