Search in sources :

Example 1 with JobBuilder

use of io.fabric8.kubernetes.api.model.batch.JobBuilder in project elastest-torm by elastest.

the class K8sService method deployJob.

public JobResult deployJob(DockerContainer container, String namespace) throws Exception {
    final JobResult result = new JobResult();
    container.getCmd().get().forEach((command) -> {
        logger.debug("deployJob => Commands to execute: {}", command);
    });
    try {
        logger.info("Starting deploy of Job with name {} in namespace {}", container.getContainerName().get(), namespace);
        logger.info(String.join(",", container.getCmd().get()));
        Map<String, String> k8sJobLabels = container.getLabels().get();
        String containerNameWithoutUnderscore = container.getContainerName().get().replace("_", "-");
        k8sJobLabels.put(LABEL_JOB_NAME, containerNameWithoutUnderscore);
        k8sJobLabels.put(LABEL_COMPONENT, containerNameWithoutUnderscore);
        k8sJobLabels.put(LABEL_COMPONENT_TYPE, ElastestComponentType.JOB.value);
        final Job job = new JobBuilder(Boolean.FALSE).withApiVersion("batch/v1").withNewMetadata().withName(containerNameWithoutUnderscore).withLabels(k8sJobLabels).endMetadata().withNewSpec().withNewTemplate().withNewMetadata().withLabels(k8sJobLabels).endMetadata().withNewSpec().addNewContainer().withName(containerNameWithoutUnderscore).withImage(container.getImageId()).withArgs(container.getCmd().get()).withEnv(getEnvVarListFromStringList(container.getEnvs().get())).endContainer().withRestartPolicy("Never").endSpec().endTemplate().endSpec().build();
        logger.info("Creating Job: {} in namespace {}", job.getMetadata().getLabels().get(LABEL_JOB_NAME), namespace);
        client.batch().jobs().inNamespace(namespace).create(job);
        result.setResult(1);
        result.setJobName(job.getMetadata().getName());
        result.setPodName("");
        final CountDownLatch watchLatch = new CountDownLatch(1);
        try (final Watch ignored = client.pods().inNamespace(namespace).withLabel(LABEL_JOB_NAME, job.getMetadata().getName()).watch(new Watcher<Pod>() {

            @Override
            public void eventReceived(final Action action, Pod pod) {
                job.getMetadata().getLabels().forEach((label, value) -> {
                    logger.debug("Label: {}={}", label, value);
                });
                logger.debug("Job {} receives an event", job.getMetadata().getLabels().get(LABEL_JOB_NAME));
                logger.debug("Event received: {}", pod.getStatus().getPhase());
                logger.debug("Action: {}", action.toString());
                logger.debug("Enum Pending: {}", PodsStatusEnum.PENDING.toString());
                if (result.getPodName().isEmpty()) {
                    result.setPodName(pod.getMetadata().getName());
                }
                if (!(pod.getStatus().getPhase().equals(PodsStatusEnum.PENDING.toString())) && !(pod.getStatus().getPhase().equals(PodsStatusEnum.RUNNING.toString()))) {
                    logger.info("Pod executed with result: {}", pod.getStatus().getPhase());
                    result.setResult(pod.getStatus().getPhase().equals(PodsStatusEnum.SUCCEEDED.toString()) ? 0 : 1);
                    logger.info("Job {} is completed!", pod.getMetadata().getName());
                    watchLatch.countDown();
                }
            }

            @Override
            public void onClose(final KubernetesClientException e) {
            }
        })) {
            watchLatch.await();
        } catch (final KubernetesClientException e) {
            logger.error("Could not watch pod", e);
        }
    } catch (final KubernetesClientException e) {
        String msg = "Unable to create job";
        logger.error(msg, e);
        throw new Exception(msg, e);
    }
    return result;
}
Also used : VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) URL(java.net.URL) JobBuilder(io.fabric8.kubernetes.api.model.batch.JobBuilder) LoggerFactory(org.slf4j.LoggerFactory) ServicePort(io.fabric8.kubernetes.api.model.ServicePort) Watcher(io.fabric8.kubernetes.client.Watcher) SecurityContextBuilder(io.fabric8.kubernetes.api.model.SecurityContextBuilder) PreDestroy(javax.annotation.PreDestroy) Bind(com.spotify.docker.client.messages.HostConfig.Bind) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) JsonValue(com.fasterxml.jackson.annotation.JsonValue) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) HostPathVolumeSource(io.fabric8.kubernetes.api.model.HostPathVolumeSource) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) UtilTools(io.elastest.epm.client.utils.UtilTools) CapabilitiesBuilder(io.fabric8.kubernetes.api.model.CapabilitiesBuilder) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) SubjectBuilder(io.fabric8.kubernetes.api.model.rbac.SubjectBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DockerProject(io.elastest.epm.client.json.DockerProject) RoleRefBuilder(io.fabric8.kubernetes.api.model.rbac.RoleRefBuilder) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) DockerContainer(io.elastest.epm.client.DockerContainer) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StandardCharsets(java.nio.charset.StandardCharsets) DaemonSetBuilder(io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Namespace(io.fabric8.kubernetes.api.model.Namespace) PostConstruct(javax.annotation.PostConstruct) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) ServiceResource(io.fabric8.kubernetes.client.dsl.ServiceResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) HashMap(java.util.HashMap) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Watch(io.fabric8.kubernetes.client.Watch) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HostPathVolumeSourceBuilder(io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder) NodeAddress(io.fabric8.kubernetes.api.model.NodeAddress) DaemonSet(io.fabric8.kubernetes.api.model.apps.DaemonSet) Response(okhttp3.Response) Service(io.fabric8.kubernetes.api.model.Service) ExecListener(io.fabric8.kubernetes.client.dsl.ExecListener) Volume(io.fabric8.kubernetes.api.model.Volume) Job(io.fabric8.kubernetes.api.model.batch.Job) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) Capabilities(io.fabric8.kubernetes.api.model.Capabilities) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) File(java.io.File) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) TimeUnit(java.util.concurrent.TimeUnit) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) CharEncoding(org.apache.commons.codec.CharEncoding) PodList(io.fabric8.kubernetes.api.model.PodList) Paths(java.nio.file.Paths) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ClusterRoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder) Collections(java.util.Collections) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) InputStream(java.io.InputStream) Pod(io.fabric8.kubernetes.api.model.Pod) JobBuilder(io.fabric8.kubernetes.api.model.batch.JobBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) Watch(io.fabric8.kubernetes.client.Watch) Job(io.fabric8.kubernetes.api.model.batch.Job) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with JobBuilder

