From bfa46420552df58e12a7121f8d695af95e6a7100 Mon Sep 17 00:00:00 2001 From: Danny Jones Date: Fri, 18 Jul 2014 17:13:34 -0700 Subject: [PATCH] Adds a flag to specify root working directory. --root_dir specifies the directory kubelet will use for it's procedures. Currently used for volume mounts. --- cmd/kubelet/kubelet.go | 13 ++++++++++++- pkg/kubelet/kubelet.go | 11 +++++++---- pkg/kubelet/kubelet_test.go | 9 ++++++++- pkg/volume/volume.go | 29 +++++++++++++++-------------- pkg/volume/volume_test.go | 5 +++-- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/cmd/kubelet/kubelet.go b/cmd/kubelet/kubelet.go index 086c30771a..99c19f8e53 100644 --- a/cmd/kubelet/kubelet.go +++ b/cmd/kubelet/kubelet.go @@ -26,6 +26,7 @@ import ( "net/http" "os" "os/exec" + "path" "strings" "time" @@ -40,6 +41,8 @@ import ( "github.com/google/cadvisor/client" ) +const defaultRootDir = "/var/lib/kubelet" + var ( config = flag.String("config", "", "Path to the config file or directory of files") syncFrequency = flag.Duration("sync_frequency", 10*time.Second, "Max period between synchronizing running containers and config") @@ -51,6 +54,7 @@ var ( hostnameOverride = flag.String("hostname_override", "", "If non-empty, will use this string as identification instead of the actual hostname.") dockerEndpoint = flag.String("docker_endpoint", "", "If non-empty, use this for the docker endpoint to communicate with") etcdServerList util.StringList + rootDirectory = flag.String("root_dir", defaultRootDir, "Directory path for managing kubelet files (volume mounts,etc).") ) func init() { @@ -105,6 +109,12 @@ func main() { hostname := getHostname() + if *rootDirectory == "" { + glog.Fatal("Invalid root directory path.") + } + *rootDirectory = path.Clean(*rootDirectory) + os.MkdirAll(*rootDirectory, 0750) + // source of all configuration cfg := kconfig.NewPodConfig(kconfig.PodConfigNotificationSnapshotAndUpdates) @@ -133,7 +143,8 @@ func main() { getHostname(), dockerClient, cadvisorClient, - etcdClient) + etcdClient, + *rootDirectory) // start the kubelet go util.Forever(func() { k.Run(cfg.Updates()) }, 0) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 70818eb5ed..f59cde535c 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -62,12 +62,14 @@ func NewMainKubelet( hn string, dc DockerInterface, cc CadvisorInterface, - ec tools.EtcdClient) *Kubelet { + ec tools.EtcdClient, + rd string) *Kubelet { return &Kubelet{ hostname: hn, dockerClient: dc, cadvisorClient: cc, etcdClient: ec, + rootDirectory: rd, } } @@ -83,8 +85,9 @@ func NewIntegrationTestKubelet(hn string, dc DockerInterface) *Kubelet { // Kubelet is the main kubelet implementation. type Kubelet struct { - hostname string - dockerClient DockerInterface + hostname string + dockerClient DockerInterface + rootDirectory string // Optional, no events will be sent without it etcdClient tools.EtcdClient @@ -210,7 +213,7 @@ func milliCPUToShares(milliCPU int) int { func (kl *Kubelet) mountExternalVolumes(manifest *api.ContainerManifest) (volumeMap, error) { podVolumes := make(volumeMap) for _, vol := range manifest.Volumes { - extVolume, err := volume.CreateVolume(&vol, manifest.ID) + extVolume, err := volume.CreateVolume(&vol, manifest.ID, kl.rootDirectory) if err != nil { return nil, err } diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 7388f57a94..8535bcd850 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -61,6 +61,7 @@ func makeTestKubelet(t *testing.T) (*Kubelet, *tools.FakeEtcdClient, *FakeDocker kubelet.dockerClient = fakeDocker kubelet.dockerPuller = &FakeDockerPuller{} kubelet.etcdClient = fakeEtcdClient + kubelet.rootDirectory = "/tmp/kubelet" return kubelet, fakeEtcdClient, fakeDocker } @@ -445,6 +446,11 @@ func TestMakeVolumesAndBinds(t *testing.T) { Name: "disk4", ReadOnly: false, }, + { + MountPath: "/mnt/path5", + Name: "disk5", + ReadOnly: false, + }, }, } @@ -455,12 +461,13 @@ func TestMakeVolumesAndBinds(t *testing.T) { podVolumes := make(volumeMap) podVolumes["disk4"] = &volume.HostDirectory{"/mnt/host"} + podVolumes["disk5"] = &volume.EmptyDirectory{"disk5", "podID", "/var/lib/kubelet"} volumes, binds := makeVolumesAndBinds(&pod, &container, podVolumes) expectedVolumes := []string{"/mnt/path", "/mnt/path2"} expectedBinds := []string{"/exports/pod.test/disk:/mnt/path", "/exports/pod.test/disk2:/mnt/path2:ro", "/mnt/path3:/mnt/path3", - "/mnt/host:/mnt/path4"} + "/mnt/host:/mnt/path4", "/var/lib/kubelet/podID/volumes/empty/disk5:/mnt/path5"} if len(volumes) != len(expectedVolumes) { t.Errorf("Unexpected volumes. Expected %#v got %#v. Container was: %#v", expectedVolumes, volumes, container) diff --git a/pkg/volume/volume.go b/pkg/volume/volume.go index 05a590cbcb..8cd9ac5013 100644 --- a/pkg/volume/volume.go +++ b/pkg/volume/volume.go @@ -18,17 +18,18 @@ package volume import ( "errors" - "fmt" "os" + "path" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/golang/glog" ) - // All volume types are expected to implement this interface type Interface interface { // Prepares and mounts/unpacks the volume to a directory path. // This procedure must be idempotent. + // TODO(jonesdl) SetUp should return an error if it fails. SetUp() // Returns the directory path the volume is mounted to. GetPath() string @@ -56,16 +57,18 @@ func (hostVol *HostDirectory) GetPath() string { // EmptyDirectory volumes are temporary directories exposed to the pod. // These do not persist beyond the lifetime of a pod. type EmptyDirectory struct { - Name string - PodID string + Name string + PodID string + RootDir string } // SetUp creates the new directory. func (emptyDir *EmptyDirectory) SetUp() { - if _, err := os.Stat(emptyDir.GetPath()); os.IsNotExist(err) { - os.MkdirAll(emptyDir.GetPath(), 0750) + path := emptyDir.GetPath() + if _, err := os.Stat(path); os.IsNotExist(err) { + os.MkdirAll(path, 0750) } else { - glog.Warningf("Directory already exists: (%v)", emptyDir.GetPath()) + glog.Warningf("Directory already exists: (%v)", path) } } @@ -74,9 +77,7 @@ func (emptyDir *EmptyDirectory) SetUp() { func (emptyDir *EmptyDirectory) TearDown() {} func (emptyDir *EmptyDirectory) GetPath() string { - // TODO(jonesdl) We will want to add a flag to designate a root - // directory for kubelet to write to. For now this will just be /exports - return fmt.Sprintf("/exports/%v/%v", emptyDir.PodID, emptyDir.Name) + return path.Join(emptyDir.RootDir, emptyDir.PodID, "volumes", "empty", emptyDir.Name) } // Interprets API volume as a HostDirectory @@ -85,13 +86,13 @@ func createHostDirectory(volume *api.Volume) *HostDirectory { } // Interprets API volume as an EmptyDirectory -func createEmptyDirectory(volume *api.Volume, podID string) *EmptyDirectory { - return &EmptyDirectory{volume.Name, podID} +func createEmptyDirectory(volume *api.Volume, podID string, rootDir string) *EmptyDirectory { + return &EmptyDirectory{volume.Name, podID, rootDir} } // CreateVolume returns an Interface capable of mounting a volume described by an // *api.Volume and whether or not it is mounted, or an error. -func CreateVolume(volume *api.Volume, podID string) (Interface, error) { +func CreateVolume(volume *api.Volume, podID string, rootDir string) (Interface, error) { source := volume.Source // TODO(jonesdl) We will want to throw an error here when we no longer // support the default behavior. @@ -104,7 +105,7 @@ func CreateVolume(volume *api.Volume, podID string) (Interface, error) { if source.HostDirectory != nil { vol = createHostDirectory(volume) } else if source.EmptyDirectory != nil { - vol = createEmptyDirectory(volume, podID) + vol = createEmptyDirectory(volume, podID, rootDir) } else { return nil, errors.New("Unsupported volume type.") } diff --git a/pkg/volume/volume_test.go b/pkg/volume/volume_test.go index b28400f5a3..837f5a8930 100644 --- a/pkg/volume/volume_test.go +++ b/pkg/volume/volume_test.go @@ -38,9 +38,10 @@ func TestCreateVolumes(t *testing.T) { }, } fakePodID := "my-id" - expectedPaths := []string{"/dir/path", "/exports/my-id/empty-dir"} + rootDir := "/var/lib/kubelet" + expectedPaths := []string{"/dir/path", "/var/lib/kubelet/my-id/volumes/empty/empty-dir"} for i, volume := range volumes { - extVolume, _ := CreateVolume(&volume, fakePodID) + extVolume, _ := CreateVolume(&volume, fakePodID, rootDir) expectedPath := expectedPaths[i] path := extVolume.GetPath() if expectedPath != path {