new-integration-tests/src/test/java/oracle/weblogic/kubernetes/ItIstioDomainInPV.java
private void createDomainJob(String pvName,
                               String pvcName, String domainScriptCM, String namespace, V1Container jobContainer) {
    logger.info("Running Kubernetes job to create domain");
    Map<String, String> annotMap = new HashMap<String, String>();
    annotMap.put("sidecar.istio.io/inject", "false");

    V1Job jobBody = new V1Job()
        .metadata(
            new V1ObjectMeta()
                .name("create-domain-onpv-job-" + pvName) // name of the create domain job
                .namespace(namespace))
        .spec(new V1JobSpec()
            .backoffLimit(0) // try only once
            .template(new V1PodTemplateSpec()
                .metadata(new V1ObjectMeta()
                    .annotations(annotMap))
                .spec(new V1PodSpec()
                    .restartPolicy("Never")
                    .initContainers(Arrays.asList(new V1Container()
                        .name("fix-pvc-owner") // change the ownership of the pv to opc:opc
                        .image(image)
                        .addCommandItem("/bin/sh")
                        .addArgsItem("-c")
                        .addArgsItem("chown -R 1000:1000 /shared")
                        .volumeMounts(Arrays.asList(
                            new V1VolumeMount()
                                .name(pvName)
                                .mountPath("/shared")))
                        .securityContext(new V1SecurityContext()
                            .runAsGroup(0L)
                            .runAsUser(0L))))
                    .containers(Arrays.asList(jobContainer  // container containing WLST or WDT details
                        .name("create-weblogic-domain-onpv-container")
                        .image(image)
                        .imagePullPolicy("Always")
                        .ports(Arrays.asList(new V1ContainerPort()
                            .containerPort(7001)))
                        .volumeMounts(Arrays.asList(
                            new V1VolumeMount()
                                .name("create-weblogic-domain-job-cm-volume") // domain creation scripts volume
                                .mountPath("/u01/weblogic"), // availble under /u01/weblogic inside pod
                            new V1VolumeMount()
                                .name(pvName) // location to write domain
                                .mountPath("/shared"))))) // mounted under /shared inside pod
                    .volumes(Arrays.asList(
                        new V1Volume()
                            .name(pvName)
                            .persistentVolumeClaim(
                                new V1PersistentVolumeClaimVolumeSource()
                                    .claimName(pvcName)),
                        new V1Volume()
                            .name("create-weblogic-domain-job-cm-volume")
                            .configMap(
                                new V1ConfigMapVolumeSource()
                                    .name(domainScriptCM)))) //config map containing domain scripts
                    .imagePullSecrets(isUseSecret ? Arrays.asList(
                        new V1LocalObjectReference()
                            .name(OCR_SECRET_NAME))
                        : null))));
    String jobName = assertDoesNotThrow(()
        -> createNamespacedJob(jobBody), "Failed to create Job");

    logger.info("Checking if the domain creation job {0} completed in namespace {1}",
        jobName, namespace);
    withStandardRetryPolicy
        .conditionEvaluationListener(
            condition -> logger.info("Waiting for job {0} to be completed in namespace {1} "
                    + "(elapsed time {2} ms, remaining time {3} ms)",
                jobName,
                namespace,
                condition.getElapsedTimeInMS(),
                condition.getRemainingTimeInMS()))
        .until(jobCompleted(jobName, null, namespace));

    // check job status and fail test if the job failed to create domain
    V1Job job = assertDoesNotThrow(() -> getJob(jobName, namespace),
        "Getting the job failed");
    if (job != null) {
      V1JobCondition jobCondition = job.getStatus().getConditions().stream().filter(
          v1JobCondition -> "Failed".equalsIgnoreCase(v1JobCondition.getType()))
          .findAny()
          .orElse(null);
      if (jobCondition != null) {
        logger.severe("Job {0} failed to create domain", jobName);
        List<V1Pod> pods = assertDoesNotThrow(()
            -> listPods(namespace, "job-name=" + jobName).getItems(),
            "Listing pods failed");
        if (!pods.isEmpty()) {
          String podLog = assertDoesNotThrow(() -> getPodLog(pods.get(0).getMetadata().getName(), namespace),
              "Failed to get pod log");
          logger.severe(podLog);
          fail("Domain create job failed");
        }
      }
    }

  }
