'How to connect to Argo workflows using java client

I am trying to implement a simple action from the argo workflows example (https://github.com/argoproj/argo-workflows/blob/master/sdks/java/client/docs/WorkflowServiceApi.md), I can do it with the API, but I need to do it from a Java application. The Java SDK doesn't even seem to connect to the Argo API. But the UI is working.

    public static void main(String[] args) {

        ApiClient defaultClient = Configuration.getDefaultApiClient();
        String url = "http://xxx.xxx.xxx.xxx:2746";
        defaultClient.setBasePath(url);

        WorkflowServiceApi apiInstance = new WorkflowServiceApi(defaultClient);
        String namespace = "argo"; // String |
        String name = "hello-argo"; // String |
        String getOptionsResourceVersion = "argoproj.io/v1alpha1"; // String | resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.  Defaults to unset +optional
        String fields = ""; // String | Fields to be included or excluded in the response. e.g. \"spec,status.phase\", \"-status.nodes\".
        try {
            IoArgoprojWorkflowV1alpha1Workflow result = apiInstance.workflowServiceGetWorkflow(namespace, name, getOptionsResourceVersion, fields);
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling WorkflowServiceApi#workflowServiceGetWorkflow");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }
    }

Here is the error:

Exception when calling WorkflowServiceApi#workflowServiceGetWorkflow
Status code: 0
Reason: null
Response headers: null
io.argoproj.workflow.ApiException: java.io.IOException: unexpected end of stream on http://xxx.xxx.xxx.xxx:2746/...
    at io.argoproj.workflow.ApiClient.execute(ApiClient.java:930)
    at io.argoproj.workflow.apis.WorkflowServiceApi.workflowServiceGetWorkflowWithHttpInfo(WorkflowServiceApi.java:486)
    at io.argoproj.workflow.apis.WorkflowServiceApi.workflowServiceGetWorkflow(WorkflowServiceApi.java:463)
    at Argo.main(Argo.java:20)
Caused by: java.io.IOException: unexpected end of stream on http://xxx.xxx.xxx.xxx:2746/...
    at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:202)
    at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:106)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:79)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at io.argoproj.workflow.ApiClient$2.intercept(ApiClient.java:1267)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
    at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
    at io.argoproj.workflow.ApiClient.execute(ApiClient.java:926)
    ... 3 more
Caused by: java.io.EOFException: \n not found: limit=0 content=…
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:332)
    at okhttp3.internal.http1.HeadersReader.readLine(HeadersReader.kt:29)`enter code here`
    at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:178)
    ... 19 more


Solution 1:[1]

import io.argoproj.workflow.ApiClient;
import io.argoproj.workflow.ApiException;
import io.argoproj.workflow.Configuration;
import io.argoproj.workflow.models.*;
import io.argoproj.workflow.apis.WorkflowServiceApi;
import io.kubernetes.client.openapi.models.V1Container;
import io.kubernetes.client.openapi.models.V1ObjectMeta;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Argo {
    public static void main(String[] args) {
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        defaultClient.setVerifyingSsl(false);
        String url = "https://xxx.xxx.xxx.xxx:2746";
        defaultClient.setBasePath(url);

        WorkflowServiceApi apiInstance = new WorkflowServiceApi(defaultClient);
        String namespace = "argo"; // String |
        WorkflowCreateRequest body = new WorkflowCreateRequest();// WorkflowCreateRequest |

        CreateOptions options = new CreateOptions();

        Workflow workflow = new Workflow();
        workflow.setApiVersion("argoproj.io/v1alpha1");

        V1ObjectMeta meta = new V1ObjectMeta();
        meta.setNamespace("argo");
        meta.setName("hello-java-client");

        WorkflowSpec spec = new WorkflowSpec();

        spec.setEntrypoint("argosay");
        Template template = new Template();
        template.setName("argosay");
        V1Container v1Container = new V1Container();
        v1Container.setName("pod");
        v1Container.setImage("docker/whalesay:latest");
        v1Container.setCommand(List.of("cowsay"));
        v1Container.setArgs(List.of("test java client"));
        template.setContainer(v1Container);
        spec.setTemplates(List.of(template));

        workflow.setMetadata(meta);
        workflow.setSpec(spec);

        body.setNamespace("argo");
        body.setServerDryRun(false);
        body.setWorkflow(workflow);

        try {
            Workflow result = apiInstance.workflowServiceCreateWorkflow(namespace, body);
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling WorkflowServiceApi#workflowServiceCreateWorkflow");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }

Solution 2:[2]

Try some meaningful value of fields

String fields = "items.spec,items.status.phase";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xerkz
Solution 2 user2595855