blob: 336a4ed831c3b1c3dcbbb5461cc39a1172de1cef [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1beta1
18
19import (
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.
31type 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.
38type 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.
43func (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 should not happen if the DaemonSet passed validation
65 return nil, err
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.
86func (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 return nil, fmt.Errorf("invalid label selector: %v", err)
101 }
102 // If a DaemonSet with a nil or empty selector creeps in, it should match nothing, not everything.
103 if selector.Empty() || !selector.Matches(labels.Set(history.Labels)) {
104 continue
105 }
106 daemonSets = append(daemonSets, ds)
107 }
108
109 if len(daemonSets) == 0 {
110 return nil, fmt.Errorf("could not find DaemonSets for ControllerRevision %s in namespace %s with labels: %v", history.Name, history.Namespace, history.Labels)
111 }
112
113 return daemonSets, nil
114}