Last active
July 11, 2023 22:57
-
-
Save warroyo/be463b63654b596855c22786b2b8acac to your computer and use it in GitHub Desktop.
server template with probes and fix for kapp labels
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
apiVersion: carto.run/v1alpha1 | |
kind: ClusterConfigTemplate | |
metadata: | |
annotations: | |
doc: | | |
This template consumes an input named config which contains a | |
PodTemplateSpec and returns a ConfigMap which contains a | |
"delivery.yml" which contains a manifests for a Kubernetes | |
Deployment which will run the templated pod, and a "service.yml" | |
Kubernetes Service to expose the pods on the network. | |
generation: 3 | |
name: server-probes | |
spec: | |
configPath: .data | |
healthRule: | |
alwaysHealthy: {} | |
lifecycle: mutable | |
params: | |
- default: | |
- containerPort: 8080 | |
name: http | |
port: 8080 | |
name: ports | |
ytt: | | |
#@ load("@ytt:data", "data") | |
#@ load("@ytt:yaml", "yaml") | |
#@ load("@ytt:struct", "struct") | |
#@ load("@ytt:assert", "assert") | |
#@ load("@ytt:overlay", "overlay") | |
#@ def removeKappLabels(labels): | |
#@ kappless = {} | |
#@ kappless.update(labels) | |
#@ kappless.pop("kapp.k14s.io/app") | |
#@ kappless.pop("kapp.k14s.io/association") | |
#@ return kappless | |
#@ end | |
#@ def merge_labels(fixed_values): | |
#@ labels = {} | |
#@ if hasattr(data.values.workload.metadata, "labels"): | |
#@ labels.update(data.values.workload.metadata.labels) | |
#@ labels = removeKappLabels(labels) | |
#@ end | |
#@ labels.update(fixed_values) | |
#@ return labels | |
#@ end | |
#@ def intOrString(v): | |
#@ return v if type(v) == "int" else int(v.strip()) if v.strip().isdigit() else v | |
#@ end | |
#@ def merge_ports(ports_spec, containers): | |
#@ ports = {} | |
#@ for c in containers: | |
#@ for p in getattr(c, "ports", []): | |
#@ ports[p.containerPort] = {"targetPort": p.containerPort, "port": p.containerPort, "name": getattr(p, "name", str(p.containerPort))} | |
#@ end | |
#@ end | |
#@ for p in ports_spec: | |
#@ targetPort = getattr(p, "containerPort", p.port) | |
#@ type(targetPort) in ("string", "int") or fail("containerPort must be a string or int") | |
#@ targetPort = intOrString(targetPort) | |
#@ | |
#@ port = p.port | |
#@ type(port) in ("string", "int") or fail("port must be a string or int") | |
#@ port = int(port) | |
#@ ports[p.port] = {"targetPort": targetPort, "port": port, "name": getattr(p, "name", str(p.port))} | |
#@ end | |
#@ return ports.values() | |
#@ end | |
#@ def removeKapp(): | |
metadata: | |
labels: | |
#@overlay/match missing_ok=True | |
#@overlay/remove | |
kapp.k14s.io/app: | |
#@overlay/match missing_ok=True | |
#@overlay/remove | |
kapp.k14s.io/association: | |
#@ end | |
#@ def addProbes(): | |
#@ if hasattr(data.values.params, "probes"): | |
spec: | |
containers: | |
#@overlay/match by="name" | |
- name: workload | |
#@overlay/match missing_ok=True | |
livenessProbe: #@ data.values.params.probes.livenessProbe | |
#@overlay/match missing_ok=True | |
readinessProbe: #@ data.values.params.probes.readinessProbe | |
#@ end | |
#@ end | |
#@ def delivery(): | |
--- | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: #@ data.values.workload.metadata.name | |
annotations: | |
kapp.k14s.io/update-strategy: "fallback-on-replace" | |
ootb.apps.tanzu.vmware.com/servicebinding-workload: "true" | |
kapp.k14s.io/change-rule: "upsert after upserting servicebinding.io/ServiceBindings" | |
labels: #@ merge_labels({ "app.kubernetes.io/component": "run", "carto.run/workload-name": data.values.workload.metadata.name }) | |
spec: | |
selector: | |
matchLabels: #@ removeKappLabels(data.values.config.metadata.labels) | |
template: #@ overlay.apply(data.values.config,addProbes(),removeKapp()) | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: #@ data.values.workload.metadata.name | |
labels: #@ merge_labels({ "app.kubernetes.io/component": "run", "carto.run/workload-name": data.values.workload.metadata.name }) | |
spec: | |
selector: #@ removeKappLabels(data.values.config.metadata.labels) | |
ports: | |
#@ hasattr(data.values.params, "ports") and len(data.values.params.ports) or assert.fail("one or more ports param must be provided.") | |
#@ declared_ports = {} | |
#@ if "ports" in data.values.params: | |
#@ declared_ports = data.values.params.ports | |
#@ else: | |
#@ declared_ports = struct.encode([{ "containerPort": 8080, "port": 8080, "name": "http"}]) | |
#@ end | |
#@ for p in merge_ports(declared_ports, data.values.config.spec.containers): | |
- #@ p | |
#@ end | |
#@ end | |
--- | |
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: #@ data.values.workload.metadata.name + "-server" | |
labels: #@ merge_labels({ "app.kubernetes.io/component": "config" }) | |
data: | |
delivery.yml: #@ yaml.encode(delivery()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment