Java Code Examples for io.kubernetes.client.openapi.models.V1ObjectMeta#setLabels()

The following examples show how to use io.kubernetes.client.openapi.models.V1ObjectMeta#setLabels() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobMasterRequestObject.java    From twister2 with Apache License 2.0 5 votes vote down vote up
/**
 * create StatefulSet object for a job
 */
public static V1StatefulSet createStatefulSetObject() {

  if (config == null) {
    LOG.severe("JobMasterRequestObject.init method has not been called.");
    return null;
  }

  V1StatefulSet statefulSet = new V1StatefulSet();
  String statefulSetName = KubernetesUtils.createJobMasterStatefulSetName(jobID);

  // set labels for the jm stateful set
  HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
  labels.put("t2-mss", jobID); // job master statefulset

  // construct metadata and set for jobID setting
  V1ObjectMeta meta = new V1ObjectMeta();
  meta.setName(statefulSetName);
  meta.setLabels(labels);
  statefulSet.setMetadata(meta);

  // construct JobSpec and set
  V1StatefulSetSpec setSpec = new V1StatefulSetSpec();
  setSpec.serviceName(KubernetesUtils.createJobMasterServiceName(jobID));
  setSpec.setReplicas(1);

  // add selector for the job
  V1LabelSelector selector = new V1LabelSelector();
  selector.putMatchLabelsItem("t2-mp", jobID);
  setSpec.setSelector(selector);

  // construct the pod template
  V1PodTemplateSpec template = constructPodTemplate();
  setSpec.setTemplate(template);

  statefulSet.setSpec(setSpec);

  return statefulSet;
}
 
Example 2
Source File: JobMasterRequestObject.java    From twister2 with Apache License 2.0 5 votes vote down vote up
/**
 * create regular service for job master
 */
public static V1Service createJobMasterServiceObject() {

  String serviceName = KubernetesUtils.createJobMasterServiceName(jobID);

  V1Service service = new V1Service();
  service.setKind("Service");
  service.setApiVersion("v1");

  // set labels for the jm service
  HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);

  // construct and set metadata
  V1ObjectMeta meta = new V1ObjectMeta();
  meta.setName(serviceName);
  meta.setLabels(labels);
  service.setMetadata(meta);

  // construct and set service spec
  V1ServiceSpec serviceSpec = new V1ServiceSpec();
  // set selector
  HashMap<String, String> selectors = new HashMap<String, String>();
  selectors.put("t2-mp", jobID);
  serviceSpec.setSelector(selectors);
  // set port
  V1ServicePort servicePort = new V1ServicePort();
  servicePort.setName("job-master-port");
  servicePort.setPort(JobMasterContext.jobMasterPort(config));
  servicePort.setTargetPort(new IntOrString(JobMasterContext.jobMasterPort(config)));
  servicePort.setProtocol("TCP");
  serviceSpec.setPorts(Arrays.asList(servicePort));

  service.setSpec(serviceSpec);

  return service;
}
 
Example 3
Source File: JobMasterRequestObject.java    From twister2 with Apache License 2.0 5 votes vote down vote up
/**
 * create headless service for job master
 */
public static V1Service createJobMasterHeadlessServiceObject() {

  String serviceName = KubernetesUtils.createJobMasterServiceName(jobID);

  V1Service service = new V1Service();
  service.setKind("Service");
  service.setApiVersion("v1");

  // set labels for the jm service
  HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);

  // construct and set metadata
  V1ObjectMeta meta = new V1ObjectMeta();
  meta.setName(serviceName);
  meta.setLabels(labels);
  service.setMetadata(meta);

  // construct and set service spec
  V1ServiceSpec serviceSpec = new V1ServiceSpec();
  serviceSpec.setClusterIP("None");

  // set selector
  HashMap<String, String> selectors = new HashMap<String, String>();
  selectors.put("t2-mp", jobID);
  serviceSpec.setSelector(selectors);

  service.setSpec(serviceSpec);

  return service;
}
 
Example 4
Source File: RequestObjectBuilder.java    From twister2 with Apache License 2.0 5 votes vote down vote up
public static V1Service createHeadlessServiceObject(String serviceName) {

    V1Service service = new V1Service();
    service.setKind("Service");
    service.setApiVersion("v1");

    // set labels for the worker services
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);

    // construct and set metadata
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(serviceName);
    meta.setLabels(labels);
    service.setMetadata(meta);

    // construct and set service spec
    V1ServiceSpec serviceSpec = new V1ServiceSpec();
    // ClusterIP needs to be None for headless service
    serviceSpec.setClusterIP("None");
    // set selector
    HashMap<String, String> selectors = new HashMap<String, String>();
    selectors.put("t2-wp", jobID);
    serviceSpec.setSelector(selectors);

    service.setSpec(serviceSpec);

    return service;
  }
 
