Skip to content

Commit 6ec0ebd

Browse files
committed
Add KubectlLog
1 parent 5062822 commit 6ec0ebd

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

examples/src/main/java/io/kubernetes/client/examples/KubectlExample.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414

1515
import static io.kubernetes.client.extended.kubectl.Kubectl.exec;
1616
import static io.kubernetes.client.extended.kubectl.Kubectl.label;
17+
import static io.kubernetes.client.extended.kubectl.Kubectl.log;
1718
import static io.kubernetes.client.extended.kubectl.Kubectl.scale;
1819
import static io.kubernetes.client.extended.kubectl.Kubectl.version;
1920

21+
import com.google.common.io.ByteStreams;
2022
import io.kubernetes.client.common.KubernetesObject;
2123
import io.kubernetes.client.extended.kubectl.KubectlExec;
24+
import io.kubernetes.client.extended.kubectl.KubectlLog;
2225
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
2326
import io.kubernetes.client.openapi.ApiClient;
2427
import io.kubernetes.client.openapi.models.V1Deployment;
@@ -82,6 +85,12 @@ public static void main(String[] args)
8285
String name = null;
8386

8487
switch (verb) {
88+
case "log":
89+
name = args[1];
90+
KubectlLog log = log();
91+
log.name(name).namespace(ns).container(cli.getOptionValue("c", "")).execute();
92+
ByteStreams.copy(log.stream(), System.out);
93+
System.exit(0);
8594
case "scale":
8695
kind = args[1];
8796
name = args[2];

extended/src/main/java/io/kubernetes/client/extended/kubectl/Kubectl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ public static KubectlExec exec() {
136136
return exec(Configuration.getDefaultApiClient());
137137
}
138138

139+
/**
140+
* Equivalent for `kubectl log`
141+
*
142+
* @param apiClient The api client instance
143+
* @return the kubectl log operator
144+
*/
145+
public static KubectlLog log(ApiClient apiClient) {
146+
return new KubectlLog(apiClient);
147+
}
148+
149+
/**
150+
* Equivalent for `kubectl log`
151+
*
152+
* @return the kubectl log operator
153+
*/
154+
public static KubectlLog log() {
155+
return log(Configuration.getDefaultApiClient());
156+
}
157+
139158
/**
140159
* Executable executes a kubectl helper.
141160
*
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.PodLogs;
16+
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
17+
import io.kubernetes.client.openapi.ApiClient;
18+
import io.kubernetes.client.openapi.ApiException;
19+
import io.kubernetes.client.openapi.models.V1ObjectMeta;
20+
import io.kubernetes.client.openapi.models.V1Pod;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
24+
public class KubectlLog extends Kubectl.ResourceAndContainerBuilder<V1Pod, KubectlLog>
25+
implements Kubectl.Executable<V1Pod> {
26+
private InputStream result;
27+
28+
KubectlLog(ApiClient client) {
29+
super(client, V1Pod.class);
30+
}
31+
32+
public InputStream stream() {
33+
return result;
34+
}
35+
36+
@Override
37+
public V1Pod execute() throws KubectlException {
38+
validate();
39+
V1Pod pod = new V1Pod().metadata(new V1ObjectMeta().name(name).namespace(namespace));
40+
41+
PodLogs logs = new PodLogs(apiClient);
42+
String ns = (this.namespace == null ? "default" : this.namespace);
43+
try {
44+
result = logs.streamNamespacedPodLog(this.name, ns, this.container);
45+
} catch (ApiException | IOException ex) {
46+
throw new KubectlException(ex);
47+
}
48+
return pod;
49+
}
50+
51+
private void validate() throws KubectlException {
52+
if (name == null || name.length() == 0) {
53+
throw new KubectlException("missing name!");
54+
}
55+
if (container == null || name.length() == 0) {
56+
throw new KubectlException("missing container!");
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)