Skip to content

Commit

Permalink
Merge pull request kubernetes#7870 from yifan-gu/rkt_volume
Browse files Browse the repository at this point in the history
Add volumeGetter to rkt.
  • Loading branch information
vmarmol committed May 7, 2015
2 parents 581821d + a8f86da commit 35c644a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
3 changes: 3 additions & 0 deletions pkg/kubelet/container/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
)

type Version interface {
Expand Down Expand Up @@ -205,6 +206,8 @@ type RunContainerOptions struct {
CgroupParent string
}

type VolumeMap map[string]volume.Volume

type Pods []*Pod

// FindPodByID finds and returns a pod in the pod list by UID. It will return an empty pod
Expand Down
4 changes: 1 addition & 3 deletions pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ type SyncHandler interface {

type SourcesReadyFn func() bool

type volumeMap map[string]volume.Volume

// Wait for the container runtime to be up with a timeout.
func waitUntilRuntimeIsUp(cr kubecontainer.Runtime, timeout time.Duration) error {
var err error = nil
Expand Down Expand Up @@ -657,7 +655,7 @@ func (kl *Kubelet) syncNodeStatus() {
}
}

func makeBinds(container *api.Container, podVolumes volumeMap) (binds []string) {
func makeBinds(container *api.Container, podVolumes kubecontainer.VolumeMap) (binds []string) {
for _, mount := range container.VolumeMounts {
vol, ok := podVolumes[mount.Name]
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ func TestMakeVolumesAndBinds(t *testing.T) {
},
}

podVolumes := volumeMap{
podVolumes := kubecontainer.VolumeMap{
"disk": &stubVolume{"/mnt/disk"},
"disk4": &stubVolume{"/mnt/host"},
"disk5": &stubVolume{"/var/lib/kubelet/podID/volumes/empty/disk5"},
Expand Down Expand Up @@ -4078,7 +4078,7 @@ func TestGetPodCreationFailureReason(t *testing.T) {
}
pods := []*api.Pod{pod}
kubelet.podManager.SetPods(pods)
kubelet.volumeManager.SetVolumes(pod.UID, volumeMap{})
kubelet.volumeManager.SetVolumes(pod.UID, kubecontainer.VolumeMap{})
// TODO: Move this test to dockertools so that we don't have to do the hacky
// type assertion here.
dm := kubelet.containerRuntime.(*dockertools.DockerManager)
Expand Down
29 changes: 20 additions & 9 deletions pkg/kubelet/rkt/rkt_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/securitycontext"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
appcschema "github.com/appc/spec/schema"
appctypes "github.com/appc/spec/schema/types"
"github.com/coreos/go-systemd/dbus"
Expand Down Expand Up @@ -101,18 +100,25 @@ type runtime struct {
recorder record.EventRecorder
prober prober.Prober
readinessManager *kubecontainer.ReadinessManager
volumeGetter volumeGetter
}

var _ kubecontainer.Runtime = &runtime{}

// TODO(yifan): Remove this when volumeManager is moved to separate package.
type volumeGetter interface {
GetVolumes(podUID types.UID) (kubecontainer.VolumeMap, bool)
}

// New creates the rkt container runtime which implements the container runtime interface.
// It will test if the rkt binary is in the $PATH, and whether we can get the
// version of it. If so, creates the rkt container runtime, otherwise returns an error.
func New(config *Config,
generator kubecontainer.RunContainerOptionsGenerator,
recorder record.EventRecorder,
containerRefManager *kubecontainer.RefManager,
readinessManager *kubecontainer.ReadinessManager) (kubecontainer.Runtime, error) {
readinessManager *kubecontainer.ReadinessManager,
volumeGetter volumeGetter) (kubecontainer.Runtime, error) {

systemdVersion, err := getSystemdVersion()
if err != nil {
Expand Down Expand Up @@ -352,7 +358,7 @@ func setApp(app *appctypes.App, c *api.Container) error {

// makePodManifest transforms a kubelet pod spec to the rkt pod manifest.
// TODO(yifan): Use the RunContainerOptions generated by GenerateRunContainerOptions().
func (r *runtime) makePodManifest(pod *api.Pod, volumeMap map[string]volume.Volume) (*appcschema.PodManifest, error) {
func (r *runtime) makePodManifest(pod *api.Pod) (*appcschema.PodManifest, error) {
manifest := appcschema.BlankPodManifest()

for _, c := range pod.Spec.Containers {
Expand Down Expand Up @@ -384,6 +390,11 @@ func (r *runtime) makePodManifest(pod *api.Pod, volumeMap map[string]volume.Volu
})
}

volumeMap, ok := r.volumeGetter.GetVolumes(pod.UID)
if !ok {
return nil, fmt.Errorf("cannot get the volumes for pod %q", kubecontainer.GetPodFullName(pod))
}

// Set global volumes.
for name, volume := range volumeMap {
volName, err := appctypes.NewACName(name)
Expand Down Expand Up @@ -458,11 +469,11 @@ func (r *runtime) apiPodToruntimePod(uuid string, pod *api.Pod) *kubecontainer.P
// On success, it will return a string that represents name of the unit file
// and a boolean that indicates if the unit file needs to be reloaded (whether
// the file is already existed).
func (r *runtime) preparePod(pod *api.Pod, volumeMap map[string]volume.Volume) (string, bool, error) {
func (r *runtime) preparePod(pod *api.Pod) (string, bool, error) {
cmds := []string{"prepare", "--quiet", "--pod-manifest"}

// Generate the pod manifest from the pod spec.
manifest, err := r.makePodManifest(pod, volumeMap)
manifest, err := r.makePodManifest(pod)
if err != nil {
return "", false, err
}
Expand Down Expand Up @@ -533,10 +544,10 @@ func (r *runtime) preparePod(pod *api.Pod, volumeMap map[string]volume.Volume) (

// RunPod first creates the unit file for a pod, and then calls
// StartUnit over d-bus.
func (r *runtime) RunPod(pod *api.Pod, volumeMap map[string]volume.Volume) error {
func (r *runtime) RunPod(pod *api.Pod) error {
glog.V(4).Infof("Rkt starts to run pod: name %q.", pod.Name)

name, needReload, err := r.preparePod(pod, volumeMap)
name, needReload, err := r.preparePod(pod)
if err != nil {
return err
}
Expand Down Expand Up @@ -784,7 +795,7 @@ func (r *runtime) SyncPod(pod *api.Pod, runningPod kubecontainer.Pod, podStatus
if len(runningPod.Containers) == 0 {
glog.V(4).Infof("Pod %q is not running, will start it", podFullName)
// TODO(yifan): Use RunContainerOptionsGeneratior to get volumeMaps, etc.
return r.RunPod(pod, nil)
return r.RunPod(pod)
}

// Add references to all containers.
Expand Down Expand Up @@ -841,7 +852,7 @@ func (r *runtime) SyncPod(pod *api.Pod, runningPod kubecontainer.Pod, podStatus
if err := r.KillPod(runningPod); err != nil {
return err
}
if err := r.RunPod(pod, nil); err != nil {
if err := r.RunPod(pod); err != nil {
return err
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kubelet/rkt/rkt_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func New(config *Config,
generator kubecontainer.RunContainerOptionsGenerator,
recorder record.EventRecorder,
containerRefManager *kubecontainer.RefManager,
readinessManager *kubecontainer.ReadinessManager) (kubecontainer.Runtime, error) {
readinessManager *kubecontainer.ReadinessManager,
volumeGetter volumeGetter) (kubecontainer.Runtime, error) {
return nil, unsupportedError
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/kubelet/volume_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package kubelet
import (
"sync"

kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
)

Expand All @@ -27,26 +28,26 @@ import (
// take care of the volumePlugins.
type volumeManager struct {
lock sync.RWMutex
volumeMaps map[types.UID]volumeMap
volumeMaps map[types.UID]kubecontainer.VolumeMap
}

func newVolumeManager() *volumeManager {
vm := &volumeManager{}
vm.volumeMaps = make(map[types.UID]volumeMap)
vm.volumeMaps = make(map[types.UID]kubecontainer.VolumeMap)
return vm
}

// SetVolumes sets the volume map for a pod.
// TODO(yifan): Currently we assume the volume is already mounted, so we only do a book keeping here.
func (vm *volumeManager) SetVolumes(podUID types.UID, podVolumes volumeMap) {
func (vm *volumeManager) SetVolumes(podUID types.UID, podVolumes kubecontainer.VolumeMap) {
vm.lock.Lock()
defer vm.lock.Unlock()
vm.volumeMaps[podUID] = podVolumes
}

// GetVolumes returns the volume map which are already mounted on the host machine
// for a pod.
func (vm *volumeManager) GetVolumes(podUID types.UID) (volumeMap, bool) {
func (vm *volumeManager) GetVolumes(podUID types.UID) (kubecontainer.VolumeMap, bool) {
vm.lock.RLock()
defer vm.lock.RUnlock()
vol, ok := vm.volumeMaps[podUID]
Expand Down
5 changes: 3 additions & 2 deletions pkg/kubelet/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/mount"
Expand Down Expand Up @@ -96,8 +97,8 @@ func (kl *Kubelet) newVolumeBuilderFromPlugins(spec *volume.Spec, podRef *api.Ob
return builder, nil
}

func (kl *Kubelet) mountExternalVolumes(pod *api.Pod) (volumeMap, error) {
podVolumes := make(volumeMap)
func (kl *Kubelet) mountExternalVolumes(pod *api.Pod) (kubecontainer.VolumeMap, error) {
podVolumes := make(kubecontainer.VolumeMap)
for i := range pod.Spec.Volumes {
volSpec := &pod.Spec.Volumes[i]

Expand Down

0 comments on commit 35c644a

Please sign in to comment.