use of io.kubernetes.client.openapi.models.V1PodTemplateSpec in project twister2 by DSC-SPIDAL.
the class JobMasterRequestObject method constructPodTemplate.
/**
* construct pod template
*/
public static V1PodTemplateSpec constructPodTemplate() {
V1PodTemplateSpec template = new V1PodTemplateSpec();
V1ObjectMeta templateMetaData = new V1ObjectMeta();
HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
// job master pod
labels.put("t2-mp", jobID);
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);
// 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;
}
use of io.kubernetes.client.openapi.models.V1PodTemplateSpec in project twister2 by DSC-SPIDAL.
the class JobMasterRequestObject method createStatefulSetObject.
/**
* 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);
// job master statefulset
labels.put("t2-mss", jobID);
// 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;
}
use of io.kubernetes.client.openapi.models.V1PodTemplateSpec in project heron by twitter.
the class V1Controller method loadPodFromTemplate.
/**
* Initiates the process of locating and loading <code>Pod Template</code> from a <code>ConfigMap</code>.
* The loaded text is then parsed into a usable <code>Pod Template</code>.
* @param isExecutor Flag to indicate loading of <code>Pod Template</code> for <code>Executor</code>
* or <code>Manager</code>.
* @return A <code>Pod Template</code> which is loaded and parsed from a <code>ConfigMap</code>.
*/
@VisibleForTesting
protected V1PodTemplateSpec loadPodFromTemplate(boolean isExecutor) {
final Pair<String, String> podTemplateConfigMapName = getPodTemplateLocation(isExecutor);
// Default Pod Template.
if (podTemplateConfigMapName == null) {
LOG.log(Level.INFO, "Configuring cluster with the Default Pod Template");
return new V1PodTemplateSpec();
}
if (isPodTemplateDisabled) {
throw new TopologySubmissionException("Custom Pod Templates are disabled");
}
final String configMapName = podTemplateConfigMapName.first;
final String podTemplateName = podTemplateConfigMapName.second;
// Attempt to locate ConfigMap with provided Pod Template name.
try {
V1ConfigMap configMap = getConfigMap(configMapName);
if (configMap == null) {
throw new ApiException(String.format("K8s client unable to locate ConfigMap '%s'", configMapName));
}
final Map<String, String> configMapData = configMap.getData();
if (configMapData != null && configMapData.containsKey(podTemplateName)) {
// NullPointerException when Pod Template is empty.
V1PodTemplateSpec podTemplate = ((V1PodTemplate) Yaml.load(configMapData.get(podTemplateName))).getTemplate();
LOG.log(Level.INFO, String.format("Configuring cluster with the %s.%s Pod Template", configMapName, podTemplateName));
return podTemplate;
}
// Failure to locate Pod Template with provided name.
throw new ApiException(String.format("Failed to locate Pod Template '%s' in ConfigMap '%s'", podTemplateName, configMapName));
} catch (ApiException e) {
KubernetesUtils.logExceptionWithDetails(LOG, e.getMessage(), e);
throw new TopologySubmissionException(e.getMessage());
} catch (IOException | ClassCastException | NullPointerException e) {
final String message = String.format("Error parsing Pod Template '%s' in ConfigMap '%s'", podTemplateName, configMapName);
KubernetesUtils.logExceptionWithDetails(LOG, message, e);
throw new TopologySubmissionException(message);
}
}
use of io.kubernetes.client.openapi.models.V1PodTemplateSpec in project heron by twitter.
the class V1Controller method createStatefulSet.
/**
* Creates and configures the <code>StatefulSet</code> which the topology's <code>executor</code>s will run in.
* @param containerResource Passed down to configure the <code>executor</code> resource limits.
* @param numberOfInstances Used to configure the execution command and ports for the <code>executor</code>.
* @param isExecutor Flag used to configure components specific to <code>executor</code> and <code>manager</code>.
* @return A fully configured <code>StatefulSet</code> for the topology's <code>executors</code>.
*/
private V1StatefulSet createStatefulSet(Resource containerResource, int numberOfInstances, boolean isExecutor) {
final String topologyName = getTopologyName();
final Config runtimeConfiguration = getRuntimeConfiguration();
final List<V1Volume> volumes = new LinkedList<>();
final List<V1VolumeMount> volumeMounts = new LinkedList<>();
// Collect Persistent Volume Claim configurations from the CLI.
final Map<String, Map<KubernetesConstants.VolumeConfigKeys, String>> configsPVC = KubernetesContext.getVolumeClaimTemplates(getConfiguration(), isExecutor);
// Collect all Volume configurations from the CLI and generate Volumes and Volume Mounts.
createVolumeAndMountsPersistentVolumeClaimCLI(configsPVC, volumes, volumeMounts);
createVolumeAndMountsHostPathCLI(KubernetesContext.getVolumeHostPath(getConfiguration(), isExecutor), volumes, volumeMounts);
createVolumeAndMountsEmptyDirCLI(KubernetesContext.getVolumeEmptyDir(getConfiguration(), isExecutor), volumes, volumeMounts);
createVolumeAndMountsNFSCLI(KubernetesContext.getVolumeNFS(getConfiguration(), isExecutor), volumes, volumeMounts);
final V1StatefulSet statefulSet = new V1StatefulSet();
// Setup StatefulSet's metadata.
final V1ObjectMeta objectMeta = new V1ObjectMeta().name(getStatefulSetName(isExecutor)).labels(getPodLabels(topologyName));
statefulSet.setMetadata(objectMeta);
// Create the StatefulSet Spec.
// Reduce replica count by one for Executors and set to one for Manager.
final int replicasCount = isExecutor ? Runtime.numContainers(runtimeConfiguration).intValue() - 1 : 1;
final V1StatefulSetSpec statefulSetSpec = new V1StatefulSetSpec().serviceName(topologyName).replicas(replicasCount);
// Parallel pod management tells the StatefulSet controller to launch or terminate
// all Pods in parallel, and not to wait for Pods to become Running and Ready or completely
// terminated prior to launching or terminating another Pod.
statefulSetSpec.setPodManagementPolicy("Parallel");
// Add selector match labels "app=heron" and "topology=topology-name"
// so we know which pods to manage.
final V1LabelSelector selector = new V1LabelSelector().matchLabels(getPodMatchLabels(topologyName));
statefulSetSpec.setSelector(selector);
// Create a Pod Template.
final V1PodTemplateSpec podTemplateSpec = loadPodFromTemplate(isExecutor);
// Set up Pod Metadata.
final V1ObjectMeta templateMetaData = new V1ObjectMeta().labels(getPodLabels(topologyName));
Map<String, String> annotations = new HashMap<>();
annotations.putAll(getPodAnnotations());
annotations.putAll(getPrometheusAnnotations());
templateMetaData.setAnnotations(annotations);
podTemplateSpec.setMetadata(templateMetaData);
configurePodSpec(podTemplateSpec, containerResource, numberOfInstances, isExecutor, volumes, volumeMounts);
statefulSetSpec.setTemplate(podTemplateSpec);
statefulSet.setSpec(statefulSetSpec);
statefulSetSpec.setVolumeClaimTemplates(createPersistentVolumeClaims(configsPVC));
return statefulSet;
}
use of io.kubernetes.client.openapi.models.V1PodTemplateSpec in project java by kubernetes-client.
the class KubectlRolloutTest method testKubectlRolloutHistoryStatefulSetWithRevisionShouldWork.
@Test
public void testKubectlRolloutHistoryStatefulSetWithRevisionShouldWork() throws KubectlException, IOException {
wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(STATEFUL_SET))))));
wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(STATEFUL_SET_CONTROLLER_REVISION_LIST))))));
wireMockRule.stubFor(patch(urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(PATCHED_STATEFUL_SET))))));
V1PodTemplateSpec template = Kubectl.rollout(V1StatefulSet.class).history().apiClient(apiClient).name("foo").namespace("default").revision(2).skipDiscovery().execute();
wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))));
wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/controllerrevisions"))).withQueryParam("labelSelector", new EqualToPattern("app = bar")));
wireMockRule.verify(1, patchRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/statefulsets/foo"))).withQueryParam("dryRun", new EqualToPattern("All")));
Assert.assertNotNull(template);
}
Aggregations