redhat-cop / resource-locker-operator

Apache License 2.0
30 stars 14 forks source link

Can patchTemplate output partial value of a field contains YAML text #65

Open morningspace opened 2 years ago

morningspace commented 2 years ago

What I'm trying is to grab text from a ConfigMap to fill the patchTemplate. For example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-cm
data:
  settings: |
    kong:
      replicaCount: 1

I'd like to fill the patchTemplate by retrieving text started from data.settings.kong. Something similar as below (of course it wouldn't work):

apiVersion: redhatcop.redhat.io/v1alpha1
kind: ResourceLocker
metadata:
  name: test-patch
  namespace: resource-locker-operator
spec:
  patches:
  - targetObjectRef:
      apiVersion: charts.helm.k8s.io/v1alpha1
      kind: Kong
      name: gateway
    patchTemplate: |
      {{ (index . 0).data.settings | yq ".kong" }}
    patchType: application/merge-patch+json
    sourceObjectRefs:
    - apiVersion: v1
      kind: ConfigMap
      name: test-cm
    id: kong-patch

Not sure if that's a common case, but wondering if there's any suggestion on this.

raffaelespazzoli commented 2 years ago

we try to make sure that the template engine has all of the same functions as you can find in helm charts. For this case the fromYaml function should work: {{ ((index . 0).data.settings | fromYaml ).kong }}

On Sun, Dec 19, 2021 at 8:57 AM MorningSpace @.***> wrote:

What I'm trying is to grab text from a ConfigMap to fill the patchTemplate. For example:

apiVersion: v1kind: ConfigMapmetadata: name: test-cmdata: settings: | kong: replicaCount: 1

I'd like to fill the patchTemplate by retrieving text started from data.settings.kong. Something similar as below (of course it wouldn't work):

apiVersion: redhatcop.redhat.io/v1alpha1kind: ResourceLockermetadata: name: test-patch namespace: resource-locker-operatorspec: patches:

  • targetObjectRef: apiVersion: charts.helm.k8s.io/v1alpha1 kind: Kong name: gateway patchTemplate: | {{ (index . 0).data.settings | yq ".kong" }} patchType: application/merge-patch+json sourceObjectRefs:
    • apiVersion: v1 kind: ConfigMap name: test-cm id: kong-patch

Not sure if that's a common case, but wondering if there's any suggestion on this.

— Reply to this email directly, view it on GitHub https://github.com/redhat-cop/resource-locker-operator/issues/65, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABPERXGP3RAX5YWTKHUXYCTURXQFVANCNFSM5KL5DHZQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- ciao/bye Raffaele

morningspace commented 2 years ago

Thanks @raffaelespazzoli for your quick response!

It sounds great, but I did a quick try and was not able to make it work in some cases. What I tried is that using below ConfigMap:

apiVersion: v1
data:
  settings: |
    kong:
      foo:
        bar: "baz"
      replicaCount: 2
kind: ConfigMap
metadata:
  name: test-cm

And, the ResourceLocker:

apiVersion: redhatcop.redhat.io/v1alpha1
kind: ResourceLocker
metadata:
  name: test-patch
  namespace: resource-locker-operator
spec:
  patches:
  - targetObjectRef:
      apiVersion: charts.helm.k8s.io/v1alpha1
      kind: Kong
      name: gateway
    patchTemplate: |
      spec:
        {{ ((index . 0).data.settings | fromYaml).kong | toYaml }}
    patchType: application/merge-patch+json
    sourceObjectRefs:
    - apiVersion: v1
      kind: ConfigMap
      name: test-cm
    id: kong-patch

The output of Kong is:

apiVersion: charts.helm.k8s.io/v1alpha1
kind: Kong
metadata:
  name: gateway
  namespace: default
replicaCount: 2
spec:
  bar: baz
  ...
status:
  ...

It appears the spec field is missing when it patches to the target resource. If I use the below ConfigMap:

apiVersion: v1
data:
  settings: |
    kong:
      spec:
        foo:
          bar: "baz"
        replicaCount: 2
kind: ConfigMap
metadata:
  name: test-cm

And, the patchTemplate as below:

    patchTemplate: |
        {{ ((index . 0).data.settings | fromYaml).kong | toYaml }}

That will work:

apiVersion: charts.helm.k8s.io/v1alpha1
kind: Kong
metadata:
  name: gateway
  namespace: default
spec:
  foo:
    bar: baz
  replicaCount: 2
  ...
status:
  ...