k8s.io/client-go@v0.31.1/listers/apps/v1beta1/statefulset_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 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/labels" 26 ) 27 28 // StatefulSetListerExpansion allows custom methods to be added to 29 // StatefulSetLister. 30 type StatefulSetListerExpansion interface { 31 GetPodStatefulSets(pod *v1.Pod) ([]*apps.StatefulSet, error) 32 } 33 34 // StatefulSetNamespaceListerExpansion allows custom methods to be added to 35 // StatefulSetNamespaceLister. 36 type StatefulSetNamespaceListerExpansion interface{} 37 38 // GetPodStatefulSets returns a list of StatefulSets that potentially match a pod. 39 // Only the one specified in the Pod's ControllerRef will actually manage it. 40 // Returns an error only if no matching StatefulSets are found. 41 func (s *statefulSetLister) GetPodStatefulSets(pod *v1.Pod) ([]*apps.StatefulSet, error) { 42 var selector labels.Selector 43 var ps *apps.StatefulSet 44 45 if len(pod.Labels) == 0 { 46 return nil, fmt.Errorf("no StatefulSets found for pod %v because it has no labels", pod.Name) 47 } 48 49 list, err := s.StatefulSets(pod.Namespace).List(labels.Everything()) 50 if err != nil { 51 return nil, err 52 } 53 54 var psList []*apps.StatefulSet 55 for i := range list { 56 ps = list[i] 57 if ps.Namespace != pod.Namespace { 58 continue 59 } 60 selector, err = metav1.LabelSelectorAsSelector(ps.Spec.Selector) 61 if err != nil { 62 // This object has an invalid selector, it does not match the pod 63 continue 64 } 65 66 // If a StatefulSet with a nil or empty selector creeps in, it should match nothing, not everything. 67 if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) { 68 continue 69 } 70 psList = append(psList, ps) 71 } 72 73 if len(psList) == 0 { 74 return nil, fmt.Errorf("could not find StatefulSet for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels) 75 } 76 77 return psList, nil 78 }