use of io.fabric8.kubernetes.api.model.batch.JobBuilder in project strimzi by strimzi.

the class BridgeClients method consumerStrimziBridge.

public Job consumerStrimziBridge() {
    Map<String, String> consumerLabels = new HashMap<>();
    consumerLabels.put("app", this.getConsumerName());
    consumerLabels.put(Constants.KAFKA_CLIENTS_LABEL_KEY, Constants.KAFKA_BRIDGE_CLIENTS_LABEL_VALUE);
    PodSpecBuilder podSpecBuilder = new PodSpecBuilder();
    if (Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET != null && !Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET.isEmpty()) {
        List<LocalObjectReference> imagePullSecrets = Collections.singletonList(new LocalObjectReference(Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET));
        podSpecBuilder.withImagePullSecrets(imagePullSecrets);
    }
    return new JobBuilder().withNewMetadata().withNamespace(this.getNamespaceName()).withLabels(consumerLabels).withName(this.getConsumerName()).endMetadata().withNewSpec().withBackoffLimit(0).withNewTemplate().withNewMetadata().withLabels(consumerLabels).endMetadata().withNewSpecLike(podSpecBuilder.build()).withRestartPolicy("OnFailure").addNewContainer().withName(this.getConsumerName()).withImagePullPolicy(Constants.IF_NOT_PRESENT_IMAGE_PULL_POLICY).withImage(Environment.TEST_HTTP_CONSUMER_IMAGE).addNewEnv().withName("HOSTNAME").withValue(this.getBootstrapAddress()).endEnv().addNewEnv().withName("PORT").withValue(Integer.toString(port)).endEnv().addNewEnv().withName("TOPIC").withValue(this.getTopicName()).endEnv().addNewEnv().withName("POLL_INTERVAL").withValue(Integer.toString(pollInterval)).endEnv().addNewEnv().withName("MESSAGE_COUNT").withValue(Integer.toString(this.getMessageCount())).endEnv().addNewEnv().withName("GROUP_ID").withValue(this.getConsumerGroup()).endEnv().endContainer().endSpec().endTemplate().endSpec().build();
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) HashMap(java.util.HashMap) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) JobBuilder(io.fabric8.kubernetes.api.model.batch.v1.JobBuilder)

Example 3 with JobBuilder

use of io.fabric8.kubernetes.api.model.batch.JobBuilder in project strimzi by strimzi.

the class KafkaClients method defaultProducerStrimzi.

