@@ -17,181 +17,16 @@ limitations under the License.
17
17
package predicates
18
18
19
19
import (
20
- "fmt"
21
20
"reflect"
22
- "sort"
23
21
"testing"
24
22
25
23
v1 "k8s.io/api/core/v1"
26
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
25
"k8s.io/apimachinery/pkg/labels"
28
- fakelisters "k8s.io/kubernetes/pkg/scheduler/listers/fake"
29
- schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
30
26
nodeinfosnapshot "k8s.io/kubernetes/pkg/scheduler/nodeinfo/snapshot"
31
27
st "k8s.io/kubernetes/pkg/scheduler/testing"
32
28
)
33
29
34
- // sortablePods lets us to sort pods.
35
- type sortablePods []* v1.Pod
36
-
37
- func (s sortablePods ) Less (i , j int ) bool {
38
- return s [i ].Namespace < s [j ].Namespace ||
39
- (s [i ].Namespace == s [j ].Namespace && s [i ].Name < s [j ].Name )
40
- }
41
- func (s sortablePods ) Len () int { return len (s ) }
42
- func (s sortablePods ) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
43
-
44
- var _ sort.Interface = & sortablePods {}
45
-
46
- // sortableServices allows us to sort services.
47
- type sortableServices []* v1.Service
48
-
49
- func (s sortableServices ) Less (i , j int ) bool {
50
- return s [i ].Namespace < s [j ].Namespace ||
51
- (s [i ].Namespace == s [j ].Namespace && s [i ].Name < s [j ].Name )
52
- }
53
- func (s sortableServices ) Len () int { return len (s ) }
54
- func (s sortableServices ) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
55
-
56
- var _ sort.Interface = & sortableServices {}
57
-
58
- // predicateMetadataEquivalent returns true if the two metadata are equivalent.
59
- // Note: this function does not compare podRequest.
60
- func predicateMetadataEquivalent (meta1 , meta2 * predicateMetadata ) error {
61
- if ! reflect .DeepEqual (meta1 .pod , meta2 .pod ) {
62
- return fmt .Errorf ("pods are not the same" )
63
- }
64
- if meta1 .serviceAffinityMetadata != nil {
65
- sortablePods1 := sortablePods (meta1 .serviceAffinityMetadata .matchingPodList )
66
- sort .Sort (sortablePods1 )
67
- sortablePods2 := sortablePods (meta2 .serviceAffinityMetadata .matchingPodList )
68
- sort .Sort (sortablePods2 )
69
- if ! reflect .DeepEqual (sortablePods1 , sortablePods2 ) {
70
- return fmt .Errorf ("serviceAffinityMatchingPodLists are not euqal" )
71
- }
72
-
73
- sortableServices1 := sortableServices (meta1 .serviceAffinityMetadata .matchingPodServices )
74
- sort .Sort (sortableServices1 )
75
- sortableServices2 := sortableServices (meta2 .serviceAffinityMetadata .matchingPodServices )
76
- sort .Sort (sortableServices2 )
77
- if ! reflect .DeepEqual (sortableServices1 , sortableServices2 ) {
78
- return fmt .Errorf ("serviceAffinityMatchingPodServices are not euqal" )
79
- }
80
- }
81
- return nil
82
- }
83
-
84
- func TestPredicateMetadata_AddRemovePod (t * testing.T ) {
85
- var label1 = map [string ]string {
86
- "region" : "r1" ,
87
- "zone" : "z11" ,
88
- }
89
- var label2 = map [string ]string {
90
- "region" : "r1" ,
91
- "zone" : "z12" ,
92
- }
93
- var label3 = map [string ]string {
94
- "region" : "r2" ,
95
- "zone" : "z21" ,
96
- }
97
- selector1 := map [string ]string {"foo" : "bar" }
98
-
99
- tests := []struct {
100
- name string
101
- pendingPod * v1.Pod
102
- addedPod * v1.Pod
103
- existingPods []* v1.Pod
104
- nodes []* v1.Node
105
- services []* v1.Service
106
- }{
107
- {
108
- name : "no anti-affinity or service affinity exist" ,
109
- pendingPod : & v1.Pod {
110
- ObjectMeta : metav1.ObjectMeta {Name : "pending" , Labels : selector1 },
111
- },
112
- existingPods : []* v1.Pod {
113
- {ObjectMeta : metav1.ObjectMeta {Name : "p1" , Labels : selector1 },
114
- Spec : v1.PodSpec {NodeName : "nodeA" },
115
- },
116
- {ObjectMeta : metav1.ObjectMeta {Name : "p2" },
117
- Spec : v1.PodSpec {NodeName : "nodeC" },
118
- },
119
- },
120
- addedPod : & v1.Pod {
121
- ObjectMeta : metav1.ObjectMeta {Name : "addedPod" , Labels : selector1 },
122
- Spec : v1.PodSpec {NodeName : "nodeB" },
123
- },
124
- nodes : []* v1.Node {
125
- {ObjectMeta : metav1.ObjectMeta {Name : "nodeA" , Labels : label1 }},
126
- {ObjectMeta : metav1.ObjectMeta {Name : "nodeB" , Labels : label2 }},
127
- {ObjectMeta : metav1.ObjectMeta {Name : "nodeC" , Labels : label3 }},
128
- },
129
- },
130
- {
131
- name : "metadata service-affinity data are updated correctly after adding and removing a pod" ,
132
- pendingPod : & v1.Pod {
133
- ObjectMeta : metav1.ObjectMeta {Name : "pending" , Labels : selector1 },
134
- },
135
- existingPods : []* v1.Pod {
136
- {ObjectMeta : metav1.ObjectMeta {Name : "p1" , Labels : selector1 },
137
- Spec : v1.PodSpec {NodeName : "nodeA" },
138
- },
139
- {ObjectMeta : metav1.ObjectMeta {Name : "p2" },
140
- Spec : v1.PodSpec {NodeName : "nodeC" },
141
- },
142
- },
143
- addedPod : & v1.Pod {
144
- ObjectMeta : metav1.ObjectMeta {Name : "addedPod" , Labels : selector1 },
145
- Spec : v1.PodSpec {NodeName : "nodeB" },
146
- },
147
- services : []* v1.Service {{Spec : v1.ServiceSpec {Selector : selector1 }}},
148
- nodes : []* v1.Node {
149
- {ObjectMeta : metav1.ObjectMeta {Name : "nodeA" , Labels : label1 }},
150
- {ObjectMeta : metav1.ObjectMeta {Name : "nodeB" , Labels : label2 }},
151
- {ObjectMeta : metav1.ObjectMeta {Name : "nodeC" , Labels : label3 }},
152
- },
153
- },
154
- }
155
-
156
- for _ , test := range tests {
157
- t .Run (test .name , func (t * testing.T ) {
158
- allPodLister := fakelisters .PodLister (append (test .existingPods , test .addedPod ))
159
- // getMeta creates predicate meta data given the list of pods.
160
- getMeta := func (pods []* v1.Pod ) (* predicateMetadata , map [string ]* schedulernodeinfo.NodeInfo ) {
161
- s := nodeinfosnapshot .NewSnapshot (nodeinfosnapshot .CreateNodeInfoMap (pods , test .nodes ))
162
- _ , precompute := NewServiceAffinityPredicate (s .NodeInfos (), s .Pods (), fakelisters .ServiceLister (test .services ), nil )
163
- RegisterPredicateMetadataProducer ("ServiceAffinityMetaProducer" , precompute )
164
- factory := & MetadataProducerFactory {}
165
- meta := factory .GetPredicateMetadata (test .pendingPod , s )
166
- return meta .(* predicateMetadata ), s .NodeInfoMap
167
- }
168
-
169
- // allPodsMeta is meta data produced when all pods, including test.addedPod
170
- // are given to the metadata producer.
171
- allPodsMeta , _ := getMeta (allPodLister )
172
- // existingPodsMeta1 is meta data produced for test.existingPods (without test.addedPod).
173
- existingPodsMeta1 , nodeInfoMap := getMeta (test .existingPods )
174
- // Add test.addedPod to existingPodsMeta1 and make sure meta is equal to allPodsMeta
175
- nodeInfo := nodeInfoMap [test .addedPod .Spec .NodeName ]
176
- if err := existingPodsMeta1 .AddPod (test .addedPod , nodeInfo .Node ()); err != nil {
177
- t .Errorf ("error adding pod to meta: %v" , err )
178
- }
179
- if err := predicateMetadataEquivalent (allPodsMeta , existingPodsMeta1 ); err != nil {
180
- t .Errorf ("meta data are not equivalent: %v" , err )
181
- }
182
- // Remove the added pod and from existingPodsMeta1 an make sure it is equal
183
- // to meta generated for existing pods.
184
- existingPodsMeta2 , _ := getMeta (fakelisters .PodLister (test .existingPods ))
185
- if err := existingPodsMeta1 .RemovePod (test .addedPod , nodeInfo .Node ()); err != nil {
186
- t .Errorf ("error removing pod from meta: %v" , err )
187
- }
188
- if err := predicateMetadataEquivalent (existingPodsMeta1 , existingPodsMeta2 ); err != nil {
189
- t .Errorf ("meta data are not equivalent: %v" , err )
190
- }
191
- })
192
- }
193
- }
194
-
195
30
func TestPodAffinityMetadata_Clone (t * testing.T ) {
196
31
source := & PodAffinityMetadata {
197
32
topologyToMatchedExistingAntiAffinityTerms : topologyToMatchedTermCount {
@@ -217,33 +52,6 @@ func TestPodAffinityMetadata_Clone(t *testing.T) {
217
52
}
218
53
}
219
54
220
- // TestPredicateMetadata_ShallowCopy tests the ShallowCopy function. It is based
221
- // on the idea that shallow-copy should produce an object that is deep-equal to the original
222
- // object.
223
- func TestPredicateMetadata_ShallowCopy (t * testing.T ) {
224
- source := predicateMetadata {
225
- pod : & v1.Pod {
226
- ObjectMeta : metav1.ObjectMeta {
227
- Name : "test" ,
228
- Namespace : "testns" ,
229
- },
230
- },
231
- serviceAffinityMetadata : & serviceAffinityMetadata {
232
- matchingPodList : []* v1.Pod {
233
- {ObjectMeta : metav1.ObjectMeta {Name : "pod1" }},
234
- {ObjectMeta : metav1.ObjectMeta {Name : "pod2" }},
235
- },
236
- matchingPodServices : []* v1.Service {
237
- {ObjectMeta : metav1.ObjectMeta {Name : "service1" }},
238
- },
239
- },
240
- }
241
-
242
- if ! reflect .DeepEqual (source .ShallowCopy ().(* predicateMetadata ), & source ) {
243
- t .Errorf ("Copy is not equal to source!" )
244
- }
245
- }
246
-
247
55
// TestGetTPMapMatchingIncomingAffinityAntiAffinity tests against method getTPMapMatchingIncomingAffinityAntiAffinity
248
56
// on Anti Affinity cases
249
57
func TestGetTPMapMatchingIncomingAffinityAntiAffinity (t * testing.T ) {
0 commit comments