From b506fcb11cce8c1c90b27c9fcc885f64ed2c609b Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 7 Jan 2025 12:43:19 +0300 Subject: [PATCH] K8s: Fix the gitVersion response (required for kubectl) (#98571) --- pkg/services/apiserver/builder/helper.go | 17 ++++++++++++++- pkg/tests/apis/helper.go | 27 ++++++++++++++++++++++++ pkg/tests/apis/playlist/playlist_test.go | 3 +++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pkg/services/apiserver/builder/helper.go b/pkg/services/apiserver/builder/helper.go index c209903b76e..fd95a2832b4 100644 --- a/pkg/services/apiserver/builder/helper.go +++ b/pkg/services/apiserver/builder/helper.go @@ -232,7 +232,22 @@ func SetupConfig( serverConfig.BuildHandlerChainFunc = buildHandlerChainFunc } - serverConfig.EffectiveVersion = utilversion.DefaultKubeEffectiveVersion() + v := utilversion.DefaultKubeEffectiveVersion() + patchver := 0 // required for semver + + info := v.BinaryVersion().Info() + info.BuildDate = time.Unix(buildTimestamp, 0).UTC().Format(time.RFC3339) + info.GitVersion = fmt.Sprintf("%s.%s.%d+grafana-v%s", info.Major, info.Minor, patchver, buildVersion) + info.GitCommit = fmt.Sprintf("%s@%s", buildBranch, buildCommit) + info.GitTreeState = fmt.Sprintf("grafana v%s", buildVersion) + + info2 := v.EmulationVersion().Info() + info2.BuildDate = info.BuildDate + info2.GitVersion = fmt.Sprintf("%s.%s.%d+grafana-v%s", info2.Major, info2.Minor, patchver, buildVersion) + info2.GitCommit = info.GitCommit + info2.GitTreeState = info.GitTreeState + + serverConfig.EffectiveVersion = v if err := AddPostStartHooks(serverConfig, builders); err != nil { return err diff --git a/pkg/tests/apis/helper.go b/pkg/tests/apis/helper.go index 0e2685e432b..7d1d3084e81 100644 --- a/pkg/tests/apis/helper.go +++ b/pkg/tests/apis/helper.go @@ -20,7 +20,9 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/serializer/yaml" "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/version" yamlutil "k8s.io/apimachinery/pkg/util/yaml" + apimachineryversion "k8s.io/apimachinery/pkg/version" "k8s.io/client-go/discovery" "k8s.io/client-go/dynamic" "k8s.io/client-go/rest" @@ -601,6 +603,31 @@ func (c *K8sTestHelper) NewDiscoveryClient() *discovery.DiscoveryClient { return client } +func (c *K8sTestHelper) GetVersionInfo() apimachineryversion.Info { + c.t.Helper() + + disco := c.NewDiscoveryClient() + req := disco.RESTClient().Get(). + Prefix("version"). + SetHeader("Accept", "application/json") + + result := req.Do(context.Background()) + require.NoError(c.t, result.Error()) + + raw, err := result.Raw() + require.NoError(c.t, err) + info := apimachineryversion.Info{} + err = json.Unmarshal(raw, &info) + require.NoError(c.t, err) + + // Make sure the gitVersion is parsable + v, err := version.Parse(info.GitVersion) + require.NoError(c.t, err) + require.Equal(c.t, info.Major, fmt.Sprintf("%d", v.Major())) + require.Equal(c.t, info.Minor, fmt.Sprintf("%d", v.Minor())) + return info +} + func (c *K8sTestHelper) GetGroupVersionInfoJSON(group string) string { c.t.Helper() diff --git a/pkg/tests/apis/playlist/playlist_test.go b/pkg/tests/apis/playlist/playlist_test.go index 37b623c5cfd..903edcf2102 100644 --- a/pkg/tests/apis/playlist/playlist_test.go +++ b/pkg/tests/apis/playlist/playlist_test.go @@ -49,6 +49,9 @@ func TestIntegrationPlaylist(t *testing.T) { EnableFeatureToggles: []string{}, })) + // Ensure the k8s version is valid + _ = h.GetVersionInfo() + // The accepted verbs will change when dual write is enabled disco := h.GetGroupVersionInfoJSON("playlist.grafana.app") // fmt.Printf("%s", disco)