Skip to content

Commit

Permalink
kubelet: use filepath.Clean before init, validate it in setupDataDirs
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoxu committed Mar 17, 2023
1 parent 8b2dae5 commit 7afcfe1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/kubelet/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package options
import (
"fmt"
_ "net/http/pprof" // Enable pprof HTTP handlers.
"path/filepath"
"strings"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -138,7 +139,7 @@ func NewKubeletFlags() *KubeletFlags {
return &KubeletFlags{
ContainerRuntimeOptions: *NewContainerRuntimeOptions(),
CertDirectory: "/var/lib/kubelet/pki",
RootDirectory: defaultRootDir,
RootDirectory: filepath.Clean(defaultRootDir),
MaxContainerCount: -1,
MaxPerPodContainerCount: 1,
MinimumGCAge: metav1.Duration{Duration: 0},
Expand Down
6 changes: 4 additions & 2 deletions pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration,
kubeClient: kubeDeps.KubeClient,
heartbeatClient: kubeDeps.HeartbeatClient,
onRepeatedHeartbeatFailure: kubeDeps.OnHeartbeatFailure,
rootDirectory: rootDirectory,
rootDirectory: filepath.Clean(rootDirectory),
resyncInterval: kubeCfg.SyncFrequency.Duration,
sourcesReady: config.NewSourcesReady(kubeDeps.PodConfig.SeenAllSources),
registerNode: registerNode,
Expand Down Expand Up @@ -1321,7 +1321,9 @@ func (kl *Kubelet) RlimitStats() (*statsapi.RlimitStats, error) {
// 4. the pod-resources directory
// 5. the checkpoint directory
func (kl *Kubelet) setupDataDirs() error {
kl.rootDirectory = filepath.Clean(kl.rootDirectory)
if cleanedRoot := filepath.Clean(kl.rootDirectory); cleanedRoot != kl.rootDirectory {
return fmt.Errorf("rootDirectory not in canonical form: expected %s, was %s", cleanedRoot, kl.rootDirectory)
}
pluginRegistrationDir := kl.getPluginsRegistrationDir()
pluginsDir := kl.getPluginsDir()
if err := os.MkdirAll(kl.getRootDir(), 0750); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubelet/runonce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package kubelet
import (
"context"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -81,7 +82,7 @@ func TestRunOnce(t *testing.T) {
}
defer os.RemoveAll(basePath)
kb := &Kubelet{
rootDirectory: basePath,
rootDirectory: filepath.Clean(basePath),
recorder: &record.FakeRecorder{},
cadvisor: cadvisor,
nodeLister: testNodeLister{},
Expand Down

0 comments on commit 7afcfe1

Please sign in to comment.