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

Optionally, overlay/insert via function #742

Merged
merged 2 commits into from
Sep 22, 2022

Conversation

mamachanko
Copy link
Contributor

@mamachanko mamachanko commented Sep 11, 2022

The insert overlay action has an optional 'via' kwarg, which receives the left and right node and produces the new right node, similar to the replace overlay action.

For example, this allows to add a ConfigMap into every matched Kubernetes Namespace without knowing their names in advance:

#@ load("@ytt:overlay", "overlay")

#@ for i in range(3):
---
apiVersion: v1
kind: Namespace
metadata:
  name: #@ i
#@ end

#@ def cm(ns):
apiVersion: v1
kind: ConfigMap
metadata:
  name: insert
  namespace: #@ ns["metadata"]["name"]
#@ end

#@overlay/match by=overlay.subset({"kind": "Namespace"}), expects="0+"
#@overlay/insert after=True, via=lamda ns,_: cm(ns)
---

produces:

apiVersion: v1
kind: Namespace
metadata:
  name: 0
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: insert
  namespace: 0
---
apiVersion: v1
kind: Namespace
metadata:
  name: 1
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: insert
  namespace: 1
---
apiVersion: v1
kind: Namespace
metadata:
  name: 2
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: insert
  namespace: 2

Addresses #738

Qs for the maintainers:

  • What do you think about the suggested API?
  • @jtigger mentioned that: "the solution to this is likely to also resolve Panics when appending to "empty" #718". However, I don't see how.
  • InsertAnnotation.Value is mostly a reproduction of ReplaceAnnotation.Value sans comments and todos. It felt not right to copy over those unless that is preferred.
  • If via=lambda ... expects more than one arg it does not produce a great error message, much like overlay/replace's via. For example: overlay.apply: Document on line stdin:16: function lambda missing 2 arguments (... Would you consider improving that part of this PR?

Companion PR for docs: https://github.com/vmware-tanzu/carvel/pull/553

Copy link
Contributor

@pivotaljohn pivotaljohn left a comment

Choose a reason for hiding this comment

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

What do you think about the suggested API?

🤔 Any reason not to include the "right" value as an argument to via=?

It doesn't always come up, but doing so allows the author to highlight a YAML fragment as an important piece. e.g. resource limits that override a targeted PodSpec of some kind where the point is to replace them.

Doing so also makes it easier to re-use a function written for @overlay/replace with @overlay/insert.

Finally, it is consistent; one less thing to explain to authors.

Thoughts?


@jtigger #738 (comment) that: "the solution to this is likely to also resolve #718". However, I don't see how.

👎🏻 I was wrong about that. I assumed that since we were talking about inserting into an empty collection, that would have been part of the implementation, here. In fact, @overlay/append is not merely an @overlay/insert after=True. 🤔💭


InsertAnnotation.Value is mostly a reproduction of ReplaceAnnotation.Value sans comments and todos. It felt not right to copy over those unless that is preferred.

👍🏻 Yeah, the comments there don't seem to restore much (if any) context that the reader would already have.


If via=lambda ... expects more than one arg it does not produce a great error message, much like overlay/replace's via. For example: overlay.apply: Document on line stdin:16: function lambda missing 2 arguments (... Would you consider improving that part of this PR?

👌🏻 I wouldn't consider it to be required scope for this PR. @overlay/replace via= has behaved this way for years.

Comment on lines 66 to 74
case InsertAnnotationKwargVia:
annotation.via = &kwarg[1]

Copy link
Contributor

Choose a reason for hiding this comment

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

(Commentary, not a request for a change, per se.)

It's interesting that in @overlay/replace (and therefore here) we store the via= as a starlark.Value (rather than a starlark.Callable).
We also don't vet the argument, here — a natural choke-point for invalid input.

The advantage of vetting/constraining as early as possible is that it generally simplifies subsequent logic, reducing complexity overall.

Since there's very little distance between NewAssertionAnnotation() and Value() (below), I don't see that complexity reduction: just moving code around.

It's more meaningful as aiming for exemplary code (i.e. worthy of imitation).

It's not the responsibility of the author of this PR to "fix" this; nor is this a blocker for this PR (that's impractical).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have refactored both annotations to store their via kwarg as a starklark.Callable. To avoid regression, the error case for both are covered with a template test.

My first intuition was to extend pkg.template.core.StarlarkValue with a AsCallable, but that does not work because Go has no function type that is arg-agnostic, which is why we like Go. And so, I settled for a straight-forward type assertion within the New* constructor of each annotation.

@pivotaljohn
Copy link
Contributor

Companion PR for docs: https://github.com/vmware-tanzu/carvel/pull/553

Wow! This is wonderful! 🙏🏻

The insert overlay action has an optional 'via' kwarg, which receives
the left and right node and produces the new right node, similar to the replace
overlay action.
@mamachanko
Copy link
Contributor Author

@pivotaljohn As per your very reasonable suggestion, the via kwarg now receives both the left and the right node. 🙇🏻‍♂️

Copy link
Contributor

@pivotaljohn pivotaljohn left a comment

Choose a reason for hiding this comment

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

Hey @mamachanko, thanks for your patience as we cleared the decks with the v0.43.0 release. 🙇🏻‍♂️

I walked through the flow again; this all looks spot on.
You included some tidying up and we appreciate that as well.
Thank you for the added tests.

@pivotaljohn pivotaljohn merged commit edd111f into carvel-dev:develop Sep 22, 2022
@mamachanko mamachanko deleted the insert-via branch September 23, 2022 05:55
@mamachanko
Copy link
Contributor Author

mamachanko commented Sep 23, 2022

@pivotaljohn wonderful! 🥹

You closed #718, but is it possible you meant to close #738 instead?

@github-actions github-actions bot added the carvel triage This issue has not yet been triaged for relevance label Sep 23, 2022
@pivotaljohn
Copy link
Contributor

Oh wow... I think it was this comment that triggered the workflow: #738 (comment)

I'll be choosing my words more wisely, going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
carvel triage This issue has not yet been triaged for relevance cla-not-required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panics when appending to "empty"
3 participants