Skip to content

Commit

Permalink
update code for kubernetes v1.31.0
Browse files Browse the repository at this point in the history
Signed-off-by: Iceber Gu <[email protected]>
  • Loading branch information
Iceber committed Aug 27, 2024
1 parent 0c66fcc commit d3e6a8e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
16 changes: 6 additions & 10 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}
}

Expand Down Expand Up @@ -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
})
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/kube_state_metrics/metrics_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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 {
Expand All @@ -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")
Expand Down
9 changes: 5 additions & 4 deletions pkg/kubeapiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ 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"

informers "github.com/clusterpedia-io/clusterpedia/pkg/generated/informers/externalversions"
"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 (
Expand Down Expand Up @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"))
}

0 comments on commit d3e6a8e

Please sign in to comment.