Search in sources :

Example 1 with ProjectHook

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();
    }
}
Also used : ProjectHook(io.dekorate.hook.ProjectHook) BuildService(io.dekorate.BuildService) KubernetesConfig(io.dekorate.kubernetes.config.KubernetesConfig) ArrayList(java.util.ArrayList) ResourceRegistry(io.dekorate.ResourceRegistry) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) OrderedHook(io.dekorate.hook.OrderedHook) DekorateException(io.dekorate.DekorateException) WithProject(io.dekorate.WithProject) Project(io.dekorate.project.Project) ResourcesApplyHook(io.dekorate.hook.ResourcesApplyHook) ImageConfiguration(io.dekorate.kubernetes.config.ImageConfiguration) ImageBuildHook(io.dekorate.hook.ImageBuildHook) ScaleDeploymentHook(io.dekorate.kubernetes.hook.ScaleDeploymentHook) ImagePushHook(io.dekorate.hook.ImagePushHook) Session(io.dekorate.Session) WithSession(io.dekorate.WithSession)

Example 2 with ProjectHook

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();
        }
    }
}
Also used : DekorateException(io.dekorate.DekorateException) WithProject(io.dekorate.WithProject) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) ImageBuildHook(io.dekorate.hook.ImageBuildHook) ProjectHook(io.dekorate.hook.ProjectHook) Project(io.dekorate.project.Project) BuildServiceFactories(io.dekorate.BuildServiceFactories) BuildService(io.dekorate.BuildService) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ArrayList(java.util.ArrayList) List(java.util.List) OrderedHook(io.dekorate.hook.OrderedHook) Session(io.dekorate.Session) ResourcesApplyHook(io.dekorate.hook.ResourcesApplyHook) ImageConfiguration(io.dekorate.kubernetes.config.ImageConfiguration) OpenshiftConfig(io.dekorate.openshift.config.OpenshiftConfig) Optional(java.util.Optional) S2iBuildConfig(io.dekorate.s2i.config.S2iBuildConfig) WithSession(io.dekorate.WithSession) ImagePushHook(io.dekorate.hook.ImagePushHook) Collections(java.util.Collections) SessionListener(io.dekorate.SessionListener) S2iBuildConfig(io.dekorate.s2i.config.S2iBuildConfig) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ProjectHook(io.dekorate.hook.ProjectHook) BuildService(io.dekorate.BuildService) ArrayList(java.util.ArrayList) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) OpenshiftConfig(io.dekorate.openshift.config.OpenshiftConfig) OrderedHook(io.dekorate.hook.OrderedHook) DekorateException(io.dekorate.DekorateException) WithProject(io.dekorate.WithProject) Project(io.dekorate.project.Project) ResourcesApplyHook(io.dekorate.hook.ResourcesApplyHook) ImageConfiguration(io.dekorate.kubernetes.config.ImageConfiguration) ImageBuildHook(io.dekorate.hook.ImageBuildHook) ImagePushHook(io.dekorate.hook.ImagePushHook) Session(io.dekorate.Session) WithSession(io.dekorate.WithSession)

Aggregations

BuildService (io.dekorate.BuildService)2 DekorateException (io.dekorate.DekorateException)2 Session (io.dekorate.Session)2 WithProject (io.dekorate.WithProject)2 WithSession (io.dekorate.WithSession)2 ImageBuildHook (io.dekorate.hook.ImageBuildHook)2 ImagePushHook (io.dekorate.hook.ImagePushHook)2 OrderedHook (io.dekorate.hook.OrderedHook)2 ProjectHook (io.dekorate.hook.ProjectHook)2 ResourcesApplyHook (io.dekorate.hook.ResourcesApplyHook)2 ImageConfiguration (io.dekorate.kubernetes.config.ImageConfiguration)2 Project (io.dekorate.project.Project)2 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)2 ArrayList (java.util.ArrayList)2 BuildServiceFactories (io.dekorate.BuildServiceFactories)1 ResourceRegistry (io.dekorate.ResourceRegistry)1 SessionListener (io.dekorate.SessionListener)1 KubernetesConfig (io.dekorate.kubernetes.config.KubernetesConfig)1 ScaleDeploymentHook (io.dekorate.kubernetes.hook.ScaleDeploymentHook)1 OpenshiftConfig (io.dekorate.openshift.config.OpenshiftConfig)1