Unify ComponentKubelet and add unit tests.

This commit is contained in:
zhifei92 2024-11-18 14:53:17 +08:00
parent a04df83f86
commit 816cd40280
3 changed files with 13 additions and 11 deletions

View File

@ -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),
),
}

View File

@ -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)

View File

@ -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()