@@ -9,18 +9,36 @@ function LabelSelector(selector, emptySelectsAll) {
99 this . _conjuncts = { } ;
1010 this . _emptySelectsAll = ! ! emptySelectsAll ;
1111 // 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+
1523 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 ) {
1826 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+ }
2442 }
2543}
2644
@@ -90,6 +108,11 @@ LabelSelector.prototype.matches = function(resource) {
90108 return false ;
91109 }
92110 break ;
111+ case "does not exist" :
112+ if ( labels [ conjunct . key ] || labels [ conjunct . key ] === "" ) {
113+ return false ;
114+ }
115+ break ;
93116 case "in" :
94117 var found = false ;
95118 if ( labels [ conjunct . key ] || labels [ conjunct . key ] === "" ) {
@@ -145,7 +168,13 @@ LabelSelector.prototype.covers = function(selector) {
145168// on k8s def for label values
146169LabelSelector . prototype . _getStringForConjunct = function ( conjunct ) {
147170 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 {
149178 if ( conjunct . operator == "not in" ) {
150179 conjunctString += " not" ;
151180 }
@@ -167,5 +196,9 @@ LabelSelector.prototype._getStringForConjunct = function(conjunct) {
167196} ;
168197
169198LabelSelector . 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 ;
171204} ;
0 commit comments