diff --git a/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlGet.java b/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlGet.java index 19479f6a8d..263baf0a8a 100644 --- a/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlGet.java +++ b/extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlGet.java @@ -29,10 +29,12 @@ public class KubectlGet private ListOptions listOptions; private Class apiTypeClass; private Class apiTypeListClass; + private boolean allNamespaces; KubectlGet(Class apiTypeClass) { this.apiTypeClass = apiTypeClass; this.listOptions = new ListOptions(); + this.allNamespaces = false; } public KubectlGet apiListTypeClass( @@ -51,6 +53,11 @@ public KubectlGet namespace(String namespace) { return this; } + public KubectlGet allNamespaces() { + this.allNamespaces = true; + return this; + } + public KubectlGetSingle name(String name) { return new KubectlGetSingle(name); } @@ -64,7 +71,9 @@ public List execute() throws KubectlException { ? getGenericApi(apiTypeClass) : getGenericApi(apiTypeClass, apiTypeListClass); try { - if (isNamespaced()) { + if (allNamespaces) { + return (List) api.list(listOptions).throwsApiException().getObject().getItems(); + } else if (isNamespaced()) { return (List) api.list(namespace, listOptions).throwsApiException().getObject().getItems(); diff --git a/extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlGetTest.java b/extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlGetTest.java index d3aad023d7..84c2ff799c 100644 --- a/extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlGetTest.java +++ b/extended/src/test/java/io/kubernetes/client/extended/kubectl/KubectlGetTest.java @@ -96,6 +96,56 @@ void getDefaultNamespacePods() throws KubectlException { assertThat(pods).hasSize(2); } + @Test + void getAllNamespacePodsWithAllNamespacesFlag() throws KubectlException { + V1PodList podList = + new V1PodList() + .items( + Arrays.asList( + new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo1")), + new V1Pod().metadata(new V1ObjectMeta().namespace("kube-system").name("foo2")))); + apiServer.stubFor( + get(urlPathEqualTo("/api/v1/pods")) + .willReturn( + aResponse().withStatus(200).withBody(apiClient.getJSON().serialize(podList)))); + + List pods = + Kubectl.get(V1Pod.class) + .apiClient(apiClient) + .skipDiscovery() + .allNamespaces() + .execute(); + + apiServer.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods"))); + assertThat(pods).hasSize(2); + } + + @Test + void getAllNamespacesFlagOverridesNamespace() throws KubectlException { + V1PodList podList = + new V1PodList() + .items( + Arrays.asList( + new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo1")), + new V1Pod().metadata(new V1ObjectMeta().namespace("kube-system").name("foo2")))); + apiServer.stubFor( + get(urlPathEqualTo("/api/v1/pods")) + .willReturn( + aResponse().withStatus(200).withBody(apiClient.getJSON().serialize(podList)))); + + // When allNamespaces() is called, it should ignore the namespace setting + List pods = + Kubectl.get(V1Pod.class) + .apiClient(apiClient) + .skipDiscovery() + .namespace("default") + .allNamespaces() + .execute(); + + apiServer.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods"))); + assertThat(pods).hasSize(2); + } + @Test void getDefaultNamespaceOnePod() throws KubectlException { V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().namespace("default").name("foo1"));