Skip to content

Commit 6dc9834

Browse files
authored
Merge branch 'master' into delete
2 parents 07dbca0 + ee0677d commit 6dc9834

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

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
@@ -57,6 +57,11 @@ public static <ApiType extends KubernetesObject> KubectlDelete<ApiType> delete(
5757
return new KubectlDelete<ApiType>(clazz);
5858
}
5959

60+
public static <ApiType extends KubernetesObject> KubectlReplace<ApiType> replace(
61+
Class<ApiType> clazz) {
62+
return new KubectlReplace<ApiType>(clazz);
63+
}
64+
6065
/**
6166
* Equivalent for `kubectl apply`
6267
*
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)