Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamic kubernetes api for classless api requesting
- Loading branch information
1 parent
70259f3
commit 5a05a4a
Showing
8 changed files
with
614 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
e2e/src/test/groovy/io/kubernetes/client/e2e/dynamic/DynamicApiTest.groovy
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,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 | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
util/src/main/java/io/kubernetes/client/util/generic/dynamic/DynamicKubernetesApi.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,78 @@ | ||
/* | ||
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 (false | ||
|| !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; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
.../src/main/java/io/kubernetes/client/util/generic/dynamic/DynamicKubernetesListObject.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,83 @@ | ||
/* | ||
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; | ||
} | ||
|
||
@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); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
util/src/main/java/io/kubernetes/client/util/generic/dynamic/DynamicKubernetesObject.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,71 @@ | ||
/* | ||
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; | ||
} | ||
|
||
@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); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...n/java/io/kubernetes/client/util/generic/dynamic/DynamicKubernetesTypeAdaptorFactory.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,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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.