k8s.io/client-go@v0.22.2/listers/policy/v1beta1/poddisruptionbudget_expansion.go (about) 1 /* 2 Copyright 2017 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 v1beta1 18 19 import ( 20 "fmt" 21 22 "k8s.io/api/core/v1" 23 policy "k8s.io/api/policy/v1beta1" 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. Returns an error only if no matching PodDisruptionBudgets are found. 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 // TODO(mml): add an event to the PDB 55 continue 56 } 57 58 // If a PDB with a nil or empty selector creeps in, it should match nothing, not everything. 59 if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) { 60 continue 61 } 62 pdbList = append(pdbList, pdb) 63 } 64 65 if len(pdbList) == 0 { 66 return nil, fmt.Errorf("could not find PodDisruptionBudget for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) 67 } 68 69 return pdbList, nil 70 }