Skip to content

Commit 093d8cd

Browse files
authored
feat(api): add DSL entrypoint getAPIVersions() in Client interface
Add DSL entrypoint `getAPIVersions()` in Client interface Signed-off-by: Rohan Kumar <[email protected]>
1 parent 1fb25af commit 093d8cd

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Fix #6052: Removed dependency on no longer maintained com.github.mifmif:generex
1414

1515
#### New Features
16+
* Fix #6066: Add support for `v1.APIVersions` in KubernetesClient
1617

1718
#### _**Note**_: Breaking changes
1819
* Check detailed migration documentation for breaking changes in [7.0.0](./doc/MIGRATION-v7.md)

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Client.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.fabric8.kubernetes.api.model.APIGroup;
1919
import io.fabric8.kubernetes.api.model.APIGroupList;
2020
import io.fabric8.kubernetes.api.model.APIResourceList;
21+
import io.fabric8.kubernetes.api.model.APIVersions;
2122
import io.fabric8.kubernetes.api.model.HasMetadata;
2223
import io.fabric8.kubernetes.api.model.KubernetesResource;
2324
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
@@ -108,6 +109,15 @@ public interface Client extends Closeable {
108109
@Override
109110
void close();
110111

112+
/**
113+
* Get the available APIversions. APIVersions lists the versions that are available,
114+
* to allow clients to discover the API at /api, which is the root path of the
115+
* legacy v1 API.
116+
*
117+
* @return the {@link APIVersions} object
118+
*/
119+
APIVersions getAPIVersions();
120+
111121
/**
112122
* Returns the api groups. This does not include the core/legacy v1 apiVersion.
113123
*

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ClientAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.fabric8.kubernetes.api.model.APIGroup;
1919
import io.fabric8.kubernetes.api.model.APIGroupList;
2020
import io.fabric8.kubernetes.api.model.APIResourceList;
21+
import io.fabric8.kubernetes.api.model.APIVersions;
2122
import io.fabric8.kubernetes.api.model.HasMetadata;
2223
import io.fabric8.kubernetes.api.model.KubernetesResource;
2324
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
@@ -117,6 +118,11 @@ public APIGroupList getApiGroups() {
117118
return client.getApiGroups();
118119
}
119120

121+
@Override
122+
public APIVersions getAPIVersions() {
123+
return client.getAPIVersions();
124+
}
125+
120126
@Override
121127
public APIGroup getApiGroup(String name) {
122128
return client.getApiGroup(name);

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/impl/BaseClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.fabric8.kubernetes.api.model.APIGroup;
1919
import io.fabric8.kubernetes.api.model.APIGroupList;
2020
import io.fabric8.kubernetes.api.model.APIResourceList;
21+
import io.fabric8.kubernetes.api.model.APIVersions;
2122
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
2223
import io.fabric8.kubernetes.api.model.HasMetadata;
2324
import io.fabric8.kubernetes.api.model.KubernetesResource;
@@ -79,6 +80,7 @@ public void onClose(Executor executor) {
7980
};
8081

8182
public static final String APIS = "/apis";
83+
private static final String API = "/api";
8284

8385
private URL masterUrl;
8486
private String apiVersion;
@@ -297,6 +299,11 @@ public APIGroup getApiGroup(String name) {
297299
return getOperationSupport().restCall(APIGroup.class, APIS, name);
298300
}
299301

302+
@Override
303+
public APIVersions getAPIVersions() {
304+
return getOperationSupport().restCall(APIVersions.class, API);
305+
}
306+
300307
private OperationSupport getOperationSupport() {
301308
if (operationSupport == null) {
302309
this.operationSupport = new OperationSupport(this);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubernetes.client.impl;
17+
18+
import io.fabric8.kubernetes.client.Config;
19+
import io.fabric8.kubernetes.client.ConfigBuilder;
20+
import io.fabric8.kubernetes.client.KubernetesClient;
21+
import io.fabric8.kubernetes.client.http.HttpClient;
22+
import io.fabric8.kubernetes.client.http.HttpRequest;
23+
import io.fabric8.kubernetes.client.http.TestHttpResponse;
24+
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
28+
import org.mockito.ArgumentCaptor;
29+
import org.mockito.Mockito;
30+
31+
import java.io.IOException;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
import java.util.concurrent.CompletableFuture;
35+
36+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
37+
import static org.mockito.ArgumentMatchers.any;
38+
import static org.mockito.Mockito.verify;
39+
import static org.mockito.Mockito.when;
40+
41+
class APIVersionsTest {
42+
private HttpClient mockClient;
43+
private KubernetesClient kubernetesClient;
44+
private List<HttpRequest.Builder> builders;
45+
46+
@BeforeEach
47+
public void setUp() throws IOException {
48+
builders = new ArrayList<>();
49+
this.mockClient = Mockito.mock(HttpClient.class, Mockito.RETURNS_DEEP_STUBS);
50+
Config config = new ConfigBuilder().withMasterUrl("https://localhost:8443/").build();
51+
when(mockClient.sendAsync(any(), Mockito.eq(byte[].class)))
52+
.thenReturn(CompletableFuture.completedFuture(TestHttpResponse.from(200,
53+
"{\"kind\":\"Pod\", \"apiVersion\":\"v1\"}")));
54+
kubernetesClient = new KubernetesClientImpl(mockClient, config, () -> Runnable::run,
55+
new KubernetesSerialization());
56+
Mockito.when(mockClient.newHttpRequestBuilder()).thenAnswer(answer -> {
57+
HttpRequest.Builder result = Mockito.mock(HttpRequest.Builder.class, Mockito.RETURNS_SELF);
58+
builders.add(result);
59+
return result;
60+
});
61+
}
62+
63+
@AfterEach
64+
void tearDown() {
65+
kubernetesClient.close();
66+
kubernetesClient = null;
67+
}
68+
69+
@Test
70+
void getApiVersions() {
71+
// When
72+
kubernetesClient.getAPIVersions();
73+
// Then
74+
verify(mockClient).sendAsync(any(), any());
75+
ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
76+
verify(builders.get(0)).uri(stringCaptor.capture());
77+
assertThat(stringCaptor.getValue())
78+
.isEqualTo("https://localhost:8443/api");
79+
}
80+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubernetes;
17+
18+
import io.fabric8.kubernetes.api.model.APIVersions;
19+
import io.fabric8.kubernetes.api.model.ServerAddressByClientCIDR;
20+
import io.fabric8.kubernetes.client.KubernetesClient;
21+
import org.assertj.core.api.InstanceOfAssertFactories;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.Collections;
25+
26+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
27+
28+
class APIVersionsIT {
29+
KubernetesClient client;
30+
31+
@Test
32+
void testApiVersions() {
33+
// Given + When
34+
APIVersions apiVersions = client.getAPIVersions();
35+
// Then
36+
assertThat(apiVersions)
37+
.isNotNull()
38+
.hasFieldOrPropertyWithValue("versions", Collections.singletonList("v1"))
39+
.extracting(APIVersions::getServerAddressByClientCIDRs)
40+
.asInstanceOf(InstanceOfAssertFactories.list(ServerAddressByClientCIDR.class))
41+
.singleElement()
42+
.hasFieldOrPropertyWithValue("clientCIDR", "0.0.0.0/0")
43+
.hasFieldOrPropertyWithValue("serverAddress",
44+
String.format("%s:%d", client.getMasterUrl().getHost(), client.getMasterUrl().getPort()));
45+
}
46+
}

0 commit comments

Comments
 (0)