|
| 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.ApiClient; |
| 19 | +import io.kubernetes.client.util.generic.GenericKubernetesApi; |
| 20 | +import io.kubernetes.client.util.generic.KubernetesApiResponse; |
| 21 | +import io.kubernetes.client.util.labels.Labels; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import org.apache.commons.lang.StringUtils; |
| 25 | + |
| 26 | +class KubectlLabel<ApiType extends KubernetesObject> implements Kubectl.Executable<ApiType> { |
| 27 | + |
| 28 | + private final ApiClient apiClient; |
| 29 | + private final Class<ApiType> apiTypeClass; |
| 30 | + private final Map<String, String> addingLabels; |
| 31 | + |
| 32 | + private String apiGroup; |
| 33 | + private String apiVersion; |
| 34 | + private String resourceNamePlural; |
| 35 | + |
| 36 | + private String namespace; |
| 37 | + private String name; |
| 38 | + |
| 39 | + KubectlLabel(ApiClient apiClient, Class<ApiType> apiTypeClass) { |
| 40 | + this.addingLabels = new HashMap<>(); |
| 41 | + this.apiTypeClass = apiTypeClass; |
| 42 | + this.apiClient = apiClient; |
| 43 | + } |
| 44 | + |
| 45 | + public KubectlLabel<ApiType> apiGroup(String apiGroup) { |
| 46 | + this.apiGroup = apiGroup; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public KubectlLabel<ApiType> apiVersion(String apiVersion) { |
| 51 | + this.apiVersion = apiVersion; |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + public KubectlLabel<ApiType> resourceNamePlural(String resourceNamePlural) { |
| 56 | + this.resourceNamePlural = resourceNamePlural; |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + public KubectlLabel<ApiType> namespace(String namespace) { |
| 61 | + this.namespace = namespace; |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + public KubectlLabel<ApiType> name(String name) { |
| 66 | + this.name = name; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public KubectlLabel<ApiType> addLabel(String key, String value) { |
| 71 | + this.addingLabels.put(key, value); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + private boolean isNamespaced() { |
| 76 | + return !StringUtils.isEmpty(namespace); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public ApiType execute() throws KubectlException { |
| 81 | + verifyArguments(); |
| 82 | + GenericKubernetesApi<ApiType, KubernetesListObject> api = |
| 83 | + new GenericKubernetesApi<>( |
| 84 | + apiTypeClass, |
| 85 | + KubernetesListObject.class, |
| 86 | + apiGroup, |
| 87 | + apiVersion, |
| 88 | + resourceNamePlural, |
| 89 | + apiClient); |
| 90 | + |
| 91 | + try { |
| 92 | + final KubernetesApiResponse<ApiType> getResponse; |
| 93 | + if (isNamespaced()) { |
| 94 | + getResponse = api.get(namespace, name); |
| 95 | + } else { |
| 96 | + getResponse = api.get(name); |
| 97 | + } |
| 98 | + if (!getResponse.isSuccess()) { |
| 99 | + throw new KubectlException(getResponse.getStatus()); |
| 100 | + } |
| 101 | + ApiType obj = getResponse.getObject(); |
| 102 | + |
| 103 | + Labels.addLabels(obj, addingLabels); |
| 104 | + |
| 105 | + final KubernetesApiResponse<ApiType> updateResponse; |
| 106 | + updateResponse = api.update(obj); |
| 107 | + if (!updateResponse.isSuccess()) { |
| 108 | + throw new KubectlException(updateResponse.getStatus()); |
| 109 | + } |
| 110 | + return updateResponse.getObject(); |
| 111 | + } catch (Throwable t) { |
| 112 | + throw new KubectlException(t); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void verifyArguments() throws KubectlException { |
| 117 | + if (null == apiGroup) { |
| 118 | + throw new KubectlException("missing apiGroup argument"); |
| 119 | + } |
| 120 | + if (null == apiVersion) { |
| 121 | + throw new KubectlException("missing apiVersion argument"); |
| 122 | + } |
| 123 | + if (null == resourceNamePlural) { |
| 124 | + throw new KubectlException("missing resourceNamePlural argument"); |
| 125 | + } |
| 126 | + if (null == name) { |
| 127 | + throw new KubectlException("missing name argument"); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments