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

Use labels to filter source in replacements #4763

Open
davinkevin opened this issue Aug 17, 2022 · 13 comments
Open

Use labels to filter source in replacements #4763

davinkevin opened this issue Aug 17, 2022 · 13 comments
Assignees
Labels
kind/feature Categorizes issue or PR as related to a new feature. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@davinkevin
Copy link

Is your feature request related to a problem? Please describe.

As a kustomize & Sealed Secrets user, I would like to use replacement to populate the field spec.tls.0.secretName of an Ingress.

The problem is I don't know beforehand the secret name, but only labels. And obviously, I have multiple SealedSecrets in my setup (some for applications, some for certificates…).

replacements:
  - source:
      kind: SealedSecret
      name: "I don't know the name…"
    targets:
      - select:
          kind: Ingress
        fieldPaths:
          - spec.tls.0.secretName
        options:
          create: true

So if I use something like this, the result is an error like this Error: multiple matches for selector SealedSecret.[noVer].[noGrp]/[noName].[noNs], which is expected from the specification

Describe the solution you'd like

I would like to add some more advanced criteria to resolve the object I want in source. In my context, and to be Kubernetes idiomatic, using labels would do the job perfectly. For example, a potential API for that:

replacements:
  - source:
      kind: SealedSecret
      matchLabels:
        app.kubernetes.io/component: certificate
        app.kubernetes.io/env: dev
    targets:
      - select:
          kind: Ingress
        fieldPaths:
          - spec.tls.0.secretName
        options:
          create: true

Describe alternatives you've considered

I don't have any alternative at the moment, so I have to duplicate the name and update the entry in my Ingress definition every time I want to deploy it.

Additional context

No additional context, feel free to ask question if you want. The method to be modified, IIUC is named selectSourceNode

@davinkevin davinkevin added the kind/feature Categorizes issue or PR as related to a new feature. label Aug 17, 2022
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Aug 17, 2022
@natasha41575
Copy link
Contributor

We will accept this request, but the syntax for label and/or annotation selection in the source selector should be identical to the label/annotation selection in targets. Looking through the kustomize code, it looks like target selectors are specified with the fields annotationSelector and labelSelector, which you can see here

LabelSelector string `json:"labelSelector,omitempty" yaml:"labelSelector,omitempty"`
.

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Sep 28, 2022
@davinkevin
Copy link
Author

Thank you 🙏 and really good thing to use this api rather than the Kubernetes one. Being able to use labels and/or annotations is a great idea too.

@jamesloosli
Copy link

jamesloosli commented Oct 28, 2022

Is there any documentation or working examples for labelSelector?

I'm trying the following;

base.yaml

---
apiVersion: getambassador.io/v3alpha1
kind: Host
metadata:
  name: api-regional
  labels:
    host-type: regional
spec:
  hostname: "api.nonprod-us-west-2.some.link"
---
apiVersion: getambassador.io/v3alpha1
kind: Host
metadata:
  name: api-apex
  labels:
    host-type: apex
spec:
  hostname: "api.some.link"

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: some-namespace
replacements:
  - path: ../replacements/tld.yaml

tld.yaml

source: { kind: ConfigMap, name: some-env, fieldPath: data.DOMAIN_SUFFIX }
targets:
  # apex hosts
  - select:
      kind: Host
      labelSelector: host-type=apex
    fieldPaths: [spec.hostname]
    options: { delimiter: ".", index: 2 }

  # regional hosts
  - select:
      kind: Host
      labelSelector: host-type=regional
    fieldPaths: [spec.hostname]
    options: { delimiter: ".", index: 3 }

I feel like there's something basic I'm missing here. I'm using the document here; https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#api, to guess at what labelSelector is expecting, but I don't seem to be on track. I get this error;

error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go struct field Selector.targets.select.labelSelector of type string

@davinkevin
Copy link
Author

@jamesloosli the proposition is not yet implemented 🤔, I think this is why you have this error.

@vugardzhamalov
Copy link

vugardzhamalov commented Jan 6, 2023

Dear @natasha41575 @davinkevin,

Is this correct to assume that what you are proposing will be somewhat identical to 'labelSelector' and/or 'annotationSelector' already implemented in patch transformer, see here? If so am I correct to assume that these could be implemented both for replacements 'source:' and 'targets[select:]' and 'targets[reject:]'? Thank you!

@vugardzhamalov
Copy link

vugardzhamalov commented Jan 6, 2023

The only thing of course is that originally @davinkevin was talking about the set of labels to match. And current implementation in patch transformer does matching on a single label/annotation. This brings me to another question (I guess I shouldn't even ask here) to @natasha41575 : do you think it will be difficult to replace strings with sets in patch transformer as well? Or to avoid breaking changes... maybe to introduce new sets in addition to existing single label and annotation selectors? Thank you!

@davinkevin
Copy link
Author

In my case, only one labelSelector or annotationSelector couldn't be enough. In k8s, we use both with a lot of information to make debug/operation simpler. In this issue context, it could looks like this:

replacements:
  - source:
      kind: SealedSecret
      labelSelector:
        app.kubernetes.io/type: certificate
        app.kubernetes.io/env: dev
        app.kuberentes.io/domain: "*.foo.com"
    targets:
      …

It was really my original request to have multiple filter. Obviously, using a syntax like this one is something possible, but a bit strange for a new API.

@vugardzhamalov
Copy link

vugardzhamalov commented Jan 8, 2023

Awesome find @davinkevin! Thank you!I had no idea! As per the link you've kindly provided earlier: "operations may specify label selectors to filter the sets of objects".
Not sure if my interpretation is off here: just had a quick test on patch transformer it seems as if 'labelSelector:' string value could be used for a comma separated object declaration. It seems as if negation (!=) is also supported. Meaning that following works for multiple labels. Interesting find I don't think I've seen it documented anywhere in the Kustomize documentation:

labelSelector: 'key01=val01,key02=val02,key03!=val02'

Had no chance to confirm but I am guessing the same will work with 'annotationSelector:'

It looks as if value based filtering with multiple operators, please see here, is also supported, e.g:

labelSelector: 'key in (val01,val02,val03)'

@davinkevin
Copy link
Author

@vugardzhamalov 👍. To be honest, I'm not a huge fan of the string api with query in it (it's good for CLI but not so much for YAML from my point of view), but if it does the job, I'm 100% in!

Thank you for this, I can't wait to see it release 😍

@jamesloosli
Copy link

Also anxiously waiting for this to release. Since 5.0.0 throws errors for unmatched replacements, we're unable to use the latest Kustomize.

@jamesloosli the proposition is not yet implemented 🤔, I think this is why you have this error.

Well that makes sense.

@vugardzhamalov
Copy link

Dear @natasha41575 @davinkevin,

and 'targets[select:]' and 'targets[reject:]'? Thank you!

Also I've just realized that it was clearly identified in merged PR that this is already implemented for both types of targets. We are just waiting for a similar change in the 'source' field now...

@4szt4l
Copy link

4szt4l commented May 10, 2023

This should be added to targets as well, if possible. E.g. replace some values in deployments having a specific annotation or label.

@morix1500
Copy link

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

7 participants