|
| 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 static com.google.common.base.Strings.isNullOrEmpty; |
| 16 | + |
| 17 | +import io.kubernetes.client.PodLogs; |
| 18 | +import io.kubernetes.client.extended.kubectl.exception.KubectlException; |
| 19 | +import io.kubernetes.client.openapi.ApiClient; |
| 20 | +import io.kubernetes.client.openapi.ApiException; |
| 21 | +import io.kubernetes.client.openapi.models.V1Pod; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | + |
| 25 | +public class KubectlLog extends Kubectl.ResourceAndContainerBuilder<V1Pod, KubectlLog> |
| 26 | + implements Kubectl.Executable<InputStream> { |
| 27 | + private InputStream result; |
| 28 | + |
| 29 | + KubectlLog(ApiClient client) { |
| 30 | + super(client, V1Pod.class); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public InputStream execute() throws KubectlException { |
| 35 | + validate(); |
| 36 | + |
| 37 | + PodLogs logs = new PodLogs(apiClient); |
| 38 | + String ns = (this.namespace == null ? "default" : this.namespace); |
| 39 | + try { |
| 40 | + return logs.streamNamespacedPodLog(this.name, ns, this.container); |
| 41 | + } catch (ApiException | IOException ex) { |
| 42 | + throw new KubectlException(ex); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void validate() throws KubectlException { |
| 47 | + StringBuilder msg = new StringBuilder(); |
| 48 | + if (isNullOrEmpty(name)) { |
| 49 | + msg.append("missing name! "); |
| 50 | + } |
| 51 | + if (isNullOrEmpty(container)) { |
| 52 | + msg.append("missing container!"); |
| 53 | + } |
| 54 | + if (msg.length() > 0) { |
| 55 | + throw new KubectlException(msg.toString()); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments