Search in sources :

Example 1 with AddAnnotationDecorator

use of io.dekorate.kubernetes.decorator.AddAnnotationDecorator in project quarkus by quarkusio.

the class KubernetesCommonHelper method createDecorators.

/**
 * Creates the common decorator build items.
 */
public static List<DecoratorBuildItem> createDecorators(Optional<Project> project, String target, String name, PlatformConfiguration config, Optional<MetricsCapabilityBuildItem> metricsConfiguration, List<KubernetesAnnotationBuildItem> annotations, List<KubernetesLabelBuildItem> labels, Optional<KubernetesCommandBuildItem> command, List<KubernetesPortBuildItem> ports, Optional<KubernetesHealthLivenessPathBuildItem> livenessProbePath, Optional<KubernetesHealthReadinessPathBuildItem> readinessProbePath, List<KubernetesRoleBuildItem> roles, List<KubernetesRoleBindingBuildItem> roleBindings) {
    List<DecoratorBuildItem> result = new ArrayList<>();
    annotations.forEach(a -> {
        result.add(new DecoratorBuildItem(a.getTarget(), new AddAnnotationDecorator(name, a.getKey(), a.getValue())));
    });
    labels.forEach(l -> {
        result.add(new DecoratorBuildItem(l.getTarget(), new AddLabelDecorator(name, l.getKey(), l.getValue())));
    });
    result.addAll(createAnnotationDecorators(project, target, name, config, metricsConfiguration, ports));
    result.addAll(createPodDecorators(project, target, name, config));
    result.addAll(createContainerDecorators(project, target, name, config));
    result.addAll(createMountAndVolumeDecorators(project, target, name, config));
    result.addAll(createAppConfigVolumeAndEnvDecorators(project, target, name, config));
    result.addAll(createCommandDecorator(project, target, name, config, command));
    result.addAll(createArgsDecorator(project, target, name, config, command));
    // Handle Probes
    if (!ports.isEmpty()) {
        result.addAll(createProbeDecorators(name, target, config.getLivenessProbe(), config.getReadinessProbe(), livenessProbePath, readinessProbePath));
    }
    // Handle RBAC
    if (!roleBindings.isEmpty()) {
        result.add(new DecoratorBuildItem(target, new ApplyServiceAccountNameDecorator()));
        result.add(new DecoratorBuildItem(target, new AddServiceAccountResourceDecorator()));
        roles.forEach(r -> result.add(new DecoratorBuildItem(target, new AddRoleResourceDecorator(name, r))));
        roleBindings.forEach(rb -> {
            result.add(new DecoratorBuildItem(target, new AddRoleBindingResourceDecorator(rb.getName(), null, rb.getRole(), rb.isClusterWide() ? AddRoleBindingResourceDecorator.RoleKind.ClusterRole : AddRoleBindingResourceDecorator.RoleKind.Role)));
            labels.forEach(l -> {
                result.add(new DecoratorBuildItem(target, new AddLabelDecorator(rb.getName(), l.getKey(), l.getValue(), "RoleBinding")));
            });
        });
    }
    return result;
}
Also used : AddRoleBindingResourceDecorator(io.dekorate.kubernetes.decorator.AddRoleBindingResourceDecorator) AddServiceAccountResourceDecorator(io.dekorate.kubernetes.decorator.AddServiceAccountResourceDecorator) DecoratorBuildItem(io.quarkus.kubernetes.spi.DecoratorBuildItem) AddAnnotationDecorator(io.dekorate.kubernetes.decorator.AddAnnotationDecorator) ArrayList(java.util.ArrayList) AddLabelDecorator(io.dekorate.kubernetes.decorator.AddLabelDecorator)

Example 2 with AddAnnotationDecorator

use of io.dekorate.kubernetes.decorator.AddAnnotationDecorator in project quarkus by quarkusio.

the class KubernetesCommonHelper method createAnnotationDecorators.

