use of io.fabric8.utils.MultiException in project fabric8 by fabric8io.
the class SessionListener method start.
public void start(@Observes final Start event, KubernetesClient client, Controller controller, Configuration configuration) throws Exception {
Objects.requireNonNull(client, "KubernetesClient most not be null!");
Session session = event.getSession();
final Logger log = session.getLogger();
String namespace = session.getNamespace();
System.setProperty(Constants.KUBERNETES_NAMESPACE, namespace);
log.status("Using Kubernetes at: " + client.getMasterUrl());
log.status("Creating kubernetes resources inside namespace: " + namespace);
log.info("if you use OpenShift then type this switch namespaces: oc project " + namespace);
log.info("if you use kubernetes then type this to switch namespaces: kubectl namespace " + namespace);
clearTestResultDirectories(session);
controller.setNamespace(namespace);
controller.setThrowExceptionOnError(true);
controller.setRecreateMode(true);
controller.setIgnoreRunningOAuthClients(true);
if (configuration.isCreateNamespaceForTest()) {
createNamespace(client, controller, session);
} else {
String namespaceToUse = configuration.getNamespace();
checkNamespace(client, controller, session, configuration);
updateConfigMapStatus(client, session, Constants.RUNNING_STATUS);
namespace = namespaceToUse;
controller.setNamespace(namespace);
}
List<KubernetesList> kubeConfigs = new LinkedList<>();
shutdownHook = new ShutdownHook(client, controller, configuration, session, kubeConfigs);
Runtime.getRuntime().addShutdownHook(shutdownHook);
try {
URL configUrl = configuration.getEnvironmentConfigUrl();
List<String> dependencies = !configuration.getEnvironmentDependencies().isEmpty() ? configuration.getEnvironmentDependencies() : resolver.resolve(session);
if (configuration.isEnvironmentInitEnabled()) {
for (String dependency : dependencies) {
log.info("Found dependency: " + dependency);
loadDependency(log, kubeConfigs, dependency, controller, configuration, namespace);
}
OpenShiftClient openShiftClient = controller.getOpenShiftClientOrNull();
if (configUrl == null) {
// lets try find the default configuration generated by the new fabric8-maven-plugin
String resourceName = "kubernetes.yml";
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE) && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.ROUTE)) {
resourceName = "openshift.yml";
}
configUrl = findConfigResource("/META-INF/fabric8/" + resourceName);
}
if (configUrl != null) {
log.status("Applying kubernetes configuration from: " + configUrl);
String configText = readAsString(configUrl);
Object dto = null;
String configPath = configUrl.getPath();
if (configPath.endsWith(".yml") || configPath.endsWith(".yaml")) {
dto = loadYaml(configText, KubernetesResource.class);
} else {
dto = loadJson(configText);
}
dto = expandTemplate(controller, configuration, log, namespace, configUrl.toString(), dto);
KubernetesList kubeList = KubernetesHelper.asKubernetesList(dto);
List<HasMetadata> items = kubeList.getItems();
kubeConfigs.add(kubeList);
}
// Lets also try to load the image stream for the project.
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE)) {
File targetDir = new File(System.getProperty("basedir", ".") + "/target");
if (targetDir.exists() && targetDir.isDirectory()) {
File[] files = targetDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith("-is.yml")) {
loadDependency(log, kubeConfigs, file.toURI().toURL().toString(), controller, configuration, namespace);
}
}
}
}
//
}
}
if (!configuration.isEnvironmentInitEnabled() || applyConfiguration(client, controller, configuration, session, kubeConfigs)) {
displaySessionStatus(client, session);
} else {
throw new IllegalStateException("Failed to apply kubernetes configuration.");
}
} catch (Exception e) {
try {
cleanupSession(client, controller, configuration, session, kubeConfigs, Constants.ERROR_STATUS);
} catch (MultiException me) {
throw e;
} finally {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
}
throw new RuntimeException(e);
}
}
use of io.fabric8.utils.MultiException in project fabric8 by jboss-fuse.
the class Util method cleanupAllResources.
public static void cleanupAllResources(KubernetesClient client, Session session, List<Throwable> errors) throws MultiException {
String sessionNamespace = session.getNamespace();
session.getLogger().info("Removing all resources in namespace " + sessionNamespace);
/**
* Lets use a loop to ensure we really do delete all the matching resources
*/
for (int i = 0; i < 10; i++) {
OpenShiftClient openShiftClient = new Controller(client).getOpenShiftClientOrNull();
if (openShiftClient != null) {
try {
openShiftClient.deploymentConfigs().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
openShiftClient.routes().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
}
try {
client.extensions().deployments().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.extensions().replicaSets().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.replicationControllers().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.pods().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.extensions().ingresses().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.services().inNamespace(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
try {
client.securityContextConstraints().withName(sessionNamespace).delete();
} catch (KubernetesClientException e) {
errors.add(e);
}
// lets see if there are any matching podList left
List<Pod> filteredPods = notNullList(client.pods().inNamespace(sessionNamespace).list().getItems());
if (filteredPods.isEmpty()) {
return;
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
use of io.fabric8.utils.MultiException in project fabric8 by jboss-fuse.
the class SessionListener method start.
public void start(@Observes final Start event, KubernetesClient client, Controller controller, Configuration configuration) throws Exception {
Objects.requireNonNull(client, "KubernetesClient most not be null!");
Session session = event.getSession();
final Logger log = session.getLogger();
String namespace = session.getNamespace();
System.setProperty(Constants.KUBERNETES_NAMESPACE, namespace);
log.status("Using Kubernetes at: " + client.getMasterUrl());
log.status("Creating kubernetes resources inside namespace: " + namespace);
log.info("if you use OpenShift then type this switch namespaces: oc project " + namespace);
log.info("if you use kubernetes then type this to switch namespaces: kubectl namespace " + namespace);
clearTestResultDirectories(session);
controller.setNamespace(namespace);
controller.setThrowExceptionOnError(true);
controller.setRecreateMode(true);
controller.setIgnoreRunningOAuthClients(true);
if (configuration.isCreateNamespaceForTest()) {
createNamespace(client, controller, session);
} else {
String namespaceToUse = configuration.getNamespace();
checkNamespace(client, controller, session, configuration);
updateConfigMapStatus(client, session, Constants.RUNNING_STATUS);
namespace = namespaceToUse;
controller.setNamespace(namespace);
}
List<KubernetesList> kubeConfigs = new LinkedList<>();
shutdownHook = new ShutdownHook(client, controller, configuration, session, kubeConfigs);
Runtime.getRuntime().addShutdownHook(shutdownHook);
try {
URL configUrl = configuration.getEnvironmentConfigUrl();
List<String> dependencies = !configuration.getEnvironmentDependencies().isEmpty() ? configuration.getEnvironmentDependencies() : resolver.resolve(session);
if (configuration.isEnvironmentInitEnabled()) {
for (String dependency : dependencies) {
log.info("Found dependency: " + dependency);
loadDependency(log, kubeConfigs, dependency, controller, configuration, namespace);
}
OpenShiftClient openShiftClient = controller.getOpenShiftClientOrNull();
if (configUrl == null) {
// lets try find the default configuration generated by the new fabric8-maven-plugin
String resourceName = "kubernetes.yml";
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE) && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.ROUTE)) {
resourceName = "openshift.yml";
}
configUrl = findConfigResource("/META-INF/fabric8/" + resourceName);
}
if (configUrl != null) {
log.status("Applying kubernetes configuration from: " + configUrl);
String configText = readAsString(configUrl);
Object dto = null;
String configPath = configUrl.getPath();
if (configPath.endsWith(".yml") || configPath.endsWith(".yaml")) {
dto = loadYaml(configText, KubernetesResource.class);
} else {
dto = loadJson(configText);
}
dto = expandTemplate(controller, configuration, log, namespace, configUrl.toString(), dto);
KubernetesList kubeList = KubernetesHelper.asKubernetesList(dto);
List<HasMetadata> items = kubeList.getItems();
kubeConfigs.add(kubeList);
}
// Lets also try to load the image stream for the project.
if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE)) {
File targetDir = new File(System.getProperty("basedir", ".") + "/target");
if (targetDir.exists() && targetDir.isDirectory()) {
File[] files = targetDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().endsWith("-is.yml")) {
loadDependency(log, kubeConfigs, file.toURI().toURL().toString(), controller, configuration, namespace);
}
}
}
}
//
}
}
if (!configuration.isEnvironmentInitEnabled() || applyConfiguration(client, controller, configuration, session, kubeConfigs)) {
displaySessionStatus(client, session);
} else {
throw new IllegalStateException("Failed to apply kubernetes configuration.");
}
} catch (Exception e) {
try {
cleanupSession(client, controller, configuration, session, kubeConfigs, Constants.ERROR_STATUS);
} catch (MultiException me) {
throw e;
} finally {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
}
throw new RuntimeException(e);
}
}
Aggregations