...
1
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
29
30 type ReplicaSetListerExpansion interface {
31 GetPodReplicaSets(pod *v1.Pod) ([]*apps.ReplicaSet, error)
32 }
33
34
35
36 type ReplicaSetNamespaceListerExpansion interface{}
37
38
39
40
41 func (s *replicaSetLister) GetPodReplicaSets(pod *v1.Pod) ([]*apps.ReplicaSet, error) {
42 if len(pod.Labels) == 0 {
43 return nil, fmt.Errorf("no ReplicaSets found for pod %v because it has no labels", pod.Name)
44 }
45
46 list, err := s.ReplicaSets(pod.Namespace).List(labels.Everything())
47 if err != nil {
48 return nil, err
49 }
50
51 var rss []*apps.ReplicaSet
52 for _, rs := range list {
53 if rs.Namespace != pod.Namespace {
54 continue
55 }
56 selector, err := metav1.LabelSelectorAsSelector(rs.Spec.Selector)
57 if err != nil {
58
59 continue
60 }
61
62
63 if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
64 continue
65 }
66 rss = append(rss, rs)
67 }
68
69 if len(rss) == 0 {
70 return nil, fmt.Errorf("could not find ReplicaSet for pod %s in namespace %s with labels: %v", pod.Name, pod.Namespace, pod.Labels)
71 }
72
73 return rss, nil
74 }
75
View as plain text