From 933b194cbfbfd0adbd3107660b54dabb0430f9a2 Mon Sep 17 00:00:00 2001 From: luomingmeng Date: Sat, 13 Apr 2024 01:44:40 +0800 Subject: [PATCH] GetSPDForPod support disable checkSPDMatchWithPod --- pkg/controller/spd/cnc.go | 2 +- pkg/util/spd.go | 23 ++++++++++++--------- pkg/util/spd_test.go | 6 +++--- pkg/webhook/mutating/pod/spd_ref_mutator.go | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pkg/controller/spd/cnc.go b/pkg/controller/spd/cnc.go index ebd76f288..6a25d5446 100644 --- a/pkg/controller/spd/cnc.go +++ b/pkg/controller/spd/cnc.go @@ -403,7 +403,7 @@ func (c *cncCacheController) getSPDMapForCNC(cnc *configapis.CustomNodeConfig) ( continue } - spd, err := util.GetSPDForPod(pod, c.spdIndexer, c.workloadGVKLister, c.spdLister) + spd, err := util.GetSPDForPod(pod, c.spdIndexer, c.workloadGVKLister, c.spdLister, false) if err != nil && !errors.IsNotFound(err) { return nil, err } diff --git a/pkg/util/spd.go b/pkg/util/spd.go index 4bf48a41f..89e7254a6 100644 --- a/pkg/util/spd.go +++ b/pkg/util/spd.go @@ -125,15 +125,15 @@ func GetSPDForWorkload(workload *unstructured.Unstructured, spdIndexer cache.Ind return nil, apierrors.NewNotFound(apiworkload.Resource(apiworkload.ResourceNameServiceProfileDescriptors), "spd for workload") } -// GetSPDForPod is used to get spd that should manage the given vpa, +// GetSPDForPod is used to get spd that should manage the given pod, // we'll try to find by annotation for pod, and then go through workload if not exist, // and we will find it recursively since we don't know in which level the owner will be. func GetSPDForPod(pod *core.Pod, spdIndexer cache.Indexer, workloadListerMap map[schema.GroupVersionKind]cache.GenericLister, - spdLister workloadlister.ServiceProfileDescriptorLister) (*apiworkload.ServiceProfileDescriptor, error) { + spdLister workloadlister.ServiceProfileDescriptorLister, checkSPDMatchWithPod bool) (*apiworkload.ServiceProfileDescriptor, error) { // different with vpa, we will store spd name in pod name, so we will check whether it's still valid if spdName, ok := pod.GetAnnotations()[apiconsts.PodAnnotationSPDNameKey]; ok { spd, err := spdLister.ServiceProfileDescriptors(pod.GetNamespace()).Get(spdName) - if err == nil && CheckSPDMatchWithPod(pod, spd, workloadListerMap) { + if err == nil { return spd, nil } } @@ -160,16 +160,19 @@ func GetSPDForPod(pod *core.Pod, spdIndexer cache.Indexer, workloadListerMap map } } - spdList, err := spdLister.List(labels.Everything()) - if err != nil { - return nil, err - } + if checkSPDMatchWithPod { + spdList, err := spdLister.List(labels.Everything()) + if err != nil { + return nil, err + } - for _, spd := range spdList { - if CheckSPDMatchWithPod(pod, spd, workloadListerMap) { - return spd, nil + for _, spd := range spdList { + if CheckSPDMatchWithPod(pod, spd, workloadListerMap) { + return spd, nil + } } } + return nil, apierrors.NewNotFound(apiworkload.Resource(apiworkload.ResourceNameServiceProfileDescriptors), "spd for pod") } diff --git a/pkg/util/spd_test.go b/pkg/util/spd_test.go index fc2c8a73f..93e1bf43b 100644 --- a/pkg/util/spd_test.go +++ b/pkg/util/spd_test.go @@ -143,7 +143,7 @@ func TestGetSPDForPod(t *testing.T) { } _ = spdInformer.Informer().GetStore().Add(spd) - s, err := GetSPDForPod(pod1, spdInformer.Informer().GetIndexer(), workloadInformers, spdInformer.Lister()) + s, err := GetSPDForPod(pod1, spdInformer.Informer().GetIndexer(), workloadInformers, spdInformer.Lister(), true) assert.NoError(t, err) assert.Equal(t, spd, s) @@ -151,7 +151,7 @@ func TestGetSPDForPod(t *testing.T) { apiconsts.PodAnnotationSPDNameKey: spd.Name, } - s, err = GetSPDForPod(pod1, spdInformer.Informer().GetIndexer(), workloadInformers, spdInformer.Lister()) + s, err = GetSPDForPod(pod1, spdInformer.Informer().GetIndexer(), workloadInformers, spdInformer.Lister(), true) assert.NoError(t, err) assert.Equal(t, spd, s) @@ -169,7 +169,7 @@ func TestGetSPDForPod(t *testing.T) { }, } - s, err = GetSPDForPod(pod2, spdInformer.Informer().GetIndexer(), workloadInformers, spdInformer.Lister()) + s, err = GetSPDForPod(pod2, spdInformer.Informer().GetIndexer(), workloadInformers, spdInformer.Lister(), true) assert.Nil(t, s) assert.Error(t, err) } diff --git a/pkg/webhook/mutating/pod/spd_ref_mutator.go b/pkg/webhook/mutating/pod/spd_ref_mutator.go index 40fb6306f..33244aa50 100644 --- a/pkg/webhook/mutating/pod/spd_ref_mutator.go +++ b/pkg/webhook/mutating/pod/spd_ref_mutator.go @@ -63,7 +63,7 @@ func (s *WebhookPodSPDReferenceMutator) MutatePod(pod *core.Pod, namespace strin } pod.Namespace = namespace - spd, err := katalystutil.GetSPDForPod(pod, s.spdIndexer, s.workloadLister, s.spdLister) + spd, err := katalystutil.GetSPDForPod(pod, s.spdIndexer, s.workloadLister, s.spdLister, true) if err != nil || spd == nil { klog.Warningf("didn't to find spd of pod %v/%v, err: %v", pod.Namespace, pod.Name, err) return false, nil