service selectors

This commit is contained in:
Stavros kois
2023-02-04 22:29:12 +02:00
parent b902b39b95
commit 49b6e452f9
6 changed files with 97 additions and 34 deletions

View File

@@ -23,7 +23,7 @@ helm dependency update "$chart_path"
if [ $1 == "template" ]; then
echo "🧪 Running <helm template ./$chart_path"
helm template "./$chart_path" --debug --set workload.main.enabled=true
helm template -f "$chart_path/default-values.yaml" "./$chart_path" --debug
else
echo "🏁 Running <helm install --dry-run --debug common-test ${extra_args[@]} ./$chart_path"
helm install --dry-run --debug common-test "${extra_args[@]}" "./$chart_path"

View File

@@ -96,38 +96,6 @@ tests:
metallb.universe.tf/allow-shared-ip: release-name-common-test
traefik.ingress.kubernetes.io/service.serversscheme: https
- it: should pass with correct selector without targetSelector
set:
service:
my-service1:
enabled: true
primary: true
ports:
port-name:
enabled: true
primary: true
workload:
my-workload:
enabled: true
primary: true
type: Deployment
podSpec: {}
my-workload2:
enabled: true
type: StatefulSet
podSpec: {}
asserts:
- documentIndex: *serviceDoc
isKind:
of: Service
- documentIndex: *serviceDoc
equal:
path: selector
value:
pod.name: my-workload
app.kubernetes.io/name: common-test
app.kubernetes.io/instance: RELEASE-NAME
- it: should pass with correct selector with targetSelector
set:
service:
@@ -156,3 +124,35 @@ tests:
pod.name: my-workload
app.kubernetes.io/name: common-test
app.kubernetes.io/instance: RELEASE-NAME
- it: should pass with correct selector without targetSelector
set:
service:
my-service1:
enabled: true
primary: true
ports:
port-name:
enabled: true
primary: true
workload:
my-workload:
enabled: true
primary: true
type: Deployment
podSpec: {}
my-workload2:
enabled: true
type: StatefulSet
podSpec: {}
asserts:
- documentIndex: &serviceDoc 2
isKind:
of: Service
- documentIndex: *serviceDoc
equal:
path: selector
value:
pod.name: my-workload
app.kubernetes.io/name: common-test
app.kubernetes.io/instance: RELEASE-NAME

View File

@@ -17,6 +17,7 @@ service:
# The above just makes it easier to test with specific values,
# set on each test case, without having to worry about pre-defined values.
# TODO:
# It will be a test case using a different values.yaml (default-values.yaml),
# that do not disable any objects. So we can make sure that defaults apply.

View File

@@ -0,0 +1,52 @@
# Service
| Key | Type | Required | Helm Template | Default | Description |
| :------------------------------------ | :-------: | :------: | :----------------: | :---------: | :---------------------------------------------------------------------- |
| service | `dict` | ❌ | ❌ | `{}` | Define the service as dicts |
| service.[service-name] | `dict` | ✅ | ❌ | `{}` | Holds service definition |
| service.[service-name].enabled | `boolean` | ✅ | ❌ | `false` | Enables or Disables the service |
| service.[service-name].labels | `dict` | ❌ | ✅ (On value only) | `{}` | Additional labels for service |
| service.[service-name].annotations | `dict` | ❌ | ✅ (On value only) | `{}` | Additional annotations for service |
| service.[service-name].type | `string` | ❌ | ✅ | `ClusterIP` | Define the service type |
| service.[service-name].sharedKey | `string` | ❌ | ✅ | `$FullName` | Custom Shared Key for MetalLB Annotation |
| service.[service-name].targetSelector | `string` | ❌ | ✅ | `""` | Define the pod to link the service, by default will use the primary pod |
---
Appears in:
- `.Values.service`
---
Naming scheme:
- Primary: `$FullName` (release-name-chart-name)
- Non-Primary: `$FullName-$ServiceName` (release-name-chart-name-ServiceName)
---
Examples:
```yaml
service:
service-name:
enabled: true
primary: true
type: ClusterIP
targetSelector: pod-name
sharedKey: custom-shared-key
ports:
port-name:
enabled: true
primary: true
container-name: container-name
other-service-name:
enabled: true
type: ClusterIP
ports:
other-port-name:
enabled: true
primary: true
```

View File

@@ -56,7 +56,7 @@ metadata:
{{- end -}}
{{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "ix.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}}
{{- if eq $svcType "LoadBalancer" -}}
{{- include "ix.v1.common.lib.service.metalLBAnnotations" (dict "rootCtx" $rootCtx "annotations" $annotations) -}}
{{- include "ix.v1.common.lib.service.metalLBAnnotations" (dict "rootCtx" $rootCtx "objectData" $objectData "annotations" $annotations) -}}
{{- end -}}
{{- if and $hasHTTPSPort -}}
{{- include "ix.v1.common.lib.service.traefikAnnotations" (dict "rootCtx" $rootCtx "annotations" $annotations) -}}
@@ -66,5 +66,9 @@ metadata:
{{- . | nindent 4 }}
{{- end }}
spec:
{{- if not (mustHas $svcType (list "ExternalName" "ExternalIP")) }}
selector:
{{- include "ix.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "podName" $podValues.shortName) | trim | nindent 2 }}
{{- end -}}
{{/* TODO: */}}
{{- end -}}

View File

@@ -6,10 +6,16 @@ rootCtx: The root context of the service
{{- define "ix.v1.common.lib.service.metalLBAnnotations" -}}
{{- $rootCtx := .rootCtx -}}
{{- $objectData := .objectData -}}
{{- $annotations := .annotations -}}
{{- $sharedKey := include "ix.v1.common.lib.chart.names.fullname" $rootCtx -}}
{{/* A custom shared key can be defined per service even between multiple charts */}}
{{- with $objectData.sharedKey -}}
{{- $sharedKey = tpl . $rootCtx -}}
{{- end -}}
{{- if $rootCtx.Values.global.addMetalLBAnnotations -}}
{{- $_ := set $annotations "metallb.universe.tf/allow-shared-ip" $sharedKey -}}
{{- end -}}