private static List<DecoratorBuildItem> createAnnotationDecorators(Optional<Project> project, String target, String name, PlatformConfiguration config, Optional<MetricsCapabilityBuildItem> metricsConfiguration, List<KubernetesPortBuildItem> ports) {
    List<DecoratorBuildItem> result = new ArrayList<>();
    ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
    project.ifPresent(p -> {
        ScmInfo scm = p.getScmInfo();
        String vcsUrl = scm != null ? scm.getRemote().get("origin") : null;
        String commitId = scm != null ? scm.getCommit() : null;
        // Dekorate uses its own annotations. Let's replace them with the quarkus ones.
        result.add(new DecoratorBuildItem(target, new RemoveAnnotationDecorator(Annotations.VCS_URL)));
        result.add(new DecoratorBuildItem(target, new RemoveAnnotationDecorator(Annotations.COMMIT_ID)));
        // Add quarkus vcs annotations
        if (commitId != null) {
            result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name, new Annotation(QUARKUS_ANNOTATIONS_COMMIT_ID, commitId, new String[0]))));
        }
        if (vcsUrl != null) {
            result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name, new Annotation(QUARKUS_ANNOTATIONS_VCS_URL, vcsUrl, new String[0]))));
        }
    });
    if (config.isAddBuildTimestamp()) {
        result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name, new Annotation(QUARKUS_ANNOTATIONS_BUILD_TIMESTAMP, now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd - HH:mm:ss Z")), new String[0]))));
    }
    if (config.getExposition().isPresent() && config.getExposition().get().expose) {
        Map<String, String> expostionAnnotations = config.getExposition().get().annotations;
        String kind = "Ingress";
        if (config.getTargetPlatformName().equals(OPENSHIFT)) {
            kind = "Route";
        }
        for (Map.Entry<String, String> annotation : expostionAnnotations.entrySet()) {
            result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name, annotation.getKey(), annotation.getValue(), kind)));
        }
    }
    if (config.getPrometheusConfig().annotations) {
        // Add metrics annotations
        metricsConfiguration.ifPresent(m -> {
            String path = m.metricsEndpoint();
            String prefix = config.getPrometheusConfig().prefix;
            if (!ports.isEmpty() && path != null) {
                result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name, config.getPrometheusConfig().scrape.orElse(prefix + "/scrape"), "true", PROMETHEUS_ANNOTATION_TARGETS)));
                result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name, config.getPrometheusConfig().path.orElse(prefix + "/path"), path, PROMETHEUS_ANNOTATION_TARGETS)));
                result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name, config.getPrometheusConfig().port.orElse(prefix + "/port"), "" + ports.get(0).getPort(), PROMETHEUS_ANNOTATION_TARGETS)));
                result.add(new DecoratorBuildItem(target, new AddAnnotationDecorator(name, config.getPrometheusConfig().scheme.orElse(prefix + "/scheme"), "http", PROMETHEUS_ANNOTATION_TARGETS)));
            }
        });
    }
    // Add metrics annotations
    return result;
}
Also used : DecoratorBuildItem(io.quarkus.kubernetes.spi.DecoratorBuildItem) AddAnnotationDecorator(io.dekorate.kubernetes.decorator.AddAnnotationDecorator) ZonedDateTime(java.time.ZonedDateTime) RemoveAnnotationDecorator(io.dekorate.kubernetes.decorator.RemoveAnnotationDecorator) ArrayList(java.util.ArrayList) ScmInfo(io.dekorate.project.ScmInfo) Map(java.util.Map) HashMap(java.util.HashMap) Annotation(io.dekorate.kubernetes.config.Annotation)

Example 3 with AddAnnotationDecorator

use of io.dekorate.kubernetes.decorator.AddAnnotationDecorator in project dekorate by dekorateio.

the class JaegerProcessor method generate.

@Override
public void generate(JaegerAgentConfig config) {
    if (config.isOperatorEnabled()) {
        resourceRegistry.decorate(new AddAnnotationDecorator(new AnnotationBuilder().withKey("sidecar.jaegertracing.io/inject").withValue("true").build()));
    } else {
        ContainerBuilder builder = new ContainerBuilder().withName(AGENT_NAME).withImage(AGENT_IMAGE + ":" + config.getVersion()).withArguments("--collector.host-port=" + collectorHostPort(config));
        for (Port port : config.getPorts()) {
            // We can't use the AddPortToContainerDecorator as it expects to be applies on a top level resource.
            builder = builder.addNewPort().withName(port.getName()).withHostPort(port.getHostPort() > 0 ? port.getHostPort() : 0).withContainerPort(port.getContainerPort()).withProtocol(port.getProtocol() != null ? port.getProtocol() : Protocol.TCP).endPort();
        }
        resourceRegistry.decorate(new AddSidecarDecorator(builder.build()));
    }
}
Also used : ContainerBuilder(io.dekorate.kubernetes.config.ContainerBuilder) AddAnnotationDecorator(io.dekorate.kubernetes.decorator.AddAnnotationDecorator) AnnotationBuilder(io.dekorate.kubernetes.config.AnnotationBuilder) Port(io.dekorate.kubernetes.config.Port) AddSidecarDecorator(io.dekorate.kubernetes.decorator.AddSidecarDecorator)