/**
   * Create a job to create a domain in persistent volume.
   *
   * @param pvName         name of the persistent volume to create domain in
   * @param pvcName        name of the persistent volume claim
   * @param domainScriptCM configmap holding domain creation script files
   * @param namespace      name of the domain namespace in which the job is created
   * @param jobContainer   V1Container with job commands to create domain
   */
Create a job to create a domain in persistent volume.
Create a job to create a domain in persistent volume .
private void createDomainJob(String pvName,
                               String pvcName, String domainScriptCM, String namespace, V1Container jobContainer) {
    logger.info("Running Kubernetes job to create domain");
    Map<String, String> annotMap = new HashMap<String, String>();
    annotMap.put("sidecar.istio.io/inject", "false");
    V1Job jobBody = new V1Job()
        .metadata(
            new V1ObjectMeta()
                .name("create-domain-onpv-job-" + pvName) 
                .namespace(namespace))
        .spec(new V1JobSpec()
            .backoffLimit(0) 
            .template(new V1PodTemplateSpec()
                .metadata(new V1ObjectMeta()
                    .annotations(annotMap))
                .spec(new V1PodSpec()
                    .restartPolicy("Never")
                    .initContainers(Arrays.asList(new V1Container()
                        .name("fix-pvc-owner") 
                        .image(image)
                        .addCommandItem("/bin/sh")
                        .addArgsItem("-c")
                        .addArgsItem("chown -R 1000:1000 /shared")
                        .volumeMounts(Arrays.asList(
                            new V1VolumeMount()
                                .name(pvName)
                                .mountPath("/shared")))
                        .securityContext(new V1SecurityContext()
                            .runAsGroup(0L)
                            .runAsUser(0L))))
                    .containers(Arrays.asList(jobContainer  
                        .name("create-weblogic-domain-onpv-container")
                        .image(image)
                        .imagePullPolicy("Always")
                        .ports(Arrays.asList(new V1ContainerPort()
                            .containerPort(7001)))
                        .volumeMounts(Arrays.asList(
                            new V1VolumeMount()
                                .name("create-weblogic-domain-job-cm-volume") 
                                .mountPath("/u01/weblogic"), 
                            new V1VolumeMount()
                                .name(pvName) 
                                .mountPath("/shared"))))) 
                    .volumes(Arrays.asList(
                        new V1Volume()
                            .name(pvName)
                            .persistentVolumeClaim(
                                new V1PersistentVolumeClaimVolumeSource()
                                    .claimName(pvcName)),
                        new V1Volume()
                            .name("create-weblogic-domain-job-cm-volume")
                            .configMap(
                                new V1ConfigMapVolumeSource()
                                    .name(domainScriptCM)))) 
                    .imagePullSecrets(isUseSecret ? Arrays.asList(
                        new V1LocalObjectReference()
                            .name(OCR_SECRET_NAME))
                        : null))));
    String jobName = assertDoesNotThrow(()
        -> createNamespacedJob(jobBody), "Failed to create Job");
    logger.info("Checking if the domain creation job {0} completed in namespace {1}",
        jobName, namespace);
    withStandardRetryPolicy
        .conditionEvaluationListener(
            condition -> logger.info("Waiting for job {0} to be completed in namespace {1} "
                    + "(elapsed time {2} ms, remaining time {3} ms)",
                jobName,
                namespace,
                condition.getElapsedTimeInMS(),
                condition.getRemainingTimeInMS()))
        .until(jobCompleted(jobName, null, namespace));
    V1Job job = assertDoesNotThrow(() -> getJob(jobName, namespace),
        "Getting the job failed");
    if (job != null) {
      V1JobCondition jobCondition = job.getStatus().getConditions().stream().filter(
          v1JobCondition -> "Failed".equalsIgnoreCase(v1JobCondition.getType()))
          .findAny()
          .orElse(null);
      if (jobCondition != null) {
        logger.severe("Job {0} failed to create domain", jobName);
        List<V1Pod> pods = assertDoesNotThrow(()
            -> listPods(namespace, "job-name=" + jobName).getItems(),
            "Listing pods failed");
        if (!pods.isEmpty()) {
          String podLog = assertDoesNotThrow(() -> getPodLog(pods.get(0).getMetadata().getName(), namespace),
              "Failed to get pod log");
          logger.severe(podLog);
          fail("Domain create job failed");
        }
      }
    }
  }
