k8s.io/client-go@v0.31.1/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 ) 27 28 // PodDisruptionBudgetListerExpansion allows custom methods to be added to 29 // PodDisruptionBudgetLister. 30 type PodDisruptionBudgetListerExpansion interface { 31 GetPodPodDisruptionBudgets(pod *v1.Pod) ([]*policy.PodDisruptionBudget, error) 32 } 33 34 // PodDisruptionBudgetNamespaceListerExpansion allows custom methods to be added to 35 // PodDisruptionBudgetNamespaceLister. 36 type PodDisruptionBudgetNamespaceListerExpansion interface{} 37 38 // GetPodPodDisruptionBudgets returns a list of PodDisruptionBudgets matching a pod. 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 // This object has an invalid selector, it does not match the pod 53 continue 54 } 55 56 // Unlike the v1beta version, here we let an empty selector match everything. 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 }