Search in sources :

Example 1 with KubernetesConfig

use of io.dekorate.kubernetes.config.KubernetesConfig in project quarkus by quarkusio.

the class MinikubeManifestGenerator method generate.

public void generate(KubernetesConfig config) {
    ImageConfiguration imageConfig = getImageConfiguration(getProject(), config, configurationRegistry);
    Optional<Deployment> existingDeployment = resourceRegistry.groups().getOrDefault(MINIKUBE, new KubernetesListBuilder()).buildItems().stream().filter(i -> i instanceof Deployment).map(i -> (Deployment) i).filter(i -> i.getMetadata().getName().equals(config.getName())).findAny();
    if (!existingDeployment.isPresent()) {
        resourceRegistry.add(MINIKUBE, createDeployment(config, imageConfig));
    }
    addDecorators(MINIKUBE, config);
    if (config.isHeadless()) {
        resourceRegistry.decorate(MINIKUBE, new ApplyHeadlessDecorator(config.getName()));
    }
    if (config.getReplicas() != 1) {
        resourceRegistry.decorate(MINIKUBE, new ApplyReplicasToDeploymentDecorator(config.getName(), config.getReplicas()));
        resourceRegistry.decorate(MINIKUBE, new ApplyReplicasToStatefulSetDecorator(config.getName(), config.getReplicas()));
    }
    String image = Strings.isNotNullOrEmpty(imageConfig.getImage()) ? imageConfig.getImage() : Images.getImage(imageConfig.isAutoPushEnabled() ? (Strings.isNullOrEmpty(imageConfig.getRegistry()) ? DEFAULT_REGISTRY : imageConfig.getRegistry()) : imageConfig.getRegistry(), imageConfig.getGroup(), imageConfig.getName(), imageConfig.getVersion());
    resourceRegistry.decorate(MINIKUBE, new ApplyImageDecorator(config.getName(), image));
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) WithProject(io.dekorate.WithProject) Strings(io.dekorate.utils.Strings) Images(io.dekorate.utils.Images) KubernetesConfigBuilder(io.dekorate.kubernetes.config.KubernetesConfigBuilder) AddIngressDecorator(io.dekorate.kubernetes.decorator.AddIngressDecorator) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Project(io.dekorate.project.Project) AbstractKubernetesManifestGenerator(io.dekorate.AbstractKubernetesManifestGenerator) ApplyProjectInfo(io.dekorate.project.ApplyProjectInfo) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) ApplyDeployToApplicationConfiguration(io.dekorate.kubernetes.configurator.ApplyDeployToApplicationConfiguration) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Labels(io.dekorate.utils.Labels) ConfigurationSupplier(io.dekorate.config.ConfigurationSupplier) AddServiceResourceDecorator(io.dekorate.kubernetes.decorator.AddServiceResourceDecorator) ApplyImageDecorator(io.dekorate.kubernetes.decorator.ApplyImageDecorator) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) EditableKubernetesConfig(io.dekorate.kubernetes.config.EditableKubernetesConfig) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) Configuration(io.dekorate.kubernetes.config.Configuration) ConfigurationRegistry(io.dekorate.ConfigurationRegistry) BuildServiceFactories(io.dekorate.BuildServiceFactories) AddInitContainerDecorator(io.dekorate.kubernetes.decorator.AddInitContainerDecorator) ImageConfigurationBuilder(io.dekorate.kubernetes.config.ImageConfigurationBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Container(io.dekorate.kubernetes.config.Container) ImageConfiguration(io.dekorate.kubernetes.config.ImageConfiguration) ResourceRegistry(io.dekorate.ResourceRegistry) KubernetesConfig(io.dekorate.kubernetes.config.KubernetesConfig) Optional(java.util.Optional) ApplyHeadlessDecorator(io.dekorate.kubernetes.decorator.ApplyHeadlessDecorator) ApplyReplicasToDeploymentDecorator(io.dekorate.kubernetes.decorator.ApplyReplicasToDeploymentDecorator) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ImageConfiguration(io.dekorate.kubernetes.config.ImageConfiguration) ApplyReplicasToDeploymentDecorator(io.dekorate.kubernetes.decorator.ApplyReplicasToDeploymentDecorator) ApplyImageDecorator(io.dekorate.kubernetes.decorator.ApplyImageDecorator) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ApplyHeadlessDecorator(io.dekorate.kubernetes.decorator.ApplyHeadlessDecorator)

Example 2 with KubernetesConfig

use of io.dekorate.kubernetes.config.KubernetesConfig 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 3 with KubernetesConfig

use of io.dekorate.kubernetes.config.KubernetesConfig in project dekorate by dekorateio.

the class KubernetesManifestGenerator method generate.

public void generate(KubernetesConfig config) {
    LOGGER.info("Processing kubernetes configuration.");
    ImageConfiguration imageConfig = getImageConfiguration(getProject(), config, configurationRegistry);
    Optional<Deployment> existingDeployment = resourceRegistry.groups().getOrDefault(KUBERNETES, new KubernetesListBuilder()).buildItems().stream().filter(i -> i instanceof Deployment).map(i -> (Deployment) i).filter(i -> i.getMetadata().getName().equals(config.getName())).findAny();
    if (!existingDeployment.isPresent()) {
        resourceRegistry.add(KUBERNETES, createDeployment(config, imageConfig));
    }
    addDecorators(KUBERNETES, config);
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) WithProject(io.dekorate.WithProject) Strings(io.dekorate.utils.Strings) Images(io.dekorate.utils.Images) KubernetesConfigBuilder(io.dekorate.kubernetes.config.KubernetesConfigBuilder) AddIngressDecorator(io.dekorate.kubernetes.decorator.AddIngressDecorator) Project(io.dekorate.project.Project) AbstractKubernetesManifestGenerator(io.dekorate.AbstractKubernetesManifestGenerator) ApplyProjectInfo(io.dekorate.project.ApplyProjectInfo) HashMap(java.util.HashMap) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) AddVcsUrlAnnotationDecorator(io.dekorate.kubernetes.decorator.AddVcsUrlAnnotationDecorator) ApplyDeployToApplicationConfiguration(io.dekorate.kubernetes.configurator.ApplyDeployToApplicationConfiguration) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Labels(io.dekorate.utils.Labels) ConfigurationSupplier(io.dekorate.config.ConfigurationSupplier) AddServiceResourceDecorator(io.dekorate.kubernetes.decorator.AddServiceResourceDecorator) ApplyImageDecorator(io.dekorate.kubernetes.decorator.ApplyImageDecorator) AddIngressRuleDecorator(io.dekorate.kubernetes.decorator.AddIngressRuleDecorator) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) EditableKubernetesConfig(io.dekorate.kubernetes.config.EditableKubernetesConfig) ApplyApplicationContainerDecorator(io.dekorate.kubernetes.decorator.ApplyApplicationContainerDecorator) Logger(io.dekorate.Logger) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Annotations(io.dekorate.utils.Annotations) Configuration(io.dekorate.kubernetes.config.Configuration) ConfigurationRegistry(io.dekorate.ConfigurationRegistry) LoggerFactory(io.dekorate.LoggerFactory) BuildServiceFactories(io.dekorate.BuildServiceFactories) AddInitContainerDecorator(io.dekorate.kubernetes.decorator.AddInitContainerDecorator) ApplyDeploymentStrategyDecorator(io.dekorate.kubernetes.decorator.ApplyDeploymentStrategyDecorator) ApplyReplicasDecorator(io.dekorate.kubernetes.decorator.ApplyReplicasDecorator) VcsConfig(io.dekorate.option.config.VcsConfig) ImageConfigurationBuilder(io.dekorate.kubernetes.config.ImageConfigurationBuilder) Ports(io.dekorate.utils.Ports) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) ImagePullPolicy(io.dekorate.kubernetes.annotation.ImagePullPolicy) Container(io.dekorate.kubernetes.config.Container) ImageConfiguration(io.dekorate.kubernetes.config.ImageConfiguration) ResourceRegistry(io.dekorate.ResourceRegistry) KubernetesConfig(io.dekorate.kubernetes.config.KubernetesConfig) Optional(java.util.Optional) ApplyHeadlessDecorator(io.dekorate.kubernetes.decorator.ApplyHeadlessDecorator) ContainerBuilder(io.dekorate.kubernetes.config.ContainerBuilder) AddCommitIdAnnotationDecorator(io.dekorate.kubernetes.decorator.AddCommitIdAnnotationDecorator) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Git(io.dekorate.utils.Git) ImageConfiguration(io.dekorate.kubernetes.config.ImageConfiguration) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment)

