mirror of https://github.com/helm/helm
When Helm is built with Bazel or other non-go-build toolchains, runtime/debug.ReadBuildInfo() returns false because no Go module info is embedded. This caused a nil pointer dereference panic when initializing DefaultCapabilities at package load time. The fix addresses two layers: 1. internal/version/clientgo.go: Add defensive nil checks for both the ok flag and the info pointer, as well as a nil Deps guard. Extract ReadBuildInfo as a package variable to enable unit testing of all failure modes. 2. pkg/chart/common/capabilities.go: When K8sIOClientGoModVersion() fails, fall back to the same default Kubernetes version used during testing (v1.20) instead of returning an error that triggers the panic in the DefaultCapabilities initializer. Log a warning via slog so the fallback is observable. Fixes #31904 Signed-off-by: Yongjie Gong <emerson_gong@hotmail.com>pull/32292/head
parent
543b94d673
commit
18191659a4
@ -0,0 +1,83 @@
|
||||
/*
|
||||
Copyright The Helm Authors.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
|
||||
helmversion "helm.sh/helm/v4/internal/version"
|
||||
)
|
||||
|
||||
// TestMakeDefaultCapabilities_FallbackOnBuildInfoFailure simulates the Bazel
|
||||
// build scenario (issue #31904) where debug.ReadBuildInfo() returns false.
|
||||
// Verifies that makeDefaultCapabilities falls back to defaults instead of
|
||||
// returning an error that would trigger the panic in DefaultCapabilities.
|
||||
func TestMakeDefaultCapabilities_FallbackOnBuildInfoFailure(t *testing.T) {
|
||||
// Override isTesting to return false so we bypass the testing early-return
|
||||
origIsTesting := isTesting
|
||||
isTesting = func() bool { return false }
|
||||
t.Cleanup(func() { isTesting = origIsTesting })
|
||||
|
||||
// Override ReadBuildInfo to simulate Bazel (returns false) by which go binary doesn't contain build info.
|
||||
origReadBuildInfo := helmversion.ReadBuildInfo
|
||||
helmversion.ReadBuildInfo = func() (*debug.BuildInfo, bool) {
|
||||
return nil, false
|
||||
}
|
||||
t.Cleanup(func() { helmversion.ReadBuildInfo = origReadBuildInfo })
|
||||
|
||||
caps, err := makeDefaultCapabilities()
|
||||
if err != nil {
|
||||
t.Fatalf("makeDefaultCapabilities() returned error (would cause panic): %v", err)
|
||||
}
|
||||
if caps == nil {
|
||||
t.Fatal("makeDefaultCapabilities() returned nil capabilities")
|
||||
}
|
||||
// Should fall back to testing defaults
|
||||
if caps.KubeVersion.Major != "1" {
|
||||
t.Errorf("Expected fallback Major=1, got %q", caps.KubeVersion.Major)
|
||||
}
|
||||
if caps.KubeVersion.Minor != "20" {
|
||||
t.Errorf("Expected fallback Minor=20, got %q", caps.KubeVersion.Minor)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMakeDefaultCapabilities_FallbackOnInvalidVersion simulates a scenario
|
||||
// where ReadBuildInfo succeeds but the client-go version string is unparseable.
|
||||
func TestMakeDefaultCapabilities_FallbackOnInvalidVersion(t *testing.T) {
|
||||
origIsTesting := isTesting
|
||||
isTesting = func() bool { return false }
|
||||
t.Cleanup(func() { isTesting = origIsTesting })
|
||||
|
||||
// Return a valid BuildInfo but with an unparseable version
|
||||
origReadBuildInfo := helmversion.ReadBuildInfo
|
||||
helmversion.ReadBuildInfo = func() (*debug.BuildInfo, bool) {
|
||||
return &debug.BuildInfo{
|
||||
Deps: []*debug.Module{
|
||||
{Path: "k8s.io/client-go", Version: "not-a-semver"},
|
||||
},
|
||||
}, true
|
||||
}
|
||||
t.Cleanup(func() { helmversion.ReadBuildInfo = origReadBuildInfo })
|
||||
|
||||
caps, err := makeDefaultCapabilities()
|
||||
if err != nil {
|
||||
t.Fatalf("makeDefaultCapabilities() returned error (would cause panic): %v", err)
|
||||
}
|
||||
if caps.KubeVersion.Major != "1" || caps.KubeVersion.Minor != "20" {
|
||||
t.Errorf("Expected fallback v1.20, got v%s.%s", caps.KubeVersion.Major, caps.KubeVersion.Minor)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue