-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6a1928
commit dd3a485
Showing
6 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
examples/src/main/java/io/kubernetes/client/examples/ProtoExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package io.kubernetes.client.examples; | ||
|
||
import io.kubernetes.client.ApiClient; | ||
import io.kubernetes.client.ApiException; | ||
import io.kubernetes.client.Configuration; | ||
import io.kubernetes.client.ProtoClient; | ||
import io.kubernetes.client.proto.V1.Pod; | ||
import io.kubernetes.client.proto.V1.PodList; | ||
import io.kubernetes.client.util.Config; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
/** | ||
* A simple example of how to use the Java API | ||
* | ||
* Easiest way to run this: | ||
* mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.ProtoExample" | ||
* | ||
* From inside $REPO_DIR/examples | ||
*/ | ||
public class ProtoExample { | ||
public static void main(String[] args) throws IOException, ApiException, InterruptedException { | ||
ApiClient client = Config.defaultClient(); | ||
Configuration.setDefaultApiClient(client); | ||
|
||
ProtoClient pc = new ProtoClient(client); | ||
PodList list = pc.list(PodList.newBuilder(), "/api/v1/namespaces/default/pods"); | ||
|
||
if (list.getItemsCount() > 0) { | ||
Pod p = list.getItems(0); | ||
System.out.println(p.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
util/src/main/java/io/kubernetes/client/ProtoClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package io.kubernetes.client; | ||
|
||
import io.kubernetes.client.ApiException; | ||
import io.kubernetes.client.Configuration; | ||
import io.kubernetes.client.proto.Runtime.Unknown; | ||
|
||
import com.google.common.io.ByteStreams; | ||
import com.google.protobuf.Message; | ||
import com.squareup.okhttp.Request; | ||
import com.squareup.okhttp.Response; | ||
import com.squareup.okhttp.ResponseBody; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class ProtoClient { | ||
private ApiClient apiClient; | ||
// Magic number for the beginning of proto encoded. | ||
// https://github.com/kubernetes/apimachinery/blob/master/pkg/runtime/serializer/protobuf/protobuf.go#L42 | ||
private static final byte[] MAGIC = new byte[] { 0x6b, 0x38, 0x73, 0x00 }; | ||
|
||
/** | ||
* Simple Protocol Budder API client constructor, uses default configuration | ||
*/ | ||
public ProtoClient() { | ||
this(Configuration.getDefaultApiClient()); | ||
} | ||
|
||
/** | ||
* ProtocolBuffer Client Constructor | ||
* @param apiClient The api client to use. | ||
*/ | ||
public ProtoClient(ApiClient apiClient) { | ||
this.apiClient = apiClient; | ||
} | ||
|
||
/** | ||
* Get the API client for these ProtocolBuffer operations. | ||
* @return The API client that will be used. | ||
*/ | ||
public ApiClient getApiClient() { | ||
return apiClient; | ||
} | ||
|
||
/** | ||
* Set the API client for subsequent ProtocolBuffer operations. | ||
* @param apiClient The new API client to use. | ||
*/ | ||
public void setApiClient(ApiClient apiClient) { | ||
this.apiClient = apiClient; | ||
} | ||
|
||
/** | ||
* Get a Kubernetes API object using protocol buffer encoding. | ||
* @param builder The appropriate Builder for the object receveived from the request. | ||
* @param path The URL path to call (e.g. /api/v1/namespaces/default/pods/pod-name) | ||
* @return A Message of type T | ||
*/ | ||
public <T extends Message> T get(T.Builder builder, String path) throws ApiException, IOException { | ||
return (T) request(builder, path, "GET"); | ||
} | ||
|
||
/** | ||
* List is fluent, semantic sugar method on top of get, which is intended | ||
* to convey that the object is a List of objects rather than a single object | ||
* @param builder The appropriate Builder for the object receveived from the request. | ||
* @param path The URL path to call (e.g. /api/v1/namespaces/default/pods/pod-name) | ||
* @return A Message of type T | ||
*/ | ||
public <T extends Message> T list(T.Builder listObj, String path) throws ApiException, IOException { | ||
return get(listObj, path); | ||
} | ||
|
||
/** | ||
* Generic protocol buffer based HTTP request. | ||
* Not intended for general consumption, but public for advance use cases. | ||
* @param builder The appropriate Builder for the object receveived from the request. | ||
* @param method The HTTP method (e.g. GET) for this request. | ||
* @param path The URL path to call (e.g. /api/v1/namespaces/default/pods/pod-name) | ||
* @return A Message of type T | ||
*/ | ||
public <T extends Message> T request(T.Builder builder, String path, String method) throws ApiException, IOException { | ||
HashMap<String, String> headers = new HashMap<String, String>(); | ||
headers.put("Content-type", "application/vnd.kubernetes.protobuf"); | ||
headers.put("Accept", "application/vnd.kubernetes.protobuf"); | ||
Request request = apiClient.buildRequest(path, method, new ArrayList<Pair>(), new ArrayList<Pair>(), null, | ||
headers, new HashMap<String, Object>(), new String[0], null); | ||
Response resp = apiClient.getHttpClient().newCall(request).execute(); | ||
Unknown u = parse(resp.body().byteStream()); | ||
return (T) builder.mergeFrom(u.getRaw()).build(); | ||
} | ||
|
||
private Unknown parse(InputStream stream) throws ApiException, IOException { | ||
// This isn't really documented anywhere except the code, but | ||
// the proto-buf format is: | ||
// * 4 byte magic number | ||
// * Protocol Buffer encoded object of type runtime.Unknown | ||
// * the 'raw' field in that object contains a Protocol Buffer | ||
// encoding of the actual object. | ||
// TODO: Document this somewhere proper. | ||
byte[] magic = new byte[4]; | ||
stream.read(magic); | ||
for (int i = 0; i < MAGIC.length; i++) { | ||
if (magic[i] != MAGIC[i]) { | ||
throw new ApiException("Unexpected magic number: " + magic); | ||
} | ||
} | ||
return Unknown.parseFrom(stream); | ||
} | ||
} |