Skip to content

Commit

Permalink
GetSPDForPod support disable checkSPDMatchWithPod
Browse files Browse the repository at this point in the history
  • Loading branch information
luomingmeng committed Apr 16, 2024
1 parent 136af06 commit 933b194
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/spd/cnc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/util/spd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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")
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/util/spd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ 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)

pod1.Annotations = map[string]string{
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)

Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/mutating/pod/spd_ref_mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 933b194

Please sign in to comment.