Merge pull request #527 from Sarsate/rootdir-flag

Adds a flag to specify root working directory.
pull/6/head
Daniel Smith 2014-07-24 13:12:55 -07:00
commit 41eb15bcff
5 changed files with 45 additions and 22 deletions

View File

@ -26,6 +26,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path"
"strings" "strings"
"time" "time"
@ -40,6 +41,8 @@ import (
"github.com/google/cadvisor/client" "github.com/google/cadvisor/client"
) )
const defaultRootDir = "/var/lib/kubelet"
var ( var (
config = flag.String("config", "", "Path to the config file or directory of files") 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") 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.") 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") dockerEndpoint = flag.String("docker_endpoint", "", "If non-empty, use this for the docker endpoint to communicate with")
etcdServerList util.StringList etcdServerList util.StringList
rootDirectory = flag.String("root_dir", defaultRootDir, "Directory path for managing kubelet files (volume mounts,etc).")
) )
func init() { func init() {
@ -105,6 +109,12 @@ func main() {
hostname := getHostname() hostname := getHostname()
if *rootDirectory == "" {
glog.Fatal("Invalid root directory path.")
}
*rootDirectory = path.Clean(*rootDirectory)
os.MkdirAll(*rootDirectory, 0750)
// source of all configuration // source of all configuration
cfg := kconfig.NewPodConfig(kconfig.PodConfigNotificationSnapshotAndUpdates) cfg := kconfig.NewPodConfig(kconfig.PodConfigNotificationSnapshotAndUpdates)
@ -133,7 +143,8 @@ func main() {
getHostname(), getHostname(),
dockerClient, dockerClient,
cadvisorClient, cadvisorClient,
etcdClient) etcdClient,
*rootDirectory)
// start the kubelet // start the kubelet
go util.Forever(func() { k.Run(cfg.Updates()) }, 0) go util.Forever(func() { k.Run(cfg.Updates()) }, 0)

View File

@ -62,12 +62,14 @@ func NewMainKubelet(
hn string, hn string,
dc DockerInterface, dc DockerInterface,
cc CadvisorInterface, cc CadvisorInterface,
ec tools.EtcdClient) *Kubelet { ec tools.EtcdClient,
rd string) *Kubelet {
return &Kubelet{ return &Kubelet{
hostname: hn, hostname: hn,
dockerClient: dc, dockerClient: dc,
cadvisorClient: cc, cadvisorClient: cc,
etcdClient: ec, etcdClient: ec,
rootDirectory: rd,
} }
} }
@ -83,8 +85,9 @@ func NewIntegrationTestKubelet(hn string, dc DockerInterface) *Kubelet {
// Kubelet is the main kubelet implementation. // Kubelet is the main kubelet implementation.
type Kubelet struct { type Kubelet struct {
hostname string hostname string
dockerClient DockerInterface dockerClient DockerInterface
rootDirectory string
// Optional, no events will be sent without it // Optional, no events will be sent without it
etcdClient tools.EtcdClient etcdClient tools.EtcdClient
@ -210,7 +213,7 @@ func milliCPUToShares(milliCPU int) int {
func (kl *Kubelet) mountExternalVolumes(manifest *api.ContainerManifest) (volumeMap, error) { func (kl *Kubelet) mountExternalVolumes(manifest *api.ContainerManifest) (volumeMap, error) {
podVolumes := make(volumeMap) podVolumes := make(volumeMap)
for _, vol := range manifest.Volumes { for _, vol := range manifest.Volumes {
extVolume, err := volume.CreateVolume(&vol, manifest.ID) extVolume, err := volume.CreateVolume(&vol, manifest.ID, kl.rootDirectory)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -61,6 +61,7 @@ func makeTestKubelet(t *testing.T) (*Kubelet, *tools.FakeEtcdClient, *FakeDocker
kubelet.dockerClient = fakeDocker kubelet.dockerClient = fakeDocker
kubelet.dockerPuller = &FakeDockerPuller{} kubelet.dockerPuller = &FakeDockerPuller{}
kubelet.etcdClient = fakeEtcdClient kubelet.etcdClient = fakeEtcdClient
kubelet.rootDirectory = "/tmp/kubelet"
return kubelet, fakeEtcdClient, fakeDocker return kubelet, fakeEtcdClient, fakeDocker
} }
@ -445,6 +446,11 @@ func TestMakeVolumesAndBinds(t *testing.T) {
Name: "disk4", Name: "disk4",
ReadOnly: false, ReadOnly: false,
}, },
{
MountPath: "/mnt/path5",
Name: "disk5",
ReadOnly: false,
},
}, },
} }
@ -455,12 +461,13 @@ func TestMakeVolumesAndBinds(t *testing.T) {
podVolumes := make(volumeMap) podVolumes := make(volumeMap)
podVolumes["disk4"] = &volume.HostDirectory{"/mnt/host"} podVolumes["disk4"] = &volume.HostDirectory{"/mnt/host"}
podVolumes["disk5"] = &volume.EmptyDirectory{"disk5", "podID", "/var/lib/kubelet"}
volumes, binds := makeVolumesAndBinds(&pod, &container, podVolumes) volumes, binds := makeVolumesAndBinds(&pod, &container, podVolumes)
expectedVolumes := []string{"/mnt/path", "/mnt/path2"} 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", 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) { if len(volumes) != len(expectedVolumes) {
t.Errorf("Unexpected volumes. Expected %#v got %#v. Container was: %#v", expectedVolumes, volumes, container) t.Errorf("Unexpected volumes. Expected %#v got %#v. Container was: %#v", expectedVolumes, volumes, container)

View File

@ -18,17 +18,18 @@ package volume
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"path"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/golang/glog" "github.com/golang/glog"
) )
// All volume types are expected to implement this interface // All volume types are expected to implement this interface
type Interface interface { type Interface interface {
// Prepares and mounts/unpacks the volume to a directory path. // Prepares and mounts/unpacks the volume to a directory path.
// This procedure must be idempotent. // This procedure must be idempotent.
// TODO(jonesdl) SetUp should return an error if it fails.
SetUp() SetUp()
// Returns the directory path the volume is mounted to. // Returns the directory path the volume is mounted to.
GetPath() string GetPath() string
@ -56,16 +57,18 @@ func (hostVol *HostDirectory) GetPath() string {
// EmptyDirectory volumes are temporary directories exposed to the pod. // EmptyDirectory volumes are temporary directories exposed to the pod.
// These do not persist beyond the lifetime of a pod. // These do not persist beyond the lifetime of a pod.
type EmptyDirectory struct { type EmptyDirectory struct {
Name string Name string
PodID string PodID string
RootDir string
} }
// SetUp creates the new directory. // SetUp creates the new directory.
func (emptyDir *EmptyDirectory) SetUp() { func (emptyDir *EmptyDirectory) SetUp() {
if _, err := os.Stat(emptyDir.GetPath()); os.IsNotExist(err) { path := emptyDir.GetPath()
os.MkdirAll(emptyDir.GetPath(), 0750) if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, 0750)
} else { } 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) TearDown() {}
func (emptyDir *EmptyDirectory) GetPath() string { func (emptyDir *EmptyDirectory) GetPath() string {
// TODO(jonesdl) We will want to add a flag to designate a root return path.Join(emptyDir.RootDir, emptyDir.PodID, "volumes", "empty", emptyDir.Name)
// directory for kubelet to write to. For now this will just be /exports
return fmt.Sprintf("/exports/%v/%v", emptyDir.PodID, emptyDir.Name)
} }
// Interprets API volume as a HostDirectory // Interprets API volume as a HostDirectory
@ -85,13 +86,13 @@ func createHostDirectory(volume *api.Volume) *HostDirectory {
} }
// Interprets API volume as an EmptyDirectory // Interprets API volume as an EmptyDirectory
func createEmptyDirectory(volume *api.Volume, podID string) *EmptyDirectory { func createEmptyDirectory(volume *api.Volume, podID string, rootDir string) *EmptyDirectory {
return &EmptyDirectory{volume.Name, podID} return &EmptyDirectory{volume.Name, podID, rootDir}
} }
// CreateVolume returns an Interface capable of mounting a volume described by an // CreateVolume returns an Interface capable of mounting a volume described by an
// *api.Volume and whether or not it is mounted, or an error. // *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 source := volume.Source
// TODO(jonesdl) We will want to throw an error here when we no longer // TODO(jonesdl) We will want to throw an error here when we no longer
// support the default behavior. // support the default behavior.
@ -104,7 +105,7 @@ func CreateVolume(volume *api.Volume, podID string) (Interface, error) {
if source.HostDirectory != nil { if source.HostDirectory != nil {
vol = createHostDirectory(volume) vol = createHostDirectory(volume)
} else if source.EmptyDirectory != nil { } else if source.EmptyDirectory != nil {
vol = createEmptyDirectory(volume, podID) vol = createEmptyDirectory(volume, podID, rootDir)
} else { } else {
return nil, errors.New("Unsupported volume type.") return nil, errors.New("Unsupported volume type.")
} }

View File

@ -38,9 +38,10 @@ func TestCreateVolumes(t *testing.T) {
}, },
} }
fakePodID := "my-id" 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 { for i, volume := range volumes {
extVolume, _ := CreateVolume(&volume, fakePodID) extVolume, _ := CreateVolume(&volume, fakePodID, rootDir)
expectedPath := expectedPaths[i] expectedPath := expectedPaths[i]
path := extVolume.GetPath() path := extVolume.GetPath()
if expectedPath != path { if expectedPath != path {