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