Skip to content

Commit

Permalink
Helm Release: populate ApiVersions and KubeVersion during templating (#…
Browse files Browse the repository at this point in the history
…2653)

<!--Thanks for your contribution. See [CONTRIBUTING](CONTRIBUTING.md)
    for Pulumi's contribution guidelines.

    Help us merge your changes more quickly by adding more details such
    as labels, milestones, and reviewers.-->

### Proposed changes

This PR passes kubeVersion and apiVersions info from the server to Helm
to fix a regression that occurred in
#2568.

### Related issues (optional)

<!--Refer to related PRs or issues: #1234, or 'Fixes #1234' or 'Closes
#1234'.
Or link to full URLs to issues or pull requests in other GitHub
repositories. -->
Fixes: #2631
  • Loading branch information
EronWright authored Nov 8, 2023
1 parent 2e30ca6 commit f1ddfa4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

- Fix: Update pulumi version to 3.91.1 to pick up fixes in python codegen (https://github.com/pulumi/pulumi-kubernetes/pull/2647)
- Fix: Helm Release: chart requires kubeVersion (https://github.com/pulumi/pulumi-kubernetes/pull/2653)

## 4.5.2 (October 26, 2023)
- Fix: Do not patch field managers for Patch resources (https://github.com/pulumi/pulumi-kubernetes/pull/2640)
Expand Down
7 changes: 7 additions & 0 deletions provider/pkg/provider/helm_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,13 @@ func (r *helmReleaseProvider) helmTemplate(ctx context.Context, urn resource.URN
client.PostRenderer = pr
}

if r.clientSet != nil {
err = setKubeVersionAndAPIVersions(r.clientSet, client)
if err != nil {
return "", err
}
}

logger.V(9).Infof("template helm chart")
rel, err := client.RunWithContext(r.canceler.context, c, values)
if err != nil {
Expand Down
66 changes: 38 additions & 28 deletions provider/pkg/provider/invoke_helm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,34 +258,9 @@ func (c *chart) template(clientSet *clients.DynamicClientSet) (string, error) {
}

if clientSet != nil {
dc := clientSet.DiscoveryClientCached
dc.Invalidate()
// The following code to discover Kubernetes version and API versions comes
// from the Helm project:
// https://github.com/helm/helm/blob/d7b4c38c42cb0b77f1bcebf9bb4ae7695a10da0b/pkg/action/action.go#L239
if installAction.KubeVersion == nil {
kubeVersion, err := dc.ServerVersion()
if err != nil {
return "", fmt.Errorf("could not get server version from Kubernetes: %w", err)
}
installAction.KubeVersion = &chartutil.KubeVersion{
Version: kubeVersion.GitVersion,
Major: kubeVersion.Major,
Minor: kubeVersion.Minor,
}
}
// Client-Go emits an error when an API service is registered but unimplemented.
// Since the discovery client continues building the API object, it is correctly
// populated with all valid APIs.
// See https://github.com/kubernetes/kubernetes/issues/72051#issuecomment-521157642
if installAction.APIVersions == nil {
apiVersions, err := action.GetVersionSet(dc)
if err != nil {
if !discovery.IsGroupDiscoveryFailedError(err) {
return "", fmt.Errorf("could not get apiVersions from Kubernetes: %w", err)
}
}
installAction.APIVersions = apiVersions
err = setKubeVersionAndAPIVersions(clientSet, installAction)
if err != nil {
return "", err
}
}

Expand Down Expand Up @@ -349,3 +324,38 @@ func getManifest(rel *release.Release, includeHookResources, includeTestHookReso

return manifests.String()
}

func setKubeVersionAndAPIVersions(clientSet *clients.DynamicClientSet, installAction *action.Install) error {
dc := clientSet.DiscoveryClientCached
dc.Invalidate()

// The following code to discover Kubernetes version and API versions comes
// from the Helm project:
// https://github.com/helm/helm/blob/d7b4c38c42cb0b77f1bcebf9bb4ae7695a10da0b/pkg/action/action.go#L239
if installAction.KubeVersion == nil {
kubeVersion, err := dc.ServerVersion()
if err != nil {
return fmt.Errorf("could not get server version from Kubernetes: %w", err)
}
installAction.KubeVersion = &chartutil.KubeVersion{
Version: kubeVersion.GitVersion,
Major: kubeVersion.Major,
Minor: kubeVersion.Minor,
}
}

// Client-Go emits an error when an API service is registered but unimplemented.
// Since the discovery client continues building the API object, it is correctly
// populated with all valid APIs.
// See https://github.com/kubernetes/kubernetes/issues/72051#issuecomment-521157642
if installAction.APIVersions == nil {
apiVersions, err := action.GetVersionSet(dc)
if err != nil {
if !discovery.IsGroupDiscoveryFailedError(err) {
return fmt.Errorf("could not get apiVersions from Kubernetes: %w", err)
}
}
installAction.APIVersions = apiVersions
}
return nil
}

0 comments on commit f1ddfa4

Please sign in to comment.