Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap more comments in pkg/volume #26848

Merged
merged 1 commit into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions pkg/volume/metrics_cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@ import (

var _ MetricsProvider = &cachedMetrics{}

// cachedMetrics represents a MetricsProvider that wraps another provider and caches the result.
// cachedMetrics represents a MetricsProvider that wraps another provider and
// caches the result.
type cachedMetrics struct {
wrapped MetricsProvider
resultError error
resultMetrics *Metrics
once cacheOnce
}

// NewCachedMetrics creates a new cachedMetrics wrapping another MetricsProvider and caching the results.
// NewCachedMetrics creates a new cachedMetrics wrapping another
// MetricsProvider and caching the results.
func NewCachedMetrics(provider MetricsProvider) MetricsProvider {
return &cachedMetrics{wrapped: provider}
}

// GetMetrics runs the wrapped metrics provider's GetMetrics methd once and
// caches the result. Will not cache result if there is an error.
// See MetricsProvider.GetMetrics
// Runs GetMetrics Once and caches the result. Will not cache result if there is an error.
func (md *cachedMetrics) GetMetrics() (*Metrics, error) {
md.once.cache(func() error {
md.resultMetrics, md.resultError = md.wrapped.GetMetrics()
Expand All @@ -46,13 +49,15 @@ func (md *cachedMetrics) GetMetrics() (*Metrics, error) {
return md.resultMetrics, md.resultError
}

// Copied from sync.Once but we don't want to cache the results if there is an error
// Copied from sync.Once but we don't want to cache the results if there is an
// error
type cacheOnce struct {
m sync.Mutex
done uint32
}

// Copied from sync.Once but we don't want to cache the results if there is an error
// Copied from sync.Once but we don't want to cache the results if there is an
// error
func (o *cacheOnce) cache(f func() error) {
if atomic.LoadUint32(&o.done) == 1 {
return
Expand Down
10 changes: 6 additions & 4 deletions pkg/volume/metrics_du.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (

var _ MetricsProvider = &metricsDu{}

// metricsDu represents a MetricsProvider that calculates the used and available
// Volume space by executing the "du" command and gathering filesystem info for the Volume path.
// metricsDu represents a MetricsProvider that calculates the used and
// available Volume space by executing the "du" command and gathering
// filesystem info for the Volume path.
type metricsDu struct {
// the directory path the volume is mounted to.
path string
Expand All @@ -38,9 +39,9 @@ func NewMetricsDu(path string) MetricsProvider {
return &metricsDu{path}
}

// See MetricsProvider.GetMetrics
// GetMetrics calculates the volume usage and device free space by executing "du"
// and gathering filesystem info for the Volume path.
// See MetricsProvider.GetMetrics
func (md *metricsDu) GetMetrics() (*Metrics, error) {
metrics := &Metrics{}
if md.path == "" {
Expand Down Expand Up @@ -70,7 +71,8 @@ func (md *metricsDu) runDu(metrics *Metrics) error {
return nil
}

// getFsInfo writes metrics.Capacity and metrics.Available from the filesystem info
// getFsInfo writes metrics.Capacity and metrics.Available from the filesystem
// info
func (md *metricsDu) getFsInfo(metrics *Metrics) error {
available, capacity, _, err := util.FsInfo(md.path)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/volume/metrics_nil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import "errors"
var _ MetricsProvider = &MetricsNil{}

// MetricsNil represents a MetricsProvider that does not support returning
// Metrics. It serves as a placeholder for Volumes that do not yet support metrics.
// Metrics. It serves as a placeholder for Volumes that do not yet support
// metrics.
type MetricsNil struct{}

// See MetricsProvider.GetMetrics
// GetMetrics returns an empty Metrics and an error.
// See MetricsProvider.GetMetrics
func (*MetricsNil) GetMetrics() (*Metrics, error) {
return &Metrics{}, errors.New("metrics are not supported for MetricsNil Volumes")
}
28 changes: 15 additions & 13 deletions pkg/volume/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import (
// attempted before returning.
//
// In case there is a pod with the same namespace+name already running, this
// function assumes it's an older instance of the recycler pod and watches this
// old pod instead of starting a new one.
// function assumes it's an older instance of the recycler pod and watches
// this old pod instead of starting a new one.
//
// pod - the pod designed by a volume plugin to recycle the volume. pod.Name
// will be overwritten with unique name based on PV.Name.
Expand All @@ -49,7 +49,8 @@ func RecycleVolumeByWatchingPodUntilCompletion(pvName string, pod *api.Pod, kube
return internalRecycleVolumeByWatchingPodUntilCompletion(pvName, pod, newRecyclerClient(kubeClient))
}

// same as above func comments, except 'recyclerClient' is a narrower pod API interface to ease testing
// same as above func comments, except 'recyclerClient' is a narrower pod API
// interface to ease testing
func internalRecycleVolumeByWatchingPodUntilCompletion(pvName string, pod *api.Pod, recyclerClient recyclerClient) error {
glog.V(5).Infof("creating recycler pod for volume %s\n", pod.Name)

Expand Down Expand Up @@ -93,7 +94,7 @@ func internalRecycleVolumeByWatchingPodUntilCompletion(pvName string, pod *api.P
}

// recyclerClient abstracts access to a Pod by providing a narrower interface.
// this makes it easier to mock a client for testing
// This makes it easier to mock a client for testing.
type recyclerClient interface {
CreatePod(pod *api.Pod) (*api.Pod, error)
GetPod(name, namespace string) (*api.Pod, error)
Expand Down Expand Up @@ -122,8 +123,8 @@ func (c *realRecyclerClient) DeletePod(name, namespace string) error {
}

// 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.
// to close the reflector backing the watch. The caller is responsible for
// derring a close on the channel to stop the reflector.
func (c *realRecyclerClient) WatchPod(name, namespace string, stopChannel chan struct{}) func() *api.Pod {
fieldSelector, _ := fields.ParseSelector("metadata.name=" + name)

Expand All @@ -146,8 +147,10 @@ func (c *realRecyclerClient) WatchPod(name, namespace string, stopChannel chan s
}
}

// 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.
// 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]
Expand All @@ -170,11 +173,10 @@ func RoundUpSize(volumeSizeBytes int64, allocationUnitBytes int64) int64 {
return (volumeSizeBytes + allocationUnitBytes - 1) / allocationUnitBytes
}

// GenerateVolumeName returns a PV name with clusterName prefix.
// The function should be used to generate a name of GCE PD or Cinder volume.
// It basically adds "<clusterName>-dynamic-" before the PV name,
// making sure the resulting string fits given length and cuts "dynamic"
// if not.
// GenerateVolumeName returns a PV name with clusterName prefix. The function
// should be used to generate a name of GCE PD or Cinder volume. It basically
// adds "<clusterName>-dynamic-" before the PV name, making sure the resulting
// string fits given length and cuts "dynamic" if not.
func GenerateVolumeName(clusterName, pvName string, maxLength int) string {
prefix := clusterName + "-dynamic"
pvLen := len(pvName)
Expand Down