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

✨ Template defaults with envsubst #3270

Conversation

wfernandes
Copy link
Contributor

What this PR does / why we need it:
This PR introduces support for default variables in the SimpleProcessor. It leverages the library https://github.com/drone/envsubst to parse and evaluate the yaml.

Since the library by default doesn't support ${ VAR}, ${ VAR }, ${VAR }, ${VA$R}, we've added backward compatibility for the first three, however we've dropped support for ${VA$R}.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #3189

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 29, 2020
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 29, 2020
@wfernandes
Copy link
Contributor Author

/test pull-cluster-api-e2e

@vincepri
Copy link
Member

/milestone v0.3.x

@k8s-ci-robot k8s-ci-robot added this to the v0.3.x milestone Jun 30, 2020
Copy link
Member

@fabriziopandini fabriziopandini left a comment

Choose a reason for hiding this comment

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

@wfernandes the PR looks great to me (few small nits)
it will be great to update the clusterct provider contract adding a note about the legacy format (support will be dropped in future) and providing a table of supported syntax for variables (something similar to this but with a brief explanation for each function)

Copy link
Member

@fabriziopandini fabriziopandini left a comment

Choose a reason for hiding this comment

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

PR looks good, some nits on the doc

cmd/clusterctl/client/yamlprocessor/simple_processor.go Outdated Show resolved Hide resolved
docs/book/src/clusterctl/provider-contract.md Outdated Show resolved Hide resolved
docs/book/src/clusterctl/provider-contract.md Show resolved Hide resolved
@wfernandes wfernandes force-pushed the template-defaults-with-envsubst branch from 915d0db to 2f14a19 Compare July 1, 2020 22:49
Copy link
Contributor

@CecileRobertMichon CecileRobertMichon 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 doing this @wfernandes! I will pull your branch and give this a try with some Azure var defaults

docs/book/src/clusterctl/provider-contract.md Outdated Show resolved Hide resolved
@CecileRobertMichon
Copy link
Contributor

@wfernandes to clarify, ${AZURE_VNET_NAME:-${CLUSTER_NAME}-vnet} is not supported syntax?

@CecileRobertMichon
Copy link
Contributor

@fabriziopandini
Copy link
Member

/test pull-cluster-api-e2e

@wfernandes
Copy link
Contributor Author

@wfernandes to clarify, ${AZURE_VNET_NAME:-${CLUSTER_NAME}-vnet} is not supported syntax?

@CecileRobertMichon I tested it out and I believe that will work. I built the binary locally and did the following.

$ export CLUSTER_NAME=foobar
$ echo "AzureVnetName: ${AZURE_VNET_NAME:-${CLUSTER_NAME}-vnet}" | ./envsubst
AzureVnetName: foobar-vnet

I can add a test case if that would help.

@wfernandes
Copy link
Contributor Author

@CecileRobertMichon So it seems that I misspoke. As I was writing the test for the above using the library, the use case you mentioned above was failing.
Testing envsubst in bash as I did above doesn't produce the expected results as it evaluates the string before sending it into envsubst. 🙄
So your issue drone/envsubst#21 is still valid.

@wfernandes wfernandes force-pushed the template-defaults-with-envsubst branch from 2f14a19 to 5f9fe82 Compare July 6, 2020 17:06
@vincepri
Copy link
Member

vincepri commented Jul 6, 2020

That'd be a nice addition, let's see if we can add support for it — it should be as easy as having a commit merged to the library and vendoring it, or do we need other special code for it?

@wfernandes
Copy link
Contributor Author

/hold Working on a fix to envsubst to support syntax like ${VAR:-${DEF_VAR}-suffix}

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 7, 2020
@wfernandes
Copy link
Contributor Author

I created this PR drone/envsubst#23 to add support for the syntax discussed above.

However, I've also updated this PR with a go.mod replace directive to use my fork temporarily. Once upstream drone/envsubst has been merged and tagged, we can switch to that.
@vincepri Let me know if there are any problems with this or if we need to do something more stable for my fork.

@CecileRobertMichon Feel free to test this out and let me know if this works for you. 🙂

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 7, 2020
@vincepri
Copy link
Member

vincepri commented Jul 7, 2020