Example 4 with AddAnnotationDecorator

use of io.dekorate.kubernetes.decorator.AddAnnotationDecorator in project dekorate by dekorateio.

the class AbstractKubernetesManifestGenerator method addDecorators.

/**
 * Add all decorator to the resources.
 * This method will read the config and then add all the required decorator to the resources.
 * The method is intended to be called from the generate method and thus marked as protected.
 *
 * @param group The group..
 * @param config The config.
 */
protected void addDecorators(String group, C config) {
    if (Strings.isNotNullOrEmpty(config.getServiceAccount())) {
        resourceRegistry.decorate(new ApplyServiceAccountNamedDecorator(config.getName(), config.getServiceAccount()));
    }
    if (config.getImagePullPolicy() != ImagePullPolicy.IfNotPresent) {
        resourceRegistry.decorate(group, new ApplyImagePullPolicyDecorator(config.getImagePullPolicy()));
    }
    for (String imagePullSecret : config.getImagePullSecrets()) {
        resourceRegistry.decorate(new AddImagePullSecretDecorator(config.getName(), imagePullSecret));
    }
    // Metadata handling
    Labels.createLabels(config).forEach(l -> {
        resourceRegistry.decorate(group, new AddLabelDecorator(config.getName(), l));
        resourceRegistry.decorate(group, new AddToSelectorDecorator(config.getName(), l.getKey(), l.getValue()));
        resourceRegistry.decorate(group, new AddToMatchingLabelsDecorator(config.getName(), l.getKey(), l.getValue()));
    });
    for (Annotation annotation : config.getAnnotations()) {
        resourceRegistry.decorate(new AddAnnotationDecorator(config.getName(), annotation));
    }
    if (Strings.isNotNullOrEmpty(config.getServiceAccount())) {
        resourceRegistry.decorate(group, new ApplyServiceAccountNamedDecorator(config.getName(), config.getServiceAccount()));
    }
    if (config.getImagePullPolicy() != ImagePullPolicy.IfNotPresent) {
        resourceRegistry.decorate(group, new ApplyImagePullPolicyDecorator(config.getName(), config.getImagePullPolicy()));
    }
    for (String imagePullSecret : config.getImagePullSecrets()) {
        resourceRegistry.decorate(group, new AddImagePullSecretDecorator(config.getName(), imagePullSecret));
    }
    for (HostAlias hostAlias : config.getHostAliases()) {
        resourceRegistry.decorate(new AddHostAliasesDecorator(config.getName(), hostAlias));
    }
    for (Container container : config.getSidecars()) {
        resourceRegistry.decorate(group, new AddSidecarDecorator(config.getName(), container));
    }
    for (Env env : config.getEnvVars()) {
        resourceRegistry.decorate(group, new AddEnvVarDecorator(config.getName(), config.getName(), env));
    }
    for (Port port : config.getPorts()) {
        resourceRegistry.decorate(group, new AddPortDecorator(config.getName(), config.getName(), port));
    }
    for (Mount mount : config.getMounts()) {
        resourceRegistry.decorate(group, new AddMountDecorator(config.getName(), config.getName(), mount));
    }
    for (SecretVolume volume : config.getSecretVolumes()) {
        validateVolume(volume);
        resourceRegistry.decorate(group, new AddSecretVolumeDecorator(volume));
    }
    for (ConfigMapVolume volume : config.getConfigMapVolumes()) {
        validateVolume(volume);
        resourceRegistry.decorate(group, new AddConfigMapVolumeDecorator(volume));
    }
    for (PersistentVolumeClaimVolume volume : config.getPvcVolumes()) {
        resourceRegistry.decorate(group, new AddPvcVolumeDecorator(volume));
    }
    for (AzureFileVolume volume : config.getAzureFileVolumes()) {
        resourceRegistry.decorate(group, new AddAzureFileVolumeDecorator(volume));
    }
    for (AzureDiskVolume volume : config.getAzureDiskVolumes()) {
        resourceRegistry.decorate(group, new AddAzureDiskVolumeDecorator(volume));
    }
    for (AwsElasticBlockStoreVolume volume : config.getAwsElasticBlockStoreVolumes()) {
        resourceRegistry.decorate(group, new AddAwsElasticBlockStoreVolumeDecorator(volume));
    }
    if (config.getCommand() != null && config.getCommand().length > 0) {
        resourceRegistry.decorate(group, new ApplyCommandDecorator(config.getName(), config.getName(), config.getCommand()));
    }
    if (config.getArguments() != null && config.getArguments().length > 0) {
        resourceRegistry.decorate(group, new ApplyArgsDecorator(config.getName(), config.getName(), config.getArguments()));
    }
    if (Probes.isConfigured(config.getLivenessProbe())) {
        resourceRegistry.decorate(group, new AddLivenessProbeDecorator(config.getName(), config.getName(), config.getLivenessProbe()));
    }
    if (Probes.isConfigured(config.getReadinessProbe())) {
        resourceRegistry.decorate(group, new AddReadinessProbeDecorator(config.getName(), config.getName(), config.getReadinessProbe()));
    }
    // Container resources
    if (config.getLimitResources() != null) {
        if (Strings.isNotNullOrEmpty(config.getLimitResources().getCpu())) {
            resourceRegistry.decorate(group, new ApplyLimitsCpuDecorator(config.getName(), config.getName(), config.getLimitResources().getCpu()));
        }
        if (Strings.isNotNullOrEmpty(config.getLimitResources().getMemory())) {
            resourceRegistry.decorate(group, new ApplyLimitsMemoryDecorator(config.getName(), config.getName(), config.getLimitResources().getMemory()));
        }
    }
    if (config.getRequestResources() != null) {
        if (Strings.isNotNullOrEmpty(config.getRequestResources().getCpu())) {
            resourceRegistry.decorate(group, new ApplyRequestsCpuDecorator(config.getName(), config.getName(), config.getRequestResources().getCpu()));
        }
        if (Strings.isNotNullOrEmpty(config.getRequestResources().getMemory())) {
            resourceRegistry.decorate(group, new ApplyRequestsMemoryDecorator(config.getName(), config.getName(), config.getRequestResources().getMemory()));
        }
    }
    resourceRegistry.decorate(group, new RemoveProbesFromInitContainerDecorator());
}
Also used : AddPvcVolumeDecorator(io.dekorate.kubernetes.decorator.AddPvcVolumeDecorator) AddImagePullSecretDecorator(io.dekorate.kubernetes.decorator.AddImagePullSecretDecorator) AddAzureFileVolumeDecorator(io.dekorate.kubernetes.decorator.AddAzureFileVolumeDecorator) AzureDiskVolume(io.dekorate.kubernetes.config.AzureDiskVolume) PersistentVolumeClaimVolume(io.dekorate.kubernetes.config.PersistentVolumeClaimVolume) AddLivenessProbeDecorator(io.dekorate.kubernetes.decorator.AddLivenessProbeDecorator) AddAzureDiskVolumeDecorator(io.dekorate.kubernetes.decorator.AddAzureDiskVolumeDecorator) Port(io.dekorate.kubernetes.config.Port) ApplyLimitsMemoryDecorator(io.dekorate.kubernetes.decorator.ApplyLimitsMemoryDecorator) Mount(io.dekorate.kubernetes.config.Mount) AddEnvVarDecorator(io.dekorate.kubernetes.decorator.AddEnvVarDecorator) AddConfigMapVolumeDecorator(io.dekorate.kubernetes.decorator.AddConfigMapVolumeDecorator) Env(io.dekorate.kubernetes.config.Env) AddSidecarDecorator(io.dekorate.kubernetes.decorator.AddSidecarDecorator) ApplyRequestsMemoryDecorator(io.dekorate.kubernetes.decorator.ApplyRequestsMemoryDecorator) ApplyImagePullPolicyDecorator(io.dekorate.kubernetes.decorator.ApplyImagePullPolicyDecorator) Container(io.dekorate.kubernetes.config.Container) ApplyLimitsCpuDecorator(io.dekorate.kubernetes.decorator.ApplyLimitsCpuDecorator) AddToSelectorDecorator(io.dekorate.kubernetes.decorator.AddToSelectorDecorator) AddPortDecorator(io.dekorate.kubernetes.decorator.AddPortDecorator) AwsElasticBlockStoreVolume(io.dekorate.kubernetes.config.AwsElasticBlockStoreVolume) ConfigMapVolume(io.dekorate.kubernetes.config.ConfigMapVolume) ApplyCommandDecorator(io.dekorate.kubernetes.decorator.ApplyCommandDecorator) RemoveProbesFromInitContainerDecorator(io.dekorate.kubernetes.decorator.RemoveProbesFromInitContainerDecorator) AddAnnotationDecorator(io.dekorate.kubernetes.decorator.AddAnnotationDecorator) AddHostAliasesDecorator(io.dekorate.kubernetes.decorator.AddHostAliasesDecorator) SecretVolume(io.dekorate.kubernetes.config.SecretVolume) AddToMatchingLabelsDecorator(io.dekorate.kubernetes.decorator.AddToMatchingLabelsDecorator) AddAwsElasticBlockStoreVolumeDecorator(io.dekorate.kubernetes.decorator.AddAwsElasticBlockStoreVolumeDecorator) Annotation(io.dekorate.kubernetes.config.Annotation) HostAlias(io.dekorate.kubernetes.config.HostAlias) AzureFileVolume(io.dekorate.kubernetes.config.AzureFileVolume) ApplyArgsDecorator(io.dekorate.kubernetes.decorator.ApplyArgsDecorator) AddReadinessProbeDecorator(io.dekorate.kubernetes.decorator.AddReadinessProbeDecorator) ApplyRequestsCpuDecorator(io.dekorate.kubernetes.decorator.ApplyRequestsCpuDecorator) AddSecretVolumeDecorator(io.dekorate.kubernetes.decorator.AddSecretVolumeDecorator) AddLabelDecorator(io.dekorate.kubernetes.decorator.AddLabelDecorator) ApplyServiceAccountNamedDecorator(io.dekorate.kubernetes.decorator.ApplyServiceAccountNamedDecorator) AddMountDecorator(io.dekorate.kubernetes.decorator.AddMountDecorator)

