Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lint on pkg/kubelet/server/... #73966

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions hack/.golint_failures
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ pkg/kubelet/prober/testing
pkg/kubelet/qos
pkg/kubelet/remote
pkg/kubelet/secret
pkg/kubelet/server
pkg/kubelet/server/portforward
pkg/kubelet/server/stats
pkg/kubelet/server/streaming
pkg/kubelet/stats
pkg/kubelet/status
pkg/kubelet/status/testing
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubelet/dockershim/docker_streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (ds *dockerService) ExecSync(_ context.Context, req *runtimeapi.ExecSyncReq
// Exec prepares a streaming endpoint to execute a command in the container, and returns the address.
func (ds *dockerService) Exec(_ context.Context, req *runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error) {
if ds.streamingServer == nil {
return nil, streaming.ErrorStreamingDisabled("exec")
return nil, streaming.NewErrorStreamingDisabled("exec")
}
_, err := checkContainerStatus(ds.client, req.ContainerId)
if err != nil {
Expand All @@ -119,7 +119,7 @@ func (ds *dockerService) Exec(_ context.Context, req *runtimeapi.ExecRequest) (*
// Attach prepares a streaming endpoint to attach to a running container, and returns the address.
func (ds *dockerService) Attach(_ context.Context, req *runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error) {
if ds.streamingServer == nil {
return nil, streaming.ErrorStreamingDisabled("attach")
return nil, streaming.NewErrorStreamingDisabled("attach")
}
_, err := checkContainerStatus(ds.client, req.ContainerId)
if err != nil {
Expand All @@ -131,7 +131,7 @@ func (ds *dockerService) Attach(_ context.Context, req *runtimeapi.AttachRequest
// PortForward prepares a streaming endpoint to forward ports from a PodSandbox, and returns the address.
func (ds *dockerService) PortForward(_ context.Context, req *runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error) {
if ds.streamingServer == nil {
return nil, streaming.ErrorStreamingDisabled("port forward")
return nil, streaming.NewErrorStreamingDisabled("port forward")
}
_, err := checkContainerStatus(ds.client, req.PodSandboxId)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/metrics/collectors/volume_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ var (
)

type volumeStatsCollector struct {
statsProvider serverstats.StatsProvider
statsProvider serverstats.Provider
}

// NewVolumeStatsCollector creates a volume stats prometheus collector.
func NewVolumeStatsCollector(statsProvider serverstats.StatsProvider) prometheus.Collector {
func NewVolumeStatsCollector(statsProvider serverstats.Provider) prometheus.Collector {
return &volumeStatsCollector{statsProvider: statsProvider}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/kubelet/server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func NewKubeletAuth(authenticator authenticator.Request, authorizerAttributeGett
return &KubeletAuth{authenticator, authorizerAttributeGetter, authorizer}
}

// NewNodeAuthorizerAttributesGetter creates a new authorizer.RequestAttributesGetter for the node.
func NewNodeAuthorizerAttributesGetter(nodeName types.NodeName) authorizer.RequestAttributesGetter {
return nodeAuthorizerAttributesGetter{nodeName: nodeName}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/kubelet/server/portforward/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// package portforward contains server-side logic for handling port forwarding requests.
// Package portforward contains server-side logic for handling port forwarding requests.
package portforward

// The subprotocol "portforward.k8s.io" is used for port forwarding.
// ProtocolV1Name is the name of the subprotocol used for port forwarding.
const ProtocolV1Name = "portforward.k8s.io"

// SupportedProtocols are the supported port forwarding protocols.
var SupportedProtocols = []string{ProtocolV1Name}
2 changes: 1 addition & 1 deletion pkg/kubelet/server/portforward/httpstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"k8s.io/klog"
)

func handleHttpStreams(req *http.Request, w http.ResponseWriter, portForwarder PortForwarder, podName string, uid types.UID, supportedPortForwardProtocols []string, idleTimeout, streamCreationTimeout time.Duration) error {
func handleHTTPStreams(req *http.Request, w http.ResponseWriter, portForwarder PortForwarder, podName string, uid types.UID, supportedPortForwardProtocols []string, idleTimeout, streamCreationTimeout time.Duration) error {
_, err := httpstream.Handshake(req, w, supportedPortForwardProtocols)
// negotiated protocol isn't currently used server side, but could be in the future
if err != nil {
Expand Down
28 changes: 14 additions & 14 deletions pkg/kubelet/server/portforward/httpstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestHTTPStreamReceived(t *testing.T) {
for name, test := range tests {
streams := make(chan httpstream.Stream, 1)
f := httpStreamReceived(streams)
stream := newFakeHttpStream()
stream := newFakeHTTPStream()
if len(test.port) > 0 {
stream.headers.Set("port", test.port)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestGetStreamPair(t *testing.T) {
}

// removed via complete
dataStream := newFakeHttpStream()
dataStream := newFakeHTTPStream()
dataStream.headers.Set(api.StreamType, api.StreamTypeData)
complete, err := p.add(dataStream)
if err != nil {
Expand All @@ -145,7 +145,7 @@ func TestGetStreamPair(t *testing.T) {
t.Fatalf("unexpected complete")
}

errorStream := newFakeHttpStream()
errorStream := newFakeHTTPStream()
errorStream.headers.Set(api.StreamType, api.StreamTypeError)
complete, err = p.add(errorStream)
if err != nil {
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestGetStreamPair(t *testing.T) {
func TestRequestID(t *testing.T) {
h := &httpStreamHandler{}

s := newFakeHttpStream()
s := newFakeHTTPStream()
s.headers.Set(api.StreamType, api.StreamTypeError)
s.id = 1
if e, a := "1", h.requestID(s); e != a {
Expand All @@ -208,39 +208,39 @@ func TestRequestID(t *testing.T) {
}
}

type fakeHttpStream struct {
type fakeHTTPStream struct {
headers http.Header
id uint32
}

func newFakeHttpStream() *fakeHttpStream {
return &fakeHttpStream{
func newFakeHTTPStream() *fakeHTTPStream {
return &fakeHTTPStream{
headers: make(http.Header),
}
}

var _ httpstream.Stream = &fakeHttpStream{}
var _ httpstream.Stream = &fakeHTTPStream{}

func (s *fakeHttpStream) Read(data []byte) (int, error) {
func (s *fakeHTTPStream) Read(data []byte) (int, error) {
return 0, nil
}

func (s *fakeHttpStream) Write(data []byte) (int, error) {
func (s *fakeHTTPStream) Write(data []byte) (int, error) {
return 0, nil
}

func (s *fakeHttpStream) Close() error {
func (s *fakeHTTPStream) Close() error {
return nil
}

func (s *fakeHttpStream) Reset() error {
func (s *fakeHTTPStream) Reset() error {
return nil
}

func (s *fakeHttpStream) Headers() http.Header {
func (s *fakeHTTPStream) Headers() http.Header {
return s.headers
}

func (s *fakeHttpStream) Identifier() uint32 {
func (s *fakeHTTPStream) Identifier() uint32 {
return s.id
}
2 changes: 1 addition & 1 deletion pkg/kubelet/server/portforward/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func ServePortForward(w http.ResponseWriter, req *http.Request, portForwarder Po
if wsstream.IsWebSocketRequest(req) {
err = handleWebSocketStreams(req, w, portForwarder, podName, uid, portForwardOptions, supportedProtocols, idleTimeout, streamCreationTimeout)
} else {
err = handleHttpStreams(req, w, portForwarder, podName, uid, supportedProtocols, idleTimeout, streamCreationTimeout)
err = handleHTTPStreams(req, w, portForwarder, podName, uid, supportedProtocols, idleTimeout, streamCreationTimeout)
}

if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubelet/server/portforward/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ const (
v4Base64WebsocketProtocol = "v4." + wsstream.Base64ChannelWebSocketProtocol
)

// options contains details about which streams are required for
// port forwarding.
// V4Options contains details about which streams are required for port
// forwarding.
// All fields included in V4Options need to be expressed explicitly in the
// CRI (pkg/kubelet/apis/cri/{version}/api.proto) PortForwardRequest.
type V4Options struct {
Ports []int32
}

// newOptions creates a new options from the Request.
// NewV4Options creates a new options from the Request.
func NewV4Options(req *http.Request) (*V4Options, error) {
if !wsstream.IsWebSocketRequest(req) {
return &V4Options{}, nil
Expand Down
21 changes: 11 additions & 10 deletions pkg/kubelet/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Server struct {
redirectContainerStreaming bool
}

// TLSOptions holds the TLS options.
type TLSOptions struct {
Config *tls.Config
CertFile string
Expand Down Expand Up @@ -165,7 +166,7 @@ func ListenAndServeKubeletReadOnlyServer(host HostInterface, resourceAnalyzer st
klog.Fatal(server.ListenAndServe())
}

// ListenAndServePodResources initializes a grpc server to serve the PodResources service
// ListenAndServePodResources initializes a gRPC server to serve the PodResources service
func ListenAndServePodResources(socket string, podsProvider podresources.PodsProvider, devicesProvider podresources.DevicesProvider) {
server := grpc.NewServer()
podresourcesapi.RegisterPodResourcesListerServer(server, podresources.NewPodResourcesServer(podsProvider, devicesProvider))
Expand All @@ -186,7 +187,7 @@ type AuthInterface interface {
// HostInterface contains all the kubelet methods required by the server.
// For testability.
type HostInterface interface {
stats.StatsProvider
stats.Provider
GetVersionInfo() (*cadvisorapi.VersionInfo, error)
GetCachedMachineInfo() (*cadvisorapi.MachineInfo, error)
GetRunningPods() ([]*v1.Pod, error)
Expand Down Expand Up @@ -533,7 +534,7 @@ func (s *Server) getContainerLogs(request *restful.Request, response *restful.Re

pod, ok := s.host.GetPodByName(podNamespace, podID)
if !ok {
response.WriteError(http.StatusNotFound, fmt.Errorf("pod %q does not exist\n", podID))
response.WriteError(http.StatusNotFound, fmt.Errorf("pod %q does not exist", podID))
return
}
// Check if containerName is valid.
Expand All @@ -553,12 +554,12 @@ func (s *Server) getContainerLogs(request *restful.Request, response *restful.Re
}
}
if !containerExists {
response.WriteError(http.StatusNotFound, fmt.Errorf("container %q not found in pod %q\n", containerName, podID))
response.WriteError(http.StatusNotFound, fmt.Errorf("container %q not found in pod %q", containerName, podID))
return
}

if _, ok := response.ResponseWriter.(http.Flusher); !ok {
response.WriteError(http.StatusInternalServerError, fmt.Errorf("unable to convert %v into http.Flusher, cannot show logs\n", reflect.TypeOf(response)))
response.WriteError(http.StatusInternalServerError, fmt.Errorf("unable to convert %v into http.Flusher, cannot show logs", reflect.TypeOf(response)))
return
}
fw := flushwriter.Wrap(response.ResponseWriter)
Expand Down Expand Up @@ -591,7 +592,7 @@ func (s *Server) getPods(request *restful.Request, response *restful.Response) {
response.WriteError(http.StatusInternalServerError, err)
return
}
writeJsonResponse(response, data)
writeJSONResponse(response, data)
}

// getRunningPods returns a list of pods running on Kubelet. The list is
Expand All @@ -608,7 +609,7 @@ func (s *Server) getRunningPods(request *restful.Request, response *restful.Resp
response.WriteError(http.StatusInternalServerError, err)
return
}
writeJsonResponse(response, data)
writeJSONResponse(response, data)
}

// getLogs handles logs requests against the Kubelet.
Expand Down Expand Up @@ -747,11 +748,11 @@ func (s *Server) getRun(request *restful.Request, response *restful.Response) {
response.WriteError(http.StatusInternalServerError, err)
return
}
writeJsonResponse(response, data)
writeJSONResponse(response, data)
}

// Derived from go-restful writeJSON.
func writeJsonResponse(response *restful.Response, data []byte) {
func writeJSONResponse(response *restful.Response, data []byte) {
if data == nil {
response.WriteHeader(http.StatusOK)
// do not write a nil representation
Expand Down Expand Up @@ -834,7 +835,7 @@ func (a prometheusHostAdapter) GetMachineInfo() (*cadvisorapi.MachineInfo, error
return a.host.GetCachedMachineInfo()
}

func containerPrometheusLabelsFunc(s stats.StatsProvider) metrics.ContainerLabelsFunc {
func containerPrometheusLabelsFunc(s stats.Provider) metrics.ContainerLabelsFunc {
// containerPrometheusLabels maps cAdvisor labels to prometheus labels.
return func(c *cadvisorapi.ContainerInfo) map[string]string {
// Prometheus requires that all metrics in the same family have the same labels,
Expand Down
29 changes: 14 additions & 15 deletions pkg/kubelet/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (fk *fakeKubelet) GetCachedMachineInfo() (*cadvisorapi.MachineInfo, error)
return fk.machineInfoFunc()
}

func (_ *fakeKubelet) GetVersionInfo() (*cadvisorapi.VersionInfo, error) {
func (*fakeKubelet) GetVersionInfo() (*cadvisorapi.VersionInfo, error) {
return &cadvisorapi.VersionInfo{}, nil
}

Expand Down Expand Up @@ -249,23 +249,23 @@ func (fk *fakeKubelet) GetPortForward(podName, podNamespace string, podUID types
}

// Unused functions
func (_ *fakeKubelet) GetNode() (*v1.Node, error) { return nil, nil }
func (_ *fakeKubelet) GetNodeConfig() cm.NodeConfig { return cm.NodeConfig{} }
func (_ *fakeKubelet) GetPodCgroupRoot() string { return "" }
func (_ *fakeKubelet) GetPodByCgroupfs(cgroupfs string) (*v1.Pod, bool) { return nil, false }
func (*fakeKubelet) GetNode() (*v1.Node, error) { return nil, nil }
func (*fakeKubelet) GetNodeConfig() cm.NodeConfig { return cm.NodeConfig{} }
func (*fakeKubelet) GetPodCgroupRoot() string { return "" }
func (*fakeKubelet) GetPodByCgroupfs(cgroupfs string) (*v1.Pod, bool) { return nil, false }
func (fk *fakeKubelet) ListVolumesForPod(podUID types.UID) (map[string]volume.Volume, bool) {
return map[string]volume.Volume{}, true
}

func (_ *fakeKubelet) RootFsStats() (*statsapi.FsStats, error) { return nil, nil }
func (_ *fakeKubelet) ListPodStats() ([]statsapi.PodStats, error) { return nil, nil }
func (_ *fakeKubelet) ListPodCPUAndMemoryStats() ([]statsapi.PodStats, error) { return nil, nil }
func (_ *fakeKubelet) ImageFsStats() (*statsapi.FsStats, error) { return nil, nil }
func (_ *fakeKubelet) RlimitStats() (*statsapi.RlimitStats, error) { return nil, nil }
func (_ *fakeKubelet) GetCgroupStats(cgroupName string, updateStats bool) (*statsapi.ContainerStats, *statsapi.NetworkStats, error) {
func (*fakeKubelet) RootFsStats() (*statsapi.FsStats, error) { return nil, nil }
func (*fakeKubelet) ListPodStats() ([]statsapi.PodStats, error) { return nil, nil }
func (*fakeKubelet) ListPodCPUAndMemoryStats() ([]statsapi.PodStats, error) { return nil, nil }
func (*fakeKubelet) ImageFsStats() (*statsapi.FsStats, error) { return nil, nil }
func (*fakeKubelet) RlimitStats() (*statsapi.RlimitStats, error) { return nil, nil }
func (*fakeKubelet) GetCgroupStats(cgroupName string, updateStats bool) (*statsapi.ContainerStats, *statsapi.NetworkStats, error) {
return nil, nil, nil
}
func (_ *fakeKubelet) GetCgroupCPUAndMemoryStats(cgroupName string, updateStats bool) (*statsapi.ContainerStats, error) {
func (*fakeKubelet) GetCgroupCPUAndMemoryStats(cgroupName string, updateStats bool) (*statsapi.ContainerStats, error) {
return nil, nil
}

Expand Down Expand Up @@ -744,7 +744,7 @@ func TestAuthFilters(t *testing.T) {
The kubelet API has likely registered a handler for a new path.
If the new path has a use case for partitioned authorization when requested from the kubelet API,
add a specific subresource for it in auth.go#GetRequestAttributes() and in TestAuthFilters().
Otherwise, add it to the expected list of paths that map to the "proxy" subresource in TestAuthFilters().`, path))
Otherwise, add it to the expected list of paths that map to the "proxy" subresource in TestAuthFilters()`, path))
}
}
attributesGetter := NewNodeAuthorizerAttributesGetter(types.NodeName("test"))
Expand Down Expand Up @@ -1553,9 +1553,8 @@ func TestServePortForward(t *testing.T) {
if test.redirect {
assert.Equal(t, http.StatusFound, resp.StatusCode, "status code")
return
} else {
assert.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode, "status code")
}
assert.Equal(t, http.StatusSwitchingProtocols, resp.StatusCode, "status code")

conn, err := upgradeRoundTripper.NewConnection(resp)
require.NoError(t, err, "creating streaming connection")
Expand Down