use of io.kubernetes.client.openapi.ApiException 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.ApiException in project java by kubernetes-client.
the class LeaderElectingControllerTest method testLeaderElectingController.
@Test(timeout = 90000)
public void testLeaderElectingController() throws ApiException, InterruptedException {
AtomicReference<LeaderElectionRecord> record = new AtomicReference<>();
record.set(new LeaderElectionRecord());
Semaphore latch = new Semaphore(2);
Semaphore controllerLatch = new Semaphore(2);
when(mockLock.identity()).thenReturn("foo");
when(mockLock.get()).thenThrow(new ApiException("Record Not Found", HttpURLConnection.HTTP_NOT_FOUND, null, null)).thenReturn(record.get());
doAnswer(invocationOnMock -> {
record.set(invocationOnMock.getArgument(0));
latch.release();
return true;
}).when(mockLock).create(any());
doAnswer(invocationOnMock -> {
latch.release();
return false;
}).when(mockLock).update(any());
doAnswer(invocationOnMock -> {
controllerLatch.release();
return null;
}).when(mockController).run();
doAnswer(invocationOnMock -> {
controllerLatch.release();
return null;
}).when(mockController).shutdown();
LeaderElectingController leaderElectingController = new LeaderElectingController(new LeaderElector(new LeaderElectionConfig(mockLock, Duration.ofMillis(300), Duration.ofMillis(200), Duration.ofMillis(100))), mockController);
latch.acquire(2);
controllerLatch.acquire(2);
Thread controllerThread = new Thread(leaderElectingController::run);
controllerThread.start();
latch.acquire(2);
controllerThread.interrupt();
verify(mockLock, times(1)).create(any());
verify(mockLock, atLeastOnce()).update(any());
controllerLatch.acquire(2);
verify(mockController, times(1)).run();
verify(mockController, times(1)).shutdown();
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class KubectlGetTest method testGetDefaultNamespaceOnePodForbiddenShouldThrowException.
@Test
public void testGetDefaultNamespaceOnePodForbiddenShouldThrowException() {
wireMockRule.stubFor(get(urlPathEqualTo("/api/v1/namespaces/default/pods/foo1")).willReturn(aResponse().withStatus(403).withBody(apiClient.getJSON().serialize(new V1Status().code(403)))));
try {
V1Pod getPod = Kubectl.get(V1Pod.class).apiClient(apiClient).skipDiscovery().namespace(// no namespace specified
"default").name("foo1").execute();
} catch (KubectlException e) {
assertTrue(e.getCause() instanceof ApiException);
return;
} finally {
wireMockRule.verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/pods/foo1")));
}
fail();
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class KubectlCreate method execute.
@Override
public ApiType execute() throws KubectlException {
refreshDiscovery();
GenericKubernetesApi<ApiType, KubernetesListObject> api = getGenericApi();
if (ModelMapper.isNamespaced(this.targetObj.getClass())) {
String targetNamespace = namespace != null ? namespace : Strings.isNullOrEmpty(targetObj.getMetadata().getNamespace()) ? Namespaces.NAMESPACE_DEFAULT : targetObj.getMetadata().getNamespace();
try {
return api.create(targetNamespace, targetObj, new CreateOptions()).throwsApiException().getObject();
} catch (ApiException e) {
throw new KubectlException(e);
}
} else {
try {
return api.create(targetObj, new CreateOptions()).throwsApiException().getObject();
} catch (ApiException e) {
throw new KubectlException(e);
}
}
}
use of io.kubernetes.client.openapi.ApiException in project java by kubernetes-client.
the class KubectlLog method execute.
@Override
public InputStream execute() throws KubectlException {
validate();
PodLogs logs = new PodLogs(apiClient);
String ns = (this.namespace == null ? "default" : this.namespace);
try {
return logs.streamNamespacedPodLog(ns, this.name, this.container);
} catch (ApiException | IOException ex) {
throw new KubectlException(ex);
}
}
Aggregations