Skip to content
Merged
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
34 changes: 34 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ func renderManifests(cfg *helmaction.Configuration, hr *v2.HelmRelease, chartDir
inst.DisableHooks = true

if plain {
kubeVer, err := discoverKubeVersion(rc)
if err != nil {
return "", err
}
inst.KubeVersion = &kubeVer
vers, err := discoverAPIVersions(rc)
Comment on lines 240 to 246
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

*Guard against nil rest.Config before querying the cluster
renderManifests() assumes that rc is non-nil. When cmdShow is invoked with --plain and there is no valid KUBECONFIG, restConfig() returns (nil, err) but the error is ignored (rc, _ := restConfig()).
Calling discoverKubeVersion(rc) (or discoverAPIVersions(rc)) with a nil pointer will panic inside the REST client constructor.

-if plain {
-    kubeVer, err := discoverKubeVersion(rc)
+if plain {
+    if rc == nil {
+        return "", fmt.Errorf("renderManifests: kubeconfig not found – cannot discover cluster version")
+    }
+    kubeVer, err := discoverKubeVersion(rc)

Fail fast with a clear error instead of a runtime panic.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if plain {
kubeVer, err := discoverKubeVersion(rc)
if err != nil {
return "", err
}
inst.KubeVersion = &kubeVer
vers, err := discoverAPIVersions(rc)
if plain {
if rc == nil {
return "", fmt.Errorf("renderManifests: kubeconfig not found – cannot discover cluster version")
}
kubeVer, err := discoverKubeVersion(rc)
if err != nil {
return "", err
}
inst.KubeVersion = &kubeVer
vers, err := discoverAPIVersions(rc)
🤖 Prompt for AI Agents
In main.go around lines 240 to 246, the code calls discoverKubeVersion(rc) and
discoverAPIVersions(rc) without checking if rc (a *rest.Config) is nil, which
can cause a panic if restConfig() returned nil due to missing or invalid
KUBECONFIG. Modify the code to check if rc is nil immediately after calling
restConfig(), and if so, return a clear error indicating the missing or invalid
kubeconfig before proceeding to call any functions that use rc.

if err != nil {
return "", err
Expand Down Expand Up @@ -319,6 +324,16 @@ func realHelmDiff(cfg *helmaction.Configuration, hr *v2.HelmRelease, chartDir st
inst.DisableHooks = true
inst.PostRenderer = &fluxPostRenderer{name: hr.Name, ns: hr.Namespace}

rc, err := restConfig()
if err != nil {
return "", err
}
kubeVer, err := discoverKubeVersion(rc)
if err != nil {
return "", err
}
inst.KubeVersion = &kubeVer

ch, err := loader.Load(chartDir)
if err != nil {
return "", err
Expand Down Expand Up @@ -1219,3 +1234,22 @@ func cmdReconcile() *cobra.Command {
"Force a one-off upgrade of the HelmRelease")
return cmd
}

func discoverKubeVersion(rc *rest.Config) (hchart.KubeVersion, error) {
if rc == nil {
return hchart.KubeVersion{}, fmt.Errorf("renderManifests: kubeconfig not found – cannot discover cluster version")
}
dc, err := discovery.NewDiscoveryClientForConfig(rc)
if err != nil {
return hchart.KubeVersion{}, err
}
info, err := dc.ServerVersion()
if err != nil {
return hchart.KubeVersion{}, err
}
return hchart.KubeVersion{
Version: info.GitVersion,
Major: info.Major,
Minor: info.Minor,
}, nil
}
Loading