From d3e6a8e356fef2ebc2454c1f9fb75d4c36dcf5d6 Mon Sep 17 00:00:00 2001 From: Iceber Gu Date: Tue, 27 Aug 2024 11:33:21 +0800 Subject: [PATCH] update code for kubernetes v1.31.0 Signed-off-by: Iceber Gu --- pkg/apiserver/apiserver.go | 16 ++++++---------- pkg/kube_state_metrics/metrics_handler.go | 8 ++++---- pkg/kubeapiserver/apiserver.go | 9 +++++---- pkg/metrics/metrics.go | 4 ++-- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 88fff05a8..4d927b2aa 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -10,10 +10,10 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/serializer" - "k8s.io/apimachinery/pkg/version" "k8s.io/apiserver/pkg/registry/rest" genericapiserver "k8s.io/apiserver/pkg/server" "k8s.io/apiserver/pkg/server/healthz" + "k8s.io/apiserver/pkg/util/version" "k8s.io/client-go/discovery" clientrest "k8s.io/client-go/rest" "k8s.io/client-go/restmapper" @@ -84,17 +84,13 @@ type CompletedConfig struct { // Complete fills in any fields not set that are required to have valid data. It's mutating the receiver. func (cfg *Config) Complete() CompletedConfig { + cfg.GenericConfig.EffectiveVersion = version.DefaultBuildEffectiveVersion() + c := completedConfig{ cfg.GenericConfig.Complete(), cfg.GenericConfig.ClientConfig, cfg.StorageFactory, } - - c.GenericConfig.Version = &version.Info{ - Major: "1", - Minor: "0", - } - return CompletedConfig{&c} } @@ -159,8 +155,8 @@ func (config completedConfig) New() (*ClusterPediaServer, error) { } genericServer.AddPostStartHookOrDie("start-clusterpedia-informers", func(context genericapiserver.PostStartHookContext) error { - clusterpediaInformerFactory.Start(context.StopCh) - clusterpediaInformerFactory.WaitForCacheSync(context.StopCh) + clusterpediaInformerFactory.Start(context.Done()) + clusterpediaInformerFactory.WaitForCacheSync(context.Done()) return nil }) @@ -171,7 +167,7 @@ func (config completedConfig) New() (*ClusterPediaServer, error) { } func (server *ClusterPediaServer) Run(ctx context.Context) error { - return server.GenericAPIServer.PrepareRun().Run(ctx.Done()) + return server.GenericAPIServer.PrepareRun().RunWithContext(ctx) } type hooksDelegate struct { diff --git a/pkg/kube_state_metrics/metrics_handler.go b/pkg/kube_state_metrics/metrics_handler.go index 1efad9256..c48dc1936 100644 --- a/pkg/kube_state_metrics/metrics_handler.go +++ b/pkg/kube_state_metrics/metrics_handler.go @@ -51,8 +51,8 @@ func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { contentType := expfmt.NegotiateIncludingOpenMetrics(r.Header) // We do not support protobuf at the moment. Fall back to FmtText if the negotiated exposition format is not FmtOpenMetrics See: https://github.com/kubernetes/kube-state-metrics/issues/2022 - if contentType != expfmt.FmtOpenMetrics_1_0_0 && contentType != expfmt.FmtOpenMetrics_0_0_1 { - contentType = expfmt.FmtText + if contentType.FormatType() != expfmt.TypeOpenMetrics { + contentType = expfmt.NewFormat(expfmt.TypeTextPlain) } resHeader.Set("Content-Type", string(contentType)) @@ -70,7 +70,7 @@ func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - metricsWriters := metricsstore.SanitizeHeaders(writers) + metricsWriters := metricsstore.SanitizeHeaders(string(contentType), writers) for _, w := range metricsWriters { err := w.WriteAll(writer) if err != nil { @@ -79,7 +79,7 @@ func (m *MetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // OpenMetrics spec requires that we end with an EOF directive. - if contentType == expfmt.FmtOpenMetrics_1_0_0 || contentType == expfmt.FmtOpenMetrics_0_0_1 { + if contentType.FormatType() == expfmt.TypeOpenMetrics { _, err := writer.Write([]byte("# EOF\n")) if err != nil { klog.ErrorS(err, "Failed to write EOF directive") diff --git a/pkg/kubeapiserver/apiserver.go b/pkg/kubeapiserver/apiserver.go index ac9c731d7..870239709 100644 --- a/pkg/kubeapiserver/apiserver.go +++ b/pkg/kubeapiserver/apiserver.go @@ -16,6 +16,7 @@ import ( genericfilters "k8s.io/apiserver/pkg/server/filters" "k8s.io/apiserver/pkg/server/healthz" utilfeature "k8s.io/apiserver/pkg/util/feature" + "k8s.io/apiserver/pkg/util/version" "k8s.io/client-go/restmapper" "k8s.io/component-base/tracing" @@ -23,7 +24,6 @@ import ( "github.com/clusterpedia-io/clusterpedia/pkg/kubeapiserver/discovery" "github.com/clusterpedia-io/clusterpedia/pkg/storage" "github.com/clusterpedia-io/clusterpedia/pkg/utils/filters" - "github.com/clusterpedia-io/clusterpedia/pkg/version" ) var ( @@ -81,10 +81,11 @@ func (c *Config) Complete() CompletedConfig { ExtraConfig: &c.ExtraConfig, } - if c.GenericConfig.Version == nil { - version := version.GetKubeVersion() - c.GenericConfig.Version = &version + binaryVersion := c.GenericConfig.EffectiveVersion.BinaryVersion() + if binaryVersion.Major() == 0 && binaryVersion.Minor() == 0 { + c.GenericConfig.EffectiveVersion = version.DefaultKubeEffectiveVersion() } + c.GenericConfig.RequestInfoResolver = wrapRequestInfoResolverForNamespace{ c.GenericConfig.RequestInfoResolver, } diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 3773f7e64..3b6d9ea9a 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -2,7 +2,7 @@ package metrics import ( "github.com/prometheus/client_golang/prometheus" - prometheusversion "github.com/prometheus/common/version" + versionCollector "github.com/prometheus/client_golang/prometheus/collectors/version" ) var registry = prometheus.NewRegistry() @@ -12,5 +12,5 @@ func DefaultRegistry() prometheus.Registerer { } func init() { - registry.MustRegister(prometheusversion.NewCollector("clusterpedia_kube_state_metrics")) + registry.MustRegister(versionCollector.NewCollector("clusterpedia_kube_state_metrics")) }