public JobBuilder defaultProducerStrimzi() {
    if (producerName == null || producerName.isEmpty()) {
        throw new InvalidParameterException("Producer name is not set.");
    }
    Map<String, String> producerLabels = new HashMap<>();
    producerLabels.put("app", producerName);
    producerLabels.put(Constants.KAFKA_CLIENTS_LABEL_KEY, Constants.KAFKA_CLIENTS_LABEL_VALUE);
    PodSpecBuilder podSpecBuilder = new PodSpecBuilder();
    if (Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET != null && !Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET.isEmpty()) {
        List<LocalObjectReference> imagePullSecrets = Collections.singletonList(new LocalObjectReference(Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET));
        podSpecBuilder.withImagePullSecrets(imagePullSecrets);
    }
    return new JobBuilder().withNewMetadata().withNamespace(this.getNamespaceName()).withLabels(producerLabels).withName(producerName).endMetadata().withNewSpec().withBackoffLimit(0).withNewTemplate().withNewMetadata().withName(producerName).withNamespace(this.getNamespaceName()).withLabels(producerLabels).endMetadata().withNewSpecLike(podSpecBuilder.build()).withRestartPolicy("Never").withContainers().addNewContainer().withName(producerName).withImagePullPolicy(Constants.IF_NOT_PRESENT_IMAGE_PULL_POLICY).withImage(Environment.TEST_PRODUCER_IMAGE).addNewEnv().withName("BOOTSTRAP_SERVERS").withValue(this.getBootstrapAddress()).endEnv().addNewEnv().withName("TOPIC").withValue(this.getTopicName()).endEnv().addNewEnv().withName("DELAY_MS").withValue(String.valueOf(delayMs)).endEnv().addNewEnv().withName("LOG_LEVEL").withValue("DEBUG").endEnv().addNewEnv().withName("MESSAGE_COUNT").withValue(String.valueOf(messageCount)).endEnv().addNewEnv().withName("MESSAGE").withValue(message).endEnv().addNewEnv().withName("PRODUCER_ACKS").withValue("all").endEnv().addNewEnv().withName("ADDITIONAL_CONFIG").withValue(this.getAdditionalConfig()).endEnv().addNewEnv().withName("BLOCKING_PRODUCER").withValue("true").endEnv().endContainer().endSpec().endTemplate().endSpec();
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) InvalidParameterException(java.security.InvalidParameterException) HashMap(java.util.HashMap) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) JobBuilder(io.fabric8.kubernetes.api.model.batch.v1.JobBuilder)

Example 4 with JobBuilder

use of io.fabric8.kubernetes.api.model.batch.JobBuilder in project strimzi by strimzi.

the class KafkaTracingClients method kafkaStreamsWithTracing.

public Job kafkaStreamsWithTracing() {
    String kafkaStreamsName = "hello-world-streams";
    Map<String, String> kafkaStreamLabels = new HashMap<>();
    kafkaStreamLabels.put("app", kafkaStreamsName);
    PodSpecBuilder podSpecBuilder = new PodSpecBuilder();
    if (Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET != null && !Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET.isEmpty()) {
        List<LocalObjectReference> imagePullSecrets = Collections.singletonList(new LocalObjectReference(Environment.SYSTEM_TEST_STRIMZI_IMAGE_PULL_SECRET));
        podSpecBuilder.withImagePullSecrets(imagePullSecrets);
    }
    return new JobBuilder().withNewMetadata().withNamespace(ResourceManager.kubeClient().getNamespace()).withLabels(kafkaStreamLabels).withName(kafkaStreamsName).endMetadata().withNewSpec().withBackoffLimit(0).withNewTemplate().withNewMetadata().withLabels(kafkaStreamLabels).endMetadata().withNewSpecLike(podSpecBuilder.build()).withRestartPolicy("Never").withContainers().addNewContainer().withName(kafkaStreamsName).withImage(Environment.TEST_STREAMS_IMAGE).addNewEnv().withName("BOOTSTRAP_SERVERS").withValue(this.getBootstrapAddress()).endEnv().addNewEnv().withName("APPLICATION_ID").withValue(kafkaStreamsName).endEnv().addNewEnv().withName("SOURCE_TOPIC").withValue(this.getTopicName()).endEnv().addNewEnv().withName("TARGET_TOPIC").withValue(streamsTopicTargetName).endEnv().addNewEnv().withName("LOG_LEVEL").withValue("DEBUG").endEnv().addNewEnv().withName("JAEGER_SERVICE_NAME").withValue(jaegerServiceStreamsName).endEnv().addNewEnv().withName("JAEGER_AGENT_HOST").withValue(jaegerServerAgentName).endEnv().addNewEnv().withName("JAEGER_SAMPLER_TYPE").withValue(JAEGER_SAMPLER_TYPE).endEnv().addNewEnv().withName("JAEGER_SAMPLER_PARAM").withValue(JAEGER_SAMPLER_PARAM).endEnv().endContainer().endSpec().endTemplate().endSpec().build();
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) HashMap(java.util.HashMap) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) JobBuilder(io.fabric8.kubernetes.api.model.batch.v1.JobBuilder)

