use of io.dekorate.openshift.config.OpenshiftConfig in project dekorate by dekorateio.
the class OpenshiftSessionListener method onClosed.
@Override
public void onClosed() {
Session session = getSession();
Project project = getProject();
// We ned to set the TTCL, becuase the KubenretesClient used in this part of
// code, needs TTCL so that java.util.ServiceLoader can work.
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
List<ProjectHook> hooks = new ArrayList<>();
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
Optional<OpenshiftConfig> optionalAppConfig = session.getConfigurationRegistry().get(OpenshiftConfig.class);
Optional<ImageConfiguration> optionalImageConfig = session.getConfigurationRegistry().getImageConfig(BuildServiceFactories.supplierMatches(project));
if (!optionalAppConfig.isPresent() || !optionalImageConfig.isPresent()) {
return;
}
OpenshiftConfig openshiftConfig = optionalAppConfig.get();
ImageConfiguration imageConfig = optionalImageConfig.get();
String name = session.getConfigurationRegistry().get(OpenshiftConfig.class).map(c -> c.getName()).orElse(getProject().getBuildInfo().getName());
BuildService buildService = null;
boolean s2iEnabled = imageConfig instanceof S2iBuildConfig && ((S2iBuildConfig) imageConfig).isEnabled();
if (imageConfig.isAutoBuildEnabled() || imageConfig.isAutoPushEnabled() || openshiftConfig.isAutoDeployEnabled()) {
KubernetesList list = session.getGeneratedResources().get("openshift");
List<HasMetadata> generated = list != null ? list.getItems() : Collections.emptyList();
try {
buildService = optionalImageConfig.map(BuildServiceFactories.create(getProject(), generated)).orElseThrow(() -> new IllegalStateException("No applicable BuildServiceFactory found."));
} catch (Exception e) {
BuildServiceFactories.log(project, session.getConfigurationRegistry().getAll(ImageConfiguration.class));
throw DekorateException.launderThrowable("Failed to lookup BuildService.", e);
}
hooks.add(new ImageBuildHook(getProject(), buildService));
}
if (imageConfig.isAutoPushEnabled() && !s2iEnabled) {
hooks.add(new ImagePushHook(getProject(), buildService));
}
if (openshiftConfig.isAutoDeployEnabled()) {
hooks.add(new ResourcesApplyHook(getProject(), OPENSHIFT, "oc"));
}
} finally {
Thread.currentThread().setContextClassLoader(tccl);
if (!hooks.isEmpty()) {
OrderedHook hook = OrderedHook.create(hooks.toArray(new ProjectHook[hooks.size()]));
hook.register();
}
}
}
use of io.dekorate.openshift.config.OpenshiftConfig in project dekorate by dekorateio.
the class OpenshiftManifestGenerator method generate.
public void generate(OpenshiftConfig config) {
LOGGER.info("Processing openshift configuration.");
ImageConfiguration imageConfig = getImageConfiguration(getProject(), config, configurationRegistry);
Optional<DeploymentConfig> existingDeploymentConfig = resourceRegistry.groups().getOrDefault(OPENSHIFT, new KubernetesListBuilder()).buildItems().stream().filter(i -> i instanceof DeploymentConfig).map(i -> (DeploymentConfig) i).filter(i -> i.getMetadata().getName().equals(config.getName())).findAny();
if (!existingDeploymentConfig.isPresent()) {
resourceRegistry.add(OPENSHIFT, createDeploymentConfig(config, imageConfig));
}
if (config.isHeadless()) {
resourceRegistry.decorate(OPENSHIFT, new ApplyHeadlessDecorator(config.getName()));
}
for (Container container : config.getInitContainers()) {
resourceRegistry.decorate(OPENSHIFT, new AddInitContainerDecorator(config.getName(), container));
}
if (config.getPorts().length > 0) {
resourceRegistry.decorate(OPENSHIFT, new AddServiceResourceDecorator(config));
}
addDecorators(OPENSHIFT, config, imageConfig);
}
use of io.dekorate.openshift.config.OpenshiftConfig in project dekorate by dekorateio.
the class OpenshiftConfigGeneratorTest method shouldGenerateOpenshiftAndWriteToTheFilesystem.
@Test
public void shouldGenerateOpenshiftAndWriteToTheFilesystem() {
WithProject withProject = new WithProject() {
};
withProject.setProject(FileProjectFactory.create(new File(".")).withDekorateOutputDir(tempDir.toAbsolutePath().toString()).withDekorateMetaDir(tempDir.toAbsolutePath().toString()));
SessionWriter writer = new SimpleFileWriter(withProject.getProject());
Session session = Session.getSession();
session.setWriter(writer);
OpenshiftConfigGenerator generator = new DefaultOpenshiftConfigGenerator(session.getConfigurationRegistry());
Map<String, Object> openshiftConfig = new HashMap<String, Object>() {
{
put(OpenshiftConfig.class.getName(), new HashMap<String, Object>() {
{
put("name", "generator-test");
put("version", "latest");
put("replicas", 2);
}
});
}
};
generator.addPropertyConfiguration(openshiftConfig);
final Map<String, String> result = session.close();
KubernetesList list = session.getGeneratedResources().get("openshift");
assertThat(list).isNotNull();
assertThat(list.getItems()).filteredOn(i -> "DeploymentConfig".equals(i.getKind())).filteredOn(i -> ((DeploymentConfig) i).getSpec().getReplicas() == 2).isNotEmpty();
assertThat(tempDir.resolve("openshift.json")).exists();
assertThat(tempDir.resolve("openshift.yml")).exists();
assertThat(result).hasSize(5);
}
use of io.dekorate.openshift.config.OpenshiftConfig in project dekorate by dekorateio.
the class OpenshiftExtension method startProject.
private void startProject(ExtensionContext context, Project project) throws InterruptedException {
LOGGER.info("Starting project at " + project.getRoot());
OpenshiftIntegrationTestConfig config = getOpenshiftIntegrationTestConfig(context);
KubernetesClient client = getKubernetesClient(context);
KubernetesList list = getOpenshiftResources(context, project);
OpenshiftConfig openshiftConfig = getOpenshiftConfig(project);
ImageConfiguration imageConfiguration = ImageConfiguration.from(openshiftConfig);
BuildService buildService = null;
try {
BuildServiceFactory buildServiceFactory = BuildServiceFactories.find(project, imageConfiguration).orElseThrow(() -> new IllegalStateException("No applicable BuildServiceFactory found."));
buildService = buildServiceFactory.create(project, imageConfiguration, list.getItems());
} catch (Exception e) {
throw DekorateException.launderThrowable("Failed to lookup BuildService.", e);
}
if (config.isPushEnabled()) {
buildService.prepare();
buildService.build();
buildService.push();
} else if (config.isBuildEnabled()) {
buildService.prepare();
buildService.build();
}
if (config.isDeployEnabled()) {
// Create the remaining resources.
list.getItems().stream().filter(i -> !(i instanceof BuildConfig)).forEach(i -> {
try {
HasMetadata r = client.resource(i).fromServer().get();
if (r == null) {
client.resource(i).apply();
} else if (r instanceof ImageStream) {
// let's not delete image streams at this point
} else if (deleteAndWait(context, i, 1, TimeUnit.MINUTES)) {
client.resource(i).apply();
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
LOGGER.info("Created: " + i.getKind() + " name:" + i.getMetadata().getName() + ".");
});
List<HasMetadata> waitables = list.getItems().stream().filter(i -> i instanceof Deployment || i instanceof DeploymentConfig || i instanceof Pod || i instanceof ReplicaSet || i instanceof ReplicationController).collect(Collectors.toList());
long started = System.currentTimeMillis();
LOGGER.info("Waiting until ready (" + config.getReadinessTimeout() + " ms)...");
waitUntilCondition(context, waitables, i -> OpenshiftReadiness.isReady(i), config.getReadinessTimeout(), TimeUnit.MILLISECONDS);
long ended = System.currentTimeMillis();
LOGGER.info("Waited: " + (ended - started) + " ms.");
// Display the item status
waitables.stream().map(r -> client.resource(r).fromServer().get()).forEach(i -> {
if (!OpenshiftReadiness.isReady(i)) {
readinessFailed(context);
LOGGER.warning(i.getKind() + ":" + i.getMetadata().getName() + " not ready!");
}
});
if (hasReadinessFailed(context)) {
throw new IllegalStateException("Readiness Failed");
}
}
}
use of io.dekorate.openshift.config.OpenshiftConfig in project dekorate by dekorateio.
the class OpenshiftExtension method deleteProject.
private void deleteProject(ExtensionContext context, Project project, OpenShiftClient client) {
getOpenshiftResources(context, project).getItems().stream().filter(r -> !(r instanceof ImageStream)).forEach(r -> {
try {
LOGGER.info("Deleting: " + r.getKind() + " name:" + r.getMetadata().getName() + ". Deleted:" + client.resource(r).delete());
} catch (Exception e) {
}
});
OpenshiftConfig openshiftConfig = getOpenshiftConfig(project);
List<HasMetadata> buildPods = client.pods().list().getItems().stream().filter(i -> i.getMetadata().getName().matches(openshiftConfig.getName() + "-\\d-build")).collect(Collectors.toList());
try {
client.resourceList(buildPods).delete();
client.deploymentConfigs().withName(openshiftConfig.getName()).delete();
} catch (Exception e) {
}
}
Aggregations