From 816cd40280651064208c100b4824fbe4d3fc2665 Mon Sep 17 00:00:00 2001 From: zhifei92 Date: Mon, 18 Nov 2024 14:53:17 +0800 Subject: [PATCH] Unify ComponentKubelet and add unit tests. --- cmd/kubelet/app/server.go | 13 ++++--------- pkg/kubelet/server/server.go | 8 ++++++-- pkg/kubelet/server/server_test.go | 3 +++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cmd/kubelet/app/server.go b/cmd/kubelet/app/server.go index cfcf6e7d5cd..888cc17fa0a 100644 --- a/cmd/kubelet/app/server.go +++ b/cmd/kubelet/app/server.go @@ -132,14 +132,9 @@ func init() { otel.SetMeterProvider(noop.NewMeterProvider()) } -const ( - // Kubelet component name - componentKubelet = "kubelet" -) - // NewKubeletCommand creates a *cobra.Command object with default parameters func NewKubeletCommand() *cobra.Command { - cleanFlagSet := pflag.NewFlagSet(componentKubelet, pflag.ContinueOnError) + cleanFlagSet := pflag.NewFlagSet(server.ComponentKubelet, pflag.ContinueOnError) cleanFlagSet.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) kubeletFlags := options.NewKubeletFlags() @@ -151,7 +146,7 @@ func NewKubeletCommand() *cobra.Command { } cmd := &cobra.Command{ - Use: componentKubelet, + Use: server.ComponentKubelet, Long: `The kubelet is the primary "node agent" that runs on each node. It can register the node with the apiserver using one of: the hostname; a flag to override the hostname; or specific logic for a cloud provider. @@ -562,7 +557,7 @@ func makeEventRecorder(ctx context.Context, kubeDeps *kubelet.Dependencies, node return } eventBroadcaster := record.NewBroadcaster(record.WithContext(ctx)) - kubeDeps.Recorder = eventBroadcaster.NewRecorder(legacyscheme.Scheme, v1.EventSource{Component: componentKubelet, Host: string(nodeName)}) + kubeDeps.Recorder = eventBroadcaster.NewRecorder(legacyscheme.Scheme, v1.EventSource{Component: server.ComponentKubelet, Host: string(nodeName)}) eventBroadcaster.StartStructuredLogging(3) if kubeDeps.EventClient != nil { klog.V(4).InfoS("Sending events to api server") @@ -1386,7 +1381,7 @@ func newTracerProvider(s *options.KubeletServer) (oteltrace.TracerProvider, erro } resourceOpts := []otelsdkresource.Option{ otelsdkresource.WithAttributes( - semconv.ServiceNameKey.String(componentKubelet), + semconv.ServiceNameKey.String(server.ComponentKubelet), semconv.HostNameKey.String(hostname), ), } diff --git a/pkg/kubelet/server/server.go b/pkg/kubelet/server/server.go index 1fbff6b21ec..c44112d3de9 100644 --- a/pkg/kubelet/server/server.go +++ b/pkg/kubelet/server/server.go @@ -107,8 +107,11 @@ const ( debugFlagPath = "/debug/flags/v" podsPath = "/pods" runningPodsPath = "/runningpods/" +) - kubeletComponent = "kubelet" +const ( + // Kubelet component name + ComponentKubelet = "kubelet" ) // Server is a http.Handler which exposes kubelet functionality over HTTP. @@ -411,7 +414,8 @@ func (s *Server) InstallDefaultHandlers() { healthz.InstallHandler(s.restfulCont, checkers...) if utilfeature.DefaultFeatureGate.Enabled(zpagesfeatures.ComponentStatusz) { - statusz.Install(s.restfulCont, kubeletComponent, statusz.NewRegistry()) + s.addMetricsBucketMatcher("statusz") + statusz.Install(s.restfulCont, ComponentKubelet, statusz.NewRegistry()) } slis.SLIMetricsWithReset{}.Install(s.restfulCont) diff --git a/pkg/kubelet/server/server_test.go b/pkg/kubelet/server/server_test.go index a79e71742bf..02f3194498c 100644 --- a/pkg/kubelet/server/server_test.go +++ b/pkg/kubelet/server/server_test.go @@ -57,6 +57,7 @@ import ( "k8s.io/apiserver/pkg/server/healthz" utilfeature "k8s.io/apiserver/pkg/util/feature" featuregatetesting "k8s.io/component-base/featuregate/testing" + zpagesfeatures "k8s.io/component-base/zpages/features" "k8s.io/kubelet/pkg/cri/streaming" "k8s.io/kubelet/pkg/cri/streaming/portforward" remotecommandserver "k8s.io/kubelet/pkg/cri/streaming/remotecommand" @@ -1648,9 +1649,11 @@ func TestMetricBuckets(t *testing.T) { "runningpods": {url: "/runningpods/", bucket: "runningpods"}, "stats": {url: "/stats/", bucket: "stats"}, "stats summary sub": {url: "/stats/summary", bucket: "stats"}, + "statusz": {url: "/statusz", bucket: "statusz"}, "invalid path": {url: "/junk", bucket: "other"}, "invalid path starting with good": {url: "/healthzjunk", bucket: "other"}, } + featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, zpagesfeatures.ComponentStatusz, true) fw := newServerTest() defer fw.testHTTPServer.Close()