Kubernetes-client: createOrReplace method doesn't support generateName for Jobs

Created on 22 Sep 2020  路  7Comments  路  Source: fabric8io/kubernetes-client

createOrReplace method throws java.lang.IllegalArgumentException: Name must be provided. when I try to create Job resource from YAML job definition file which has generateName property instead of name. Here is my YAML definition for a Job.

apiVersion: batch/v1
kind: Job
metadata:
 generateName: eventester
spec:
 template:
   metadata:
     name: eventester
   spec:
     containers:
     - name: eventester
       image: phx.ocir.io/ax022wvgmjpq/helloworld-oke-verifier:latest
       ports:
          - containerPort: 80
       command:
         - "/bin/bash"
         - "-c"
         - "if (( RANDOM % 2 )); then exit 0; exit 2;fi"
     restartPolicy: Never

Thanks,
Venky

bug

Most helpful comment

@rohanKanojia Thanks for the update, yes I could work with create() method instead of createOrReplace().

All 7 comments

Is this still happening in 4.11.1?

Yes, this is still an issue on 4.11.1. KubernetesClient requires name field to be provided, otherwise it throws error. We should try to generate a name when generateName is specified.

Upon investigation, it looks like KubernetesClient is behaving correctly. name field is required for createOrReplace(which is client's equivalent for kubectl apply). KubernetesClient works fine with plain create(). Even kubectl apply throws an error when generateName is used:

kubernetes-client : $ kubectl apply -f ~/work/k8-resource-yamls/test-pod.yml 
error: from ex: cannot use generate name with apply
kubernetes-client : $ kubectl create -f ~/work/k8-resource-yamls/test-pod.yml 
pod/exp79v4 created

@hegdevenky: I'm able to create a Job with generateName like this:

  @Test
  public void testCreateWithGenerateName() {
    // Given
    Job job = getJobBuilder().editMetadata()
      .withName(null)
      .withGenerateName("test-job-")
      .endMetadata().build();

    // When
    Job jobCreated = client.batch().jobs().inNamespace(session.getNamespace()).create(job);

    // Then
    assertNotNull(jobCreated);
    assertTrue(jobCreated.getMetadata().getName().contains("test-job-"));
    assertEquals("test-job-", jobCreated.getMetadata().getGenerateName());
    assertNotNull(jobCreated.getMetadata().getName());
    assertNotEquals("test-job-", jobCreated.getMetadata().getName());
    assertTrue(client.batch().jobs().inNamespace(session.getNamespace()).withName(jobCreated.getMetadata().getName()).delete());
  }

@hegdevenky: I've created a PR https://github.com/fabric8io/kubernetes-client/pull/2544 for an integration test which tests generateName for job creation(using create()). Could you please try using create() instead of createOrReplace() and provide feedback?

@rohanKanojia Thanks for the update, yes I could work with create() method instead of createOrReplace().

Was this page helpful?
0 / 5 - 0 ratings