...
1
16
17 package v1
18
19 import (
20 "fmt"
21
22 "k8s.io/api/core/v1"
23 policy "k8s.io/api/policy/v1"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/labels"
26 )
27
28
29
30 type PodDisruptionBudgetListerExpansion interface {
31 GetPodPodDisruptionBudgets(pod *v1.Pod) ([]*policy.PodDisruptionBudget, error)
32 }
33
34
35
36 type PodDisruptionBudgetNamespaceListerExpansion interface{}
37
38
39 func (s *podDisruptionBudgetLister) GetPodPodDisruptionBudgets(pod *v1.Pod) ([]*policy.PodDisruptionBudget, error) {
40 var selector labels.Selector
41
42 list, err := s.PodDisruptionBudgets(pod.Namespace).List(labels.Everything())
43 if err != nil {
44 return nil, err
45 }
46
47 var pdbList []*policy.PodDisruptionBudget
48 for i := range list {
49 pdb := list[i]
50 selector, err = metav1.LabelSelectorAsSelector(pdb.Spec.Selector)
51 if err != nil {
52
53 continue
54 }
55
56
57 if !selector.Matches(labels.Set(pod.Labels)) {
58 continue
59 }
60 pdbList = append(pdbList, pdb)
61 }
62
63 if len(pdbList) == 0 {
64 return nil, fmt.Errorf("could not find PodDisruptionBudget for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
65 }
66
67 return pdbList, nil
68 }
69
View as plain text