Aggregations

AddAnnotationDecorator (io.dekorate.kubernetes.decorator.AddAnnotationDecorator)4 Annotation (io.dekorate.kubernetes.config.Annotation)2 Port (io.dekorate.kubernetes.config.Port)2 AddLabelDecorator (io.dekorate.kubernetes.decorator.AddLabelDecorator)2 AddSidecarDecorator (io.dekorate.kubernetes.decorator.AddSidecarDecorator)2 AnnotationBuilder (io.dekorate.kubernetes.config.AnnotationBuilder)1 AwsElasticBlockStoreVolume (io.dekorate.kubernetes.config.AwsElasticBlockStoreVolume)1 AzureDiskVolume (io.dekorate.kubernetes.config.AzureDiskVolume)1 AzureFileVolume (io.dekorate.kubernetes.config.AzureFileVolume)1 ConfigMapVolume (io.dekorate.kubernetes.config.ConfigMapVolume)1 Container (io.dekorate.kubernetes.config.Container)1 ContainerBuilder (io.dekorate.kubernetes.config.ContainerBuilder)1 Env (io.dekorate.kubernetes.config.Env)1 HostAlias (io.dekorate.kubernetes.config.HostAlias)1 Mount (io.dekorate.kubernetes.config.Mount)1 PersistentVolumeClaimVolume (io.dekorate.kubernetes.config.PersistentVolumeClaimVolume)1 SecretVolume (io.dekorate.kubernetes.config.SecretVolume)1 AddAwsElasticBlockStoreVolumeDecorator (io.dekorate.kubernetes.decorator.AddAwsElasticBlockStoreVolumeDecorator)1 AddAzureDiskVolumeDecorator (io.dekorate.kubernetes.decorator.AddAzureDiskVolumeDecorator)1 AddAzureFileVolumeDecorator (io.dekorate.kubernetes.decorator.AddAzureFileVolumeDecorator)1