mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 03:57:41 +00:00
kubelet: Merge orphaned mirror pod names into GetPodsAndMirrorPods
There is only one caller and both sets of data are part of the resync operation between kubelet's desired state and the actual state of the pod workers. Reduces the size of the interface so that it is easier to create another pod manager.
This commit is contained in:
parent
f8086f2dac
commit
e7207c8546
@ -980,23 +980,6 @@ func (kl *Kubelet) removeOrphanedPodStatuses(pods []*v1.Pod, mirrorPods []*v1.Po
|
||||
kl.statusManager.RemoveOrphanedStatuses(podUIDs)
|
||||
}
|
||||
|
||||
// deleteOrphanedMirrorPods checks whether pod killer has done with orphaned mirror pod.
|
||||
// If pod killing is done, podManager.DeleteMirrorPod() is called to delete mirror pod
|
||||
// from the API server
|
||||
func (kl *Kubelet) deleteOrphanedMirrorPods() {
|
||||
mirrorPods := kl.podManager.GetOrphanedMirrorPodNames()
|
||||
for _, podFullname := range mirrorPods {
|
||||
if !kl.podWorkers.IsPodForMirrorPodTerminatingByFullName(podFullname) {
|
||||
_, err := kl.podManager.DeleteMirrorPod(podFullname, nil)
|
||||
if err != nil {
|
||||
klog.ErrorS(err, "Encountered error when deleting mirror pod", "podName", podFullname)
|
||||
} else {
|
||||
klog.V(3).InfoS("Deleted mirror pod", "podName", podFullname)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HandlePodCleanups performs a series of cleanup work, including terminating
|
||||
// pod workers, killing unwanted pods, and removing orphaned volumes/pod
|
||||
// directories. No config changes are sent to pod workers while this method
|
||||
@ -1027,7 +1010,7 @@ func (kl *Kubelet) HandlePodCleanups(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
allPods, mirrorPods := kl.podManager.GetPodsAndMirrorPods()
|
||||
allPods, mirrorPods, orphanedMirrorPodFullnames := kl.podManager.GetPodsAndMirrorPods()
|
||||
|
||||
// Pod phase progresses monotonically. Once a pod has reached a final state,
|
||||
// it should never leave regardless of the restart policy. The statuses
|
||||
@ -1123,7 +1106,16 @@ func (kl *Kubelet) HandlePodCleanups(ctx context.Context) error {
|
||||
// Remove any orphaned mirror pods (mirror pods are tracked by name via the
|
||||
// pod worker)
|
||||
klog.V(3).InfoS("Clean up orphaned mirror pods")
|
||||
kl.deleteOrphanedMirrorPods()
|
||||
for _, podFullname := range orphanedMirrorPodFullnames {
|
||||
if !kl.podWorkers.IsPodForMirrorPodTerminatingByFullName(podFullname) {
|
||||
_, err := kl.podManager.DeleteMirrorPod(podFullname, nil)
|
||||
if err != nil {
|
||||
klog.ErrorS(err, "Encountered error when deleting mirror pod", "podName", podFullname)
|
||||
} else {
|
||||
klog.V(3).InfoS("Deleted mirror pod", "podName", podFullname)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// After pruning pod workers for terminated pods get the list of active pods for
|
||||
// metrics and to determine restarts.
|
||||
|
@ -60,8 +60,9 @@ type Manager interface {
|
||||
// GetMirrorPodByPod returns the mirror pod for the given static pod and
|
||||
// whether it was known to the pod manager.
|
||||
GetMirrorPodByPod(*v1.Pod) (*v1.Pod, bool)
|
||||
// GetPodsAndMirrorPods returns the both regular and mirror pods.
|
||||
GetPodsAndMirrorPods() ([]*v1.Pod, []*v1.Pod)
|
||||
// GetPodsAndMirrorPods returns the set of pods, the set of mirror pods, and
|
||||
// the pod fullnames of any orphaned mirror pods.
|
||||
GetPodsAndMirrorPods() (allPods []*v1.Pod, allMirrorPods []*v1.Pod, orphanedMirrorPodFullnames []string)
|
||||
// SetPods replaces the internal pods with the new pods.
|
||||
// It is currently only used for testing.
|
||||
SetPods(pods []*v1.Pod)
|
||||
@ -73,8 +74,6 @@ type Manager interface {
|
||||
// this means deleting the mappings related to mirror pods. For non-
|
||||
// mirror pods, this means deleting from indexes for all non-mirror pods.
|
||||
DeletePod(pod *v1.Pod)
|
||||
// GetOrphanedMirrorPodNames returns names of orphaned mirror pods
|
||||
GetOrphanedMirrorPodNames() []string
|
||||
// TranslatePodUID returns the actual UID of a pod. If the UID belongs to
|
||||
// a mirror pod, returns the UID of its static pod. Otherwise, returns the
|
||||
// original UID.
|
||||
@ -211,12 +210,18 @@ func (pm *basicManager) GetPods() []*v1.Pod {
|
||||
return podsMapToPods(pm.podByUID)
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetPodsAndMirrorPods() ([]*v1.Pod, []*v1.Pod) {
|
||||
func (pm *basicManager) GetPodsAndMirrorPods() (allPods []*v1.Pod, allMirrorPods []*v1.Pod, orphanedMirrorPodFullnames []string) {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
pods := podsMapToPods(pm.podByUID)
|
||||
mirrorPods := mirrorPodsMapToMirrorPods(pm.mirrorPodByUID)
|
||||
return pods, mirrorPods
|
||||
allPods = podsMapToPods(pm.podByUID)
|
||||
allMirrorPods = mirrorPodsMapToMirrorPods(pm.mirrorPodByUID)
|
||||
|
||||
for podFullName := range pm.mirrorPodByFullName {
|
||||
if _, ok := pm.podByFullName[podFullName]; !ok {
|
||||
orphanedMirrorPodFullnames = append(orphanedMirrorPodFullnames, podFullName)
|
||||
}
|
||||
}
|
||||
return allPods, allMirrorPods, orphanedMirrorPodFullnames
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetPodByUID(uid types.UID) (*v1.Pod, bool) {
|
||||
@ -277,18 +282,6 @@ func (pm *basicManager) GetUIDTranslations() (podToMirror map[kubetypes.Resolved
|
||||
return podToMirror, mirrorToPod
|
||||
}
|
||||
|
||||
func (pm *basicManager) GetOrphanedMirrorPodNames() []string {
|
||||
pm.lock.RLock()
|
||||
defer pm.lock.RUnlock()
|
||||
var podFullNames []string
|
||||
for podFullName := range pm.mirrorPodByFullName {
|
||||
if _, ok := pm.podByFullName[podFullName]; !ok {
|
||||
podFullNames = append(podFullNames, podFullName)
|
||||
}
|
||||
}
|
||||
return podFullNames
|
||||
}
|
||||
|
||||
// IsMirrorPodOf returns true if pod and mirrorPod are associated with each other.
|
||||
func IsMirrorPodOf(mirrorPod, pod *v1.Pod) bool {
|
||||
// Check name and namespace first.
|
||||
|
@ -20,7 +20,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
podtest "k8s.io/kubernetes/pkg/kubelet/pod/testing"
|
||||
@ -154,7 +154,7 @@ func TestDeletePods(t *testing.T) {
|
||||
t.Fatalf("Run DeletePod() error, expected %d pods, got %d pods; ", len(expectedPods)-1, len(actualPods))
|
||||
}
|
||||
|
||||
orphanedMirrorPodNames := podManager.GetOrphanedMirrorPodNames()
|
||||
_, _, orphanedMirrorPodNames := podManager.GetPodsAndMirrorPods()
|
||||
expectedOrphanedMirrorPodNameNum := 1
|
||||
if len(orphanedMirrorPodNames) != expectedOrphanedMirrorPodNameNum {
|
||||
t.Fatalf("Run getOrphanedMirrorPodNames() error, expected %d orphaned mirror pods, got %d orphaned mirror pods; ", expectedOrphanedMirrorPodNameNum, len(orphanedMirrorPodNames))
|
||||
|
@ -120,20 +120,6 @@ func (mr *MockManagerMockRecorder) GetMirrorPodByPod(arg0 interface{}) *gomock.C
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMirrorPodByPod", reflect.TypeOf((*MockManager)(nil).GetMirrorPodByPod), arg0)
|
||||
}
|
||||
|
||||
// GetOrphanedMirrorPodNames mocks base method.
|
||||
func (m *MockManager) GetOrphanedMirrorPodNames() []string {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetOrphanedMirrorPodNames")
|
||||
ret0, _ := ret[0].([]string)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// GetOrphanedMirrorPodNames indicates an expected call of GetOrphanedMirrorPodNames.
|
||||
func (mr *MockManagerMockRecorder) GetOrphanedMirrorPodNames() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOrphanedMirrorPodNames", reflect.TypeOf((*MockManager)(nil).GetOrphanedMirrorPodNames))
|
||||
}
|
||||
|
||||
// GetPodByFullName mocks base method.
|
||||
func (m *MockManager) GetPodByFullName(arg0 string) (*v1.Pod, bool) {
|
||||
m.ctrl.T.Helper()
|
||||
@ -209,12 +195,13 @@ func (mr *MockManagerMockRecorder) GetPods() *gomock.Call {
|
||||
}
|
||||
|
||||
// GetPodsAndMirrorPods mocks base method.
|
||||
func (m *MockManager) GetPodsAndMirrorPods() ([]*v1.Pod, []*v1.Pod) {
|
||||
func (m *MockManager) GetPodsAndMirrorPods() ([]*v1.Pod, []*v1.Pod, []string) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetPodsAndMirrorPods")
|
||||
ret0, _ := ret[0].([]*v1.Pod)
|
||||
ret1, _ := ret[1].([]*v1.Pod)
|
||||
return ret0, ret1
|
||||
ret2, _ := ret[2].([]string)
|
||||
return ret0, ret1, ret2
|
||||
}
|
||||
|
||||
// GetPodsAndMirrorPods indicates an expected call of GetPodsAndMirrorPods.
|
||||
|
Loading…
Reference in New Issue
Block a user