Example 5 with JobBuilder

use of io.fabric8.kubernetes.api.model.batch.JobBuilder in project hugegraph-computer by hugegraph.

the class ComputerJobDeployer method getJob.

private Job getJob(String crName, ObjectMeta meta, ComputerJobSpec spec, int instances, List<Container> containers) {
    List<Volume> volumes = spec.getVolumes();
    if (volumes == null) {
        volumes = new ArrayList<>();
    } else {
        volumes = Lists.newArrayList(volumes);
    }
    volumes.addAll(this.getConfigMapAndSecretVolumes(spec));
    String configMapName = KubeUtil.configMapName(crName);
    Volume configVolume = this.getComputerConfigVolume(configMapName);
    volumes.add(configVolume);
    // Support PodSpec template
    PodTemplateSpec podTemplateSpec = spec.getPodTemplateSpec();
    if (podTemplateSpec == null) {
        podTemplateSpec = new PodTemplateSpec();
    } else {
        podTemplateSpec = Serialization.clone(podTemplateSpec);
    }
    ObjectMeta metadata = podTemplateSpec.getMetadata();
    if (metadata == null) {
        metadata = new ObjectMeta();
    }
    metadata = new ObjectMetaBuilder(metadata).addToLabels(meta.getLabels()).addToAnnotations(meta.getAnnotations()).build();
    podTemplateSpec.setMetadata(metadata);
    PodSpec podSpec = podTemplateSpec.getSpec();
    if (podSpec == null) {
        podSpec = new PodSpec();
    }
    podSpec.setVolumes(volumes);
    podSpec.setContainers(containers);
    podSpec.setRestartPolicy(JOB_RESTART_POLICY);
    if (podSpec.getTerminationGracePeriodSeconds() == null) {
        podSpec.setTerminationGracePeriodSeconds(TERMINATION_GRACE_PERIOD);
    }
    if (CollectionUtils.isEmpty(podSpec.getImagePullSecrets())) {
        podSpec.setImagePullSecrets(spec.getPullSecrets());
    }
    if (CollectionUtils.isEmpty(podSpec.getTopologySpreadConstraints())) {
        // Pod topology spread constraints default by node
        LabelSelector labelSelector = new LabelSelector();
        labelSelector.setMatchLabels(meta.getLabels());
        TopologySpreadConstraint spreadConstraint = new TopologySpreadConstraint(labelSelector, MAX_SKEW, TOPOLOGY_KEY, SCHEDULE_ANYWAY);
        podSpec.setTopologySpreadConstraints(Lists.newArrayList(spreadConstraint));
    }
    podTemplateSpec.setSpec(podSpec);
    return new JobBuilder().withMetadata(meta).withNewSpec().withParallelism(instances).withCompletions(instances).withBackoffLimit(JOB_BACKOFF_LIMIT).withTemplate(podTemplateSpec).endSpec().build();
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Volume(io.fabric8.kubernetes.api.model.Volume) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) TopologySpreadConstraint(io.fabric8.kubernetes.api.model.TopologySpreadConstraint) JobBuilder(io.fabric8.kubernetes.api.model.batch.v1.JobBuilder) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder)

Aggregations

JobBuilder (io.fabric8.kubernetes.api.model.batch.v1.JobBuilder)15 HashMap (java.util.HashMap)13 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)12 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)12 InvalidParameterException (java.security.InvalidParameterException)4 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)2 Volume (io.fabric8.kubernetes.api.model.Volume)2 ParallelNamespaceTest (io.strimzi.systemtest.annotations.ParallelNamespaceTest)2 KafkaClients (io.strimzi.systemtest.kafkaclients.internalClients.KafkaClients)2 KafkaClientsBuilder (io.strimzi.systemtest.kafkaclients.internalClients.KafkaClientsBuilder)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)1 JsonValue (com.fasterxml.jackson.annotation.JsonValue)1 Bind (com.spotify.docker.client.messages.HostConfig.Bind)1 DockerContainer (io.elastest.epm.client.DockerContainer)1 DockerProject (io.elastest.epm.client.json.DockerProject)1 UtilTools (io.elastest.epm.client.utils.UtilTools)1 Capabilities (io.fabric8.kubernetes.api.model.Capabilities)1 CapabilitiesBuilder (io.fabric8.kubernetes.api.model.CapabilitiesBuilder)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1