kptdev / kpt

Automate Kubernetes Configuration Editing
https://kpt.dev
Apache License 2.0
1.71k stars 227 forks source link

Reference Substrings in Apply Time Mutations #3966

Open tylerreidwaze opened 1 year ago

tylerreidwaze commented 1 year ago

Describe your problem

I am trying to create a Memcached Instance and a DNS Entry for that instance in a single package

I'm trying to use apply-time-mutation which will get the discoveryEndpoint of the memcached and create a DNS record for it in DNS.

The problem is the result includes the port:

kubectl get MemcacheInstance memcached-kpt-test -o jsonpath='{.status.discoveryEndpoint}'
10.19.208.3:11211

And I can't find a way to split it as jsonPath doesnt let me index strings (strings aren't arrays I suppose?)

> kubectl get MemcacheInstance memcached-kpt-test -o jsonpath='{.status.discoveryEndpoint[0:-6]}' -n waze-platform-codelabs
error: error executing jsonpath "{.status.discoveryEndpoint[0:-6]}": Error executing template: string is not array or slice. Printing more information for debugging the template:
    template was:
        {.status.discoveryEndpoint[0:-6]}

The whole object we tried as reference

metadata:
  name: memcached-kpt-test
  annotations:
    config.kubernetes.io/apply-time-mutation: |
      - sourceRef:
          kind: MemcacheInstance
          name: memcached-kpt-test
        sourcePath: $.status.discoveryEndpoint[0:10]
        targetPath: $.spec.rrdatas.value
        token: ${memcache-discovery-ip}
spec:
  name: "memcached-kpt-test.dretest."
  type: "A"
  ttl: 60
  managedZoneRef:
    external: dretest
  rrdatas:
  - "${memcache-discovery-ip}"

This is not a massive blocker for us as Apply Time Mutations aren't supported yet in Config Sync which is the eventual consumer of this output.

Let us know if you need more information or want us to test something out. Or, if there is a more kpt native way to do this.

Thanks in advance!

droot commented 1 year ago

/cc @karlkfi since he has expertise in this area.

karlkfi commented 1 year ago

Sounds like it’s working as intended. The error message is even pretty clear. ;)

I don’t think there’s really any way to do what you’re trying to do with the current design.

Ironically, I initially proposed yq as a parser, which would have allowed this. The jsonPath code can’t easily be modified to add this kind of thing.

We could hypothetically add another field to describe how to process the value, but it’d probably need to support more than just substring operations by index. Regex might work.

pattern: “(.+):\d+” value: “\1”

tylerreidwaze commented 1 year ago

Regex by pattern would work for us. Anyway to extract some of the value instead of all of it really