Example 4 with KubernetesConfig

use of io.dekorate.kubernetes.config.KubernetesConfig in project dekorate by dekorateio.

the class KubernetesConfigAdapterTest method testKubernetesAppWithPorts.

@Test
public void testKubernetesAppWithPorts() {
    final Map<String, Object>[] ports = new Map[1];
    ports[0] = new HashMap<String, Object>() {

        {
            put("name", "http");
            put("containerPort", 8080);
            put("protocol", Protocol.TCP);
        }
    };
    Map<String, Object> map = new HashMap<String, Object>() {

        {
            put("name", "generator-test");
            put("group", "generator-test-group");
            put("version", "latest");
            put("replicas", 2);
            put("ports", ports);
        }
    };
    KubernetesConfig config = KubernetesConfigAdapter.adapt(map);
    assertNotNull(config);
    assertEquals(1, config.getPorts().length);
    assertEquals(8080, config.getPorts()[0].getContainerPort());
}
Also used : HashMap(java.util.HashMap) KubernetesConfig(io.dekorate.kubernetes.config.KubernetesConfig) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Aggregations

KubernetesConfig (io.dekorate.kubernetes.config.KubernetesConfig)4 ResourceRegistry (io.dekorate.ResourceRegistry)3 WithProject (io.dekorate.WithProject)3 ImageConfiguration (io.dekorate.kubernetes.config.ImageConfiguration)3 Project (io.dekorate.project.Project)3 AbstractKubernetesManifestGenerator (io.dekorate.AbstractKubernetesManifestGenerator)2 BuildServiceFactories (io.dekorate.BuildServiceFactories)2 ConfigurationRegistry (io.dekorate.ConfigurationRegistry)2 ConfigurationSupplier (io.dekorate.config.ConfigurationSupplier)2 Configuration (io.dekorate.kubernetes.config.Configuration)2 Container (io.dekorate.kubernetes.config.Container)2 EditableKubernetesConfig (io.dekorate.kubernetes.config.EditableKubernetesConfig)2 ImageConfigurationBuilder (io.dekorate.kubernetes.config.ImageConfigurationBuilder)2 KubernetesConfigBuilder (io.dekorate.kubernetes.config.KubernetesConfigBuilder)2 ApplyDeployToApplicationConfiguration (io.dekorate.kubernetes.configurator.ApplyDeployToApplicationConfiguration)2 AddIngressDecorator (io.dekorate.kubernetes.decorator.AddIngressDecorator)2 AddInitContainerDecorator (io.dekorate.kubernetes.decorator.AddInitContainerDecorator)2 AddServiceResourceDecorator (io.dekorate.kubernetes.decorator.AddServiceResourceDecorator)2 ApplyHeadlessDecorator (io.dekorate.kubernetes.decorator.ApplyHeadlessDecorator)2 ApplyImageDecorator (io.dekorate.kubernetes.decorator.ApplyImageDecorator)2