Skip to content

Feat: Kubectl get implementation #1251

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

Merged
merged 1 commit into from
Sep 15, 2020
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.
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 @@ -20,7 +20,10 @@ class KubectlApplyTest extends Specification {
.metadata(new V1ObjectMeta().name("apply-foo")))
.execute()
expect:
appliedNamespace != null
Kubectl.get(V1Namespace.class)
.apiClient(apiClient)
.name("apply-foo")
.execute() != null
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class KubectlCreateTest extends Specification {
.metadata(new V1ObjectMeta().name("create-foo")))
.execute()
expect:
createdNamespace != null
Kubectl.get(V1Namespace.class)
.apiClient(apiClient)
.name("create-foo")
.execute() != null
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
* kubectl commands.
*/
public class Kubectl {

/** Equivalent for `kubectl get` */
public static <ApiType extends KubernetesObject> KubectlGet<ApiType> get(
Class<ApiType> apiTypeClass) {
return new KubectlGet<>(apiTypeClass);
}

/** Equivalent for `kubectl drain` */
public static KubectlDrain drain() {
return new KubectlDrain();
Expand Down Expand Up @@ -60,6 +67,7 @@ public static KubectlCreate create() {
public static KubectlApply apply() {
return new KubectlApply();
}

/**
* Equivalent for `kubectl top`
*
Expand Down Expand Up @@ -198,15 +206,38 @@ protected void refreshDiscovery() throws KubectlException {
}
}

protected GenericKubernetesApi<? extends KubernetesObject, KubernetesListObject> getGenericApi(
Class<? extends KubernetesObject> apiTypeClass) {
protected <ApiType extends KubernetesObject>
GenericKubernetesApi<ApiType, ? extends KubernetesListObject> getGenericApi(
Class<ApiType> apiTypeClass) throws KubectlException {

// load list type class dynamically from class-loader
String apiListTypeClassName = apiTypeClass.getName() + "List";
try {
Class<? extends KubernetesListObject> apiTypeListClass;
apiTypeListClass =
(Class<? extends KubernetesListObject>)
apiTypeClass.getClassLoader().loadClass(apiListTypeClassName);
return getGenericApi(apiTypeClass, apiTypeListClass);
} catch (ClassNotFoundException e) {
throw new KubectlException(
new StringBuilder()
.append("No such api list type class ")
.append(apiListTypeClassName)
.append(", consider explicitly load the class by apiListTypeClass()?")
.toString());
}
}

protected <ApiType extends KubernetesObject, ApiListType extends KubernetesListObject>
GenericKubernetesApi<ApiType, ApiListType> getGenericApi(
Class<ApiType> apiTypeClass, Class<ApiListType> apiListTypeClass) {
GroupVersionResource groupVersionResource =
ModelMapper.getGroupVersionResourceByClass(apiTypeClass);

GenericKubernetesApi<? extends KubernetesObject, KubernetesListObject> api =
GenericKubernetesApi<ApiType, ApiListType> api =
new GenericKubernetesApi<>(
apiTypeClass,
KubernetesListObject.class,
apiListTypeClass,
groupVersionResource.getGroup(),
groupVersionResource.getVersion(),
groupVersionResource.getResource(),
Expand Down Expand Up @@ -241,18 +272,7 @@ public T name(String name) {
}

protected GenericKubernetesApi<ApiType, KubernetesListObject> getGenericApi() {
GroupVersionResource groupVersionResource =
ModelMapper.getGroupVersionResourceByClass(apiTypeClass);

GenericKubernetesApi<ApiType, KubernetesListObject> api =
new GenericKubernetesApi<>(
apiTypeClass,
KubernetesListObject.class,
groupVersionResource.getGroup(),
groupVersionResource.getVersion(),
groupVersionResource.getResource(),
apiClient);
return api;
return getGenericApi(apiTypeClass, KubernetesListObject.class);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
Copyright 2020 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.extended.kubectl;

import io.kubernetes.client.common.KubernetesListObject;
import io.kubernetes.client.common.KubernetesObject;
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.util.generic.GenericKubernetesApi;
import io.kubernetes.client.util.generic.options.ListOptions;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public class KubectlGet<ApiType extends KubernetesObject>
extends Kubectl.ApiClientBuilder<KubectlGet<ApiType>>
implements Kubectl.Executable<List<ApiType>> {

private String namespace;
private ListOptions listOptions;
private Class<ApiType> apiTypeClass;
private Class<? extends KubernetesListObject> apiTypeListClass;

KubectlGet(Class<ApiType> apiTypeClass) {
this.apiTypeClass = apiTypeClass;
this.listOptions = new ListOptions();
}

public KubectlGet<ApiType> apiListTypeClass(
Class<? extends KubernetesListObject> apiTypeListClass) {
this.apiTypeListClass = apiTypeListClass;
return this;
}

public KubectlGet<ApiType> options(ListOptions listOptions) {
this.listOptions = listOptions;
return this;
}

public KubectlGet<ApiType> namespace(String namespace) {
this.namespace = namespace;
return this;
}

public KubectlGetSingle name(String name) {
return new KubectlGetSingle(name);
}

@Override
public List<ApiType> execute() throws KubectlException {
GenericKubernetesApi<ApiType, ? extends KubernetesListObject> api =
apiTypeListClass == null
? getGenericApi(apiTypeClass)
: getGenericApi(apiTypeClass, apiTypeListClass);
try {
if (isNamespaced()) {
return (List<ApiType>)
api.list(namespace, listOptions)
.onFailure(
errorStatus -> {
throw new ApiException(errorStatus.toString());
})
.getObject()
.getItems();

} else {
return (List<ApiType>)
api.list(listOptions)
.onFailure(
errorStatus -> {
throw new ApiException(errorStatus.toString());
})
.getObject()
.getItems();
}
} catch (ApiException e) {
throw new KubectlException(e);
}
}

private boolean isNamespaced() {
return !StringUtils.isEmpty(namespace);
}

public class KubectlGetSingle extends Kubectl.ResourceBuilder<ApiType, KubectlGetSingle>
implements Kubectl.Executable<ApiType> {

private KubectlGetSingle(String name) {
super(KubectlGet.this.apiTypeClass);
KubectlGetSingle.this.name = name;
KubectlGetSingle.this.namespace = KubectlGet.this.namespace;
KubectlGetSingle.this.apiClient = KubectlGet.this.apiClient;
KubectlGetSingle.this.skipDiscovery = KubectlGet.this.skipDiscovery;
}

private boolean isNamespaced() {
return !StringUtils.isEmpty(namespace);
}

@Override
public ApiType execute() throws KubectlException {
GenericKubernetesApi<ApiType, ? extends KubernetesListObject> api =
getGenericApi(KubectlGetSingle.this.apiTypeClass);
try {
if (isNamespaced()) {
return api.get(KubectlGetSingle.this.namespace, KubectlGetSingle.this.name)
.onFailure(
errorStatus -> {
throw new ApiException(errorStatus.toString());
})
.getObject();

} else {
return api.get(KubectlGetSingle.this.name)
.onFailure(
errorStatus -> {
throw new ApiException(errorStatus.toString());
})
.getObject();
}
} catch (ApiException e) {
throw new KubectlException(e);
}
}
}
}
Loading