Skip to content

Commit

Permalink
initial protobuf client.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanburns committed Sep 9, 2017
1 parent b6a1928 commit dd3a485
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>client-java-util</artifactId>
<version>0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java-proto</artifactId>
<version>0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
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());
}
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<module>util</module>
<module>examples</module>
<module>kubernetes</module>
<module>proto</module>
</modules>

<scm>
Expand Down
2 changes: 1 addition & 1 deletion proto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.kubernetes</groupId>
<artifactId>client-java-proto</artifactId>
<version>0.1-SNAPSHOT</version>
<version>0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>client-java-proto</name>
<url>https://github.com/kubernetes-client/java</url>
Expand Down
10 changes: 10 additions & 0 deletions util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<artifactId>client-java-api</artifactId>
<version>0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java-proto</artifactId>
<version>0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down Expand Up @@ -59,6 +64,11 @@
<artifactId>system-rules</artifactId>
<version>1.16.1</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
112 changes: 112 additions & 0 deletions util/src/main/java/io/kubernetes/client/ProtoClient.java
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);
}
}

0 comments on commit dd3a485

Please sign in to comment.