Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ public class KubectlGet<ApiType extends KubernetesObject>
private ListOptions listOptions;
private Class<ApiType> apiTypeClass;
private Class<? extends KubernetesListObject> apiTypeListClass;
private boolean allNamespaces;

KubectlGet(Class<ApiType> apiTypeClass) {
this.apiTypeClass = apiTypeClass;
this.listOptions = new ListOptions();
this.allNamespaces = false;
}

public KubectlGet<ApiType> apiListTypeClass(
Expand All @@ -51,6 +53,11 @@ public KubectlGet<ApiType> namespace(String namespace) {
return this;
}

public KubectlGet<ApiType> allNamespaces() {
this.allNamespaces = true;
return this;
}

public KubectlGetSingle name(String name) {
return new KubectlGetSingle(name);
}
Expand All @@ -64,7 +71,9 @@ public List<ApiType> execute() throws KubectlException {
? getGenericApi(apiTypeClass)
: getGenericApi(apiTypeClass, apiTypeListClass);
try {
if (isNamespaced()) {
if (allNamespaces) {
return (List<ApiType>) api.list(listOptions).throwsApiException().getObject().getItems();
} else if (isNamespaced()) {
return (List<ApiType>)
api.list(namespace, listOptions).throwsApiException().getObject().getItems();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<V1Pod> 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<V1Pod> 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"));
Expand Down
Loading