Skip to content

Commit f26111b

Browse files
ukclivecoxbrendanburns
authored andcommitted
fixes for #124
1 parent 1e3fb8a commit f26111b

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

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

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
package io.kubernetes.client;
22

3-
import io.kubernetes.client.ApiException;
4-
import io.kubernetes.client.Configuration;
5-
import io.kubernetes.client.models.V1ObjectMeta;
6-
import io.kubernetes.client.proto.Meta.Status;
7-
import io.kubernetes.client.proto.Runtime.TypeMeta;
8-
import io.kubernetes.client.proto.Runtime.Unknown;
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.HashMap;
98

109
import com.google.common.io.ByteStreams;
1110
import com.google.common.primitives.Bytes;
12-
import com.google.protobuf.Descriptors;
1311
import com.google.protobuf.Message;
1412
import com.squareup.okhttp.MediaType;
1513
import com.squareup.okhttp.Request;
1614
import com.squareup.okhttp.RequestBody;
1715
import com.squareup.okhttp.Response;
18-
import com.squareup.okhttp.ResponseBody;
19-
import okio.ByteString;
2016

21-
import java.io.IOException;
22-
import java.io.InputStream;
23-
import java.util.Arrays;
24-
import java.util.ArrayList;
25-
import java.util.HashMap;
17+
import io.kubernetes.client.proto.Meta.DeleteOptions;
18+
import io.kubernetes.client.proto.Meta.Status;
19+
import io.kubernetes.client.proto.Runtime.TypeMeta;
20+
import io.kubernetes.client.proto.Runtime.Unknown;
2621

2722
public class ProtoClient {
2823
/**
@@ -139,8 +134,42 @@ public <T extends Message> ObjectOrStatus<T> update(T obj, String path, String a
139134
* @param path The path to call in the API server
140135
* @return The response status
141136
*/
142-
public <T extends Message> Status delete(T.Builder builder, String path) throws ApiException, IOException {
143-
return request(builder, path, "DELETE", null, null, null).status;
137+
public <T extends Message> ObjectOrStatus<T> delete(T.Builder builder, String path) throws ApiException, IOException {
138+
return request(builder, path, "DELETE", null, null, null);
139+
}
140+
141+
/**
142+
* Delete a kubernetes API object using protocol buffer encoding.
143+
* @param builder The builder for the response
144+
* @param path The path to call in the API server
145+
* @param deleteOptions optional deleteOptions
146+
* @return The response status
147+
*/
148+
public <T extends Message> ObjectOrStatus<T> delete(T.Builder builder, String path,DeleteOptions deleteOptions) throws ApiException, IOException {
149+
if (deleteOptions == null) {
150+
return delete(builder,path);
151+
}
152+
else {
153+
HashMap<String, String> headers = new HashMap<String, String>();
154+
headers.put("Content-Type", MEDIA_TYPE);
155+
headers.put("Accept", MEDIA_TYPE);
156+
Request request = apiClient.buildRequest(path, "DELETE", new ArrayList<Pair>(), new ArrayList<Pair>(), null,
157+
headers, new HashMap<String, Object>(), new String[0], null);
158+
byte[] bytes = encode(deleteOptions, "v1", "DeleteOptions");
159+
request = request.newBuilder().delete(RequestBody.create(MediaType.parse(MEDIA_TYPE), bytes)).build();
160+
Response resp = apiClient.getHttpClient().newCall(request).execute();
161+
Unknown u = parse(resp.body().byteStream());
162+
resp.body().close();
163+
164+
if (u.getTypeMeta().getApiVersion().equals("v1") &&
165+
u.getTypeMeta().getKind().equals("Status")) {
166+
Status status = Status.newBuilder().mergeFrom(u.getRaw()).build();
167+
return new ObjectOrStatus(null, status);
168+
}
169+
170+
return new ObjectOrStatus((T) builder.mergeFrom(u.getRaw()).build(), null);
171+
}
172+
144173
}
145174

146175
/**
@@ -157,13 +186,20 @@ public <T extends Message> Status delete(T.Builder builder, String path) throws
157186
public <T extends Message> ObjectOrStatus<T> request(T.Builder builder, String path, String method, T body, String apiVersion,
158187
String kind) throws ApiException, IOException {
159188
HashMap<String, String> headers = new HashMap<String, String>();
160-
headers.put("Content-type", MEDIA_TYPE);
189+
headers.put("Content-Type", MEDIA_TYPE);
161190
headers.put("Accept", MEDIA_TYPE);
162191
Request request = apiClient.buildRequest(path, method, new ArrayList<Pair>(), new ArrayList<Pair>(), null,
163192
headers, new HashMap<String, Object>(), new String[0], null);
164193
if (body != null) {
165194
byte[] bytes = encode(body, apiVersion, kind);
166-
request = request.newBuilder().post(RequestBody.create(MediaType.parse(MEDIA_TYPE), bytes)).build();
195+
if ("POST".equals(method))
196+
request = request.newBuilder().post(RequestBody.create(MediaType.parse(MEDIA_TYPE), bytes)).build();
197+
else if ("PUT".equals(method))
198+
request = request.newBuilder().put(RequestBody.create(MediaType.parse(MEDIA_TYPE), bytes)).build();
199+
else if ("PATCH".equals(method))
200+
request = request.newBuilder().patch(RequestBody.create(MediaType.parse(MEDIA_TYPE), bytes)).build();
201+
else
202+
throw new ApiException("Unknown proto client API method: "+method);
167203
}
168204
Response resp = apiClient.getHttpClient().newCall(request).execute();
169205
Unknown u = parse(resp.body().byteStream());
@@ -203,4 +239,4 @@ private Unknown parse(InputStream stream) throws ApiException, IOException {
203239
}
204240
return Unknown.parseFrom(stream);
205241
}
206-
}
242+
}

0 commit comments

Comments
 (0)