use of io.kubernetes.client.openapi.models.V1ConfigMap in project pravega by pravega.
the class K8sClient method createConfigMap.
/**
* Create ConfigMap.
* @param namespace The namespace where the ConfigMap should be created.
* @param binding The cluster ConfigMap.
* @return A future indicating the status of the ConfigMap operation.
*/
@SneakyThrows(ApiException.class)
public CompletableFuture<V1ConfigMap> createConfigMap(String namespace, V1ConfigMap binding) {
CoreV1Api api = new CoreV1Api();
K8AsyncCallback<V1ConfigMap> callback = new K8AsyncCallback<>("createConfigMap-" + binding.getMetadata().getName());
api.createNamespacedConfigMapAsync(namespace, binding, PRETTY_PRINT, DRY_RUN, FIELD_MANAGER, callback);
return exceptionallyExpecting(callback.getFuture(), isConflict, null);
}
use of io.kubernetes.client.openapi.models.V1ConfigMap in project pravega by pravega.
the class K8sClient method getConfigMap.
/**
* Method to get V1ConfigMap.
* @param name Name of the ConfigMap.
* @param namespace Namespace on which the pod(s) reside.
* @return Future representing the V1ConfigMap.
*/
@SneakyThrows(ApiException.class)
public CompletableFuture<V1ConfigMap> getConfigMap(String name, String namespace) {
CoreV1Api api = new CoreV1Api();
K8AsyncCallback<V1ConfigMap> callback = new K8AsyncCallback<>("readNamespacedConfigMap-" + name);
api.readNamespacedConfigMapAsync(name, namespace, PRETTY_PRINT, false, false, callback);
return callback.getFuture();
}
use of io.kubernetes.client.openapi.models.V1ConfigMap 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.V1ConfigMap in project heron by twitter.
the class V1ControllerTest method testLoadPodFromTemplateBadTargetConfigMap.
@Test
public void testLoadPodFromTemplateBadTargetConfigMap() {
// ConfigMap with target ConfigMap and an invalid Pod Template.
final V1ConfigMap configMapInvalidPod = new V1ConfigMapBuilder().withNewMetadata().withName(CONFIGMAP_NAME).endMetadata().addToData(POD_TEMPLATE_NAME, "Dummy Value").build();
// ConfigMap with target ConfigMaps and an empty Pod Template.
final V1ConfigMap configMapEmptyPod = new V1ConfigMapBuilder().withNewMetadata().withName(CONFIGMAP_NAME).endMetadata().addToData(POD_TEMPLATE_NAME, "").build();
// Test case container.
// Input: ConfigMap to setup mock V1Controller, Boolean flag for executor/manager switch.
// Output: The expected error message.
final List<TestTuple<Pair<V1ConfigMap, Boolean>, String>> testCases = new LinkedList<>();
testCases.add(new TestTuple<>("Executor invalid Pod Template", new Pair<>(configMapInvalidPod, true), "Error parsing"));
testCases.add(new TestTuple<>("Manager invalid Pod Template", new Pair<>(configMapInvalidPod, false), "Error parsing"));
testCases.add(new TestTuple<>("Executor empty Pod Template", new Pair<>(configMapEmptyPod, true), "Error parsing"));
testCases.add(new TestTuple<>("Manager empty Pod Template", new Pair<>(configMapEmptyPod, false), "Error parsing"));
// Test loop.
for (TestTuple<Pair<V1ConfigMap, Boolean>, String> testCase : testCases) {
doReturn(testCase.input.first).when(v1ControllerWithPodTemplate).getConfigMap(anyString());
String message = "";
try {
v1ControllerWithPodTemplate.loadPodFromTemplate(testCase.input.second);
} catch (TopologySubmissionException e) {
message = e.getMessage();
}
Assert.assertTrue(testCase.description, message.contains(testCase.expected));
}
}
use of io.kubernetes.client.openapi.models.V1ConfigMap in project heron by twitter.
the class V1ControllerTest method testLoadPodFromTemplateNoConfigMap.
@Test
public void testLoadPodFromTemplateNoConfigMap() {
final List<TestTuple<Boolean, String>> testCases = new LinkedList<>();
testCases.add(new TestTuple<>("Executor no ConfigMap", true, "Failed to locate Pod Template"));
testCases.add(new TestTuple<>("Manager no ConfigMap", false, "Failed to locate Pod Template"));
for (TestTuple<Boolean, String> testCase : testCases) {
doReturn(new V1ConfigMap()).when(v1ControllerWithPodTemplate).getConfigMap(anyString());
String message = "";
try {
v1ControllerWithPodTemplate.loadPodFromTemplate(testCase.input);
} catch (TopologySubmissionException e) {
message = e.getMessage();
}
Assert.assertTrue(testCase.description, message.contains(testCase.expected));
}
}
Aggregations