k8s.io/client-go@v0.22.2/listers/policy/v1/poddisruptionbudget_expansion.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 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 "k8s.io/klog/v2" 27 ) 28 29 // PodDisruptionBudgetListerExpansion allows custom methods to be added to 30 // PodDisruptionBudgetLister. 31 type PodDisruptionBudgetListerExpansion interface { 32 GetPodPodDisruptionBudgets(pod *v1.Pod) ([]*policy.PodDisruptionBudget, error) 33 } 34 35 // PodDisruptionBudgetNamespaceListerExpansion allows custom methods to be added to 36 // PodDisruptionBudgetNamespaceLister. 37 type PodDisruptionBudgetNamespaceListerExpansion interface{} 38 39 // GetPodPodDisruptionBudgets returns a list of PodDisruptionBudgets matching a pod. 40 func (s *podDisruptionBudgetLister) GetPodPodDisruptionBudgets(pod *v1.Pod) ([]*policy.PodDisruptionBudget, error) { 41 var selector labels.Selector 42 43 list, err := s.PodDisruptionBudgets(pod.Namespace).List(labels.Everything()) 44 if err != nil { 45 return nil, err 46 } 47 48 var pdbList []*policy.PodDisruptionBudget 49 for i := range list { 50 pdb := list[i] 51 selector, err = metav1.LabelSelectorAsSelector(pdb.Spec.Selector) 52 if err != nil { 53 klog.Warningf("invalid selector: %v", err) 54 continue 55 } 56 57 // Unlike the v1beta version, here we let an empty selector match everything. 58 if !selector.Matches(labels.Set(pod.Labels)) { 59 continue 60 } 61 pdbList = append(pdbList, pdb) 62 } 63 64 if len(pdbList) == 0 { 65 return nil, fmt.Errorf("could not find PodDisruptionBudget for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) 66 } 67 68 return pdbList, nil 69 }