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

Feat: Dynamic kubernetes api for classless api requesting #1528

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
}
}
2 changes: 1 addition & 1 deletion examples/examples-release-12/pom.xml
Expand Up @@ -5,7 +5,7 @@
<groupId>io.kubernetes</groupId>
<artifactId>client-java-examples-parent</artifactId>
<version>11.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>client-java-examples-release-12</artifactId>
Expand Down
@@ -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();
}
}
@@ -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);
}
}