Skip to content

install: Add ability to auto-fill or manually set .Capabilities.APIVersions in helm - #1176

Merged
michi-covalent merged 3 commits into
cilium:masterfrom
learnitall:pr/learnitall/add-apiversions-to-install
Oct 29, 2022
Merged

michi-covalent merged 3 commits into
cilium:masterfrom
learnitall:pr/learnitall/add-apiversions-to-install

Conversation

@learnitall

@learnitall learnitall commented Oct 26, 2022 •

Copy link
Copy Markdown
Contributor

This is an attempt to fix #1107 by introducing two ways to set .Capabilities.APIVersions in helm while rendering Cilium templates:

  • Query the api-server to find all known needed api-resources in a cluster for verifying cilium templates, passing them to .Capabilities.APIVersions.
  • Let a user manually set the list by using the --api-versions CLI argument.

I've also adjusted the code that prints out the helm template command to include any equivalent --api-versions args when setting .Capabilities.APIVersions using one of the above methods.

To do this, I changed following functions/interfaces:

  1. hubble/hubble.go: k8sHubble.genManifests: Adjust for breaking changes in internal/helm.go
  2. install/helm.go: K8sInstaller.generateManifests: Pull api versions from CLI params if available, otherwise use client to auto-populate. If auto-populate fails, log a warning to the user.
  3. install/install.go: Parameters: Add ApiVersions parameter.
  4. install/install.go: k8sInstallerImplementation: Add ListApiResources method signature.
  5. k8s/client.go: Client.ListApiResources: Add ListApiResources implementation.
  6. internal/helm/helm.go: newClient: Add argument for setting client.APIVersions.
  7. internal/helm/helm.go: GenManifests: Add argument for passing api versions to newClient.
  8. internal/helm/helm.go: MergeVals: Move helm template logging PrintHelmTemplateCommand
  9. internal/helm/helm.go: PrintHelmTemplateCommand: New function for logging the helm template with a parameter for setting --api-versions.

@learnitall
learnitall temporarily deployed to ci October 26, 2022 21:03 Inactive
@learnitall learnitall changed the title Add ability to auto-fill or manually set .Capabilities.APIVersions in helm install: Add ability to auto-fill or manually set .Capabilities.APIVersions in helm Oct 26, 2022

@michi-covalent michi-covalent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm other than lint issues

@michi-covalent

Copy link
Copy Markdown
Contributor

btw gke workflows are broken right now. should be fixed in #1177

@michi-covalent

Copy link
Copy Markdown
Contributor

ok #1177 is merged. please rebase 🙏

@learnitall
learnitall force-pushed the pr/learnitall/add-apiversions-to-install branch from 1cda8b8 to 89e54ea Compare October 27, 2022 21:15
@learnitall
learnitall temporarily deployed to ci October 27, 2022 21:15 Inactive
@learnitall

Copy link
Copy Markdown
Contributor Author

Rebased! Additionally, I adjust the code to only include known-needed CRDs (right now just monitoriong.coreos.com/v1) if they are found, rather than take every single found api resource in the cluster, so the helm template log looks better. Only caveat is that if service monitors are not enabled and if monitoring.coreos.com/v1 is found in the cluster, it'll still be included even though it won't be needed when verifying the chart.

@learnitall
learnitall marked this pull request as ready for review October 27, 2022 21:26
@learnitall
learnitall requested review from a team as code owners October 27, 2022 21:26
@learnitall
learnitall requested review from kaworu, ldelossa, michi-covalent and tommyp1ckles and removed request for kaworu, ldelossa and tommyp1ckles October 27, 2022 21:26

@michi-covalent michi-covalent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚢

@tommyp1ckles

Copy link
Copy Markdown
Contributor

@learnitall thanks for the PR, taking a look now.

I also noticed a spelling mistake in the first commit: The main use caes for such a method is filling in helm's

Do you mind rewording that

