k8s.io/client-go@v0.31.1/listers/extensions/v1beta1/daemonset_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 apps "k8s.io/api/apps/v1beta1" 23 "k8s.io/api/core/v1" 24 "k8s.io/api/extensions/v1beta1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/labels" 27 ) 28 29 // DaemonSetListerExpansion allows custom methods to be added to 30 // DaemonSetLister. 31 type DaemonSetListerExpansion interface { 32 GetPodDaemonSets(pod *v1.Pod) ([]*v1beta1.DaemonSet, error) 33 GetHistoryDaemonSets(history *apps.ControllerRevision) ([]*v1beta1.DaemonSet, error) 34 } 35 36 // DaemonSetNamespaceListerExpansion allows custom methods to be added to 37 // DaemonSetNamespaceLister. 38 type DaemonSetNamespaceListerExpansion interface{} 39 40 // GetPodDaemonSets returns a list of DaemonSets that potentially match a pod. 41 // Only the one specified in the Pod's ControllerRef will actually manage it. 42 // Returns an error only if no matching DaemonSets are found. 43 func (s *daemonSetLister) GetPodDaemonSets(pod *v1.Pod) ([]*v1beta1.DaemonSet, error) { 44 var selector labels.Selector 45 var daemonSet *v1beta1.DaemonSet 46 47 if len(pod.Labels) == 0 { 48 return nil, fmt.Errorf("no daemon sets found for pod %v because it has no labels", pod.Name) 49 } 50 51 list, err := s.DaemonSets(pod.Namespace).List(labels.Everything()) 52 if err != nil { 53 return nil, err 54 } 55 56 var daemonSets []*v1beta1.DaemonSet 57 for i := range list { 58 daemonSet = list[i] 59 if daemonSet.Namespace != pod.Namespace { 60 continue 61 } 62 selector, err = metav1.LabelSelectorAsSelector(daemonSet.Spec.Selector) 63 if err != nil { 64 // This object has an invalid selector, it does not match the pod 65 continue 66 } 67 68 // If a daemonSet with a nil or empty selector creeps in, it should match nothing, not everything. 69 if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) { 70 continue 71 } 72 daemonSets = append(daemonSets, daemonSet) 73 } 74 75 if len(daemonSets) == 0 { 76 return nil, fmt.Errorf("could not find daemon set for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) 77 } 78 79 return daemonSets, nil 80 } 81 82 // GetHistoryDaemonSets returns a list of DaemonSets that potentially 83 // match a ControllerRevision. Only the one specified in the ControllerRevision's ControllerRef 84 // will actually manage it. 85 // Returns an error only if no matching DaemonSets are found. 86 func (s *daemonSetLister) GetHistoryDaemonSets(history *apps.ControllerRevision) ([]*v1beta1.DaemonSet, error) { 87 if len(history.Labels) == 0 { 88 return nil, fmt.Errorf("no DaemonSet found for ControllerRevision %s because it has no labels", history.Name) 89 } 90 91 list, err := s.DaemonSets(history.Namespace).List(labels.Everything()) 92 if err != nil { 93 return nil, err 94 } 95 96 var daemonSets []*v1beta1.DaemonSet 97 for _, ds := range list { 98 selector, err := metav1.LabelSelectorAsSelector(ds.Spec.Selector) 99 if err != nil { 100 // This object has an invalid selector, it does not match the history object 101 continue 102 } 103 // If a DaemonSet with a nil or empty selector creeps in, it should match nothing, not everything. 104 if selector.Empty() || !selector.Matches(labels.Set(history.Labels)) { 105 continue 106 } 107 daemonSets = append(daemonSets, ds) 108 } 109 110 if len(daemonSets) == 0 { 111 return nil, fmt.Errorf("could not find DaemonSets for ControllerRevision %s in namespace %s with labels: %v", history.Name, history.Namespace, history.Labels) 112 } 113 114 return daemonSets, nil 115 }