X Tutup
Skip to content

Commit 8b5c359

Browse files
authored
Merge pull request kubernetes-client#1880 from dddddai/master
Tweak call for CoreV1 group properly
2 parents bedecd8 + 77622a0 commit 8b5c359

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
*/
1313
package io.kubernetes.client.extended.kubectl;
1414

15-
import static com.github.tomakehurst.wiremock.client.WireMock.*;
15+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
16+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
17+
import static com.github.tomakehurst.wiremock.client.WireMock.patch;
18+
import static com.github.tomakehurst.wiremock.client.WireMock.patchRequestedFor;
1619
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
17-
import static org.junit.Assert.*;
20+
import static org.junit.Assert.assertNotNull;
1821

1922
import com.github.tomakehurst.wiremock.junit.WireMockRule;
2023
import com.github.tomakehurst.wiremock.matching.EqualToPattern;

util/src/main/java/io/kubernetes/client/util/generic/GenericKubernetesApi.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,17 @@ private Call tweakCallForCoreV1Group(Call call) {
824824
return call;
825825
}
826826
HttpUrl url = call.request().url();
827-
HttpUrl tweakedUrl = url.newBuilder().removePathSegment(1).setPathSegment(0, "api").build();
827+
String basePath = customObjectsApi.getApiClient().getBasePath();
828+
// count segments in the basePath
829+
int offset = 0;
830+
for (int i = basePath.indexOf("://") + 3; i < basePath.length(); ++i) {
831+
if (basePath.charAt(i) == '/') {
832+
++offset;
833+
}
834+
}
835+
HttpUrl tweakedUrl =
836+
url.newBuilder().removePathSegment(offset + 1).setPathSegment(offset, "api").build();
837+
828838
return this.customObjectsApi
829839
.getApiClient()
830840
.getHttpClient()

util/src/test/java/io/kubernetes/client/util/generic/GenericKubernetesApiForCoreApiTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,36 @@ public void patchNamespacedPodReturningObject() {
169169
verify(1, patchRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo1")));
170170
}
171171

172+
@Test
173+
public void patchNamespacedPodWithApiPrefix() {
174+
V1Patch v1Patch = new V1Patch("{}");
175+
V1Pod foo1 =
176+
new V1Pod().kind("Pod").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
177+
// add api prefix
178+
String prefix = "/k8s/clusters/c-7q988";
179+
stubFor(
180+
patch(urlEqualTo(prefix + "/api/v1/namespaces/default/pods/foo1"))
181+
.withHeader("Content-Type", containing(V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH))
182+
.willReturn(aResponse().withStatus(200).withBody(new Gson().toJson(foo1))));
183+
184+
GenericKubernetesApi<V1Pod, V1PodList> rancherPodClient =
185+
new GenericKubernetesApi<>(
186+
V1Pod.class,
187+
V1PodList.class,
188+
"",
189+
"v1",
190+
"pods",
191+
new ClientBuilder().setBasePath("http://localhost:" + 8181 + prefix).build());
192+
KubernetesApiResponse<V1Pod> podPatchResp =
193+
rancherPodClient.patch(
194+
"default", "foo1", V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, v1Patch);
195+
196+
assertTrue(podPatchResp.isSuccess());
197+
assertEquals(foo1, podPatchResp.getObject());
198+
assertNull(podPatchResp.getStatus());
199+
verify(1, patchRequestedFor(urlPathEqualTo(prefix + "/api/v1/namespaces/default/pods/foo1")));
200+
}
201+
172202
@Test
173203
public void testReadTimeoutShouldThrowException() {
174204
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8181).build();

0 commit comments

Comments
 (0)
X Tutup