|
| 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.KubernetesObject; |
| 16 | +import io.kubernetes.client.extended.kubectl.exception.KubectlException; |
| 17 | +import io.kubernetes.client.openapi.ApiException; |
| 18 | +import io.kubernetes.client.util.ModelMapper; |
| 19 | +import org.apache.commons.lang.StringUtils; |
| 20 | + |
| 21 | +public class KubectlDelete<ApiType extends KubernetesObject> |
| 22 | + extends Kubectl.ResourceBuilder<ApiType, KubectlDelete<ApiType>> |
| 23 | + implements Kubectl.Executable<ApiType> { |
| 24 | + |
| 25 | + KubectlDelete(Class<ApiType> apiTypeClass) { |
| 26 | + super(apiTypeClass); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public ApiType execute() throws KubectlException { |
| 31 | + verifyArguments(); |
| 32 | + refreshDiscovery(); |
| 33 | + |
| 34 | + if (isNamespaced(apiTypeClass)) { |
| 35 | + try { |
| 36 | + return getGenericApi() |
| 37 | + .delete(namespace, name) |
| 38 | + .onFailure( |
| 39 | + errorStatus -> { |
| 40 | + throw new ApiException(errorStatus.toString()); |
| 41 | + }) |
| 42 | + .getObject(); |
| 43 | + } catch (ApiException e) { |
| 44 | + throw new KubectlException(e); |
| 45 | + } |
| 46 | + } else { |
| 47 | + try { |
| 48 | + return getGenericApi() |
| 49 | + .delete(name) |
| 50 | + .onFailure( |
| 51 | + errorStatus -> { |
| 52 | + throw new ApiException(errorStatus.toString()); |
| 53 | + }) |
| 54 | + .getObject(); |
| 55 | + } catch (ApiException e) { |
| 56 | + throw new KubectlException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public boolean isNamespaced(Class<ApiType> apiTypeClass) { |
| 62 | + Boolean isNamespaced = ModelMapper.isNamespaced(apiTypeClass); |
| 63 | + if (isNamespaced == null) { // unknown |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + return isNamespaced || !StringUtils.isEmpty(namespace); |
| 68 | + } |
| 69 | + |
| 70 | + private void verifyArguments() throws KubectlException { |
| 71 | + if (null == name) { |
| 72 | + throw new KubectlException("missing name argument"); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments