Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example mock "kubectl get configmap xx -o json" #639

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@
*/
package io.kubernetes.client.examples;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.squareup.okhttp.Response;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.util.Config;
import java.io.FileReader;
import java.io.IOException;
Expand Down Expand Up @@ -61,4 +66,31 @@ public static void main(String[] args) throws IOException, ApiException, ClassNo

System.out.println(output);
}

/**
* a example to mock "kubectl get configName -o json"
*
* @return @throws ApiException,IOException
*/
public static void mockKubeJson() throws ApiException, IOException {

ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);

CoreV1Api api = new CoreV1Api();
com.squareup.okhttp.Call call =
api.readNamespacedConfigMapCall("configmap-name", "default", null, null, null, null, null);

Response response = call.execute();

if (!response.isSuccessful()) {
return;
}

String body = response.body().string();

JsonObject returnData = new JsonParser().parse(body).getAsJsonObject();
Copy link
Member

@yue9944882 yue9944882 Jul 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this work with the custom-type adapter? in the existing example, it's parsing by using the nested gson instance where we register the adapters?

    Object obj2 = client
            .getJSON()	

Copy link
Contributor

@brendandburns brendandburns Jul 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, I think that Gson adapters are injected via annotation so that they would work everywhere, e.g. https://github.com/kubernetes-client/java/blob/master/kubernetes/src/main/java/io/kubernetes/client/custom/Quantity.java#L14

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public JSON() {
gson = new GsonBuilder()
.registerTypeAdapter(Date.class, dateTypeAdapter)
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
.registerTypeAdapter(DateTime.class, dateTimeTypeAdapter)
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
.registerTypeAdapter(byte[].class, byteArrayTypeAdapter)
.create();
}

but here we're registering the adapters explicitly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's true for those built-ins we can't annotate...


System.out.println(returnData);
}
}