Skip to content

Commit 3fc1f7a

Browse files
committed
kubectl label equivalence
1 parent 9dae95d commit 3fc1f7a

File tree

7 files changed

+520
-1
lines changed

7 files changed

+520
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.ApiClient;
18+
import io.kubernetes.client.openapi.Configuration;
19+
20+
/**
21+
* Kubectl provides a set of helper functions that has the same functionalities as corresponding
22+
* kubectl commands.
23+
*/
24+
public class Kubectl {
25+
26+
/**
27+
* Equivalence for `kubectl label`.
28+
*
29+
* @param <ApiType> the target api type
30+
* @param apiTypeClass the api type class
31+
* @return the kubectl label
32+
*/
33+
public static <ApiType extends KubernetesObject> KubectlLabel<ApiType> label(
34+
Class<ApiType> apiTypeClass) {
35+
return label(Configuration.getDefaultApiClient(), apiTypeClass);
36+
}
37+
38+
/**
39+
* Equivalence for `kubectl label`.
40+
*
41+
* @param <ApiType> the target api type
42+
* @param apiClient the api client instance
43+
* @param apiTypeClass the api type class
44+
* @return the kubectl label
45+
*/
46+
public static <ApiType extends KubernetesObject> KubectlLabel<ApiType> label(
47+
ApiClient apiClient, Class<ApiType> apiTypeClass) {
48+
return new KubectlLabel<>(apiClient, apiTypeClass);
49+
}
50+
51+
/**
52+
* Executable executes a kubectl helper.
53+
*
54+
* @param <OUTPUT> the type parameter
55+
*/
56+
public interface Executable<OUTPUT> {
57+
58+
/**
59+
* Run and retrieve the output from the kubectl helpers.
60+
*
61+
* @return the output, can be Void
62+
* @throws KubectlException the kubectl exception
63+
*/
64+
OUTPUT execute() throws KubectlException;
65+
}
66+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.KubernetesListObject;
16+
import io.kubernetes.client.common.KubernetesObject;
17+
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
18+
import io.kubernetes.client.openapi.ApiClient;
19+
import io.kubernetes.client.util.generic.GenericKubernetesApi;
20+
import io.kubernetes.client.util.generic.KubernetesApiResponse;
21+
import io.kubernetes.client.util.labels.Labels;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import org.apache.commons.lang.StringUtils;
25+
26+
class KubectlLabel<ApiType extends KubernetesObject> implements Kubectl.Executable<ApiType> {
27+
28+
private final ApiClient apiClient;
29+
private final Class<ApiType> apiTypeClass;
30+
private final Map<String, String> addingLabels;
31+
32+
private String apiGroup;
33+
private String apiVersion;
34+
private String resourceNamePlural;
35+
36+
private String namespace;
37+
private String name;
38+
39+
KubectlLabel(ApiClient apiClient, Class<ApiType> apiTypeClass) {
40+
this.addingLabels = new HashMap<>();
41+
this.apiTypeClass = apiTypeClass;
42+
this.apiClient = apiClient;
43+
}
44+
45+
public KubectlLabel<ApiType> apiGroup(String apiGroup) {
46+
this.apiGroup = apiGroup;
47+
return this;
48+
}
49+
50+
public KubectlLabel<ApiType> apiVersion(String apiVersion) {
51+
this.apiVersion = apiVersion;
52+
return this;
53+
}
54+
55+
public KubectlLabel<ApiType> resourceNamePlural(String resourceNamePlural) {
56+
this.resourceNamePlural = resourceNamePlural;
57+
return this;
58+
}
59+
60+
public KubectlLabel<ApiType> namespace(String namespace) {
61+
this.namespace = namespace;
62+
return this;
63+
}
64+
65+
public KubectlLabel<ApiType> name(String name) {
66+
this.name = name;
67+
return this;
68+
}
69+
70+
public KubectlLabel<ApiType> addLabel(String key, String value) {
71+
this.addingLabels.put(key, value);
72+
return this;
73+
}
74+
75+
private boolean isNamespaced() {
76+
return !StringUtils.isEmpty(namespace);
77+
}
78+
79+
@Override
80+
public ApiType execute() throws KubectlException {
81+
verifyArguments();
82+
GenericKubernetesApi<ApiType, KubernetesListObject> api =
83+
new GenericKubernetesApi<>(
84+
apiTypeClass,
85+
KubernetesListObject.class,
86+
apiGroup,
87+
apiVersion,
88+
resourceNamePlural,
89+
apiClient);
90+
91+
try {
92+
final KubernetesApiResponse<ApiType> getResponse;
93+
if (isNamespaced()) {
94+
getResponse = api.get(namespace, name);
95+
} else {
96+
getResponse = api.get(name);
97+
}
98+
if (!getResponse.isSuccess()) {
99+
throw new KubectlException(getResponse.getStatus());
100+
}
101+
ApiType obj = getResponse.getObject();
102+
103+
Labels.addLabels(obj, addingLabels);
104+
105+
final KubernetesApiResponse<ApiType> updateResponse;
106+
updateResponse = api.update(obj);
107+
if (!updateResponse.isSuccess()) {
108+
throw new KubectlException(updateResponse.getStatus());
109+
}
110+
return updateResponse.getObject();
111+
} catch (Throwable t) {
112+
throw new KubectlException(t);
113+
}
114+
}
115+
116+
private void verifyArguments() throws KubectlException {
117+
if (null == apiGroup) {
118+
throw new KubectlException("missing apiGroup argument");
119+
}
120+
if (null == apiVersion) {
121+
throw new KubectlException("missing apiVersion argument");
122+
}
123+
if (null == resourceNamePlural) {
124+
throw new KubectlException("missing resourceNamePlural argument");
125+
}
126+
if (null == name) {
127+
throw new KubectlException("missing name argument");
128+
}
129+
}
130+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.exception;
14+
15+
import io.kubernetes.client.openapi.models.V1Status;
16+
17+
/** KubectlException is thrown upon any failure from Kubectl helpers. */
18+
public class KubectlException extends Exception {
19+
20+
public KubectlException(String message) {
21+
super(message);
22+
}
23+
24+
public KubectlException(V1Status status) {
25+
super(status.toString());
26+
}
27+
28+
public KubectlException(Throwable cause) {
29+
super(cause);
30+
}
31+
}

0 commit comments

Comments
 (0)