Search in sources :

Example 1 with ResourceRegistry

use of io.dekorate.ResourceRegistry 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 ResourceRegistry

use of io.dekorate.ResourceRegistry in project dekorate by dekorateio.

the class KubernetesManifestGeneratorTest method shouldAcceptKubernetesConfig.

@Test
public void shouldAcceptKubernetesConfig() {
    KubernetesManifestGenerator generator = new KubernetesManifestGenerator(new ResourceRegistry(), new ConfigurationRegistry());
    assertTrue(generator.accepts(KubernetesConfig.class));
}
Also used : EditableKubernetesConfig(io.dekorate.kubernetes.config.EditableKubernetesConfig) KubernetesConfig(io.dekorate.kubernetes.config.KubernetesConfig) ConfigurationRegistry(io.dekorate.ConfigurationRegistry) ResourceRegistry(io.dekorate.ResourceRegistry) KubernetesManifestGenerator(io.dekorate.kubernetes.manifest.KubernetesManifestGenerator) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceRegistry

use of io.dekorate.ResourceRegistry in project dekorate by dekorateio.

the class KubernetesManifestGeneratorTest method shouldNotAcceptKubernetesConfigSubclasses.

@Test
public void shouldNotAcceptKubernetesConfigSubclasses() {
    KubernetesManifestGenerator generator = new KubernetesManifestGenerator(new ResourceRegistry(), new ConfigurationRegistry());
    assertFalse(generator.accepts(KubernetesConfigSubclass.class));
}
Also used : ConfigurationRegistry(io.dekorate.ConfigurationRegistry) ResourceRegistry(io.dekorate.ResourceRegistry) KubernetesManifestGenerator(io.dekorate.kubernetes.manifest.KubernetesManifestGenerator) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceRegistry

use of io.dekorate.ResourceRegistry in project dekorate by dekorateio.

the class KubernetesManifestGeneratorTest method shouldAcceptEditableKubernetesConfig.

@Test
public void shouldAcceptEditableKubernetesConfig() {
    KubernetesManifestGenerator generator = new KubernetesManifestGenerator(new ResourceRegistry(), new ConfigurationRegistry());
    assertTrue(generator.accepts(EditableKubernetesConfig.class));
}
Also used : ConfigurationRegistry(io.dekorate.ConfigurationRegistry) ResourceRegistry(io.dekorate.ResourceRegistry) EditableKubernetesConfig(io.dekorate.kubernetes.config.EditableKubernetesConfig) KubernetesManifestGenerator(io.dekorate.kubernetes.manifest.KubernetesManifestGenerator) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceRegistry (io.dekorate.ResourceRegistry)4 ConfigurationRegistry (io.dekorate.ConfigurationRegistry)3 KubernetesManifestGenerator (io.dekorate.kubernetes.manifest.KubernetesManifestGenerator)3 Test (org.junit.jupiter.api.Test)3 EditableKubernetesConfig (io.dekorate.kubernetes.config.EditableKubernetesConfig)2 KubernetesConfig (io.dekorate.kubernetes.config.KubernetesConfig)2 BuildService (io.dekorate.BuildService)1 DekorateException (io.dekorate.DekorateException)1 Session (io.dekorate.Session)1 WithProject (io.dekorate.WithProject)1 WithSession (io.dekorate.WithSession)1 ImageBuildHook (io.dekorate.hook.ImageBuildHook)1 ImagePushHook (io.dekorate.hook.ImagePushHook)1 OrderedHook (io.dekorate.hook.OrderedHook)1 ProjectHook (io.dekorate.hook.ProjectHook)1 ResourcesApplyHook (io.dekorate.hook.ResourcesApplyHook)1 ImageConfiguration (io.dekorate.kubernetes.config.ImageConfiguration)1 ScaleDeploymentHook (io.dekorate.kubernetes.hook.ScaleDeploymentHook)1 Project (io.dekorate.project.Project)1 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)1