Skip to content

Commit

Permalink
feat: Add support for raw resources
Browse files Browse the repository at this point in the history
  • Loading branch information
bjw-s committed Jun 27, 2024
1 parent b46faa9 commit 42bd486
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 7 deletions.
15 changes: 15 additions & 0 deletions charts/library/common-test/ci/advanced-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,18 @@ persistence:
- path: /data/config.yaml
readOnly: false
subPath: config.yaml

rawResources:
unsupportedEndpoint:
enabled: true
nameOverride: localhost-https
apiVersion: v1
kind: Endpoint
spec:
subsets:
- addresses:
- ip: 127.0.0.1
ports:
- name: "{{ .Release.Name }}"
port: 443
protocol: TCP
98 changes: 98 additions & 0 deletions charts/library/common-test/tests/rawResource/metadata_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json
suite: rawResource metadata
templates:
- common.yaml
values:
- ../_values/controllers_main_default_container.yaml
set:
rawResources:
config:
apiVersion: v1
kind: Endpoint
spec:
test: test
tests:
- it: default metadata should pass
asserts:
- hasDocuments:
count: 2
- documentIndex: &rawResourceDocument 1
isKind:
of: Endpoint
- documentIndex: *rawResourceDocument
notExists:
path: metadata.annotations
- documentIndex: *rawResourceDocument
equal:
path: metadata.labels
value:
app.kubernetes.io/instance: RELEASE-NAME
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: RELEASE-NAME
helm.sh/chart: common-test-1.0.0

- it: custom metadata should pass
set:
rawResources:
config:
annotations:
test_annotation: test
labels:
test_label: test
asserts:
- hasDocuments:
count: 2
- documentIndex: &rawResourceDocument 1
isKind:
of: Endpoint
- documentIndex: *rawResourceDocument
equal:
path: metadata.annotations
value:
test_annotation: test
- documentIndex: *rawResourceDocument
equal:
path: metadata.labels
value:
app.kubernetes.io/instance: RELEASE-NAME
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: RELEASE-NAME
helm.sh/chart: common-test-1.0.0
test_label: test

- it: custom metadata with global metadata should pass
set:
global:
labels:
global_label: test
annotations:
global_annotation: test
rawResources:
config:
annotations:
test_annotation: test
labels:
test_label: test
asserts:
- hasDocuments:
count: 2
- documentIndex: &rawResourceDocument 1
isKind:
of: Endpoint
- documentIndex: *rawResourceDocument
equal:
path: metadata.annotations
value:
global_annotation: test
test_annotation: test
- documentIndex: *rawResourceDocument
equal:
path: metadata.labels
value:
app.kubernetes.io/instance: RELEASE-NAME
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: RELEASE-NAME
global_label: test
helm.sh/chart: common-test-1.0.0
test_label: test
78 changes: 78 additions & 0 deletions charts/library/common-test/tests/rawResource/values_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/helm-unittest/helm-unittest/main/schema/helm-testsuite.json
suite: rawResource values
templates:
- common.yaml
values:
- ../_values/controllers_main_default_container.yaml
set:
rawResources:
endpoint:
apiVersion: v1
kind: Endpoint
spec:
subsets:
- addresses:
- ip: 127.0.0.1
ports:
- name: "{{ .Release.Name }}"
port: 443
protocol: TCP
tests:
- it: default should pass
asserts:
- hasDocuments:
count: 2
- documentIndex: &rawResourceDocument 1
isKind:
of: Endpoint
- documentIndex: *rawResourceDocument
equal:
path: subsets[0].addresses[0].ip
value: 127.0.0.1

- it: nameOverride should pass
set:
rawResources.endpoint:
nameOverride: test
asserts:
- documentIndex: &rawResourceDocument 1
isKind:
of: Endpoint
- documentIndex: *rawResourceDocument
equal:
path: metadata.name
value: RELEASE-NAME-test

- it: nameOverride with template should pass
set:
rawResources.endpoint:
nameOverride: "{{ .Release.Name }}"
asserts:
- documentIndex: &rawResourceDocument 1
isKind:
of: Endpoint
- documentIndex: *rawResourceDocument
equal:
path: metadata.name
value: RELEASE-NAME

- it: spec with template should pass
set:
rawResources.endpoint:
spec:
subsets:
- addresses:
- ip: 127.0.0.1
ports:
- name: "{{ .Release.Name }}"
port: 443
protocol: TCP
asserts:
- documentIndex: &rawResourceDocument 1
isKind:
of: Endpoint
- documentIndex: *rawResourceDocument
equal:
path: subsets[0].ports[0].name
value: RELEASE-NAME
2 changes: 1 addition & 1 deletion charts/library/common/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ annotations:
artifacthub.io/changes: |-
- kind: added
description: |-
# TODO: Update
Added support for specifying unsupported raw resources.
4 changes: 2 additions & 2 deletions charts/library/common/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# common