Example 5
Source File: Jobs.java    From java with Apache License 2.0 4 votes vote down vote up
/**
 * Convert V1beta1CronJob object into V1Job object, based on kubectl code
 * https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/create/create_job.go
 *
 * @param cronJob cronJob object (required)
 * @param jobName cronJob name
 * @return V1Job object
 */
public static V1Job cronJobToJob(V1beta1CronJob cronJob, String jobName) {

  Map<String, String> annotations = new HashMap<>();
  Map<String, String> labels = new HashMap<>();
  V1JobSpec jobSpec = null;

  V1beta1CronJobSpec cronJobSpec = cronJob.getSpec();

  if (cronJobSpec != null && cronJobSpec.getJobTemplate() != null) {
    V1ObjectMeta metadata = cronJobSpec.getJobTemplate().getMetadata();
    if (metadata != null) {
      if (metadata.getAnnotations() != null) {
        annotations.putAll(metadata.getAnnotations());
      }
      if (metadata.getLabels() != null) {
        labels.putAll(metadata.getLabels());
      }
    }
    jobSpec = cronJobSpec.getJobTemplate().getSpec();
  }

  annotations.put("cronjob.kubernetes.io/instantiate", "manual");

  V1OwnerReference v1OwnerReference = new V1OwnerReference();
  v1OwnerReference.setKind("CronJob");
  v1OwnerReference.setName(cronJob.getMetadata().getName());
  v1OwnerReference.setBlockOwnerDeletion(true);
  v1OwnerReference.setController(true);
  v1OwnerReference.setUid(cronJob.getMetadata().getUid());
  v1OwnerReference.setApiVersion("batch/v1beta1");

  V1ObjectMeta jobMetadata = new V1ObjectMeta();
  jobMetadata.setName(jobName != null ? jobName : cronJob.getMetadata().getName() + "-manual");
  jobMetadata.setAnnotations(annotations);
  jobMetadata.setLabels(labels);
  jobMetadata.setOwnerReferences(Arrays.asList(v1OwnerReference));

  V1Job job = new V1Job();
  job.setKind("Job");
  job.setApiVersion("batch/v1");
  job.setMetadata(jobMetadata);
  job.setSpec(jobSpec);

  return job;
}
 
Example 6
Source File: JobMasterRequestObject.java    From twister2 with Apache License 2.0 4 votes vote down vote up
/**
 * construct pod template
 */
public static V1PodTemplateSpec constructPodTemplate() {

  V1PodTemplateSpec template = new V1PodTemplateSpec();
  V1ObjectMeta templateMetaData = new V1ObjectMeta();
  HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
  labels.put("t2-mp", jobID); // job master pod

  templateMetaData.setLabels(labels);
  template.setMetadata(templateMetaData);

  V1PodSpec podSpec = new V1PodSpec();
  podSpec.setTerminationGracePeriodSeconds(0L);

  ArrayList<V1Volume> volumes = new ArrayList<>();
  V1Volume memoryVolume = new V1Volume();
  memoryVolume.setName(KubernetesConstants.POD_MEMORY_VOLUME_NAME);
  V1EmptyDirVolumeSource volumeSource1 = new V1EmptyDirVolumeSource();
  volumeSource1.setMedium("Memory");
  memoryVolume.setEmptyDir(volumeSource1);
  volumes.add(memoryVolume);

  // a volatile disk based volume
  // create it if the requested disk space is positive
  if (JobMasterContext.volatileVolumeRequested(config)) {
    double vSize = JobMasterContext.volatileVolumeSize(config);
    V1Volume volatileVolume = RequestObjectBuilder.createVolatileVolume(vSize);
    volumes.add(volatileVolume);
  }

  if (JobMasterContext.persistentVolumeRequested(config)) {
    String claimName = jobID;
    V1Volume persistentVolume = RequestObjectBuilder.createPersistentVolume(claimName);
    volumes.add(persistentVolume);
  }

  podSpec.setVolumes(volumes);

  ArrayList<V1Container> containers = new ArrayList<V1Container>();
  containers.add(constructContainer());
  podSpec.setContainers(containers);

  template.setSpec(podSpec);
  return template;
}
 
Example 7
Source File: RequestObjectBuilder.java    From twister2 with Apache License 2.0 4 votes vote down vote up
/**
 * create StatefulSet object for a job
 */
public static V1StatefulSet createStatefulSetForWorkers(ComputeResource computeResource) {

  if (config == null) {
    LOG.severe("RequestObjectBuilder.init method has not been called.");
    return null;
  }

  String statefulSetName =
      KubernetesUtils.createWorkersStatefulSetName(jobID, computeResource.getIndex());

  V1StatefulSet statefulSet = new V1StatefulSet();

  // set labels for the worker stateful set
  HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
  labels.put("t2-wss", jobID); // worker statefulset

  // construct metadata and set for jobID setting
  V1ObjectMeta meta = new V1ObjectMeta();
  meta.setName(statefulSetName);
  meta.setLabels(labels);
  statefulSet.setMetadata(meta);

  // construct JobSpec and set
  V1StatefulSetSpec setSpec = new V1StatefulSetSpec();
  setSpec.serviceName(KubernetesUtils.createServiceName(jobID));
  // pods will be started in parallel
  // by default they are started sequentially
  setSpec.setPodManagementPolicy("Parallel");

  int numberOfPods = computeResource.getInstances();
  setSpec.setReplicas(numberOfPods);

  // add selector for the job
  V1LabelSelector selector = new V1LabelSelector();
  selector.putMatchLabelsItem("t2-wp", jobID);
  setSpec.setSelector(selector);

  // construct the pod template
  V1PodTemplateSpec template = constructPodTemplate(computeResource);

  setSpec.setTemplate(template);

  statefulSet.setSpec(setSpec);

  return statefulSet;
}
 
Example 8
Source File: RequestObjectBuilder.java    From twister2 with Apache License 2.0 4 votes vote down vote up
/**
 * construct pod template
 */
public static V1PodTemplateSpec constructPodTemplate(ComputeResource computeResource) {

  V1PodTemplateSpec template = new V1PodTemplateSpec();
  V1ObjectMeta templateMetaData = new V1ObjectMeta();
  HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
  labels.put("t2-wp", jobID); // worker pod

  templateMetaData.setLabels(labels);
  template.setMetadata(templateMetaData);

  V1PodSpec podSpec = new V1PodSpec();
  podSpec.setTerminationGracePeriodSeconds(0L);

  ArrayList<V1Volume> volumes = new ArrayList<>();
  V1Volume memoryVolume = new V1Volume();
  memoryVolume.setName(KubernetesConstants.POD_MEMORY_VOLUME_NAME);
  V1EmptyDirVolumeSource volumeSource1 = new V1EmptyDirVolumeSource();
  volumeSource1.setMedium("Memory");
  memoryVolume.setEmptyDir(volumeSource1);
  volumes.add(memoryVolume);

  // a volatile disk based volume
  // create it if the requested disk space is positive
  if (computeResource.getDiskGigaBytes() > 0) {
    double volumeSize = computeResource.getDiskGigaBytes() * computeResource.getWorkersPerPod();
    V1Volume volatileVolume = createVolatileVolume(volumeSize);
    volumes.add(volatileVolume);
  }

  if (SchedulerContext.persistentVolumeRequested(config)) {
    String claimName = jobID;
    V1Volume persistentVolume = createPersistentVolume(claimName);
    volumes.add(persistentVolume);
  }

  // if openmpi is used, we initialize a Secret volume on each pod
  if (SchedulerContext.useOpenMPI(config)) {
    String secretName = KubernetesContext.secretName(config);
    V1Volume secretVolume = createSecretVolume(secretName);
    volumes.add(secretVolume);
  }

  podSpec.setVolumes(volumes);

  int containersPerPod = computeResource.getWorkersPerPod();

  // if openmpi is used, we initialize only one container for each pod
  if (SchedulerContext.useOpenMPI(config)) {
    containersPerPod = 1;
  }

  ArrayList<V1Container> containers = new ArrayList<V1Container>();
  for (int i = 0; i < containersPerPod; i++) {
    containers.add(constructContainer(computeResource, i));
  }
  podSpec.setContainers(containers);

  if (computeResource.getIndex() == 0) {
    constructAffinity(podSpec);
  }

  template.setSpec(podSpec);

  return template;
}
 
Example 9
Source File: RequestObjectBuilder.java    From twister2 with Apache License 2.0 4 votes vote down vote up
/**
   * create service for NodePort
   * @return
   */
  public static V1Service createNodePortServiceObject() {

    String serviceName = KubernetesUtils.createServiceName(jobID);
    int workerPort = KubernetesContext.workerBasePort(config);
    int nodePort = KubernetesContext.serviceNodePort(config);
    String protocol = KubernetesContext.workerTransportProtocol(config);

    V1Service service = new V1Service();
    service.setKind("Service");
    service.setApiVersion("v1");

    // set labels for the worker services
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);

    // construct and set metadata
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(serviceName);
    meta.setLabels(labels);
    service.setMetadata(meta);

    // construct and set service spec
    V1ServiceSpec serviceSpec = new V1ServiceSpec();
    // ClusterIP needs to be None for headless service
    serviceSpec.setType("NodePort");
    // set selector
    HashMap<String, String> selectors = new HashMap<String, String>();
    selectors.put("t2-wp", jobID);
    serviceSpec.setSelector(selectors);

    ArrayList<V1ServicePort> ports = new ArrayList<V1ServicePort>();
    V1ServicePort servicePort = new V1ServicePort();
    servicePort.setPort(workerPort);
    servicePort.setProtocol(protocol);
//    servicePort.setTargetPort(new IntOrString("port11"));
    if (nodePort != 0) {
      servicePort.nodePort(nodePort);
    }
    ports.add(servicePort);
    serviceSpec.setPorts(ports);

    service.setSpec(serviceSpec);

    return service;
  }