From d03a1c3d4b6777b904e60cd3a1cb0706164a0534 Mon Sep 17 00:00:00 2001 From: Eli Judah Date: Sun, 18 Apr 2021 00:15:00 +0100 Subject: [PATCH] Replace package level default variables with functions that provide the same utility. These package level variables cause data races when helm template or any command that relies on these defaults are called concurrently. Updated package level tests to use replacement functions. Signed-off-by: Eli Judah --- pkg/chartutil/capabilities.go | 33 +++++++++++++++--------------- pkg/chartutil/capabilities_test.go | 6 +++--- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/pkg/chartutil/capabilities.go b/pkg/chartutil/capabilities.go index c002e33f2..87b9563e6 100644 --- a/pkg/chartutil/capabilities.go +++ b/pkg/chartutil/capabilities.go @@ -32,22 +32,6 @@ const ( k8sVersionMinor = 20 ) -var ( - // DefaultVersionSet is the default version set, which includes only Core V1 ("v1"). - DefaultVersionSet = allKnownVersions() - - // DefaultCapabilities is the default set of capabilities. - DefaultCapabilities = &Capabilities{ - KubeVersion: KubeVersion{ - Version: fmt.Sprintf("v%d.%d.0", k8sVersionMajor, k8sVersionMinor), - Major: strconv.Itoa(k8sVersionMajor), - Minor: strconv.Itoa(k8sVersionMinor), - }, - APIVersions: DefaultVersionSet, - HelmVersion: helmversion.Get(), - } -) - // Capabilities describes the capabilities of the Kubernetes cluster. type Capabilities struct { // KubeVersion is the Kubernetes version. @@ -102,3 +86,20 @@ func allKnownVersions() VersionSet { } return vs } + +// DefaultCapabilities returns the default set of capabilities. +func DefaultCapabilities() *Capabilities { + return &Capabilities{KubeVersion: KubeVersion{ + Version: fmt.Sprintf("v%d.%d.0", k8sVersionMajor, k8sVersionMinor), + Major: strconv.Itoa(k8sVersionMajor), + Minor: strconv.Itoa(k8sVersionMinor), + }, + APIVersions: DefaultVersionSet(), + HelmVersion: helmversion.Get(), + } +} + +// DefaultVersionSet is the default version set, which includes only Core V1 ("v1"). +func DefaultVersionSet() VersionSet { + return allKnownVersions() +} diff --git a/pkg/chartutil/capabilities_test.go b/pkg/chartutil/capabilities_test.go index 7134abfc5..e80c14c9a 100644 --- a/pkg/chartutil/capabilities_test.go +++ b/pkg/chartutil/capabilities_test.go @@ -35,13 +35,13 @@ func TestVersionSet(t *testing.T) { } func TestDefaultVersionSet(t *testing.T) { - if !DefaultVersionSet.Has("v1") { + if !DefaultVersionSet().Has("v1") { t.Error("Expected core v1 version set") } } func TestDefaultCapabilities(t *testing.T) { - kv := DefaultCapabilities.KubeVersion + kv := DefaultCapabilities().KubeVersion if kv.String() != "v1.20.0" { t.Errorf("Expected default KubeVersion.String() to be v1.20.0, got %q", kv.String()) } @@ -60,7 +60,7 @@ func TestDefaultCapabilities(t *testing.T) { } func TestDefaultCapabilitiesHelmVersion(t *testing.T) { - hv := DefaultCapabilities.HelmVersion + hv := DefaultCapabilities().HelmVersion if hv.Version != "v3.5" { t.Errorf("Expected default HelmVersion to be v3.5, got %q", hv.Version)