Skip to content

Commit d1af24d

Browse files
Add allNamespaces() method to KubectlGet to match kubectl --all-namespaces/-A flag
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
1 parent e64ade8 commit d1af24d

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ public class KubectlGet<ApiType extends KubernetesObject>
2929
private ListOptions listOptions;
3030
private Class<ApiType> apiTypeClass;
3131
private Class<? extends KubernetesListObject> apiTypeListClass;
32+
private boolean allNamespaces;
3233

3334
KubectlGet(Class<ApiType> apiTypeClass) {
3435
this.apiTypeClass = apiTypeClass;
3536
this.listOptions = new ListOptions();
37+
this.allNamespaces = false;
3638
}
3739

3840
public KubectlGet<ApiType> apiListTypeClass(
@@ -51,6 +53,11 @@ public KubectlGet<ApiType> namespace(String namespace) {
5153
return this;
5254
}
5355

56+
public KubectlGet<ApiType> allNamespaces() {
57+
this.allNamespaces = true;
58+
return this;
59+
}
60+
5461
public KubectlGetSingle name(String name) {
5562
return new KubectlGetSingle(name);
5663
}
@@ -64,7 +71,9 @@ public List<ApiType> execute() throws KubectlException {
6471
? getGenericApi(apiTypeClass)
6572
: getGenericApi(apiTypeClass, apiTypeListClass);
6673
try {
67-
if (isNamespaced()) {
74+
if (allNamespaces) {
75+
return (List<ApiType>) api.list(listOptions).throwsApiException().getObject().getItems();
76+
} else if (isNamespaced()) {
6877
return (List<ApiType>)
6978
api.list(namespace, listOptions).throwsApiException().getObject().getItems();
7079

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,56 @@ void getDefaultNamespacePods() throws KubectlException {
9696
assertThat(pods).hasSize(2);
9797
}
9898

99+
@Test
100+
void getAllNamespacePodsWithAllNamespacesFlag() throws KubectlException {
101+
V1PodList podList =
102+
new V1PodList()
103+
.items(
104+
Arrays.asList(
105+
new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo1")),
106+
new V1Pod().metadata(new V1ObjectMeta().namespace("kube-system").name("foo2"))));
107+
apiServer.stubFor(
108+
get(urlPathEqualTo("/api/v1/pods"))
109+
.willReturn(
110+
aResponse().withStatus(200).withBody(apiClient.getJSON().serialize(podList))));
111+
112+
List<V1Pod> pods =
113+
Kubectl.get(V1Pod.class)
114+
.apiClient(apiClient)
115+
.skipDiscovery()
116+
.allNamespaces()
117+
.execute();
118+
119+
apiServer.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")));
120+
assertThat(pods).hasSize(2);
121+
}
122+
123+
@Test
124+
void getAllNamespacesFlagOverridesNamespace() throws KubectlException {
125+
V1PodList podList =
126+
new V1PodList()
127+
.items(
128+
Arrays.asList(
129+
new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo1")),
130+
new V1Pod().metadata(new V1ObjectMeta().namespace("kube-system").name("foo2"))));
131+
apiServer.stubFor(
132+
get(urlPathEqualTo("/api/v1/pods"))
133+
.willReturn(
134+
aResponse().withStatus(200).withBody(apiClient.getJSON().serialize(podList))));
135+
136+
// When allNamespaces() is called, it should ignore the namespace setting
137+
List<V1Pod> pods =
138+
Kubectl.get(V1Pod.class)
139+
.apiClient(apiClient)
140+
.skipDiscovery()
141+
.namespace("default")
142+
.allNamespaces()
143+
.execute();
144+
145+
apiServer.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")));
146+
assertThat(pods).hasSize(2);
147+
}
148+
99149
@Test
100150
void getDefaultNamespaceOnePod() throws KubectlException {
101151
V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo1"));

0 commit comments

Comments
 (0)