Skip to content

Commit ae0d63d

Browse files
authored
Merge pull request #1255 from brendandburns/delete
Add kubectl delete.
2 parents ee0677d + 6dc9834 commit ae0d63d

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static io.kubernetes.client.extended.kubectl.Kubectl.apiResources;
1616
import static io.kubernetes.client.extended.kubectl.Kubectl.copy;
1717
import static io.kubernetes.client.extended.kubectl.Kubectl.cordon;
18+
import static io.kubernetes.client.extended.kubectl.Kubectl.delete;
1819
import static io.kubernetes.client.extended.kubectl.Kubectl.drain;
1920
import static io.kubernetes.client.extended.kubectl.Kubectl.exec;
2021
import static io.kubernetes.client.extended.kubectl.Kubectl.label;
@@ -107,7 +108,10 @@ public static void main(String[] args)
107108
String name = null;
108109

109110
switch (verb) {
110-
// TODO: add support for create and replace here.
111+
case "delete":
112+
kind = args[1];
113+
name = args[2];
114+
delete(getClassForKind(kind)).namespace(ns).name(name).execute();
111115
case "drain":
112116
name = args[1];
113117
drain().apiClient(client).name(name).execute();

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public static KubectlCreate create() {
5252
return new KubectlCreate();
5353
}
5454

55+
public static <ApiType extends KubernetesObject> KubectlDelete<ApiType> delete(
56+
Class<ApiType> clazz) {
57+
return new KubectlDelete<ApiType>(clazz);
58+
}
59+
5560
public static <ApiType extends KubernetesObject> KubectlReplace<ApiType> replace(
5661
Class<ApiType> clazz) {
5762
return new KubectlReplace<ApiType>(clazz);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.common.KubernetesObject;
16+
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
17+
import io.kubernetes.client.openapi.ApiException;
18+
import io.kubernetes.client.util.ModelMapper;
19+
import org.apache.commons.lang.StringUtils;
20+
21+
public class KubectlDelete<ApiType extends KubernetesObject>
22+
extends Kubectl.ResourceBuilder<ApiType, KubectlDelete<ApiType>>
23+
implements Kubectl.Executable<ApiType> {
24+
25+
KubectlDelete(Class<ApiType> apiTypeClass) {
26+
super(apiTypeClass);
27+
}
28+
29+
@Override
30+
public ApiType execute() throws KubectlException {
31+
verifyArguments();
32+
refreshDiscovery();
33+
34+
if (isNamespaced(apiTypeClass)) {
35+
try {
36+
return getGenericApi()
37+
.delete(namespace, name)
38+
.onFailure(
39+
errorStatus -> {
40+
throw new ApiException(errorStatus.toString());
41+
})
42+
.getObject();
43+
} catch (ApiException e) {
44+
throw new KubectlException(e);
45+
}
46+
} else {
47+
try {
48+
return getGenericApi()
49+
.delete(name)
50+
.onFailure(
51+
errorStatus -> {
52+
throw new ApiException(errorStatus.toString());
53+
})
54+
.getObject();
55+
} catch (ApiException e) {
56+
throw new KubectlException(e);
57+
}
58+
}
59+
}
60+
61+
public boolean isNamespaced(Class<ApiType> apiTypeClass) {
62+
Boolean isNamespaced = ModelMapper.isNamespaced(apiTypeClass);
63+
if (isNamespaced == null) { // unknown
64+
return false;
65+
}
66+
67+
return isNamespaced || !StringUtils.isEmpty(namespace);
68+
}
69+
70+
private void verifyArguments() throws KubectlException {
71+
if (null == name) {
72+
throw new KubectlException("missing name argument");
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)