Skip to content

Commit

Permalink
feat: dynamic kubernetes api for classless api requesting
Browse files Browse the repository at this point in the history
  • Loading branch information
yue9944882 committed Feb 18, 2021
1 parent 70259f3 commit 1f3f4b7
Show file tree
Hide file tree
Showing 8 changed files with 639 additions and 0 deletions.
@@ -0,0 +1,26 @@
package io.kubernetes.client.e2e.dynamic


import io.kubernetes.client.openapi.models.V1Namespace
import io.kubernetes.client.openapi.models.V1ObjectMeta
import io.kubernetes.client.util.ClientBuilder
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesApi
import io.kubernetes.client.util.generic.dynamic.Dynamics
import spock.lang.Specification

class DynamicApiTest extends Specification {
def "Create Namespace then Delete should work"() {
given:
def apiClient = ClientBuilder.defaultClient()
def dynamicApi = new DynamicKubernetesApi("", "v1", "namespaces", apiClient)
def namespaceFoo = new V1Namespace().metadata(new V1ObjectMeta().name("e2e-dynamic"))
when:
def createdNamespace = dynamicApi.create(Dynamics.newFromJson(apiClient.getJSON().serialize(namespaceFoo)))
then:
createdNamespace != null
when:
def deleted = dynamicApi.delete("e2e-dynamic").throwsApiException().getObject()
then:
deleted != null
}
}
@@ -0,0 +1,77 @@
/*
Copyright 2021 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.util.generic.dynamic;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import io.kubernetes.client.common.KubernetesListObject;
import io.kubernetes.client.common.KubernetesObject;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.util.generic.GenericKubernetesApi;

/**
* DynamicKubernetesApi can be used for reading and writing arbitrary resources without knowing it's
* java definitions.
*/
public class DynamicKubernetesApi
extends GenericKubernetesApi<DynamicKubernetesObject, DynamicKubernetesListObject> {

private final Gson gson;

/**
* Instantiates a new Dynamic kubernetes api.
*
* @param apiGroup the api group
* @param apiVersion the api version
* @param resourcePlural the resource plural
* @param apiClient the api client
*/
public DynamicKubernetesApi(
String apiGroup, String apiVersion, String resourcePlural, ApiClient apiClient) {
super(
DynamicKubernetesObject.class,
DynamicKubernetesListObject.class,
apiGroup,
apiVersion,
resourcePlural,
apiClient);
TypeAdapter<KubernetesObject> objAdapter =
apiClient.getJSON().getGson().getAdapter(KubernetesObject.class);
TypeAdapter<KubernetesListObject> objListAdapter =
apiClient.getJSON().getGson().getAdapter(KubernetesListObject.class);
if (!DynamicKubernetesTypeAdaptorFactory.GenericListObjectCreator.class.equals(
objListAdapter.getClass())
|| !DynamicKubernetesTypeAdaptorFactory.GenericObjectCreator.class.equals(
objAdapter.getClass())) {
apiClient
.getJSON()
.setGson(
apiClient
.getJSON()
.getGson()
.newBuilder()
.registerTypeAdapterFactory(new DynamicKubernetesTypeAdaptorFactory())
.create());
}
gson = apiClient.getJSON().getGson();
}

/**
* Gets gson instance.
*
* @return the gson
*/
public Gson getGson() {
return gson;
}
}
@@ -0,0 +1,96 @@
/*
Copyright 2021 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.util.generic.dynamic;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.kubernetes.client.common.KubernetesListObject;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.models.V1ListMeta;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* DynamicKubernetesListObject is a wrapper for {@link JsonObject} that fits the common kubernetes
* list type interface {@link KubernetesListObject}.
*/
public class DynamicKubernetesListObject implements KubernetesListObject {

public DynamicKubernetesListObject() {
this(new JsonObject());
}

public DynamicKubernetesListObject(JsonObject obj) {
this.raw = obj;
}

private final JsonObject raw;

@Override
public V1ListMeta getMetadata() {
return Configuration.getDefaultApiClient()
.getJSON()
.getGson()
.fromJson(this.raw.get("metadata"), V1ListMeta.class);
}

@Override
public String getApiVersion() {
return this.raw.get("apiVersion").getAsString();
}

@Override
public String getKind() {
return this.raw.get("kind").getAsString();
}

@Override
public List<DynamicKubernetesObject> getItems() {
List<DynamicKubernetesObject> list = new ArrayList<>();
for (JsonElement e : this.raw.get("items").getAsJsonArray()) {
list.add(new DynamicKubernetesObject(e.getAsJsonObject()));
}
return list;
}

public JsonObject getRaw() {
return raw;
}

public void setApiVersion(String apiVersion) {
this.raw.addProperty("apiVersion", apiVersion);
}

public void setKind(String kind) {
this.raw.addProperty("kind", kind);
}

public void setMetadata(V1ListMeta objectMeta) {
this.raw.add(
"metadata", Configuration.getDefaultApiClient().getJSON().getGson().toJsonTree(objectMeta));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DynamicKubernetesListObject that = (DynamicKubernetesListObject) o;
return Objects.equals(raw, that.raw);
}

@Override
public int hashCode() {
return Objects.hash(raw);
}
}
@@ -0,0 +1,84 @@
/*
Copyright 2021 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.util.generic.dynamic;

import com.google.gson.JsonObject;
import io.kubernetes.client.common.KubernetesObject;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import java.util.Objects;

/**
* DynamicKubernetesObject is a wrapper for {@link JsonObject} that fits the common kubernetes type
* interface {@link KubernetesObject}.
*/
public class DynamicKubernetesObject implements KubernetesObject {

public DynamicKubernetesObject() {
this(new JsonObject());
}

public DynamicKubernetesObject(JsonObject obj) {
this.raw = obj;
}

private final JsonObject raw;

@Override
public V1ObjectMeta getMetadata() {
return Configuration.getDefaultApiClient()
.getJSON()
.getGson()
.fromJson(this.raw.get("metadata"), V1ObjectMeta.class);
}

@Override
public String getApiVersion() {
return this.raw.get("apiVersion").getAsString();
}

@Override
public String getKind() {
return this.raw.get("kind").getAsString();
}

public JsonObject getRaw() {
return raw;
}

public void setApiVersion(String apiVersion) {
this.raw.addProperty("apiVersion", apiVersion);
}

public void setKind(String kind) {
this.raw.addProperty("kind", kind);
}

public void setMetadata(V1ObjectMeta objectMeta) {
this.raw.add(
"metadata", Configuration.getDefaultApiClient().getJSON().getGson().toJsonTree(objectMeta));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DynamicKubernetesObject that = (DynamicKubernetesObject) o;
return Objects.equals(raw, that.raw);
}

@Override
public int hashCode() {
return Objects.hash(raw);
}
}
@@ -0,0 +1,103 @@
/*
Copyright 2021 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.util.generic.dynamic;

import com.google.gson.Gson;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.kubernetes.client.common.KubernetesListObject;
import io.kubernetes.client.common.KubernetesObject;
import java.io.IOException;
import java.lang.reflect.Type;

public class DynamicKubernetesTypeAdaptorFactory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (shouldHandleAsSingleObject(typeToken)) {
return (TypeAdapter<T>) (new GenericObjectCreator(gson));
}
if (shouldHandleAsListObject(typeToken)) {
return (TypeAdapter<T>) (new GenericListObjectCreator(gson));
}
return gson.getDelegateAdapter(this, typeToken);
}

private boolean shouldHandleAsSingleObject(TypeToken typeToken) {
if (TypeToken.get(KubernetesObject.class).equals(typeToken)) {
return true;
}
return TypeToken.get(DynamicKubernetesObject.class).equals(typeToken);
}

private boolean shouldHandleAsListObject(TypeToken typeToken) {
if (TypeToken.get(KubernetesListObject.class).equals(typeToken)) {
return true;
}
return TypeToken.get(DynamicKubernetesListObject.class).equals(typeToken);
}

class GenericListObjectCreator extends TypeAdapter<DynamicKubernetesListObject>
implements InstanceCreator<DynamicKubernetesListObject> {

GenericListObjectCreator(Gson delegate) {
this.delegate = delegate;
}

private final Gson delegate;

@Override
public DynamicKubernetesListObject createInstance(Type type) {
return new DynamicKubernetesListObject();
}

@Override
public void write(JsonWriter jsonWriter, DynamicKubernetesListObject t) throws IOException {
jsonWriter.jsonValue(delegate.toJson(t.getRaw()));
}

@Override
public DynamicKubernetesListObject read(JsonReader jsonReader) throws IOException {
return new DynamicKubernetesListObject(delegate.fromJson(jsonReader, JsonObject.class));
}
}

class GenericObjectCreator extends TypeAdapter<DynamicKubernetesObject>
implements InstanceCreator<DynamicKubernetesObject> {

GenericObjectCreator(Gson delegate) {
this.delegate = delegate;
}

private final Gson delegate;

@Override
public DynamicKubernetesObject createInstance(Type type) {
return new DynamicKubernetesObject();
}

@Override
public void write(JsonWriter jsonWriter, DynamicKubernetesObject t) throws IOException {
jsonWriter.jsonValue(delegate.toJson(t.getRaw()));
}

@Override
public DynamicKubernetesObject read(JsonReader jsonReader) throws IOException {
return new DynamicKubernetesObject(delegate.fromJson(jsonReader, JsonObject.class));
}
}
}

0 comments on commit 1f3f4b7

Please sign in to comment.