private void createDomainJob ( String pvName , String pvcName , String domainScriptCM , String namespace , V1Container jobContainer ) { logger . info ( "Running Kubernetes job to create domain" ) ; Map < String , String > annotMap = new HashMap < String , String > ( ) ; annotMap . put ( "sidecar.istio.io/inject" , "false" ) ; V1Job jobBody = new V1Job ( ) . metadata ( new V1ObjectMeta ( ) . name ( "create-domain-onpv-job-" + pvName ) . namespace ( namespace ) ) . spec ( new V1JobSpec ( ) . backoffLimit ( 0 ) . template ( new V1PodTemplateSpec ( ) . metadata ( new V1ObjectMeta ( ) . annotations ( annotMap ) ) . spec ( new V1PodSpec ( ) . restartPolicy ( "Never" ) . initContainers ( Arrays . asList ( new V1Container ( ) . name ( "fix-pvc-owner" ) . image ( image ) . addCommandItem ( "/bin/sh" ) . addArgsItem ( "-c" ) . addArgsItem ( "chown -R 1000:1000 /shared" ) . volumeMounts ( Arrays . asList ( new V1VolumeMount ( ) . name ( pvName ) . mountPath ( "/shared" ) ) ) . securityContext ( new V1SecurityContext ( ) . runAsGroup ( 0L ) . runAsUser ( 0L ) ) ) ) . containers ( Arrays . asList ( jobContainer . name ( "create-weblogic-domain-onpv-container" ) . image ( image ) . imagePullPolicy ( "Always" ) . ports ( Arrays . asList ( new V1ContainerPort ( ) . containerPort ( 7001 ) ) ) . volumeMounts ( Arrays . asList ( new V1VolumeMount ( ) . name ( "create-weblogic-domain-job-cm-volume" ) . mountPath ( "/u01/weblogic" ) , new V1VolumeMount ( ) . name ( pvName ) . mountPath ( "/shared" ) ) ) ) ) . volumes ( Arrays . asList ( new V1Volume ( ) . name ( pvName ) . persistentVolumeClaim ( new V1PersistentVolumeClaimVolumeSource ( ) . claimName ( pvcName ) ) , new V1Volume ( ) . name ( "create-weblogic-domain-job-cm-volume" ) . configMap ( new V1ConfigMapVolumeSource ( ) . name ( domainScriptCM ) ) ) ) . imagePullSecrets ( isUseSecret ? Arrays . asList ( new V1LocalObjectReference ( ) . name ( OCR_SECRET_NAME ) ) : null ) ) ) ) ; String jobName = assertDoesNotThrow ( ( ) -> createNamespacedJob ( jobBody ) , "Failed to create Job" ) ; logger . info ( "Checking if the domain creation job {0} completed in namespace {1}" , jobName , namespace ) ; withStandardRetryPolicy . conditionEvaluationListener ( condition -> logger . info ( "Waiting for job {0} to be completed in namespace {1} " + "(elapsed time {2} ms, remaining time {3} ms)" , jobName , namespace , condition . getElapsedTimeInMS ( ) , condition . getRemainingTimeInMS ( ) ) ) . until ( jobCompleted ( jobName , null , namespace ) ) ; V1Job job = assertDoesNotThrow ( ( ) -> getJob ( jobName , namespace ) , "Getting the job failed" ) ; if ( job != null ) { V1JobCondition jobCondition = job . getStatus ( ) . getConditions ( ) . stream ( ) . filter ( v1JobCondition -> "Failed" . equalsIgnoreCase ( v1JobCondition . getType ( ) ) ) . findAny ( ) . orElse ( null ) ; if ( jobCondition != null ) { logger . severe ( "Job {0} failed to create domain" , jobName ) ; List < V1Pod > pods = assertDoesNotThrow ( ( ) -> listPods ( namespace , "job-name=" + jobName ) . getItems ( ) , "Listing pods failed" ) ; if ( ! pods . isEmpty ( ) ) { String podLog = assertDoesNotThrow ( ( ) -> getPodLog ( pods . get ( 0 ) . getMetadata ( ) . getName ( ) , namespace ) , "Failed to get pod log" ) ; logger . severe ( podLog ) ; fail ( "Domain create job failed" ) ; } } } }
Create a job to create a domain in persistent volume.
Create a job to create a domain in persistent volume .
// name of the create domain job // try only once // change the ownership of the pv to opc:opc // container containing WLST or WDT details // domain creation scripts volume // availble under /u01/weblogic inside pod // location to write domain // mounted under /shared inside pod //config map containing domain scripts // check job status and fail test if the job failed to create domain