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

feat(chart): allow to run tpl on ServiceAccount annotations #4958

Merged

Conversation

fcrespofastly
Copy link
Contributor

@fcrespofastly fcrespofastly commented Dec 17, 2024

Description

The basic use-case is to add dynamic environmental metadata to external-dns KSA so we can take advantage of advanced cloud IAM features such as IRSA or Workload Identity. We already provide the necessary metadata through values so we could DRY those values and render them in values files. For instance:

serviceAccount:
  annotations:
    iam.gke.io/gcp-service-account: "external-dns@{{ .Values.metadata.gcpProject }}.iam.gserviceaccount"

This change doesn't break any previous behavior.

Copy link

linux-foundation-easycla bot commented Dec 17, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Dec 17, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @fcrespofastly!

It looks like this is your first PR to kubernetes-sigs/external-dns 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/external-dns has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @fcrespofastly. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Dec 17, 2024
@fcrespofastly
Copy link
Contributor Author

Hey @szuecs @stevehipwell 👋🏻 !

How do you feel about this change? it should be basically noop, can we launch the tests?

@fcrespofastly
Copy link
Contributor Author

I also updated the CHANGELOG adding an entry under UNRELEASED.

@szuecs @stevehipwell ☝🏻 I think that's correct right?

@mloiseleur
Copy link
Contributor

/ok-to-test
/lgtm
/retitle feat(chart): allow to run tpl on ServiceAccount annotations

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Dec 22, 2024
@k8s-ci-robot k8s-ci-robot changed the title Allow run tpl on serviceaccount annotations feat(chart): allow to run tpl on ServiceAccount annotations Dec 22, 2024
@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Dec 22, 2024
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 22, 2024
@mloiseleur
Copy link
Contributor

/assign @stevehipwell
/unassign @szuecs

@mloiseleur mloiseleur removed their assignment Dec 22, 2024
@ivankatliarchuk
Copy link
Contributor

Is it not safer to create a helper function in _helpers.tpl, and reference it in template?

{{- define "external-dns.serviceAccountAnnotations" -}}
...

Then for example if someone deploy external-dns with umbrella chart can override behaviour in local _helpers.tpl?

Example

apiVersion: v2
name: external-dns-umbrella
dependencies:
- name: external-dns
  version: 1.15.0
  repository: https://kubernetes-sigs.github.io/external-dns/

and in my local _helper.tpl

{{- define "external-dns.serviceAccountAnnotations" -}}
.. behaviour override

@fcrespofastly
Copy link
Contributor Author

fcrespofastly commented Jan 7, 2025

Hey @ivankatliarchuk 👋🏻 !

Thanks for suggesting this, that's an interesting approach as well! I don't mind iterating on this towards your suggested approach, but I want to make sure we're all on the same page:

  • By running tpl() we're not changing the behaviour, right? We're just allowing to treat a string as a template. Is there any use case where this change breaks template rendering that I'm missing?
  • If we do this with service account annotations, then should we do it with podAnnotations too just to be consistent? If the answer is yes, then the scope of this PR will be bigger I guess.
  • What would be the default template when the local helper is not defined?

@stevehipwell thoughts?

Thanks folks!

Copy link
Contributor

@stevehipwell stevehipwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @fcrespofastly but the implementation isn't correct due to the way helm works with YAML blobs. This can be solved with a loop as the annotations are a fixed simple type.

Please could you also update the comment in values.yaml so the ability to template is added to the docs (you need to run helm-docs after making this change to update README.md).

@@ -11,7 +11,7 @@ metadata:
{{- end }}
{{- with .Values.serviceAccount.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- tpl (toYaml .) $ | nindent 4 }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{{- tpl (toYaml .) $ | nindent 4 }}
{{- range $k, $v := . }}
{{- printf "%s: %s" (tpl $k $) (tpl $v $) }}
{{- end }}

@@ -27,6 +27,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed automatic addition of pod selector labels to `affinity` and `topologySpreadConstraints` if not defined. _@pvickery-ParamountCommerce_

### Changed

- Allow templatizing `serviceaccount.annotations` in values, by rendering them using `tpl` built-in function: [#4958](https://github.com/kubernetes-sigs/external-dns/pull/4958) @fcrespofastly
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Allow templatizing `serviceaccount.annotations` in values, by rendering them using `tpl` built-in function: [#4958](https://github.com/kubernetes-sigs/external-dns/pull/4958) @fcrespofastly
- Allow templating `serviceaccount.annotations` keys and values, by rendering them using the `tpl` built-in function. ([#4958](https://github.com/kubernetes-sigs/external-dns/pull/4958)) _@fcrespofastly_

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 7, 2025
@k8s-ci-robot k8s-ci-robot removed the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Jan 7, 2025
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jan 7, 2025
@fcrespofastly
Copy link
Contributor Author

@stevehipwell thanks for the feedback!! I implemented the suggested changes, plus a minor nit (nindent).

Out of curiosity now, why the initial implementation doesn't work exactly? You mentioned due to the way Helm handles YAML blobs but I was wondering if you could provide with an example (again, just curiosity)

Thanks again!

Comment on lines 34 to 36
# -- Annotations to add to the service account. Templates are allowed here. Example:
# annotations:
# example.com/annotation: {{ .Values.nameOverride }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# -- Annotations to add to the service account. Templates are allowed here. Example:
# annotations:
# example.com/annotation: {{ .Values.nameOverride }}
# -- Annotations to add to the service account. Templates are allowed in both the key and value; for example `example.com/annotation/{{ .Values.nameOverride }}: {{ .Values.nameOverride }}`.

Helm docs doesn't support multi-line strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! thanks!

Copy link
Contributor

@stevehipwell stevehipwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fcrespofastly. Could you add a templated annotation to the charts/external-dns/ci/ci-values.yaml file so this pattern is validated?

@fcrespofastly
Copy link
Contributor Author

@stevehipwell done! 🚀

@stevehipwell
Copy link
Contributor

/label tide/merge-method-squash
/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Jan 7, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: stevehipwell

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 7, 2025
@k8s-ci-robot k8s-ci-robot merged commit f8057a7 into kubernetes-sigs:master Jan 7, 2025
15 checks passed
fjvela pushed a commit to fjvela/external-dns that referenced this pull request Jan 8, 2025
…etes-sigs#4958)

* Allow run tpl on serviceaccount annotations

* updated the CHANGELOG

* iterate over kv and tpl and update docs

* fix multiline-string docs

* add CI values

* add more CI values

* fix ci values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants