Skip to content

Commit da29333

Browse files
authored
Merge pull request #1265 from yue9944882/homogenize-kubectl-subcommands
Refactor(kubectl): Homogenize kubectl resource manipulating sub-commands (kubectl create/apply)
2 parents 02c8b32 + 9a9f441 commit da29333

File tree

7 files changed

+62
-66
lines changed

7 files changed

+62
-66
lines changed

e2e/src/test/groovy/io/kubernetes/client/e2e/kubectl/KubectlApplyTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class KubectlApplyTest extends Specification {
1212
given:
1313
def apiClient = ClientBuilder.defaultClient();
1414
apiClient.setDebugging(true)
15-
def appliedNamespace = Kubectl.apply()
15+
def appliedNamespace = Kubectl.apply(V1Namespace.class)
1616
.apiClient(apiClient)
1717
.resource(new V1Namespace()
1818
.apiVersion("v1")

e2e/src/test/groovy/io/kubernetes/client/e2e/kubectl/KubectlCreateTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class KubectlCreateTest extends Specification {
1111
def "Kubectl create namespace should work"() {
1212
given:
1313
def apiClient = ClientBuilder.defaultClient();
14-
def createdNamespace = Kubectl.create()
14+
def createdNamespace = Kubectl.create(V1Namespace.class)
1515
.apiClient(apiClient)
1616
.resource(new V1Namespace()
1717
.apiVersion("v1")

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ public static <ApiType extends KubernetesObject> KubectlGet<ApiType> get(
3535
return new KubectlGet<>(apiTypeClass);
3636
}
3737

38+
/** Equivalent for `kubectl create` */
39+
public static <ApiType extends KubernetesObject> KubectlCreate<ApiType> create(
40+
Class<ApiType> clazz) {
41+
return new KubectlCreate<>(clazz);
42+
}
43+
44+
/** Equivalent for `kubectl delete` */
45+
public static <ApiType extends KubernetesObject> KubectlDelete<ApiType> delete(
46+
Class<ApiType> clazz) {
47+
return new KubectlDelete<>(clazz);
48+
}
49+
50+
/** Equivalent for `kubectl replace` */
51+
public static <ApiType extends KubernetesObject> KubectlReplace<ApiType> replace(
52+
Class<ApiType> clazz) {
53+
return new KubectlReplace<>(clazz);
54+
}
55+
56+
/** Equivalent for `kubectl apply` */
57+
public static <ApiType extends KubernetesObject> KubectlApply<ApiType> apply(
58+
Class<ApiType> clazz) {
59+
return new KubectlApply(clazz);
60+
}
61+
3862
/** Equivalent for `kubectl drain` */
3963
public static KubectlDrain drain() {
4064
return new KubectlDrain();
@@ -50,34 +74,6 @@ public static KubectlCordon uncordon() {
5074
return new KubectlCordon(false);
5175
}
5276

53-
/**
54-
* Equivalent for `kubectl create`
55-
*
56-
* @return the kubectl create
57-
*/
58-
public static KubectlCreate create() {
59-
return new KubectlCreate();
60-
}
61-
62-
public static <ApiType extends KubernetesObject> KubectlDelete<ApiType> delete(
63-
Class<ApiType> clazz) {
64-
return new KubectlDelete<ApiType>(clazz);
65-
}
66-
67-
public static <ApiType extends KubernetesObject> KubectlReplace<ApiType> replace(
68-
Class<ApiType> clazz) {
69-
return new KubectlReplace<ApiType>(clazz);
70-
}
71-
72-
/**
73-
* Equivalent for `kubectl apply`
74-
*
75-
* @return the kubectl create
76-
*/
77-
public static KubectlApply apply() {
78-
return new KubectlApply();
79-
}
80-
8177
/**
8278
* Equivalent for `kubectl top`
8379
*

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,33 @@
2424
import io.kubernetes.client.util.generic.KubernetesApiResponse;
2525
import io.kubernetes.client.util.generic.options.PatchOptions;
2626

27-
public class KubectlApply extends Kubectl.NamespacedApiClientBuilder<KubectlApply>
28-
implements Kubectl.Executable<KubernetesObject> {
27+
public class KubectlApply<ApiType extends KubernetesObject>
28+
extends Kubectl.ResourceBuilder<ApiType, KubectlApply<ApiType>>
29+
implements Kubectl.Executable<ApiType> {
2930

30-
private KubernetesObject targetObj;
31+
private ApiType targetObj;
3132
private String fieldManager;
3233
private boolean forceConflict;
3334

3435
public static final String DEFAULT_FIELD_MANAGER = "kubernetes-java-kubectl-apply";
3536

36-
KubectlApply() {
37+
KubectlApply(Class<ApiType> apiTypeClass) {
38+
super(apiTypeClass);
3739
this.forceConflict = false;
3840
this.fieldManager = DEFAULT_FIELD_MANAGER;
3941
}
4042

41-
public KubectlApply fieldManager(String fieldManager) {
43+
public KubectlApply<ApiType> fieldManager(String fieldManager) {
4244
this.fieldManager = fieldManager;
4345
return this;
4446
}
4547

46-
public KubectlApply forceConflict(boolean isForceConflict) {
48+
public KubectlApply<ApiType> forceConflict(boolean isForceConflict) {
4749
this.forceConflict = isForceConflict;
4850
return this;
4951
}
5052

51-
public KubectlApply resource(KubernetesObject obj) {
53+
public KubectlApply<ApiType> resource(ApiType obj) {
5254
this.targetObj = obj;
5355
return this;
5456
}
@@ -60,17 +62,15 @@ private void validate() throws KubectlException {
6062
}
6163

6264
@Override
63-
public KubernetesObject execute() throws KubectlException {
65+
public ApiType execute() throws KubectlException {
6466
validate();
6567
return executeServerSideApply();
6668
}
6769

68-
private KubernetesObject executeServerSideApply() throws KubectlException {
70+
private ApiType executeServerSideApply() throws KubectlException {
6971
refreshDiscovery();
7072

71-
GenericKubernetesApi<KubernetesObject, KubernetesListObject> api =
72-
(GenericKubernetesApi<KubernetesObject, KubernetesListObject>)
73-
getGenericApi(this.targetObj.getClass());
73+
GenericKubernetesApi<ApiType, KubernetesListObject> api = getGenericApi();
7474

7575
PatchOptions patchOptions = new PatchOptions();
7676
patchOptions.setForce(this.forceConflict);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@
2222
import io.kubernetes.client.util.generic.GenericKubernetesApi;
2323
import io.kubernetes.client.util.generic.options.CreateOptions;
2424

25-
public class KubectlCreate extends Kubectl.NamespacedApiClientBuilder<KubectlCreate>
26-
implements Kubectl.Executable<KubernetesObject> {
25+
public class KubectlCreate<ApiType extends KubernetesObject>
26+
extends Kubectl.ResourceBuilder<ApiType, KubectlCreate<ApiType>>
27+
implements Kubectl.Executable<ApiType> {
2728

28-
KubectlCreate() {}
29+
KubectlCreate(Class<ApiType> apiTypeClass) {
30+
super(apiTypeClass);
31+
}
2932

30-
private KubernetesObject targetObj;
33+
private ApiType targetObj;
3134

32-
public KubectlCreate resource(KubernetesObject obj) {
35+
public KubectlCreate<ApiType> resource(ApiType obj) {
3336
this.targetObj = obj;
3437
return this;
3538
}
3639

3740
@Override
38-
public KubernetesObject execute() throws KubectlException {
41+
public ApiType execute() throws KubectlException {
3942
refreshDiscovery();
4043

41-
GenericKubernetesApi<KubernetesObject, KubernetesListObject> api =
42-
(GenericKubernetesApi<KubernetesObject, KubernetesListObject>)
43-
getGenericApi(this.targetObj.getClass());
44+
GenericKubernetesApi<ApiType, KubernetesListObject> api = getGenericApi();
4445

4546
if (ModelMapper.isNamespaced(this.targetObj.getClass())) {
4647
String targetNamespace =

extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlApplyTest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,19 @@ public void testApplyConfigMap() throws KubectlException, IOException {
8181
.withBody(new String(Files.readAllBytes(Paths.get(DISCOVERY_APIV1))))));
8282

8383
V1ConfigMap configMap =
84-
(V1ConfigMap)
85-
Kubectl.apply()
86-
.apiClient(apiClient)
87-
.resource(
88-
new V1ConfigMap()
89-
.apiVersion("v1")
90-
.metadata(new V1ObjectMeta().namespace("foo").name("bar"))
91-
.data(
92-
new HashMap<String, String>() {
93-
{
94-
put("key1", "value1");
95-
}
96-
}))
97-
.execute();
84+
Kubectl.apply(V1ConfigMap.class)
85+
.apiClient(apiClient)
86+
.resource(
87+
new V1ConfigMap()
88+
.apiVersion("v1")
89+
.metadata(new V1ObjectMeta().namespace("foo").name("bar"))
90+
.data(
91+
new HashMap<String, String>() {
92+
{
93+
put("key1", "value1");
94+
}
95+
}))
96+
.execute();
9897
wireMockRule.verify(
9998
1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/foo/configmaps/bar")));
10099
assertNotNull(configMap);

extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlCreateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void testCreateConfigMap() throws KubectlException, IOException {
7878

7979
V1ConfigMap configMap =
8080
(V1ConfigMap)
81-
Kubectl.create()
81+
Kubectl.create(V1ConfigMap.class)
8282
.apiClient(apiClient)
8383
.resource(
8484
new V1ConfigMap()

0 commit comments

Comments
 (0)