mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
configurable pv recyclers
This commit is contained in:
@@ -29,30 +29,31 @@ import (
|
||||
"k8s.io/kubernetes/pkg/watch"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
)
|
||||
|
||||
// ScrubPodVolumeAndWatchUntilCompletion is intended for use with volume Recyclers. This function will
|
||||
// RecycleVolumeByWatchingPodUntilCompletion is intended for use with volume Recyclers. This function will
|
||||
// save the given Pod to the API and watch it until it completes, fails, or the pod's ActiveDeadlineSeconds is exceeded, whichever comes first.
|
||||
// An attempt to delete a scrubber pod is always attempted before returning.
|
||||
// pod - the pod designed by a volume plugin to scrub the volume's contents
|
||||
// An attempt to delete a recycler pod is always attempted before returning.
|
||||
// pod - the pod designed by a volume plugin to recycle the volume
|
||||
// client - kube client for API operations.
|
||||
func ScrubPodVolumeAndWatchUntilCompletion(pod *api.Pod, kubeClient client.Interface) error {
|
||||
return internalScrubPodVolumeAndWatchUntilCompletion(pod, newScrubberClient(kubeClient))
|
||||
func RecycleVolumeByWatchingPodUntilCompletion(pod *api.Pod, kubeClient client.Interface) error {
|
||||
return internalRecycleVolumeByWatchingPodUntilCompletion(pod, newRecyclerClient(kubeClient))
|
||||
}
|
||||
|
||||
// same as above func comments, except 'scrubberClient' is a narrower pod API interface to ease testing
|
||||
func internalScrubPodVolumeAndWatchUntilCompletion(pod *api.Pod, scrubberClient scrubberClient) error {
|
||||
glog.V(5).Infof("Creating scrubber pod for volume %s\n", pod.Name)
|
||||
pod, err := scrubberClient.CreatePod(pod)
|
||||
// same as above func comments, except 'recyclerClient' is a narrower pod API interface to ease testing
|
||||
func internalRecycleVolumeByWatchingPodUntilCompletion(pod *api.Pod, recyclerClient recyclerClient) error {
|
||||
glog.V(5).Infof("Creating recycler pod for volume %s\n", pod.Name)
|
||||
pod, err := recyclerClient.CreatePod(pod)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unexpected error creating a pod to scrub volume %s: %+v\n", pod.Name, err)
|
||||
return fmt.Errorf("Unexpected error creating recycler pod: %+v\n", err)
|
||||
}
|
||||
|
||||
defer scrubberClient.DeletePod(pod.Name, pod.Namespace)
|
||||
defer recyclerClient.DeletePod(pod.Name, pod.Namespace)
|
||||
|
||||
stopChannel := make(chan struct{})
|
||||
defer close(stopChannel)
|
||||
nextPod := scrubberClient.WatchPod(pod.Name, pod.Namespace, pod.ResourceVersion, stopChannel)
|
||||
nextPod := recyclerClient.WatchPod(pod.Name, pod.Namespace, pod.ResourceVersion, stopChannel)
|
||||
|
||||
for {
|
||||
watchedPod := nextPod()
|
||||
@@ -71,39 +72,39 @@ func internalScrubPodVolumeAndWatchUntilCompletion(pod *api.Pod, scrubberClient
|
||||
}
|
||||
}
|
||||
|
||||
// scrubberClient abstracts access to a Pod by providing a narrower interface.
|
||||
// recyclerClient abstracts access to a Pod by providing a narrower interface.
|
||||
// this makes it easier to mock a client for testing
|
||||
type scrubberClient interface {
|
||||
type recyclerClient interface {
|
||||
CreatePod(pod *api.Pod) (*api.Pod, error)
|
||||
GetPod(name, namespace string) (*api.Pod, error)
|
||||
DeletePod(name, namespace string) error
|
||||
WatchPod(name, namespace, resourceVersion string, stopChannel chan struct{}) func() *api.Pod
|
||||
}
|
||||
|
||||
func newScrubberClient(client client.Interface) scrubberClient {
|
||||
return &realScrubberClient{client}
|
||||
func newRecyclerClient(client client.Interface) recyclerClient {
|
||||
return &realRecyclerClient{client}
|
||||
}
|
||||
|
||||
type realScrubberClient struct {
|
||||
type realRecyclerClient struct {
|
||||
client client.Interface
|
||||
}
|
||||
|
||||
func (c *realScrubberClient) CreatePod(pod *api.Pod) (*api.Pod, error) {
|
||||
func (c *realRecyclerClient) CreatePod(pod *api.Pod) (*api.Pod, error) {
|
||||
return c.client.Pods(pod.Namespace).Create(pod)
|
||||
}
|
||||
|
||||
func (c *realScrubberClient) GetPod(name, namespace string) (*api.Pod, error) {
|
||||
func (c *realRecyclerClient) GetPod(name, namespace string) (*api.Pod, error) {
|
||||
return c.client.Pods(namespace).Get(name)
|
||||
}
|
||||
|
||||
func (c *realScrubberClient) DeletePod(name, namespace string) error {
|
||||
func (c *realRecyclerClient) DeletePod(name, namespace string) error {
|
||||
return c.client.Pods(namespace).Delete(name, nil)
|
||||
}
|
||||
|
||||
// WatchPod returns a ListWatch for watching a pod. The stopChannel is used
|
||||
// to close the reflector backing the watch. The caller is responsible for derring a close on the channel to
|
||||
// stop the reflector.
|
||||
func (c *realScrubberClient) WatchPod(name, namespace, resourceVersion string, stopChannel chan struct{}) func() *api.Pod {
|
||||
func (c *realRecyclerClient) WatchPod(name, namespace, resourceVersion string, stopChannel chan struct{}) func() *api.Pod {
|
||||
fieldSelector, _ := fields.ParseSelector("metadata.name=" + name)
|
||||
|
||||
podLW := &cache.ListWatch{
|
||||
@@ -122,3 +123,18 @@ func (c *realScrubberClient) WatchPod(name, namespace, resourceVersion string, s
|
||||
return obj.(*api.Pod)
|
||||
}
|
||||
}
|
||||
|
||||
// CalculateTimeoutForVolume calculates time for a Recycler pod to complete a recycle operation.
|
||||
// The calculation and return value is either the minimumTimeout or the timeoutIncrement per Gi of storage size, whichever is greater.
|
||||
func CalculateTimeoutForVolume(minimumTimeout, timeoutIncrement int, pv *api.PersistentVolume) int64 {
|
||||
giQty := resource.MustParse("1Gi")
|
||||
pvQty := pv.Spec.Capacity[api.ResourceStorage]
|
||||
giSize := giQty.Value()
|
||||
pvSize := pvQty.Value()
|
||||
timeout := (pvSize / giSize) * int64(timeoutIncrement)
|
||||
if timeout < int64(minimumTimeout) {
|
||||
return int64(minimumTimeout)
|
||||
} else {
|
||||
return timeout
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user