Search in sources :

Example 1 with Project

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

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

the class KubernetesManifestGenerator method addDecorators.

@Override
protected void addDecorators(String group, KubernetesConfig config) {
    super.addDecorators(group, config);
    ImageConfiguration imageConfig = getImageConfiguration(getProject(), config, configurationRegistry);
    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());
    Container appContainer = new ContainerBuilder().withName(config.getName()).withImage(image).withImagePullPolicy(ImagePullPolicy.IfNotPresent).addNewEnvVar().withName(KUBERNETES_NAMESPACE).withField(METADATA_NAMESPACE).endEnvVar().build();
    Project project = getProject();
    Optional<VcsConfig> vcsConfig = configurationRegistry.get(VcsConfig.class);
    String remote = vcsConfig.map(VcsConfig::getRemote).orElse(Git.ORIGIN);
    boolean httpsPrefered = vcsConfig.map(VcsConfig::isHttpsPreferred).orElse(false);
    String vcsUrl = project.getScmInfo() != null && Strings.isNotNullOrEmpty(project.getScmInfo().getRemote().get(Git.ORIGIN)) ? Git.getRemoteUrl(project.getRoot(), remote, httpsPrefered).orElse(Labels.UNKNOWN) : Labels.UNKNOWN;
    resourceRegistry.decorate(group, new AddVcsUrlAnnotationDecorator(config.getName(), Annotations.VCS_URL, vcsUrl));
    resourceRegistry.decorate(group, new AddCommitIdAnnotationDecorator());
    resourceRegistry.decorate(group, new ApplyApplicationContainerDecorator(config.getName(), appContainer));
    resourceRegistry.decorate(group, new ApplyImageDecorator(config.getName(), image));
    for (Container container : config.getInitContainers()) {
        resourceRegistry.decorate(group, new AddInitContainerDecorator(config.getName(), container));
    }
    if (config.getPorts().length > 0) {
        resourceRegistry.decorate(group, new AddServiceResourceDecorator(config));
    }
    Ports.getHttpPort(config).ifPresent(p -> {
        resourceRegistry.decorate(group, new AddIngressDecorator(config, Labels.createLabelsAsMap(config, "Ingress")));
        resourceRegistry.decorate(group, new AddIngressRuleDecorator(config.getName(), config.getHost(), p));
    });
    if (config.isHeadless()) {
        resourceRegistry.decorate(KUBERNETES, new ApplyHeadlessDecorator(config.getName()));
    }
    if (config.getReplicas() != null && config.getReplicas() != 1) {
        resourceRegistry.decorate(KUBERNETES, new ApplyReplicasDecorator(config.getName(), config.getReplicas()));
    }
    resourceRegistry.decorate(KUBERNETES, new ApplyDeploymentStrategyDecorator(config.getName(), config.getDeploymentStrategy(), config.getRollingUpdate()));
}
Also used : AddIngressRuleDecorator(io.dekorate.kubernetes.decorator.AddIngressRuleDecorator) ApplyApplicationContainerDecorator(io.dekorate.kubernetes.decorator.ApplyApplicationContainerDecorator) ApplyImageDecorator(io.dekorate.kubernetes.decorator.ApplyImageDecorator) AddVcsUrlAnnotationDecorator(io.dekorate.kubernetes.decorator.AddVcsUrlAnnotationDecorator) AddServiceResourceDecorator(io.dekorate.kubernetes.decorator.AddServiceResourceDecorator) VcsConfig(io.dekorate.option.config.VcsConfig) ApplyHeadlessDecorator(io.dekorate.kubernetes.decorator.ApplyHeadlessDecorator) AddIngressDecorator(io.dekorate.kubernetes.decorator.AddIngressDecorator) ApplyDeploymentStrategyDecorator(io.dekorate.kubernetes.decorator.ApplyDeploymentStrategyDecorator) WithProject(io.dekorate.WithProject) Project(io.dekorate.project.Project) Container(io.dekorate.kubernetes.config.Container) ContainerBuilder(io.dekorate.kubernetes.config.ContainerBuilder) AddCommitIdAnnotationDecorator(io.dekorate.kubernetes.decorator.AddCommitIdAnnotationDecorator) ImageConfiguration(io.dekorate.kubernetes.config.ImageConfiguration) ApplyReplicasDecorator(io.dekorate.kubernetes.decorator.ApplyReplicasDecorator) AddInitContainerDecorator(io.dekorate.kubernetes.decorator.AddInitContainerDecorator)

Example 3 with Project

use of io.dekorate.project.Project 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)

Example 4 with Project

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

the class OpenshiftManifestGenerator method addDecorators.