Comment thread k8s/client.go Resolved
Comment thread k8s/client.go Resolved
Comment thread internal/helm/helm.go Resolved
Comment thread internal/helm/helm.go
apiVersionsStr := ""
if len(apiVersions) > 0 {
for _, av := range apiVersions {
apiVersionsStr = fmt.Sprintf("%s --api-versions %s", apiVersionsStr, av)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think --api-versions takes a string array, could we do that instead of adding a bunch of individual --api-versions flags to make the rendered command cleaner?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried to see if we could, but I don't think we can. You're right that helm uses a StringArrayVarP for --api-versions (template.go#L184), but any value passed to a StringArrayVarP will be taken "as-is" and not be separated by commas. That functionality is implemented in StringSliceVarP:

# https://pkg.go.dev/github.com/spf13/pflag#StringArrayVarP
StringArrayVar defines a string flag with specified name, default value, and usage string.
The argument p points to a []string variable in which to store the value of the flag.
The value of each argument will not try to be separated by comma. Use a StringSlice for that.

I don't think helm does any work to take the values from --api-versions and look for CSVs, as the string slice from --api-versions is converted to a VersionSet, which is just an alias for a string slice. It as a method Has(apiVersion string) bool which is just a simple for-loop search through the slice.

I submitted an issue on this upstream, maybe in the future we can revisit: helm/helm#11485

@maintainer-s-little-helper maintainer-s-little-helper Bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Oct 28, 2022
@learnitall
learnitall force-pushed the pr/learnitall/add-apiversions-to-install branch from 89e54ea to 25c7d70 Compare October 28, 2022 19:23
@learnitall
learnitall temporarily deployed to ci October 28, 2022 19:23 Inactive
@learnitall

Copy link
Copy Markdown
Contributor Author

Force pushed a new set of commits that address your feedback @tommyp1ckles, as well as fixing that typo. (Thank you for the nits, really helpful in getting my Go up to spec.)

This commit adds `ListApiResources`, which is based off of
kubectl's `api-resources` sub-command, allowing for consumers
of the k8s client to gain information on what GVRs are installed
into the cluster.

The main use case for such a method is filling in helm's
API Capabilities for validity checks that include CRDs.

Signed-off-by: Ryan Drew <[email protected]>
This commit takes the `helm template` logging step in `MergeVals` and
splits it off into its own function. This allows for the use of merged
helm values to decide on what to include for `--api-versions` without
having to add this logic into `MeregVals` itself.

Signed-off-by: Ryan Drew <[email protected]>
The commit introduces the `--api-versions` parameter for the `install`
sub-command, which allows for users to set helm's
Capabilities.APIVersions as needed for Cilium chart validation. If
a user does not provide a value or set of values for `--api-versions`,
then a discovery client will be used to auto-fill this value based
on the available api versions in a cluster (see install/helm.go#L271).

This change is needed because helm will not contact the k8s api server
to determine what CRDs are installed into a cluster, leading to chart
validation to fail anytime a CRD is required. For this reason, the
`--api-versions` CLI argument was introduced to `helm template` (see
pr [helm/helm#5392]). The cilium-cli does not allow for users to specify
this argument to helm, therefore users can face a similar issue when
installing cilium with certain features. For instance, enabling the use
of prometheus ServiceMonitors will trigger a check for
`monitoring.coreos.com/v1` while performing chart validation (by
[cilium/install/kubernetes/cilium/templates/validate.yaml#L21]), but
since helm does not adjust Capabilties.APIVersions based on the CRDs
installed in a cluster, helm will _always_ believe that
`monitoring.coreos.com/v1` is not available.

[helm/helm#5392]: helm/helm#5392
[cilium/install/kubernetes/cilium/templates/validate.yaml#L21]: https://github.com/cilium/cilium/blob/v1.12.2/install/kubernetes/cilium/templates/validate.yaml#L21

Fixes: cilium#1107

Signed-off-by: Ryan Drew <[email protected]>
@learnitall
learnitall force-pushed the pr/learnitall/add-apiversions-to-install branch from 25c7d70 to b012dd6 Compare October 28, 2022 21:00
@learnitall
learnitall temporarily deployed to ci October 28, 2022 21:00 Inactive

@christarazi christarazi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🚀

@michi-covalent

Copy link
Copy Markdown
Contributor

alright let's ship this

@michi-covalent
michi-covalent merged commit aec044a into cilium:master Oct 29, 2022
@learnitall

Copy link
Copy Markdown
Contributor Author

Thanks everybody! 🚀

This branch was previously deployed

1 inactive deployment
ci — b012dd6f Deployed Oct 28, 2022 by learnitall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge This PR has passed all tests and received consensus from code owners to merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cilium install fails if serviceMonitors are enabled

4 participants