Skip to content

Commit f34b586

Browse files
committed
Include pod /etc/hosts in ephemeral storage calculation for eviction
1 parent bc60bda commit f34b586

File tree

7 files changed

+27
-6
lines changed

7 files changed

+27
-6
lines changed

pkg/kubelet/eviction/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ go_library(
6565
"//staging/src/k8s.io/api/core/v1:go_default_library",
6666
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
6767
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
68+
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
6869
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
6970
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
7071
"//staging/src/k8s.io/client-go/tools/record:go_default_library",

pkg/kubelet/eviction/eviction_manager.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
v1 "k8s.io/api/core/v1"
2828
"k8s.io/apimachinery/pkg/api/resource"
29+
"k8s.io/apimachinery/pkg/types"
2930
"k8s.io/apimachinery/pkg/util/clock"
3031
utilfeature "k8s.io/apiserver/pkg/util/feature"
3132
"k8s.io/client-go/tools/record"
@@ -98,6 +99,8 @@ type managerImpl struct {
9899
thresholdNotifiers []ThresholdNotifier
99100
// thresholdsLastUpdated is the last time the thresholdNotifiers were updated.
100101
thresholdsLastUpdated time.Time
102+
// etcHostsPath is a function that will get the etc-hosts file's path for a pod given its UID
103+
etcHostsPath func(podUID types.UID) string
101104
}
102105

103106
// ensure it implements the required interface
@@ -114,6 +117,7 @@ func NewManager(
114117
recorder record.EventRecorder,
115118
nodeRef *v1.ObjectReference,
116119
clock clock.Clock,
120+
etcHostsPath func(types.UID) string,
117121
) (Manager, lifecycle.PodAdmitHandler) {
118122
manager := &managerImpl{
119123
clock: clock,
@@ -129,6 +133,7 @@ func NewManager(
129133
thresholdsFirstObservedAt: thresholdsObservedAt{},
130134
dedicatedImageFs: nil,
131135
thresholdNotifiers: []ThresholdNotifier{},
136+
etcHostsPath: etcHostsPath,
132137
}
133138
return manager, manager
134139
}
@@ -514,7 +519,7 @@ func (m *managerImpl) podEphemeralStorageLimitEviction(podStats statsapi.PodStat
514519
} else {
515520
fsStatsSet = []fsStatsType{fsStatsRoot, fsStatsLogs, fsStatsLocalVolumeSource}
516521
}
517-
podEphemeralUsage, err := podLocalEphemeralStorageUsage(podStats, pod, fsStatsSet)
522+
podEphemeralUsage, err := podLocalEphemeralStorageUsage(podStats, pod, fsStatsSet, m.etcHostsPath(pod.UID))
518523
if err != nil {
519524
klog.Errorf("eviction manager: error getting pod disk usage %v", err)
520525
return false

pkg/kubelet/eviction/helpers.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package eviction
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"sort"
2223
"strconv"
2324
"strings"
@@ -424,7 +425,7 @@ func localEphemeralVolumeNames(pod *v1.Pod) []string {
424425
}
425426

426427
// podLocalEphemeralStorageUsage aggregates pod local ephemeral storage usage and inode consumption for the specified stats to measure.
427-
func podLocalEphemeralStorageUsage(podStats statsapi.PodStats, pod *v1.Pod, statsToMeasure []fsStatsType) (v1.ResourceList, error) {
428+
func podLocalEphemeralStorageUsage(podStats statsapi.PodStats, pod *v1.Pod, statsToMeasure []fsStatsType, etcHostsPath string) (v1.ResourceList, error) {
428429
disk := resource.Quantity{Format: resource.BinarySI}
429430
inodes := resource.Quantity{Format: resource.DecimalSI}
430431

@@ -438,6 +439,12 @@ func podLocalEphemeralStorageUsage(podStats statsapi.PodStats, pod *v1.Pod, stat
438439
disk.Add(podLocalVolumeUsageList[v1.ResourceEphemeralStorage])
439440
inodes.Add(podLocalVolumeUsageList[resourceInodes])
440441
}
442+
if len(etcHostsPath) > 0 {
443+
if stat, err := os.Stat(etcHostsPath); err == nil {
444+
disk.Add(*resource.NewQuantity(int64(stat.Size()), resource.BinarySI))
445+
inodes.Add(*resource.NewQuantity(int64(1), resource.DecimalSI))
446+
}
447+
}
441448
return v1.ResourceList{
442449
v1.ResourceEphemeralStorage: disk,
443450
resourceInodes: inodes,

pkg/kubelet/kubelet.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,9 @@ func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration,
772772
klet.backOff = flowcontrol.NewBackOff(backOffPeriod, MaxContainerBackOff)
773773
klet.podKillingCh = make(chan *kubecontainer.PodPair, podKillingChannelCapacity)
774774

775+
etcHostsPathFunc := func(podUID types.UID) string { return getEtcHostsPath(klet.getPodDir(podUID)) }
775776
// setup eviction manager
776-
evictionManager, evictionAdmitHandler := eviction.NewManager(klet.resourceAnalyzer, evictionConfig, killPodNow(klet.podWorkers, kubeDeps.Recorder), klet.podManager.GetMirrorPodByPod, klet.imageManager, klet.containerGC, kubeDeps.Recorder, nodeRef, klet.clock)
777+
evictionManager, evictionAdmitHandler := eviction.NewManager(klet.resourceAnalyzer, evictionConfig, killPodNow(klet.podWorkers, kubeDeps.Recorder), klet.podManager.GetMirrorPodByPod, klet.imageManager, klet.containerGC, kubeDeps.Recorder, nodeRef, klet.clock, etcHostsPathFunc)
777778

778779
klet.evictionManager = evictionManager
779780
klet.admitHandlers.AddPodAdmitHandler(evictionAdmitHandler)

pkg/kubelet/kubelet_pods.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,16 @@ func translateMountPropagation(mountMode *v1.MountPropagationMode) (runtimeapi.M
288288
}
289289
}
290290

291+
// getEtcHostsPath returns the full host-side path to a pod's generated /etc/hosts file
292+
func getEtcHostsPath(podDir string) string {
293+
return path.Join(podDir, "etc-hosts")
294+
}
295+
291296
// makeHostsMount makes the mountpoint for the hosts file that the containers
292297
// in a pod are injected with. podIPs is provided instead of podIP as podIPs
293298
// are present even if dual-stack feature flag is not enabled.
294299
func makeHostsMount(podDir string, podIPs []string, hostName, hostDomainName string, hostAliases []v1.HostAlias, useHostNetwork bool) (*kubecontainer.Mount, error) {
295-
hostsFilePath := path.Join(podDir, "etc-hosts")
300+
hostsFilePath := getEtcHostsPath(podDir)
296301
if err := ensureHostsFile(hostsFilePath, podIPs, hostName, hostDomainName, hostAliases, useHostNetwork); err != nil {
297302
return nil, err
298303
}

pkg/kubelet/kubelet_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,9 @@ func newTestKubeletWithImageList(
292292
UID: types.UID(kubelet.nodeName),
293293
Namespace: "",
294294
}
295+
etcHostsPathFunc := func(podUID types.UID) string { return getEtcHostsPath(kubelet.getPodDir(podUID)) }
295296
// setup eviction manager
296-
evictionManager, evictionAdmitHandler := eviction.NewManager(kubelet.resourceAnalyzer, eviction.Config{}, killPodNow(kubelet.podWorkers, fakeRecorder), kubelet.podManager.GetMirrorPodByPod, kubelet.imageManager, kubelet.containerGC, fakeRecorder, nodeRef, kubelet.clock)
297+
evictionManager, evictionAdmitHandler := eviction.NewManager(kubelet.resourceAnalyzer, eviction.Config{}, killPodNow(kubelet.podWorkers, fakeRecorder), kubelet.podManager.GetMirrorPodByPod, kubelet.imageManager, kubelet.containerGC, fakeRecorder, nodeRef, kubelet.clock, etcHostsPathFunc)
297298

298299
kubelet.evictionManager = evictionManager
299300
kubelet.admitHandlers.AddPodAdmitHandler(evictionAdmitHandler)

pkg/kubelet/runonce_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ func TestRunOnce(t *testing.T) {
126126
return nil
127127
}
128128
fakeMirrodPodFunc := func(*v1.Pod) (*v1.Pod, bool) { return nil, false }
129-
evictionManager, evictionAdmitHandler := eviction.NewManager(kb.resourceAnalyzer, eviction.Config{}, fakeKillPodFunc, fakeMirrodPodFunc, nil, nil, kb.recorder, nodeRef, kb.clock)
129+
etcHostsPathFunc := func(podUID types.UID) string { return getEtcHostsPath(kb.getPodDir(podUID)) }
130+
evictionManager, evictionAdmitHandler := eviction.NewManager(kb.resourceAnalyzer, eviction.Config{}, fakeKillPodFunc, fakeMirrodPodFunc, nil, nil, kb.recorder, nodeRef, kb.clock, etcHostsPathFunc)
130131

131132
kb.evictionManager = evictionManager
132133
kb.admitHandlers.AddPodAdmitHandler(evictionAdmitHandler)

0 commit comments

Comments
 (0)