|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client.extended.kubectl; |
| 14 | + |
| 15 | +import io.kubernetes.client.common.KubernetesListObject; |
| 16 | +import io.kubernetes.client.common.KubernetesObject; |
| 17 | +import io.kubernetes.client.extended.kubectl.exception.KubectlException; |
| 18 | +import io.kubernetes.client.openapi.ApiException; |
| 19 | +import io.kubernetes.client.util.generic.GenericKubernetesApi; |
| 20 | +import io.kubernetes.client.util.generic.options.ListOptions; |
| 21 | +import java.util.List; |
| 22 | +import org.apache.commons.lang.StringUtils; |
| 23 | + |
| 24 | +public class KubectlGet<ApiType extends KubernetesObject> |
| 25 | + extends Kubectl.ApiClientBuilder<KubectlGet<ApiType>> |
| 26 | + implements Kubectl.Executable<List<ApiType>> { |
| 27 | + |
| 28 | + private String namespace; |
| 29 | + private ListOptions listOptions; |
| 30 | + private Class<ApiType> apiTypeClass; |
| 31 | + private Class<? extends KubernetesListObject> apiTypeListClass; |
| 32 | + |
| 33 | + KubectlGet(Class<ApiType> apiTypeClass) { |
| 34 | + this.apiTypeClass = apiTypeClass; |
| 35 | + this.listOptions = new ListOptions(); |
| 36 | + } |
| 37 | + |
| 38 | + public KubectlGet<ApiType> apiListTypeClass( |
| 39 | + Class<? extends KubernetesListObject> apiTypeListClass) { |
| 40 | + this.apiTypeListClass = apiTypeListClass; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public KubectlGet<ApiType> options(ListOptions listOptions) { |
| 45 | + this.listOptions = listOptions; |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public KubectlGet<ApiType> namespace(String namespace) { |
| 50 | + this.namespace = namespace; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public KubectlGetSingle name(String name) { |
| 55 | + return new KubectlGetSingle(name); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public List<ApiType> execute() throws KubectlException { |
| 60 | + GenericKubernetesApi<ApiType, ? extends KubernetesListObject> api = |
| 61 | + apiTypeListClass == null |
| 62 | + ? getGenericApi(apiTypeClass) |
| 63 | + : getGenericApi(apiTypeClass, apiTypeListClass); |
| 64 | + try { |
| 65 | + if (isNamespaced()) { |
| 66 | + return (List<ApiType>) |
| 67 | + api.list(namespace, listOptions) |
| 68 | + .onFailure( |
| 69 | + errorStatus -> { |
| 70 | + throw new ApiException(errorStatus.toString()); |
| 71 | + }) |
| 72 | + .getObject() |
| 73 | + .getItems(); |
| 74 | + |
| 75 | + } else { |
| 76 | + return (List<ApiType>) |
| 77 | + api.list(listOptions) |
| 78 | + .onFailure( |
| 79 | + errorStatus -> { |
| 80 | + throw new ApiException(errorStatus.toString()); |
| 81 | + }) |
| 82 | + .getObject() |
| 83 | + .getItems(); |
| 84 | + } |
| 85 | + } catch (ApiException e) { |
| 86 | + throw new KubectlException(e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private boolean isNamespaced() { |
| 91 | + return !StringUtils.isEmpty(namespace); |
| 92 | + } |
| 93 | + |
| 94 | + public class KubectlGetSingle extends Kubectl.ResourceBuilder<ApiType, KubectlGetSingle> |
| 95 | + implements Kubectl.Executable<ApiType> { |
| 96 | + |
| 97 | + private KubectlGetSingle(String name) { |
| 98 | + super(KubectlGet.this.apiTypeClass); |
| 99 | + KubectlGetSingle.this.name = name; |
| 100 | + KubectlGetSingle.this.namespace = KubectlGet.this.namespace; |
| 101 | + KubectlGetSingle.this.apiClient = KubectlGet.this.apiClient; |
| 102 | + KubectlGetSingle.this.skipDiscovery = KubectlGet.this.skipDiscovery; |
| 103 | + } |
| 104 | + |
| 105 | + private boolean isNamespaced() { |
| 106 | + return !StringUtils.isEmpty(namespace); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public ApiType execute() throws KubectlException { |
| 111 | + GenericKubernetesApi<ApiType, ? extends KubernetesListObject> api = |
| 112 | + getGenericApi(KubectlGetSingle.this.apiTypeClass); |
| 113 | + try { |
| 114 | + if (isNamespaced()) { |
| 115 | + return api.get(KubectlGetSingle.this.namespace, KubectlGetSingle.this.name) |
| 116 | + .onFailure( |
| 117 | + errorStatus -> { |
| 118 | + throw new ApiException(errorStatus.toString()); |
| 119 | + }) |
| 120 | + .getObject(); |
| 121 | + |
| 122 | + } else { |
| 123 | + return api.get(KubectlGetSingle.this.name) |
| 124 | + .onFailure( |
| 125 | + errorStatus -> { |
| 126 | + throw new ApiException(errorStatus.toString()); |
| 127 | + }) |
| 128 | + .getObject(); |
| 129 | + } |
| 130 | + } catch (ApiException e) { |
| 131 | + throw new KubectlException(e); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments