From 63aa661661e62c03a1add1c01bea1420f21e9807 Mon Sep 17 00:00:00 2001 From: Airren Date: Thu, 27 Jun 2024 15:00:46 +0800 Subject: [PATCH] feat: remove get pod no-cache Signed-off-by: Airren --- pkg/agent/orm/manager.go | 4 +- .../rodan/client/client_node_test.go | 4 -- .../provisioner/rodan/provisioner_test.go | 4 -- pkg/metaserver/agent/pod/pod.go | 13 ----- pkg/metaserver/agent/pod/pod_stub.go | 11 ----- pkg/metaserver/agent/pod/pod_test.go | 47 ------------------- 6 files changed, 3 insertions(+), 80 deletions(-) diff --git a/pkg/agent/orm/manager.go b/pkg/agent/orm/manager.go index b1acb921f8..cc73d80f98 100644 --- a/pkg/agent/orm/manager.go +++ b/pkg/agent/orm/manager.go @@ -50,6 +50,7 @@ import ( "github.com/kubewharf/katalyst-core/pkg/config" "github.com/kubewharf/katalyst-core/pkg/config/generic" "github.com/kubewharf/katalyst-core/pkg/metaserver" + metaserverpod "github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod" "github.com/kubewharf/katalyst-core/pkg/metrics" "github.com/kubewharf/katalyst-core/pkg/util/bitmask" cgroupmgr "github.com/kubewharf/katalyst-core/pkg/util/cgroup/manager" @@ -452,7 +453,8 @@ func (m *ManagerImpl) processAddPod(podUID string) error { err error ) if m.mode == workModeNri { - pod, err = m.metaManager.GetPodNoCache(m.ctx, podUID) + context.WithValue(m.ctx, metaserverpod.BypassCacheKey, metaserverpod.BypassCacheTrue) + pod, err = m.metaManager.GetPod(m.ctx, podUID) } else { pod, err = m.metaManager.GetPod(m.ctx, podUID) } diff --git a/pkg/metaserver/agent/metric/provisioner/rodan/client/client_node_test.go b/pkg/metaserver/agent/metric/provisioner/rodan/client/client_node_test.go index b8c21d5626..b300f10ed4 100644 --- a/pkg/metaserver/agent/metric/provisioner/rodan/client/client_node_test.go +++ b/pkg/metaserver/agent/metric/provisioner/rodan/client/client_node_test.go @@ -104,10 +104,6 @@ func (f *FakePodFetcher) GetPod(ctx context.Context, podUID string) (*v1.Pod, er return f.GetPodFunc(ctx, podUID) } -func (f *FakePodFetcher) GetPodNoCache(ctx context.Context, podUID string) (*v1.Pod, error) { - return f.GetPodFunc(ctx, podUID) -} - func (f *FakePodFetcher) GetPodList(ctx context.Context, podFilter func(*v1.Pod) bool) ([]*v1.Pod, error) { return nil, nil } diff --git a/pkg/metaserver/agent/metric/provisioner/rodan/provisioner_test.go b/pkg/metaserver/agent/metric/provisioner/rodan/provisioner_test.go index 8d858cf4d5..efb165527a 100644 --- a/pkg/metaserver/agent/metric/provisioner/rodan/provisioner_test.go +++ b/pkg/metaserver/agent/metric/provisioner/rodan/provisioner_test.go @@ -197,10 +197,6 @@ func (f *FakePodFetcher) GetPod(ctx context.Context, podUID string) (*v1.Pod, er return f.GetPodFunc(ctx, podUID) } -func (f *FakePodFetcher) GetPodNoCache(ctx context.Context, podUID string) (*v1.Pod, error) { - return f.GetPodFunc(ctx, podUID) -} - func (f *FakePodFetcher) GetPodList(ctx context.Context, podFilter func(*v1.Pod) bool) ([]*v1.Pod, error) { return f.GetPodListFunc(ctx, podFilter) } diff --git a/pkg/metaserver/agent/pod/pod.go b/pkg/metaserver/agent/pod/pod.go index e888fc8689..56fdadf6c6 100644 --- a/pkg/metaserver/agent/pod/pod.go +++ b/pkg/metaserver/agent/pod/pod.go @@ -65,7 +65,6 @@ type PodFetcher interface { GetContainerSpec(podUID, containerName string) (*v1.Container, error) // GetPod returns Pod by UID GetPod(ctx context.Context, podUID string) (*v1.Pod, error) - GetPodNoCache(ctx context.Context, podUID string) (*v1.Pod, error) } type podFetcherImpl struct { @@ -217,18 +216,6 @@ func (w *podFetcherImpl) GetPod(ctx context.Context, podUID string) (*v1.Pod, er return nil, fmt.Errorf("failed to find pod by uid %v", podUID) } -func (w *podFetcherImpl) GetPodNoCache(ctx context.Context, podUID string) (*v1.Pod, error) { - pods, err := w.kubeletPodFetcher.GetPodList(ctx, func(pod *v1.Pod) bool { - return podUID == string(pod.UID) - }) - if err != nil || len(pods) == 0 { - klog.Errorf("get pod by uid %s failed: %v", podUID, err) - return nil, err - } - klog.V(6).Infof("get pod by uid %s success", podUID) - return pods[0], nil -} - func (w *podFetcherImpl) getKubeletPodsCache(ctx context.Context) (map[string]*v1.Pod, error) { // if current kubelet pod cache is nil or enforce bypass, we sync cache first w.kubeletPodsCacheLock.RLock() diff --git a/pkg/metaserver/agent/pod/pod_stub.go b/pkg/metaserver/agent/pod/pod_stub.go index ad1787994d..4fb7580d20 100644 --- a/pkg/metaserver/agent/pod/pod_stub.go +++ b/pkg/metaserver/agent/pod/pod_stub.go @@ -57,17 +57,6 @@ func (p *PodFetcherStub) GetPod(_ context.Context, podUID string) (*v1.Pod, erro return nil, fmt.Errorf("failed to find pod by uid %v", podUID) } -func (p *PodFetcherStub) GetPodNoCache(_ context.Context, podUID string) (*v1.Pod, error) { - p.mutex.Lock() - defer p.mutex.Unlock() - for _, pod := range p.PodList { - if string(pod.UID) == podUID { - return pod, nil - } - } - return nil, fmt.Errorf("failed to find pod by uid %v", podUID) -} - func (p *PodFetcherStub) Run(_ context.Context) {} func (p *PodFetcherStub) GetContainerID(podUID, containerName string) (string, error) { diff --git a/pkg/metaserver/agent/pod/pod_test.go b/pkg/metaserver/agent/pod/pod_test.go index 892739fe90..eb836a3c76 100644 --- a/pkg/metaserver/agent/pod/pod_test.go +++ b/pkg/metaserver/agent/pod/pod_test.go @@ -17,16 +17,9 @@ limitations under the License. package pod import ( - "context" - "fmt" "reflect" "testing" - "github.com/stretchr/testify/assert" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - v1 "k8s.io/api/core/v1" - "github.com/kubewharf/katalyst-core/pkg/util/cgroup/common" ) @@ -56,43 +49,3 @@ func Test_getCgroupRootPaths(t *testing.T) { t.Errorf("getAbsCgroupRootPaths() \n got = %v, \n want = %v\n", got, want) } } - -type fakeKubeletPodFetcher struct{} - -func (f *fakeKubeletPodFetcher) GetPodList(_ context.Context, podFilter func(*v1.Pod) bool) (pods []*v1.Pod, err error) { - podList := []*v1.Pod{{ - TypeMeta: metav1.TypeMeta{}, - ObjectMeta: metav1.ObjectMeta{ - Name: "PodName", - UID: "PodUID", - }, - Spec: v1.PodSpec{}, - Status: v1.PodStatus{}, - }} - for _, pod := range podList { - if podFilter != nil && !podFilter(pod) { - continue - } - pods = append(pods, pod) - - } - if len(pods) == 0 { - return []*v1.Pod{}, fmt.Errorf("pod not found") - } - return pods, nil -} - -func Test_podFetcherImpl_GetPodNoCache(t *testing.T) { - t.Parallel() - - podFetcher := &podFetcherImpl{ - kubeletPodFetcher: &fakeKubeletPodFetcher{}, - } - - podCache1, err := podFetcher.GetPodNoCache(context.TODO(), "PodUID") - assert.Equal(t, err, nil) - assert.Equal(t, string(podCache1.UID), "PodUID") - - _, err = podFetcher.GetPodNoCache(context.TODO(), "PodUIDNotExist") - assert.Equal(t, err, fmt.Errorf("pod not found")) -}