Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow templates to define complex mappings #26

Closed
arthurdm opened this issue Apr 9, 2020 · 7 comments · Fixed by #32
Closed

Allow templates to define complex mappings #26

arthurdm opened this issue Apr 9, 2020 · 7 comments · Fixed by #32
Assignees

Comments

@arthurdm
Copy link
Member

arthurdm commented Apr 9, 2020

This was brought up in #25 (review)

We should have a way to define complex mappings inside an annotation / x-descriptor.

@sbose78
Copy link
Contributor

sbose78 commented Apr 9, 2020

cc @Avni-Sharma

@navidsh
Copy link
Contributor

navidsh commented Apr 9, 2020

@navidsh could you help me with a complete example please? I will work on introducing this in a separate PR today.

@sbose78 Example for template mapping (re #25 (comment)):

Kafka CR:

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

Descriptors:

    - path: listeners
      x-descriptors:
        - servicebinding:prefix:elementType=template:source:{{ range $idx1, $el1 := .status.listeners }}{{ range $idx2, $el2 := $el1.addresses }}{{ $el1.type }}_{{ $idx2 }}={{ $el2.host }}:{{ $el2.port }}{{ printf "\n" }}{{ end }}{{ end }}:bindAs=envVar

Same template from the descriptor but pretty formatted:

{{- range $idx1, $el1 := .status.listeners -}}
  {{- range $idx2, $el2 := $el1.addresses -}}
    {{ $el1.type }}_{{ $idx2 }}={{ $el2.host }}:{{ $el2.port }}{{printf "\n" }}
  {{- end -}}
{{- end -}}

Executing the template on the Kafka CR example above produces this:

plain_0=my-cluster-kafka-bootstrap.service-binding-demo.svc:9092
plain_1=my-cluster-kafka-bootstrap.service-binding-demo.svc:9093
tls_0=my-cluster-kafka-bootstrap.service-binding-demo.svc:9094

Still it needs some thinking about how properly distinguish keys from values.
<key>=<value> doesn't quite work since <value> may have = in it. It needs a better delimiter to define the boundaries of key-value pairs. (But I'm out of idea at this time of day. lol)

@arthurdm
Copy link
Member Author

@sbose78 - would you want to take a stab at this one?

@sbose78
Copy link
Contributor

sbose78 commented Apr 13, 2020

Still it needs some thinking about how properly distinguish keys from values.

@navidsh , the key should never have the = in it, so we always split on the first =?

@sbose78 sbose78 closed this as completed Apr 13, 2020
@sbose78 sbose78 reopened this Apr 13, 2020
@navidsh
Copy link
Contributor

navidsh commented Apr 13, 2020

@sbose78 The template in my example, produces the following output as "one" string once it executes:

plain_0=my-cluster-kafka-bootstrap.service-binding-demo.svc:9092
plain_1=my-cluster-kafka-bootstrap.service-binding-demo.svc:9093
tls_0=my-cluster-kafka-bootstrap.service-binding-demo.svc:9094

SBO should then, take this text and turn it into a map e.g.

{
  "plain_0": "my-cluster-kafka-bootstrap.service-binding-demo.svc:9092",
  "plain_1": "my-cluster-kafka-bootstrap.service-binding-demo.svc:9093",
  "tls_0": "my-cluster-kafka-bootstrap.service-binding-demo.svc:9094"
}

It's true that the key cannot have a = but the value can include = and \n which makes it hard to differentiate where a value ends and a key starts. The template is relying on = and \n as delimiters.

Maybe picking better delimiters or a better structure could make it easier to convert the output text into key-value map.

@navidsh
Copy link
Contributor

navidsh commented Apr 21, 2020

Kafka CR:

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

Descriptors:

    - path: listeners
      x-descriptors:
        - servicebinding:elementType=template:source={{ GO TEMPLATE }}:bindAs=envVar

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 -}}

This template produces as one string:

plain_0="<base64 of my-cluster-kafka-bootstrap.service-binding-demo.svc:9092>"
plain_1="<base64 of my-cluster-kafka-bootstrap.service-binding-demo.svc:9093>"
tls_0="<base64 my-cluster-kafka-bootstrap.service-binding-demo.svc:9094>"

The operator would then parse the above string into secret key-value pairs.

@sbose78
Copy link
Contributor

sbose78 commented Apr 21, 2020

I'm good with this. Let's take this in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants