Skip to content

Commit 8f42214

Browse files
authored
Merge pull request #2465 from shubhbapna/deleteLabel
Delete label
2 parents a13810d + 2fb3b02 commit 8f42214

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public KubectlLabel<ApiType> addLabel(String key, String value) {
3737
return this;
3838
}
3939

40+
public KubectlLabel<ApiType> deleteLabel(String key) {
41+
this.addingLabels.put(key, "null");
42+
return this;
43+
}
44+
4045
@Override
4146
public ApiType execute() throws KubectlException {
4247
verifyArguments();

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,37 @@ public void testKubectlLabelNamespacedResourceShouldWork() throws KubectlExcepti
7979
assertNotNull(labelledPod);
8080
}
8181

82+
@Test
83+
public void testKubectlDeleteLabelNamespacedResourceShouldWork() throws KubectlException {
84+
wireMockRule.stubFor(
85+
get(urlPathEqualTo("/api/v1/namespaces/default/pods/foo"))
86+
.willReturn(
87+
aResponse()
88+
.withStatus(200)
89+
.withBody("{\"metadata\":{\"name\":\"foo\",\"namespace\":\"default\"}}")));
90+
wireMockRule.stubFor(
91+
put(urlPathEqualTo("/api/v1/namespaces/default/pods/foo"))
92+
.withRequestBody(
93+
matchingJsonPath("$.metadata.labels", equalToJson("{ \"k1\": \"null\" }")))
94+
.willReturn(
95+
aResponse()
96+
.withStatus(200)
97+
.withBody("{\"metadata\":{\"name\":\"foo\",\"namespace\":\"default\"}}")));
98+
99+
V1Pod unlabelledPod =
100+
Kubectl.label(V1Pod.class)
101+
.apiClient(apiClient)
102+
.skipDiscovery()
103+
.namespace("default")
104+
.name("foo")
105+
.deleteLabel("k1")
106+
.execute();
107+
108+
wireMockRule.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")));
109+
wireMockRule.verify(1, putRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo")));
110+
assertNotNull(unlabelledPod);
111+
}
112+
82113
@Test
83114
public void testKubectlLabelNamespacedResourceReceiveForbiddenShouldThrowException()
84115
throws KubectlException {
@@ -134,6 +165,29 @@ public void testKubectlLabelClusterResourceShouldWork() throws KubectlException
134165
assertNotNull(labelledNode);
135166
}
136167

168+
@Test
169+
public void testKubectlDeleteLabelClusterResourceShouldWork() throws KubectlException {
170+
wireMockRule.stubFor(
171+
get(urlPathEqualTo("/api/v1/nodes/foo"))
172+
.willReturn(aResponse().withStatus(200).withBody("{\"metadata\":{\"name\":\"foo\"}}")));
173+
wireMockRule.stubFor(
174+
put(urlPathEqualTo("/api/v1/nodes/foo"))
175+
.withRequestBody(
176+
matchingJsonPath("$.metadata.labels", equalToJson("{ \"k1\": \"null\" }")))
177+
.willReturn(aResponse().withStatus(200).withBody("{\"metadata\":{\"name\":\"foo\"}}")));
178+
179+
V1Node unlabelledNode =
180+
Kubectl.label(V1Node.class)
181+
.apiClient(apiClient)
182+
.skipDiscovery()
183+
.name("foo")
184+
.deleteLabel("k1")
185+
.execute();
186+
wireMockRule.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/nodes/foo")));
187+
wireMockRule.verify(1, putRequestedFor(urlPathEqualTo("/api/v1/nodes/foo")));
188+
assertNotNull(unlabelledNode);
189+
}
190+
137191
@Test
138192
public void testKubectlLabelClusterResourceReceiveForbiddenShouldThrowException()
139193
throws KubectlException {

0 commit comments

Comments
 (0)