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();
}
}
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()));
}
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();
}
}
}
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());
}
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());
}
}
Aggregations