@@ -9,18 +9,36 @@ function LabelSelector(selector, emptySelectsAll) {
9
9
this . _conjuncts = { } ;
10
10
this . _emptySelectsAll = ! ! emptySelectsAll ;
11
11
// expects the JSON format as returned by k8s API
12
- // TODO - currently k8s only returns key: value
13
- // which represents 'key in (value)'
14
- // for now also handle key: null as key exists
12
+ // Supports both the old selector syntax of just key: value pairs like on RCs
13
+ // as well as the new matchLabel and matchExpression syntax on newer controllers like ReplicaSets
14
+ // For now it will also handle key: null as key exists for backwards compatibility from before
15
+ // the matchExpression support was added.
16
+ var OPERATOR_MAP = {
17
+ "In" : "in" ,
18
+ "NotIn" : "not in" ,
19
+ "Exists" : "exists" ,
20
+ "DoesNotExist" : "does not exist"
21
+ } ;
22
+
15
23
if ( selector ) {
16
- angular . forEach ( selector , function ( details , key ) {
17
- if ( details || details === "" ) {
24
+ if ( selector . matchLabels || selector . matchExpressions ) {
25
+ angular . forEach ( selector . matchLabels , function ( details , key ) {
18
26
this . addConjunct ( key , "in" , [ details ] ) ;
19
- }
20
- else {
21
- this . addConjunct ( key , "exists" , [ ] ) ;
22
- }
23
- } , this ) ;
27
+ } , this ) ;
28
+ angular . forEach ( selector . matchExpressions , function ( expression ) {
29
+ this . addConjunct ( expression . key , OPERATOR_MAP [ expression . operator ] , expression . values ) ;
30
+ } , this ) ;
31
+ }
32
+ else {
33
+ angular . forEach ( selector , function ( details , key ) {
34
+ if ( details || details === "" ) {
35
+ this . addConjunct ( key , "in" , [ details ] ) ;
36
+ }
37
+ else {
38
+ this . addConjunct ( key , "exists" , [ ] ) ;
39
+ }
40
+ } , this ) ;
41
+ }
24
42
}
25
43
}
26
44
@@ -90,6 +108,11 @@ LabelSelector.prototype.matches = function(resource) {
90
108
return false ;
91
109
}
92
110
break ;
111
+ case "does not exist" :
112
+ if ( labels [ conjunct . key ] || labels [ conjunct . key ] === "" ) {
113
+ return false ;
114
+ }
115
+ break ;
93
116
case "in" :
94
117
var found = false ;
95
118
if ( labels [ conjunct . key ] || labels [ conjunct . key ] === "" ) {
@@ -145,7 +168,13 @@ LabelSelector.prototype.covers = function(selector) {
145
168
// on k8s def for label values
146
169
LabelSelector . prototype . _getStringForConjunct = function ( conjunct ) {
147
170
var conjunctString = conjunct . key ;
148
- if ( conjunct . operator != "exists" ) {
171
+ if ( conjunct . operator == "exists" ) {
172
+ return conjunctString + " exists" ;
173
+ }
174
+ else if ( conjunct . operator == "does not exist" ) {
175
+ return conjunctString + " does not exist" ;
176
+ }
177
+ else {
149
178
if ( conjunct . operator == "not in" ) {
150
179
conjunctString += " not" ;
151
180
}
@@ -167,5 +196,9 @@ LabelSelector.prototype._getStringForConjunct = function(conjunct) {
167
196
} ;
168
197
169
198
LabelSelector . prototype . _getIdForConjunct = function ( conjunct ) {
170
- return conjunct . key + "-" + conjunct . operator + "-" + conjunct . values . join ( "," ) ;
199
+ var id = conjunct . key + "-" + conjunct . operator ;
200
+ if ( conjunct . values ) {
201
+ id += "-" + conjunct . values . join ( "," ) ;
202
+ }
203
+ return id ;
171
204
} ;
0 commit comments