Skip to content

Commit c8051a6

Browse files
authored
Merge pull request #1138 from brendandburns/kubectl
Implement Kubectl Log
2 parents 5062822 + 3988b5c commit c8051a6

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
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;
2224
import io.kubernetes.client.extended.kubectl.exception.KubectlException;
@@ -82,6 +84,12 @@ public static void main(String[] args)
8284
String name = null;
8385

8486
switch (verb) {
87+
case "log":
88+
name = args[1];
89+
ByteStreams.copy(
90+
log().name(name).namespace(ns).container(cli.getOptionValue("c", "")).execute(),
91+
System.out);
92+
System.exit(0);
8593
case "scale":
8694
kind = args[1];
8795
name = args[2];

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

+19
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
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)