From 32f5bc31d17a18a62f51516c57bc4ddeb914cf08 Mon Sep 17 00:00:00 2001 From: Navid Shakibapour Date: Tue, 21 Apr 2020 12:22:59 -0400 Subject: [PATCH 1/2] Using Go templates for complex mappings --- annotations.md | 81 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/annotations.md b/annotations.md index 598b101..96b2431 100644 --- a/annotations.md +++ b/annotations.md @@ -35,21 +35,21 @@ apiVersion: apps.kube.io/v1beta1 kind: Database metadata: - name: my-cluster + name: my-cluster spec: ... - status: + status: bootstrap: - - type: plain - url: myhost2.example.com - name: hostGroup1 - - type: tls - url: myhost1.example.com:9092,myhost2.example.com:9092 - name: hostGroup2 + - type: plain + url: myhost2.example.com + name: hostGroup1 + - type: tls + url: myhost1.example.com:9092,myhost2.example.com:9092 + name: hostGroup2 data: - dbConfiguration: database-config # configmap - dbCredentials: database-cred-Secret # Secret - url: db.stage.ibm.com + dbConfiguration: database-config # configmap + dbCredentials: database-cred-Secret # Secret + url: db.stage.ibm.com ``` @@ -225,3 +225,62 @@ x-descriptors: - servicebinding:endpoints:elementType=sliceOfMaps:sourceKey=type:sourceValue=url ``` + +9. #### Use Go template to produce key/value pairs in the binding Secret EXPERIMENTAL + + Requirement: *Extract binding information from the Kubernetes resource using Go templates and generate multiple fields in the binding Secret.* + + A sample Kafka CR: + + ``` + apiVersion: kafka.strimzi.io/v1alpha1 + kind: Kafka + metadata: + name: my-cluster + ... + status: + listeners: + - type: plain + addresses: + - host: my-cluster-kafka-bootstrap.service-binding-demo.svc + port: 9092 + - host: my-cluster-kafka-bootstrap.service-binding-demo.svc + port: 9093 + - type: tls + addresses: + - host: my-cluster-kafka-bootstrap.service-binding-demo.svc + port: 9094 + ``` + + Go Template: + ``` + {{- range $idx1, $lis := .status.listeners -}} + {{- range $idx2, $adr := $el1.addresses -}} + {{ $lis.type }}_{{ $idx2 }}={{ printf "%s:%s\n" "$adr.host" "$adr.port" | b64enc | quote }} + {{- end -}} + {{- end -}} + ``` + + The above Go template produces the following string when executed on the sample Kafka CR: + ``` + plain_0="" + plain_1="" + tls_0="" + ``` + + The string can then be parsed into key-value pairs to be added into the final binding secret. The Go template above can be written as one-liner and added as `{{GO TEMPLATE}}` in the annotation and descriptor below. + + Annotation + + ``` + “servicebinding.dev/endpoints”: + "path={.status.listeners},elementType=template,source={{GO TEMPLATE}}" + ``` + + Descriptor + + ``` + - path: listeners + x-descriptors: + - servicebinding:elementType=template:source={{GO TEMPLATE}} + ``` \ No newline at end of file From 5eda58a59161fd3f34566b00b2af8d51d59540f7 Mon Sep 17 00:00:00 2001 From: Navid Shakibapour Date: Tue, 21 Apr 2020 16:29:50 -0400 Subject: [PATCH 2/2] Using Go templates for complex mappings --- annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/annotations.md b/annotations.md index 96b2431..70b2a4c 100644 --- a/annotations.md +++ b/annotations.md @@ -273,7 +273,7 @@ Annotation ``` - “servicebinding.dev/endpoints”: + “servicebinding.dev: "path={.status.listeners},elementType=template,source={{GO TEMPLATE}}" ```