![Version: 3.2.1](https://img.shields.io/badge/Version-3.2.1-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square)
![Version: 3.3.0](https://img.shields.io/badge/Version-3.3.0-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square)

Function library for Helm charts

Expand All @@ -27,7 +27,7 @@ Include this chart as a dependency in your `Chart.yaml` e.g.
# Chart.yaml
dependencies:
- name: common
version: 3.2.1
version: 3.3.0
repository: https://bjw-s.github.io/helm-charts/
```

Expand Down
4 changes: 0 additions & 4 deletions charts/library/common/schemas/rawResource.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
"nameOverride": {
"type": "string"
},
"rootKey": {
"type": "string",
"default": "spec"
},
"spec": {}
},
"required": ["apiVersion", "kind", "spec"]
Expand Down
37 changes: 37 additions & 0 deletions charts/library/common/templates/classes/_rawResource.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{/*
This template serves as a blueprint for all raw resource objects that are created
within the common library.
*/}}
{{- define "bjw-s.common.class.rawResource" -}}
{{- $rootContext := .rootContext -}}
{{- $resourceObject := .object -}}

{{- $labels := merge
($resourceObject.labels | default dict)
(include "bjw-s.common.lib.metadata.allLabels" $rootContext | fromYaml)
-}}
{{- $annotations := merge
($resourceObject.annotations | default dict)
(include "bjw-s.common.lib.metadata.globalAnnotations" $rootContext | fromYaml)
-}}
---
apiVersion: {{ $resourceObject.apiVersion }}
kind: {{ $resourceObject.kind }}
metadata:
name: {{ $resourceObject.name }}
{{- with $labels }}
labels:
{{- range $key, $value := . }}
{{- printf "%s: %s" $key (tpl $value $rootContext | toYaml ) | nindent 4 }}
{{- end }}
{{- end }}
{{- with $annotations }}
annotations:
{{- range $key, $value := . }}
{{- printf "%s: %s" $key (tpl $value $rootContext | toYaml ) | nindent 4 }}
{{- end }}
{{- end }}
{{- with $resourceObject.spec }}
{{- tpl (toYaml .) $rootContext | nindent 0 }}
{{- end }}
{{- end -}}
7 changes: 7 additions & 0 deletions charts/library/common/templates/lib/rawResource/_validate.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{/*
Validate raw resource values
*/}}
{{- define "bjw-s.common.lib.rawResource.validate" -}}
{{- $rootContext := .rootContext -}}
{{- $resourceObject := .object -}}
{{- end -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{/*
Convert raw resource values to an object
*/}}
{{- define "bjw-s.common.lib.rawResource.valuesToObject" -}}
{{- $rootContext := .rootContext -}}
{{- $identifier := .id -}}
{{- $objectValues := .values -}}

{{- /* Determine and inject the raw resource name */ -}}
{{- $objectName := (include "bjw-s.common.lib.chart.names.fullname" $rootContext) -}}

{{- if $objectValues.nameOverride -}}
{{- $override := tpl $objectValues.nameOverride $rootContext -}}
{{- if not (eq $objectName $override) -}}
{{- $objectName = printf "%s-%s" $objectName $override -}}
{{- end -}}
{{- else -}}
{{- if not (eq $objectName $identifier) -}}
{{- $objectName = printf "%s-%s" $objectName $identifier -}}
{{- end -}}
{{- end -}}
{{- $_ := set $objectValues "name" $objectName -}}
{{- $_ := set $objectValues "identifier" $identifier -}}

{{- /* Return the raw resource object */ -}}
{{- $objectValues | toYaml -}}
{{- end -}}
1 change: 1 addition & 0 deletions charts/library/common/templates/loader/_generate.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ Secondary entrypoint and primary loader for the common chart
{{- include "bjw-s.common.render.configMaps" . | nindent 0 -}}
{{- include "bjw-s.common.render.secrets" . | nindent 0 -}}
{{- include "bjw-s.common.render.networkpolicies" . | nindent 0 -}}
{{- include "bjw-s.common.render.rawResources" . | nindent 0 -}}
{{- end -}}
26 changes: 26 additions & 0 deletions charts/library/common/templates/render/_rawResources.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{/*
Renders other arbirtrary objects required by the chart.
*/}}
{{- define "bjw-s.common.render.rawResources" -}}
{{- /* Generate pvc as required */ -}}
{{- range $key, $resource := .Values.rawResources -}}
{{- /* Enable by default, but allow override */ -}}
{{- $resourceEnabled := true -}}
{{- if hasKey $resource "enabled" -}}
{{- $resourceEnabled = $resource.enabled -}}
{{- end -}}

{{- if $resourceEnabled -}}
{{- $resourceValues := (mustDeepCopy $resource) -}}

{{- /* Create object from the raw resource values */ -}}
{{- $resourceObject := (include "bjw-s.common.lib.rawResource.valuesToObject" (dict "rootContext" $ "id" $key "values" $resourceValues)) | fromYaml -}}

{{- /* Perform validations on the resource before rendering */ -}}
{{- include "bjw-s.common.lib.rawResource.validate" (dict "rootContext" $ "object" $resourceValues) -}}

{{- /* Include the raw resource class */ -}}
{{- include "bjw-s.common.class.rawResource" (dict "rootContext" $ "object" $resourceValues) | nindent 0 -}}
{{- end -}}
{{- end -}}
{{- end -}}
19 changes: 19 additions & 0 deletions charts/library/common/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,22 @@ networkpolicies:
# # -- The egress rules for this networkPolicy. Allows all egress traffic by default.
# egress:
# - {}

# -- Configure any unsupported raw resources here.
# @default -- See below
rawResources: {}
# example:
# # -- Enables or disables the resource. Defaults to true
# enabled: false
# # -- Specify the apiVersion of the resource.
# apiVersion: v1
# # -- Specify the kind of the resource.
# kind: Endpoint
# # -- Override the name suffix that is used for this resource.
# nameOverride: ""
# # -- Provide additional annotations which may be required.
# annotations: {}
# # -- Provide additional labels which may be required.
# labels: {}
# # -- Configure the contents of the resource that is to be rendered.
# spec:

0 comments on commit 42bd486

Please sign in to comment.