Skip to content

Commit c492abb

Browse files
authored
Merge pull request #1181 from brendandburns/cordon
Add support for kubectl cordon.
2 parents 6a2a60a + 8cb507b commit c492abb

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

examples/src/main/java/io/kubernetes/client/examples/KubectlExample.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
import static io.kubernetes.client.extended.kubectl.Kubectl.apiResources;
1616
import static io.kubernetes.client.extended.kubectl.Kubectl.copy;
17+
import static io.kubernetes.client.extended.kubectl.Kubectl.cordon;
1718
import static io.kubernetes.client.extended.kubectl.Kubectl.exec;
1819
import static io.kubernetes.client.extended.kubectl.Kubectl.label;
1920
import static io.kubernetes.client.extended.kubectl.Kubectl.log;
2021
import static io.kubernetes.client.extended.kubectl.Kubectl.portforward;
2122
import static io.kubernetes.client.extended.kubectl.Kubectl.scale;
2223
import static io.kubernetes.client.extended.kubectl.Kubectl.taint;
2324
import static io.kubernetes.client.extended.kubectl.Kubectl.top;
25+
import static io.kubernetes.client.extended.kubectl.Kubectl.uncordon;
2426
import static io.kubernetes.client.extended.kubectl.Kubectl.version;
2527
import static io.kubernetes.client.extended.kubectl.KubectlTop.podMetricSum;
2628

@@ -104,6 +106,16 @@ public static void main(String[] args)
104106
String name = null;
105107

106108
switch (verb) {
109+
case "cordon":
110+
name = args[1];
111+
cordon().apiClient(client).name(name).execute();
112+
System.out.println("Node cordoned");
113+
System.exit(0);
114+
case "uncordon":
115+
name = args[1];
116+
uncordon().apiClient(client).name(name).execute();
117+
System.out.println("Node uncordoned");
118+
System.exit(0);
107119
case "top":
108120
String what = args[1];
109121
switch (what) {

extended/src/main/java/io/kubernetes/client/extended/kubectl/Kubectl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
*/
2424
public class Kubectl {
2525

26+
/** Equivalent for `kubectl cordon` */
27+
public static KubectlCordon cordon() {
28+
return new KubectlCordon(true);
29+
}
30+
31+
/** Equivalent for `kubectl uncordon` */
32+
public static KubectlCordon uncordon() {
33+
return new KubectlCordon(false);
34+
}
35+
2636
/**
2737
* Equivalent for `kubectl top`
2838
*
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.extended.kubectl;
14+
15+
import io.kubernetes.client.custom.V1Patch;
16+
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
17+
import io.kubernetes.client.openapi.ApiException;
18+
import io.kubernetes.client.openapi.apis.CoreV1Api;
19+
import io.kubernetes.client.openapi.models.V1Node;
20+
import io.kubernetes.client.util.PatchUtils;
21+
22+
public class KubectlCordon extends Kubectl.ResourceAndContainerBuilder<V1Node, KubectlCordon>
23+
implements Kubectl.Executable<V1Node> {
24+
25+
static final String CORDON_PATCH_STR =
26+
"[{\"op\":\"replace\",\"path\":\"/spec/unschedulable\",\"value\":true}]";
27+
static String UNCORDON_PATCH_STR =
28+
"[{\"op\":\"replace\",\"path\":\"/spec/unschedulable\",\"value\":false}]";
29+
30+
private boolean cordon;
31+
32+
KubectlCordon(boolean performCordon) {
33+
super(V1Node.class);
34+
this.cordon = performCordon;
35+
}
36+
37+
@Override
38+
public V1Node execute() throws KubectlException {
39+
String patch = this.cordon ? CORDON_PATCH_STR : UNCORDON_PATCH_STR;
40+
CoreV1Api api = new CoreV1Api(apiClient);
41+
42+
try {
43+
return PatchUtils.patch(
44+
V1Node.class,
45+
() ->
46+
api.patchNodeCall(
47+
name,
48+
new V1Patch(patch),
49+
null,
50+
null,
51+
null, // field-manager is optional
52+
null,
53+
null),
54+
V1Patch.PATCH_FORMAT_JSON_PATCH,
55+
apiClient);
56+
} catch (ApiException ex) {
57+
throw new KubectlException(ex);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)