Skip to content

Commit 570d76a

Browse files
authored
Merge pull request #544 from yue9944882/bugfix/intercept-patch-format-contenttype
Fixes patch format to support various patch formats
2 parents 2e43cbe + d303cbd commit 570d76a

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

kubernetes/src/main/java/io/kubernetes/client/ApiClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public class ApiClient {
7373

7474
private HttpLoggingInterceptor loggingInterceptor;
7575

76+
public static final String PATCH_FORMAT_JSON_PATCH = "application/json-patch+json";
77+
public static final String PATCH_FORMAT_JSON_MERGE_PATCH = "application/merge-patch+json";
78+
public static final String PATCH_FORMAT_STRATEGIC_MERGE_PATCH = "application/strategic-merge-patch+json";
79+
7680
/*
7781
* Constructor for ApiClient
7882
*/

util/src/main/java/io/kubernetes/client/util/ClientBuilder.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import static io.kubernetes.client.util.KubeConfig.KUBECONFIG;
2222
import static io.kubernetes.client.util.KubeConfig.KUBEDIR;
2323

24+
import com.google.common.base.Strings;
25+
import com.squareup.okhttp.*;
2426
import io.kubernetes.client.ApiClient;
2527
import io.kubernetes.client.util.credentials.AccessTokenAuthentication;
2628
import io.kubernetes.client.util.credentials.Authentication;
@@ -43,6 +45,7 @@ public class ClientBuilder {
4345
private String basePath = Config.DEFAULT_FALLBACK_HOST;
4446
private byte[] caCertBytes = null;
4547
private boolean verifyingSsl = true;
48+
private String overridePatchFormat;
4649
private Authentication authentication;
4750

4851
/**
@@ -272,6 +275,15 @@ public ClientBuilder setVerifyingSsl(boolean verifyingSsl) {
272275
return this;
273276
}
274277

278+
public String overridePatchFormat() {
279+
return overridePatchFormat;
280+
}
281+
282+
public ClientBuilder setOverridePatchFormat(String patchFormat) {
283+
this.overridePatchFormat = patchFormat;
284+
return this;
285+
}
286+
275287
public ApiClient build() {
276288
final ApiClient client = new ApiClient();
277289

@@ -301,6 +313,31 @@ public ApiClient build() {
301313
client.setSslCaCert(new ByteArrayInputStream(caCertBytes));
302314
}
303315

316+
if (!Strings.isNullOrEmpty(overridePatchFormat)) {
317+
client
318+
.getHttpClient()
319+
.interceptors()
320+
.add(
321+
new Interceptor() {
322+
@Override
323+
public Response intercept(Chain chain) throws IOException {
324+
Request request = chain.request();
325+
326+
if ("PATCH".equals(request.method())) {
327+
328+
Request newRequest =
329+
request
330+
.newBuilder()
331+
.patch(
332+
new ProxyContentTypeRequestBody(
333+
request.body(), overridePatchFormat))
334+
.build();
335+
return chain.proceed(newRequest);
336+
}
337+
return chain.proceed(request);
338+
}
339+
});
340+
}
304341
return client;
305342
}
306343
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** Alipay.com Inc. Copyright (c) 2004-2019 All Rights Reserved. */
2+
package io.kubernetes.client.util;
3+
4+
import com.google.common.base.Strings;
5+
import com.squareup.okhttp.MediaType;
6+
import com.squareup.okhttp.RequestBody;
7+
import java.io.IOException;
8+
import okio.BufferedSink;
9+
10+
public class ProxyContentTypeRequestBody extends RequestBody {
11+
12+
private String overridePatchFormat;
13+
private RequestBody delegateRequestBody;
14+
15+
public ProxyContentTypeRequestBody(RequestBody delegateRequestBody) {
16+
this.delegateRequestBody = delegateRequestBody;
17+
}
18+
19+
public ProxyContentTypeRequestBody(RequestBody delegateRequestBody, String patchFormat) {
20+
this(delegateRequestBody);
21+
this.overridePatchFormat = patchFormat;
22+
}
23+
24+
@Override
25+
public MediaType contentType() {
26+
if (!Strings.isNullOrEmpty(overridePatchFormat)) {
27+
return MediaType.parse(overridePatchFormat);
28+
}
29+
return delegateRequestBody.contentType();
30+
}
31+
32+
@Override
33+
public void writeTo(BufferedSink bufferedSink) throws IOException {
34+
delegateRequestBody.writeTo(bufferedSink);
35+
}
36+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.util;
14+
15+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
16+
17+
import com.github.tomakehurst.wiremock.client.WireMock;
18+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
19+
import com.squareup.okhttp.Call;
20+
import io.kubernetes.client.ApiClient;
21+
import io.kubernetes.client.ApiException;
22+
import java.io.IOException;
23+
import java.util.HashMap;
24+
import org.junit.Before;
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
28+
/** Tests for the ConfigBuilder helper class */
29+
public class ClientBuilderPatchTest {
30+
31+
private ApiClient client;
32+
private static final int PORT = 8089;
33+
34+
@Rule public WireMockRule wireMockRule = new WireMockRule(PORT);
35+
36+
@Before
37+
public void setup() throws IOException {
38+
client =
39+
new ClientBuilder()
40+
.setBasePath("http://localhost:" + PORT)
41+
.setOverridePatchFormat(ApiClient.PATCH_FORMAT_STRATEGIC_MERGE_PATCH)
42+
.build();
43+
}
44+
45+
@Test
46+
public void testOverridePatchFormatInterceptor() throws IOException, ApiException {
47+
stubFor(
48+
patch(urlPathEqualTo("/apis"))
49+
.willReturn(
50+
aResponse()
51+
.withStatus(200)
52+
.withHeader("Content-Type", ApiClient.PATCH_FORMAT_STRATEGIC_MERGE_PATCH)
53+
.withBody("{}")));
54+
Call call =
55+
client.buildCall(
56+
"/apis",
57+
"PATCH",
58+
null,
59+
null,
60+
null,
61+
new HashMap<>(),
62+
new HashMap<>(),
63+
new String[] {},
64+
null);
65+
client.execute(call);
66+
67+
WireMock.verify(
68+
1,
69+
patchRequestedFor(urlPathEqualTo("/apis"))
70+
.withHeader("Content-Type", equalTo(ApiClient.PATCH_FORMAT_STRATEGIC_MERGE_PATCH)));
71+
}
72+
}

util/src/test/java/io/kubernetes/client/util/ClientBuilderTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package io.kubernetes.client.util;
1414

15+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1516
import static org.hamcrest.core.Is.is;
1617
import static org.junit.Assert.assertEquals;
1718
import static org.junit.Assert.assertThat;
@@ -20,6 +21,7 @@
2021

2122
import com.google.common.io.Resources;
2223
import io.kubernetes.client.ApiClient;
24+
import io.kubernetes.client.ApiException;
2325
import io.kubernetes.client.util.credentials.Authentication;
2426
import java.io.File;
2527
import java.io.IOException;
@@ -162,4 +164,12 @@ public void testSslCertCaBad() throws Exception {
162164
.setCertificateAuthority(Files.readAllBytes(Paths.get(INVALID_SSL_CA_CERT_PATH)))
163165
.build();
164166
}
167+
168+
@Test
169+
public void testOverridePatchFormat() throws IOException, ApiException {
170+
final ApiClient client =
171+
new ClientBuilder()
172+
.setOverridePatchFormat(ApiClient.PATCH_FORMAT_STRATEGIC_MERGE_PATCH)
173+
.build();
174+
}
165175
}

0 commit comments

Comments
 (0)