Skip to content

Commit ee0677d

Browse files
authored
Merge pull request #1256 from brendandburns/replace
Add kubectl replace.
2 parents 3a1e3d9 + 8e2ec9d commit ee0677d

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public static void main(String[] args)
107107
String name = null;
108108

109109
switch (verb) {
110+
// TODO: add support for create and replace here.
110111
case "drain":
111112
name = args[1];
112113
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> KubectlReplace<ApiType> replace(
56+
Class<ApiType> clazz) {
57+
return new KubectlReplace<ApiType>(clazz);
58+
}
59+
5560
/**
5661
* Equivalent for `kubectl apply`
5762
*
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 io.kubernetes.client.util.generic.options.UpdateOptions;
20+
import org.apache.commons.lang.StringUtils;
21+
22+
public class KubectlReplace<ApiType extends KubernetesObject>
23+
extends Kubectl.ResourceBuilder<ApiType, KubectlReplace<ApiType>>
24+
implements Kubectl.Executable<ApiType> {
25+
ApiType updateObject;
26+
UpdateOptions options;
27+
28+
KubectlReplace(Class<ApiType> apiTypeClass) {
29+
super(apiTypeClass);
30+
}
31+
32+
public KubectlReplace<ApiType> resource(ApiType resource) {
33+
this.updateObject = resource;
34+
return this;
35+
}
36+
37+
public KubectlReplace<ApiType> options(UpdateOptions options) {
38+
this.options = options;
39+
return this;
40+
}
41+
42+
@Override
43+
public ApiType execute() throws KubectlException {
44+
verifyArguments();
45+
refreshDiscovery();
46+
47+
if (isNamespaced(apiTypeClass)) {
48+
try {
49+
return getGenericApi()
50+
.update(updateObject, options)
51+
.onFailure(
52+
errorStatus -> {
53+
throw new ApiException(errorStatus.toString());
54+
})
55+
.getObject();
56+
} catch (ApiException e) {
57+
throw new KubectlException(e);
58+
}
59+
} else {
60+
try {
61+
return getGenericApi()
62+
.update(updateObject, options)
63+
.onFailure(
64+
errorStatus -> {
65+
throw new ApiException(errorStatus.toString());
66+
})
67+
.getObject();
68+
} catch (ApiException e) {
69+
throw new KubectlException(e);
70+
}
71+
}
72+
}
73+
74+
public boolean isNamespaced(Class<ApiType> apiTypeClass) {
75+
Boolean isNamespaced = ModelMapper.isNamespaced(apiTypeClass);
76+
if (isNamespaced == null) { // unknown
77+
return false;
78+
}
79+
80+
return isNamespaced || !StringUtils.isEmpty(namespace);
81+
}
82+
83+
private void verifyArguments() throws KubectlException {
84+
if (null == name) {
85+
throw new KubectlException("missing name argument");
86+
}
87+
if (null == updateObject) {
88+
throw new KubectlException("missing new resource");
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)