@wfernandes In general, we should wait for the library PR to merge, let's ping the envsubst maintainers to see if they can review and get it merged within the next two weeks

@wfernandes
Copy link
Contributor Author

/hold
Adding this hold since we want to wait for the drone/envsubst library maintainers to get back to us on our PR drone/envsubst#23

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 8, 2020
@vincepri
Copy link
Member

vincepri commented Jul 9, 2020

@fabriziopandini @CecileRobertMichon @wfernandes Do we want to try to get this in v0.3.7 or wait?

@fabriziopandini
Copy link
Member

I'm +1 to get this merged with the current version of drone if the new won't get ready, because it will greatly simplify the usage of experimental features by introducing something like

- --feature-gates=ClusterResourceSet={$ENABLE_CLUSTER_RESOURCE_SET:false},MachinePools={$ENABLE_MACHINE_POOLS:false}

@CecileRobertMichon
Copy link
Contributor

Just to confirm, this allows defining defaults for the provider components as well?

if so, +1 to getting this in from me as well to be able to test ClusterResourceSet more easily as Fabrizio points out.

@vincepri
Copy link
Member

vincepri commented Jul 9, 2020

Does it? 🤔

@wfernandes
Copy link
Contributor Author

This is a change to the SimpleProcessor which is used by default by the provider components so it should affect those templates. But I'll verify this manually and report back.

Also I got feedback on the drone/envsubst#23 PR so I'll update that and hopefully it will be merged soonish. 🤷

@wfernandes
Copy link
Contributor Author

Also FYI, @vincepri Currently there is a replace directive pointing to my fork of envsubst so that others could pull this down and test it. If this is a "no-no" please let me "know-now". 😆

@vincepri
Copy link
Member

vincepri commented Jul 9, 2020

If the PR doesn't get merged in the envsubst library tomorrow or early next week, we should probably remove the fork and add the support later

@wfernandes wfernandes force-pushed the template-defaults-with-envsubst branch from 0501ce8 to c1f6c15 Compare July 9, 2020 22:31
@wfernandes
Copy link
Contributor Author

@CecileRobertMichon I did a quick test using the overrides functionality of clusterctl and verified that defaults work for infrastructure-components.yaml

@CecileRobertMichon
Copy link
Contributor

Awesome!

envsubst PR just merged 🎉

@wfernandes
Copy link
Contributor Author

@vincepri Lol...as I said that, it just got merged! drone/envsubst#23
OSS for the win!

- This is change to the SimpleProcessor so defaults will be available to
cluster templates and provider components.

- Create specific errMissingVariables error
Since we are using a map, the order of the variables in the error message was changing.
This would make the tests flaky since we were doing a fixed substring comparison.
So the error which wraps the slice of missing vars is used

- Use a specific sha of drone/envsubst until a tag is cut.
@wfernandes wfernandes force-pushed the template-defaults-with-envsubst branch from c1f6c15 to d4907ef Compare July 9, 2020 22:53
@wfernandes
Copy link
Contributor Author

I squashed the commits...let me know if there is any other feedback!

@wfernandes
Copy link
Contributor Author

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jul 9, 2020
@CecileRobertMichon
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 9, 2020
as: `${ VAR }`, `${ VAR}`,`${VAR }`. However, these formats will be deprecated
in the near future. e.g. v1alpha4.

Formats such as `${VAR$FOO}` is not supported.
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this supported now? @CecileRobertMichon @wfernandes

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, it is

Copy link
Contributor Author

@wfernandes wfernandes Jul 10, 2020

Choose a reason for hiding this comment

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

This is still not supported. There has to be an expression character like =, :=, :- between the variables. This will result in an error.

Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

/approve
/milestone v0.3.7

@k8s-ci-robot k8s-ci-robot modified the milestones: v0.3.x, v0.3.7 Jul 10, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: vincepri

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 Jul 10, 2020
@k8s-ci-robot k8s-ci-robot merged commit e020060 into kubernetes-sigs:master Jul 10, 2020
@wfernandes wfernandes deleted the template-defaults-with-envsubst branch July 10, 2020 14:28
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. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clusterctl] Allow providers to specify variable defaults for templates
5 participants