Skip to content

Commit

Permalink
Unify determination of whether a volume is ephemeral
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkm4ntr committed Mar 5, 2021
1 parent 96be00d commit fe7a862
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
5 changes: 2 additions & 3 deletions pkg/kubelet/eviction/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
v1resource "k8s.io/kubernetes/pkg/api/v1/resource"
evictionapi "k8s.io/kubernetes/pkg/kubelet/eviction/api"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
volumeutils "k8s.io/kubernetes/pkg/volume/util"
)

const (
Expand Down Expand Up @@ -343,9 +344,7 @@ func localVolumeNames(pod *v1.Pod) []string {
result := []string{}
for _, volume := range pod.Spec.Volumes {
if volume.HostPath != nil ||
(volume.EmptyDir != nil && volume.EmptyDir.Medium != v1.StorageMediumMemory) ||
volume.ConfigMap != nil ||
volume.GitRepo != nil {
volumeutils.IsLocalEphemeralVolume(volume) {
result = append(result, volume.Name)
}
}
Expand Down
11 changes: 2 additions & 9 deletions pkg/kubelet/server/stats/volume_stat_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
"k8s.io/kubernetes/pkg/volume"
"k8s.io/kubernetes/pkg/volume/util"

"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -122,7 +123,7 @@ func (s *volumeStatCalculator) calcAndStoreStats() {
}
}
volumeStats := s.parsePodVolumeStats(name, pvcRef, metric, volSpec)
if isVolumeEphemeral(volSpec) {
if util.IsLocalEphemeralVolume(volSpec) {
ephemeralStats = append(ephemeralStats, volumeStats)
} else {
persistentStats = append(persistentStats, volumeStats)
Expand Down Expand Up @@ -165,11 +166,3 @@ func (s *volumeStatCalculator) parsePodVolumeStats(podName string, pvcRef *stats
UsedBytes: &used, Inodes: &inodes, InodesFree: &inodesFree, InodesUsed: &inodesUsed},
}
}

func isVolumeEphemeral(volume v1.Volume) bool {
if (volume.EmptyDir != nil && volume.EmptyDir.Medium == v1.StorageMediumDefault) ||
volume.ConfigMap != nil || volume.GitRepo != nil {
return true
}
return false
}
6 changes: 4 additions & 2 deletions pkg/volume/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,12 @@ func GetPluginMountDir(host volume.VolumeHost, name string) string {

// IsLocalEphemeralVolume determines whether the argument is a local ephemeral
// volume vs. some other type
// Local means the volume is using storage from the local disk that is managed by kubelet.
// Ephemeral means the lifecycle of the volume is the same as the Pod.
func IsLocalEphemeralVolume(volume v1.Volume) bool {
return volume.GitRepo != nil ||
(volume.EmptyDir != nil && volume.EmptyDir.Medium != v1.StorageMediumMemory) ||
volume.ConfigMap != nil || volume.DownwardAPI != nil
(volume.EmptyDir != nil && volume.EmptyDir.Medium == v1.StorageMediumDefault) ||
volume.ConfigMap != nil
}

// GetPodVolumeNames returns names of volumes that are used in a pod,
Expand Down

0 comments on commit fe7a862

Please sign in to comment.