Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1528 from yue9944882/feat/dynamic-kubernetes-api
Feat: Dynamic kubernetes api for classless api requesting
- Loading branch information
Showing
10 changed files
with
687 additions
and
1 deletion.
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 | ||
} | ||
} |
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-release-12/src/main/java/io/kubernetes/client/examples/DynamicClientExample.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 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.examples; | ||
|
||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
import io.kubernetes.client.openapi.ApiClient; | ||
import io.kubernetes.client.openapi.ApiException; | ||
import io.kubernetes.client.util.ClientBuilder; | ||
import io.kubernetes.client.util.KubeConfig; | ||
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesApi; | ||
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesObject; | ||
|
||
public class DynamicClientExample { | ||
|
||
public static void main(String[] args) throws IOException, ApiException { | ||
|
||
ApiClient apiClient = ClientBuilder.standard().build(); | ||
|
||
// retrieving the latest state of the default namespace | ||
DynamicKubernetesApi dynamicApi = new DynamicKubernetesApi("", "v1", "namespaces", apiClient); | ||
DynamicKubernetesObject defaultNamespace = dynamicApi.get("default") | ||
.throwsApiException() | ||
.getObject(); | ||
|
||
// attaching a "foo=bar" label to the default namespace | ||
defaultNamespace.setMetadata(defaultNamespace.getMetadata().putLabelsItem("foo", "bar")); | ||
DynamicKubernetesObject updatedDefaultNamespace = dynamicApi.update(defaultNamespace) | ||
.throwsApiException() | ||
.getObject(); | ||
|
||
System.out.println(updatedDefaultNamespace); | ||
|
||
apiClient.getHttpClient().connectionPool().evictAll(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
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,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; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
.../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,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); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
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,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); | ||
} | ||
} |
Oops, something went wrong.