use of io.fabric8.arquillian.kubernetes.Session 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.arquillian.kubernetes.Session in project fabric8 by fabric8io.
the class SessionListener method stop.
public void stop(@Observes Stop event, KubernetesClient client, Controller controller, Configuration configuration, List<KubernetesList> kubeConfigs) throws Exception {
try {
Session session = event.getSession();
cleanupSession(client, controller, configuration, session, kubeConfigs, Util.getSessionStatus(session));
} finally {
if (shutdownHook != null) {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
}
}
}
use of io.fabric8.arquillian.kubernetes.Session in project fabric8 by fabric8io.
the class SessionListener method applyConfiguration.
private boolean applyConfiguration(KubernetesClient client, Controller controller, Configuration configuration, Session session, List<KubernetesList> kubeConfigs) throws Exception {
Logger log = session.getLogger();
Map<Integer, Callable<Boolean>> conditions = new TreeMap<>();
Callable<Boolean> sessionPodsReady = new SessionPodsAreReady(client, session);
Callable<Boolean> servicesReady = new SessionServicesAreReady(client, session, configuration);
Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator());
for (KubernetesList c : kubeConfigs) {
entities.addAll(enhance(session, configuration, c).getItems());
}
if (containsImageStreamResources(entities)) {
// no need to use a local image registry
// as we are using OpenShift and
} else {
String registry = getLocalDockerRegistry();
if (Strings.isNotBlank(registry)) {
log.status("Adapting resources to pull images from registry: " + registry);
addRegistryToImageNameIfNotPresent(entities, registry);
} else {
log.status("No local fabric8 docker registry found");
}
}
List<Object> items = new ArrayList<>();
items.addAll(entities);
// Ensure services are processed first.
Collections.sort(items, new Comparator<Object>() {
@Override
public int compare(Object left, Object right) {
if (left instanceof Service) {
return -1;
} else if (right instanceof Service) {
return 1;
} else {
return 0;
}
}
});
boolean isOpenshift = client.isAdaptable(OpenShiftClient.class);
String namespace = session.getNamespace();
String routeDomain = null;
if (Strings.isNotBlank(configuration.getKubernetesDomain())) {
routeDomain = configuration.getKubernetesDomain();
}
preprocessEnvironment(client, controller, configuration, session);
Set<HasMetadata> extraEntities = new TreeSet<>(new HasMetadataComparator());
for (Object entity : items) {
if (entity instanceof Pod) {
Pod pod = (Pod) entity;
log.status("Applying pod:" + getName(pod));
Set<Secret> secrets = generateSecrets(client, session, pod.getMetadata());
String serviceAccountName = pod.getSpec().getServiceAccountName();
if (Strings.isNotBlank(serviceAccountName)) {
generateServiceAccount(client, session, secrets, serviceAccountName);
}
controller.applyPod(pod, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof Service) {
Service service = (Service) entity;
String serviceName = getName(service);
log.status("Applying service:" + serviceName);
controller.applyService(service, session.getId());
conditions.put(2, servicesReady);
if (isOpenshift) {
Route route = Routes.createRouteForService(routeDomain, namespace, service, log);
if (route != null) {
log.status("Applying route for:" + serviceName);
controller.applyRoute(route, "route for " + serviceName);
extraEntities.add(route);
}
}
} else if (entity instanceof ReplicationController) {
ReplicationController replicationController = (ReplicationController) entity;
log.status("Applying replication controller:" + getName(replicationController));
Set<Secret> secrets = generateSecrets(client, session, replicationController.getSpec().getTemplate().getMetadata());
String serviceAccountName = replicationController.getSpec().getTemplate().getSpec().getServiceAccountName();
if (Strings.isNotBlank(serviceAccountName)) {
generateServiceAccount(client, session, secrets, serviceAccountName);
}
controller.applyReplicationController(replicationController, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof ReplicaSet || entity instanceof Deployment || entity instanceof DeploymentConfig) {
log.status("Applying " + entity.getClass().getSimpleName() + ".");
controller.apply(entity, session.getId());
conditions.put(1, sessionPodsReady);
} else if (entity instanceof OAuthClient) {
OAuthClient oc = (OAuthClient) entity;
// these are global so lets create a custom one for the new namespace
ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(oc);
String name = metadata.getName();
if (isOpenshift) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
OAuthClient current = openShiftClient.oAuthClients().withName(name).get();
boolean create = false;
if (current == null) {
current = oc;
create = true;
}
boolean updated = false;
// lets add a new redirect entry
List<String> redirectURIs = current.getRedirectURIs();
String namespaceSuffix = "-" + namespace;
String redirectUri = "http://" + name + namespaceSuffix;
if (Strings.isNotBlank(routeDomain)) {
redirectUri += "." + Strings.stripPrefix(routeDomain, ".");
}
if (!redirectURIs.contains(redirectUri)) {
redirectURIs.add(redirectUri);
updated = true;
}
current.setRedirectURIs(redirectURIs);
log.status("Applying OAuthClient:" + name);
controller.setSupportOAuthClients(true);
if (create) {
openShiftClient.oAuthClients().create(current);
} else {
if (updated) {
// TODO this should work!
// openShiftClient.oAuthClients().withName(name).replace(current);
openShiftClient.oAuthClients().withName(name).delete();
current.getMetadata().setResourceVersion(null);
openShiftClient.oAuthClients().create(current);
}
}
}
} else if (entity instanceof HasMetadata) {
log.status("Applying " + entity.getClass().getSimpleName() + ":" + KubernetesHelper.getName((HasMetadata) entity));
controller.apply(entity, session.getId());
} else if (entity != null) {
log.status("Applying " + entity.getClass().getSimpleName() + ".");
controller.apply(entity, session.getId());
}
}
entities.addAll(extraEntities);
// Wait until conditions are meet.
if (!conditions.isEmpty()) {
Callable<Boolean> compositeCondition = new CompositeCondition(conditions.values());
WaitStrategy waitStrategy = new WaitStrategy(compositeCondition, configuration.getWaitTimeout(), configuration.getWaitPollInterval());
if (!waitStrategy.await()) {
log.error("Timed out waiting for pods/services!");
return false;
} else {
log.status("All pods/services are currently 'running'!");
}
} else {
log.warn("No pods/services/replication controllers defined in the configuration!");
}
return true;
}
use of io.fabric8.arquillian.kubernetes.Session in project fabric8 by fabric8io.
the class SessionListener method generateServiceAccount.
private void generateServiceAccount(KubernetesClient client, Session session, Set<Secret> secrets, String serviceAccountName) {
List<ObjectReference> secretRefs = new ArrayList<>();
for (Secret secret : secrets) {
secretRefs.add(new ObjectReferenceBuilder().withNamespace(session.getNamespace()).withName(KubernetesHelper.getName(secret)).build());
}
SecurityContextConstraints securityContextConstraints = client.securityContextConstraints().withName(session.getNamespace()).get();
if (securityContextConstraints == null) {
client.securityContextConstraints().createNew().withNewMetadata().withName(session.getNamespace()).endMetadata().withAllowHostDirVolumePlugin(true).withAllowPrivilegedContainer(true).withNewRunAsUser().withType("RunAsAny").endRunAsUser().withNewSeLinuxContext().withType("RunAsAny").endSeLinuxContext().withUsers("system:serviceaccount:" + session.getNamespace() + ":" + serviceAccountName).done();
}
ServiceAccount serviceAccount = client.serviceAccounts().inNamespace(session.getNamespace()).withName(serviceAccountName).get();
if (serviceAccount == null) {
client.serviceAccounts().inNamespace(session.getNamespace()).createNew().withNewMetadata().withName(serviceAccountName).endMetadata().withSecrets(secretRefs).done();
} else {
client.serviceAccounts().inNamespace(session.getNamespace()).withName(serviceAccountName).replace(new ServiceAccountBuilder(serviceAccount).withNewMetadata().withName(serviceAccountName).endMetadata().addToSecrets(secretRefs.toArray(new ObjectReference[secretRefs.size()])).build());
}
}
use of io.fabric8.arquillian.kubernetes.Session in project fabric8 by fabric8io.
the class PodListResourceProvider method lookup.
@Override
public Object lookup(ArquillianResource resource, Annotation... qualifiers) {
KubernetesClient client = this.clientInstance.get();
Session session = sessionInstance.get();
Map<String, String> labels = getLabels(qualifiers);
if (labels.isEmpty()) {
return client.pods().inNamespace(session.getNamespace()).list();
} else {
return client.pods().inNamespace(session.getNamespace()).withLabels(labels).list();
}
}
Aggregations