Skip to content

Commit 7279660

Browse files
authored
Merge pull request #25 from jwforres/new_label_selectors
Support the new label selector syntax and add the 'does not exist' op…
2 parents 5d5a87e + eefead5 commit 7279660

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

labelFilter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,13 @@ angular.module('kubernetesUI')
222222
searchField: ["label"],
223223
options: [
224224
{type: "exists", label: "exists"},
225+
{type: "does not exist", label: "does not exist"},
225226
{type: "in", label: "in ..."},
226227
{type: "not in", label: "not in ..."}
227228
],
228229
onItemAdd: function(value, $item) {
229230
// if we selected "exists" enable the add button and stop here
230-
if (value == "exists") {
231+
if (value == "exists" || value == "does not exist") {
231232
self._labelFilterAddBtn.removeClass("disabled").prop('disabled', false).focus();
232233
return;
233234
}

labelSelector.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
146169
LabelSelector.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

169198
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;
171204
};

0 commit comments

Comments
 (0)