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

Helm Release: populate ApiVersions and KubeVersion during templating #2653

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading