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

The following examples show how to use io.kubernetes.client.openapi.models.V1ObjectMeta#setName() . 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: EndpointsLock.java    From java with Apache License 2.0 6 votes vote down vote up
@Override
public boolean create(LeaderElectionRecord record) {
  try {
    V1Endpoints endpoints = new V1Endpoints();
    V1ObjectMeta objectMeta = new V1ObjectMeta();
    objectMeta.setName(name);
    objectMeta.setNamespace(namespace);
    Map<String, String> annotations = new HashMap<>();
    annotations.put(
        LeaderElectionRecordAnnotationKey,
        coreV1Client.getApiClient().getJSON().serialize(record));
    objectMeta.setAnnotations(annotations);
    endpoints.setMetadata(objectMeta);
    V1Endpoints createdendpoints =
        coreV1Client.createNamespacedEndpoints(namespace, endpoints, null, null, null);
    endpointsRefer.set(createdendpoints);
    return true;
  } catch (Throwable t) {
    log.error("failed to create leader election record as {}", t.getMessage());
    return false;
  }
}
 
Example 2
Source File: ConfigMapLock.java    From java with Apache License 2.0 6 votes vote down vote up
@Override
public boolean create(LeaderElectionRecord record) {
  try {
    V1ConfigMap configMap = new V1ConfigMap();
    V1ObjectMeta objectMeta = new V1ObjectMeta();
    objectMeta.setName(name);
    objectMeta.setNamespace(namespace);
    Map<String, String> annotations = new HashMap<>();
    annotations.put(
        LeaderElectionRecordAnnotationKey,
        coreV1Client.getApiClient().getJSON().serialize(record));
    objectMeta.setAnnotations(annotations);
    configMap.setMetadata(objectMeta);
    V1ConfigMap createdConfigMap =
        coreV1Client.createNamespacedConfigMap(namespace, configMap, null, null, null);
    configMapRefer.set(createdConfigMap);
    return true;
  } catch (Throwable t) {
    log.error("failed to create leader election record as {}", t.getMessage());
    return false;
  }
}
 
Example 3
Source File: K8sClient.java    From pravega with Apache License 2.0 6 votes vote down vote up
/**
 * Method used to create a namespace. This blocks until the namespace is created.
 * @param namespace Namespace to be created.
 * @return V1Namespace.
 */
@SneakyThrows(ApiException.class)
public V1Namespace createNamespace(final String namespace) {
    CoreV1Api api = new CoreV1Api();
    try {
        V1Namespace existing = api.readNamespace(namespace, PRETTY_PRINT, Boolean.FALSE, Boolean.FALSE);
        if (existing != null) {
            log.info("Namespace {} already exists, ignoring namespace create operation.", namespace);
            return existing;
        }
    } catch (ApiException ignore) {
        // ignore exception and proceed with Namespace creation.
    }

    V1Namespace body = new V1Namespace();
    // Set the required api version and kind of resource
    body.setApiVersion("v1");
    body.setKind("Namespace");

    // Setup the standard object metadata
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(namespace);
    body.setMetadata(meta);

    return api.createNamespace(body, PRETTY_PRINT, DRY_RUN, FIELD_MANAGER);
}
 
Example 4
Source File: CacheTest.java    From java with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters
public static Collection data() {

  V1Pod normalPod = new V1Pod();
  V1ObjectMeta normalPodMeta = new V1ObjectMeta();
  normalPodMeta.setName("foo");
  normalPodMeta.setNamespace("default");
  normalPod.setMetadata(normalPodMeta);

  V1Pod missingNamespacePod = new V1Pod();
  V1ObjectMeta missingNamespacePodMeta = new V1ObjectMeta();
  missingNamespacePodMeta.setName("foo");
  missingNamespacePodMeta.setNamespace(null);
  missingNamespacePod.setMetadata(missingNamespacePodMeta);

  V1Pod missingNamePod = new V1Pod();
  V1ObjectMeta missingNamePodMeta = new V1ObjectMeta();
  missingNamePodMeta.setName(null);
  missingNamePodMeta.setNamespace("default");
  missingNamePod.setMetadata(missingNamePodMeta);

  return Arrays.asList(
      new Object[][] {
        {normalPod, "io.kubernetes.client.openapi.models.V1Pod"},
        {missingNamespacePod, "io.kubernetes.client.openapi.models.V1Pod"},
        {missingNamePod, "io.kubernetes.client.openapi.models.V1Pod"},
        {null, "null"},
      });
}
 
Example 5
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 6
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 7
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 8
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 9
Source File: RequestObjectBuilder.java    From twister2 with Apache License 2.0 5 votes vote down vote up
/**
   * we initially used this method to create PersistentVolumes
   * we no longer use this method
   * it is just here in case we may need it for some reason at one point
   */
  public static V1PersistentVolume createPersistentVolumeObject(String pvName) {
    V1PersistentVolume pv = new V1PersistentVolume();
    pv.setApiVersion("v1");

    // set pv name
    V1ObjectMeta meta = new V1ObjectMeta();
    meta.setName(pvName);
    pv.setMetadata(meta);

//    double volumeSize = SchedulerContext.persistentVolumeTotal(config);
    V1PersistentVolumeSpec pvSpec = new V1PersistentVolumeSpec();
    HashMap<String, Quantity> capacity = new HashMap<>();
//    capacity.put("storage", new Quantity(volumeSize + "Gi"));
    pvSpec.setCapacity(capacity);

    String storageClass = KubernetesContext.persistentStorageClass(config);
    String accessMode = KubernetesContext.storageAccessMode(config);
//    String reclaimPolicy = KubernetesContext.storageReclaimPolicy(config);
    pvSpec.setStorageClassName(storageClass);
    pvSpec.setAccessModes(Arrays.asList(accessMode));
//    pvSpec.setPersistentVolumeReclaimPolicy(reclaimPolicy);
//    pvSpec.setMountOptions(Arrays.asList("hard", "nfsvers=4.1"));

    V1NFSVolumeSource nfsVolumeSource = new V1NFSVolumeSource();
    nfsVolumeSource.setServer(SchedulerContext.nfsServerAddress(config));
    nfsVolumeSource.setPath(SchedulerContext.nfsServerPath(config));
    pvSpec.setNfs(nfsVolumeSource);

    pv.setSpec(pvSpec);

    return pv;
  }
 
Example 10
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 11
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 12
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;
  }