use of io.dekorate.hook.ProjectHook in project dekorate by dekorateio.
the class KubernetesSessionListener method onClosed.
@Override
public void onClosed() {
Session session = getSession();
Project project = getProject();
Optional<KubernetesConfig> optionalAppConfig = session.getConfigurationRegistry().get(KubernetesConfig.class);
Optional<ImageConfiguration> optionalImageConfig = session.getConfigurationRegistry().getImageConfig(BuildServiceFactories.supplierMatches(project));
if (!optionalAppConfig.isPresent() || !optionalImageConfig.isPresent()) {
return;
}
KubernetesConfig kubernetesConfig = optionalAppConfig.get();
ResourceRegistry resources = session.getResourceRegistry();
KubernetesList generated = session.getGeneratedResources().getOrDefault(KUBERNETES, new KubernetesList());
BuildService buildService = null;
ImageConfiguration imageConfig = optionalImageConfig.get();
if (imageConfig.isAutoBuildEnabled() || imageConfig.isAutoPushEnabled() || kubernetesConfig.isAutoDeployEnabled()) {
try {
buildService = optionalImageConfig.map(BuildServiceFactories.create(getProject(), generated.getItems())).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);
}
}
List<ProjectHook> hooks = new ArrayList<>();
if (kubernetesConfig.isAutoDeployEnabled()) {
hooks.add(new ResourcesApplyHook(getProject(), KUBERNETES, "kubectl"));
hooks.add(new ScaleDeploymentHook(getProject(), kubernetesConfig.getName(), 0));
}
if (imageConfig.isAutoPushEnabled()) {
// When deploy is enabled, we scale the Deployment down before push
// then scale it back up once the image has been successfully pushed
// This ensure that the pod runs the proper image
hooks.add(new ImageBuildHook(getProject(), buildService));
hooks.add(new ImagePushHook(getProject(), buildService));
} else if (imageConfig.isAutoBuildEnabled() || kubernetesConfig.isAutoDeployEnabled()) {
hooks.add(new ImageBuildHook(getProject(), buildService));
}
if (kubernetesConfig.isAutoDeployEnabled()) {
hooks.add(new ScaleDeploymentHook(getProject(), kubernetesConfig.getName(), kubernetesConfig.getReplicas()));
}
if (!hooks.isEmpty()) {
OrderedHook hook = OrderedHook.create(hooks.toArray(new ProjectHook[hooks.size()]));
hook.register();
}
}
use of io.dekorate.hook.ProjectHook 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();
}
}
}
Aggregations