Created
October 31, 2016 20:03
-
-
Save jpeeler/e2727942b215029399529402bb32b3d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2015 The Kubernetes Authors. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
package system | |
import ( | |
"fmt" | |
"github.com/golang/glog" | |
"k8s.io/kubernetes/pkg/api" | |
"k8s.io/kubernetes/pkg/types" | |
ioutil "k8s.io/kubernetes/pkg/util/io" | |
"k8s.io/kubernetes/pkg/util/mount" | |
"k8s.io/kubernetes/pkg/util/strings" | |
"k8s.io/kubernetes/pkg/volume" | |
volumeutil "k8s.io/kubernetes/pkg/volume/util" | |
) | |
const ( | |
systemPluginName = "kubernetes.io/system" | |
) | |
type systemPlugin struct { | |
host volume.VolumeHost | |
} | |
var _ volume.VolumePlugin = &systemPlugin{} | |
func wrappedVolumeSpec() volume.Spec { | |
return volume.Spec{ | |
Volume: &api.Volume{ | |
VolumeSource: api.VolumeSource{ | |
EmptyDir: &api.EmptyDirVolumeSource{Medium: api.StorageMediumMemory}, | |
}, | |
}, | |
} | |
} | |
// JPEELER: no idea how these parameters were decided or return | |
func getPath(uid types.UID, volName string, host volume.VolumeHost) string { | |
return host.GetPodVolumeDir(uid, strings.EscapeQualifiedNameForDisk(systemPluginName), volName) | |
} | |
func (plugin *systemPlugin) Init(host volume.VolumeHost) error { | |
plugin.host = host | |
return nil | |
} | |
func (plugin *systemPlugin) GetPluginName() string { | |
return systemPluginName | |
} | |
func (plugin *systemPlugin) GetVolumeName(spec *volume.Spec) (string, error) { | |
volumeSource, _, err := getVolumeSource(spec) | |
if err != nil { | |
return "", err | |
} | |
// JPEELER: spec.Name correct? downward api only volume source with an ID | |
return spec.Name(), nil | |
} | |
func (plugin *systemPlugin) CanSupport(spec *volume.Spec) bool { | |
return spec.Volume != nil && spec.Volume.SystemVolume != nil | |
} | |
func (plugin *systemPlugin) RequiresRemount() bool { | |
return true | |
} | |
func (plugin *systemPlugin) NewMounter(spec *volume.Spec, pod *api.Pod, opts volume.VolumeOptions) (volume.Mounter, error) { | |
return &systemVolumeMounter{ | |
systemVolume: &systemVolume{ | |
spec.Name(), | |
pod.UID, | |
plugin, | |
plugin.host.GetMounter(), | |
plugin.host.GetWriter(), | |
volume.MetricsNil{}, | |
}, | |
source: *spec.Volume.SystemVolume, | |
pod: *pod, | |
opts: &opts, | |
}, nil | |
} | |
func (plugin *systemPlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) { | |
return &systemVolumeUnmounter{ | |
&systemVolume{ | |
volName, | |
podUID, | |
plugin, | |
plugin.host.GetMounter(), | |
plugin.host.GetWriter(), | |
volume.MetricsNil{}, | |
}, | |
}, nil | |
} | |
func (plugin *systemPlugin) ConstructVolumeSpec(volumeName, mountPath string) (*volume.Spec, error) { | |
systemVolume := &api.Volume{ | |
Name: volumeName, | |
VolumeSource: api.VolumeSource{ | |
SystemVolume: &api.SystemVolumeSource{}, | |
}, | |
} | |
return volume.NewSpecFromVolume(systemVolume), nil | |
} | |
// JPEELER: not at all sure about these members | |
type systemVolume struct { | |
volName string | |
podUID types.UID | |
plugin *systemPlugin | |
mounter mount.Interface | |
writer ioutil.Writer | |
volume.MetricsNil // JPEELER: since configmaps doesn't support metrics (what is that?) | |
} | |
var _ volume.Volume = &systemVolume{} | |
func (sv *systemVolume) GetPath() string { | |
return getPath(sv.podUID, sv.volName, sv.plugin.host) | |
} | |
// JPEELER: not at all sure about these members | |
type systemVolumeMounter struct { | |
*systemVolume | |
source api.SystemVolumeSource | |
pod api.Pod | |
opts *volume.VolumeOptions | |
} | |
var _ volume.Mounter = &systemVolumeMounter{} | |
func (sv *systemVolume) GetAttributes() volume.Attributes { | |
return volume.Attributes{ | |
ReadOnly: true, | |
Managed: true, | |
SupportsSELinux: true, | |
} | |
} | |
func (b *systemVolumeMounter) SetUp(fsGroup *int64) error { | |
return b.SetUpAt(b.GetPath(), fsGroup) | |
} | |
func (b *systemVolumeMounter) SetUpAt(dir string, fsGroup *int64) error { | |
glog.V(3).Info("Setting up volume %v for pod %v at %v", b.volName, b.pod.UID, dir) | |
wrapped, err := b.plugin.host.NewWrapperMounter(b.volName, wrappedVolumeSpec(), &b.pod, *b.opts) | |
if err != nil { | |
return err | |
} | |
if err := wrapped.SetUpAt(dir, fsGroup); err != nil { | |
return err | |
} | |
kubeClient := b.plugin.host.GetKubeClient() | |
if kubeClient == nil { | |
return fmt.Errorf("Cannot setup system volume %v because kube client is not configured", b.volName) | |
} | |
data, err := b.collectData(b.source.DefaultMode) | |
if err != nil { | |
glog.Errorf("Error preparing data for system volume %v for pod %v/%v: %s", b.volName, b.pod.Namespace, b.pod.Name, err.Error()) | |
} | |
writerContext := fmt.Sprintf("pod %v/%v volume %v", s.pod.Namespace, s.pod.Name, s.volName) | |
writer, err := volumeutil.NewAtomicWriter(dir, writerContext) | |
if err != nil { | |
glog.Errorf("Error creating atomic writer: %v", err) | |
return err | |
} | |
err = writer.Write(data) | |
if err != nil { | |
glog.Errorf("Error writing paylot to dir: %v", err) | |
return err | |
} | |
err = volume.SetVolumeOwnership(s, fsGroup) | |
if err != nil { | |
glog.Errorf("Error applying volume ownership settings for group: %v", fsGroup) | |
return err | |
} | |
return nil | |
} | |
// JPEELER: should this be *systemVolumeMounter? | |
func (s *systemVolume) collectData(defaultMode *int32) (map[string]volumeutil.FileProjection, error) { | |
if defaultMode == nil { | |
return nil, fmt.Errorf("No defaultMode used, not even the default value for it") | |
} | |
errlist := []error{} | |
payload := make(map[string]volumeutil.FileProjection) | |
for _, fileInfo := range s.items { | |
var fileProjection volumeutil.FileProjection | |
fPath := path.Clean(fileInfo.Path) | |
if fileInfo.Mode != nil { | |
fileProjection.Mode = *fileInfo.mode | |
} else { | |
fileProjection.Mode = *defaultMode | |
} | |
// JPEELER: this (fileInfo.SecretName) is probably not right | |
if fileInfo.SecretName != nil { | |
secret, err := kubeClient.Core().Secrets(s.pod.Namespace).Get(fileInfo.SecretName) | |
if err != nil { | |
glog.Errorf("Couldn't get secret %v/%v", s.pod.Namespace, fileInfo.SecretName) | |
} | |
} | |
if fileInfo.FieldRef != nil { | |
if values, err := fieldpath.ExtractFieldPathAsString(d.pod, fileInfo.FieldRef.FieldPath); err != nil { | |
glog.Errorf("Unable to extract field %s: %s", fileInfo.FieldRef.FieldPath, err.Error()) | |
errlist = append(errlist, err) | |
} else { | |
fileProjection.Data = []byte(sortlines(values)) | |
} | |
} else if fileInfo.ResourceFieldRef != nil { | |
containerName := fileInfo.ResourceFieldRef.ContainerName | |
nodeAllocatable, err := s.plugin.host.GetNodeAllocatable() | |
if err != nil { | |
errlist = append(errlist, err) | |
} else if values, err := fieldPath.ExtractResourceValueByContainerNameAndNodeAllocatable(fileInfo.ResourceFieldRef, s.pod, containerName, nodeAllocatable); err != nil { | |
glog.Errorf("Unable to extract field %s: %s", fileInfo.ResourceFieldRef.Resource, err.Error()) | |
errlist = append(errlist, err) | |
} else { | |
fileProjection.Data = []byte(sortLines(values)) | |
} | |
} | |
payload[fPath] = fileProjection | |
} | |
return payload, utilerrors.NewAggregate(errlist) | |
} | |
func sortLines(values string) string { | |
splitted := strings.Split(values, "\n") | |
sort.Strings(splitted) | |
return strings.Join(splitted, "\n") | |
} | |
type systemVolumeUnmounter struct { | |
*systemVolume | |
} | |
var _ volume.Unmounter = &systemVolumeUnmounter{} | |
func (c *systemVolumeUnmounter) TearDown() error { | |
return c.TearDownAt(c.GetPath()) | |
} | |
func (c *systemVolumeUnmounter) TearDownAt(dir string) error { | |
glog.V(3).Info("Tearing down volume %v for pod %v at %v", c.volName, c.podUID, dir) | |
wrapped, err := c.plugin.host.NewWrapperUnmounter(c.volName, wrappedVolumeSpec(), c.podUID) | |
if err != nil { | |
return err | |
} | |
return wrapped.TearDownAt(dir) | |
} | |
func getVolumeSource(spec *volume.Spec) (*api.SystemVolumeSource, bool, error) { | |
var readOnly bool | |
var volumeSource *api.SystemVolumeSource | |
if spec.Volume != nil && spec.Volume.SystemVolume != nil { | |
volumeSource = spec.Volume.SystemVolume | |
readOnly = spec.ReadOnly | |
} | |
return volumeSource, readOnly, fmt.Errorf("Spec does not reference a System volume type") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment