Skip to content

Commit febf172

Browse files
committed
Merge pull request #14 from jwforres/handle_exists
Allow creating a label selector object with a key mapped to no value …
2 parents b733ca0 + eba671f commit febf172

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

labelSelector.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// selector (optional) - the JSON format as returned by k8s API
1+
// selector (optional) - the JSON format as returned by k8s API, will also
2+
// handle {key: null} as the key exists operator (not currently returned
3+
// by API)
24
// emptySelectsAll (optional) - whether a label selector with no conjuncts
35
// selects objects. Typical behavior is false. Example of an
46
// exceptional case is when filtering by labels, no label selectors
@@ -9,9 +11,15 @@ function LabelSelector(selector, emptySelectsAll) {
911
// expects the JSON format as returned by k8s API
1012
// TODO - currently k8s only returns key: value
1113
// which represents 'key in (value)'
14+
// for now also handle key: null as key exists
1215
if (selector) {
1316
angular.forEach(selector, function(details, key) {
14-
this.addConjunct(key, "in", [details]);
17+
if (details || details === "") {
18+
this.addConjunct(key, "in", [details]);
19+
}
20+
else {
21+
this.addConjunct(key, "exists", []);
22+
}
1523
}, this);
1624
}
1725
}
@@ -78,13 +86,13 @@ LabelSelector.prototype.matches = function(resource) {
7886
var conjunct = this._conjuncts[id];
7987
switch(conjunct.operator) {
8088
case "exists":
81-
if (!labels[conjunct.key]) {
89+
if (!labels[conjunct.key] && labels[conjunct.key] !== "") {
8290
return false;
8391
}
8492
break;
8593
case "in":
8694
var found = false;
87-
if (labels[conjunct.key]) {
95+
if (labels[conjunct.key] || labels[conjunct.key] === "") {
8896
for (var i = 0; !found && i < conjunct.values.length; i++) {
8997
if (labels[conjunct.key] == conjunct.values[i]) {
9098
found = true;
@@ -143,7 +151,12 @@ LabelSelector.prototype._getStringForConjunct = function(conjunct) {
143151
}
144152
conjunctString += " in (";
145153
for (var i = 0; i < conjunct.values.length; i++) {
146-
conjunctString += conjunct.values[i];
154+
if (conjunct.values[i] === '') {
155+
conjunctString += "\"\"";
156+
}
157+
else {
158+
conjunctString += conjunct.values[i];
159+
}
147160
if (i != conjunct.values.length - 1) {
148161
conjunctString += ", ";
149162
}

0 commit comments

Comments
 (0)