protected void addDecorators(String group, OpenshiftConfig config, ImageConfiguration imageConfig) {
    super.addDecorators(group, config);
    if (config.getReplicas() != 1) {
        resourceRegistry.decorate(group, new ApplyReplicasDecorator(config.getName(), config.getReplicas()));
    }
    resourceRegistry.decorate(group, new ApplyDeploymentTriggerDecorator(config.getName(), imageConfig.getName() + ":" + imageConfig.getVersion()));
    resourceRegistry.decorate(group, new AddRouteDecorator(config));
    if (config.hasAttribute(RUNTIME_TYPE)) {
        resourceRegistry.decorate(group, new AddLabelDecorator(config.getName(), new Label(OpenshiftLabels.RUNTIME, config.getAttribute(RUNTIME_TYPE), new String[0])));
    }
    resourceRegistry.decorate(group, new RemoveAnnotationDecorator(config.getName(), Annotations.VCS_URL));
    Project project = getProject();
    Optional<VcsConfig> vcsConfig = configurationRegistry.get(VcsConfig.class);
    String remote = vcsConfig.map(VcsConfig::getRemote).orElse(Git.ORIGIN);
    boolean httpsPrefered = vcsConfig.map(VcsConfig::isHttpsPreferred).orElse(false);
    String vcsUrl = project.getScmInfo() != null && Strings.isNotNullOrEmpty(project.getScmInfo().getRemote().get(Git.ORIGIN)) ? Git.getRemoteUrl(project.getRoot(), remote, httpsPrefered).orElse(Labels.UNKNOWN) : Labels.UNKNOWN;
    resourceRegistry.decorate(group, new AddVcsUrlAnnotationDecorator(config.getName(), OpenshiftAnnotations.VCS_URL, vcsUrl));
    resourceRegistry.decorate(group, new AddCommitIdAnnotationDecorator());
}
Also used : RemoveAnnotationDecorator(io.dekorate.kubernetes.decorator.RemoveAnnotationDecorator) Label(io.dekorate.kubernetes.config.Label) AddVcsUrlAnnotationDecorator(io.dekorate.kubernetes.decorator.AddVcsUrlAnnotationDecorator) VcsConfig(io.dekorate.option.config.VcsConfig) AddRouteDecorator(io.dekorate.openshift.decorator.AddRouteDecorator) WithProject(io.dekorate.WithProject) Project(io.dekorate.project.Project) AddCommitIdAnnotationDecorator(io.dekorate.kubernetes.decorator.AddCommitIdAnnotationDecorator) ApplyDeploymentTriggerDecorator(io.dekorate.openshift.decorator.ApplyDeploymentTriggerDecorator) AddLabelDecorator(io.dekorate.kubernetes.decorator.AddLabelDecorator) ApplyReplicasDecorator(io.dekorate.openshift.decorator.ApplyReplicasDecorator)

Example 5 with Project

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

the class AddCommitIdAnnotationDecorator method andThenVisit.

@Override
public void andThenVisit(ObjectMetaBuilder builder, ObjectMeta resourceMeta) {
    Project p = getProject();
    boolean hasCommit = p.getScmInfo() != null && p.getScmInfo().getCommit() != null;
    if (hasCommit) {
        builder.addToAnnotations(annotationKey, getProject().getScmInfo().getCommit());
    }
}
Also used : WithProject(io.dekorate.WithProject) Project(io.dekorate.project.Project)

Aggregations

Project (io.dekorate.project.Project)16 DekorateException (io.dekorate.DekorateException)9 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)9 ImageConfiguration (io.dekorate.kubernetes.config.ImageConfiguration)8 List (java.util.List)7 BuildService (io.dekorate.BuildService)6 WithProject (io.dekorate.WithProject)6 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)6 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)6 BuildServiceFactories (io.dekorate.BuildServiceFactories)5 WithProject (io.dekorate.testing.WithProject)5 ArrayList (java.util.ArrayList)5 Arrays (java.util.Arrays)4 BuildServiceFactory (io.dekorate.BuildServiceFactory)3 Logger (io.dekorate.Logger)3 LoggerFactory (io.dekorate.LoggerFactory)3 AddCommitIdAnnotationDecorator (io.dekorate.kubernetes.decorator.AddCommitIdAnnotationDecorator)3 AddVcsUrlAnnotationDecorator (io.dekorate.kubernetes.decorator.AddVcsUrlAnnotationDecorator)3 VcsConfig (io.dekorate.option.config.VcsConfig)3 OpenshiftIntegrationTestConfig (io.dekorate.testing.openshift.config.OpenshiftIntegrationTestConfig)3