【问题标题】:Fabric8 exec statuscode always 1000Fabric8 执行状态码始终为 1000
【发布时间】:2022-01-07 04:39:33
【问题描述】:

我想获取在带有 Fabric8 Java Kubernetes 客户端的容器中执行的命令的状态代码。

这是位于我的容器中的脚本:

echo Bye Bye
exit 1

当我使用 CLI 或 NodeJS 客户端运行脚本时,我可以获得输出状态代码

以下是从 fabric8 存储库中获取的示例:

package org.package;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
import okhttp3.Response;

public class OtherMain {

  public static void main(String[] args) throws InterruptedException {


    String podName = "my-pod";
    String namespace = "my-namespace";

    try (
        KubernetesClient client = new DefaultKubernetesClient();
        ExecWatch watch = newExecWatch(client, namespace, podName)) {
      Thread.sleep(10 * 1000L);
    }
  }

  private static ExecWatch newExecWatch(KubernetesClient client, String namespace, String podName) {
    return client.pods()
        .inNamespace(namespace)
        .withName(podName)
        .readingInput(System.in)
        .writingOutput(System.out)
        .writingError(System.err)
        .withTTY()
        .usingListener(new SimpleListener())
        .exec("sh", "test.sh");
  }

  private static class SimpleListener implements ExecListener {

    @Override
    public void onOpen(Response response) {
      System.out.println("The shell will remain open for 10 seconds.");
    }

    @Override
    public void onFailure(Throwable t, Response response) {
      System.err.println("shell barfed");
    }

    @Override
    public void onClose(int code, String reason) {
      System.out.println("The shell will now close.");
    }
  }
}

但是,在查看输出时,似乎一切正常。有没有办法获取输出状态码?

【问题讨论】:

标签: java kubernetes fabric8


【解决方案1】:

可以使用wri 方法:

可以使用 writingerrorchannel 并解析响应:

{
    "metadata": {},
    "status": "Failure",
    "message": "command terminated with non-zero exit code: exit status 1",
    "reason": "NonZeroExitCode",
    "details": {
        "causes": [
            {
                "reason": "ExitCode",
                "message": "1"
            }
        ]
    }
}

类型是:io.fabric8.kubernetes.api.model.Status

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多