Skip to content

Commit

Permalink
feat: Introduce KubernetesNamespaceBuildItem
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Sep 4, 2024
1 parent c4c9c1a commit f5275ea
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.kubernetes.spi.KubernetesJobBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesNamespaceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesProbePortNameBuildItem;
import io.quarkus.kubernetes.spi.KubernetesResourceMetadataBuildItem;
Expand Down Expand Up @@ -103,6 +104,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
PackageConfig packageConfig,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
Optional<KubernetesClientCapabilityBuildItem> kubernetesClientConfiguration,
List<KubernetesNamespaceBuildItem> namespaces,
List<KubernetesInitContainerBuildItem> initContainers,
List<KubernetesJobBuildItem> jobs,
List<KubernetesAnnotationBuildItem> annotations,
Expand All @@ -122,8 +124,9 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
List<KubernetesRoleBindingBuildItem> roleBindings,
Optional<CustomProjectRootBuildItem> customProjectRoot) {

return DevClusterHelper.createDecorators(KIND, applicationInfo, outputTarget, config, packageConfig,
metricsConfiguration, kubernetesClientConfiguration, initContainers, jobs, annotations, labels, envs,
return DevClusterHelper.createDecorators(KIND, KUBERNETES, applicationInfo, outputTarget, config, packageConfig,
metricsConfiguration, kubernetesClientConfiguration, namespaces, initContainers, jobs, annotations, labels,
envs,
baseImage, image, command, ports, portName,
livenessPath, readinessPath, startupPath,
roles, clusterRoles, serviceAccounts, roleBindings, customProjectRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.kubernetes.spi.KubernetesJobBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesNamespaceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesProbePortNameBuildItem;
import io.quarkus.kubernetes.spi.KubernetesResourceMetadataBuildItem;
Expand Down Expand Up @@ -99,6 +100,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
PackageConfig packageConfig,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
Optional<KubernetesClientCapabilityBuildItem> kubernetesClientConfiguration,
List<KubernetesNamespaceBuildItem> namespaces,
List<KubernetesInitContainerBuildItem> initContainers,
List<KubernetesJobBuildItem> jobs,
List<KubernetesAnnotationBuildItem> annotations,
Expand All @@ -118,8 +120,9 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
List<KubernetesRoleBindingBuildItem> roleBindings,
Optional<CustomProjectRootBuildItem> customProjectRoot) {

return DevClusterHelper.createDecorators(MINIKUBE, applicationInfo, outputTarget, config, packageConfig,
metricsConfiguration, kubernetesClientConfiguration, initContainers, jobs, annotations, labels, envs,
return DevClusterHelper.createDecorators(MINIKUBE, KUBERNETES, applicationInfo, outputTarget, config, packageConfig,
metricsConfiguration, kubernetesClientConfiguration, namespaces, initContainers, jobs, annotations, labels,
envs,
baseImage, image, command, ports, portName,
livenessPath, readinessPath, startupPath,
roles, clusterRoles, serviceAccounts, roleBindings, customProjectRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.kubernetes.spi;

import io.quarkus.builder.item.MultiBuildItem;

public final class KubernetesNamespaceBuildItem extends MultiBuildItem {

private final String target; // Kubernetes, Openshift, Knative
private final String namespace;

public KubernetesNamespaceBuildItem(String target, String namespace) {
this.target = target;
this.namespace = namespace;
}

public String getTarget() {
return target;
}

public String getNamespace() {
return namespace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.kubernetes.spi.KubernetesJobBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesNamespaceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesProbePortNameBuildItem;
import io.quarkus.kubernetes.spi.KubernetesRoleBindingBuildItem;
Expand All @@ -59,12 +60,14 @@ public class DevClusterHelper {
public static final String DEFAULT_HASH_ALGORITHM = "SHA-256";

public static List<DecoratorBuildItem> createDecorators(String clusterKind,
String deploymentTarget,
ApplicationInfoBuildItem applicationInfo,
OutputTargetBuildItem outputTarget,
KubernetesConfig config,
PackageConfig packageConfig,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
Optional<KubernetesClientCapabilityBuildItem> kubernetesClientConfiguration,
List<KubernetesNamespaceBuildItem> namespaces,
List<KubernetesInitContainerBuildItem> initContainers,
List<KubernetesJobBuildItem> jobs,
List<KubernetesAnnotationBuildItem> annotations,
Expand All @@ -86,11 +89,15 @@ public static List<DecoratorBuildItem> createDecorators(String clusterKind,

List<DecoratorBuildItem> result = new ArrayList<>();
String name = ResourceNameUtil.getResourceName(config, applicationInfo);
Optional<KubernetesNamespaceBuildItem> namespace = namespaces.stream()
.filter(n -> deploymentTarget.equals(n.getTarget()))
.findFirst();

Optional<Project> project = KubernetesCommonHelper.createProject(applicationInfo, customProjectRoot, outputTarget,
packageConfig);
Optional<Port> port = KubernetesCommonHelper.getPort(ports, config);
result.addAll(KubernetesCommonHelper.createDecorators(project, clusterKind, name, config,

result.addAll(KubernetesCommonHelper.createDecorators(project, clusterKind, name, namespace, config,
metricsConfiguration, kubernetesClientConfiguration,
annotations, labels, image, command,
port, livenessPath, readinessPath, startupPath, roles, clusterRoles, serviceAccounts, roleBindings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import io.quarkus.kubernetes.spi.KubernetesHealthReadinessPathBuildItem;
import io.quarkus.kubernetes.spi.KubernetesHealthStartupPathBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesNamespaceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesResourceMetadataBuildItem;
import io.quarkus.kubernetes.spi.KubernetesRoleBindingBuildItem;
Expand Down Expand Up @@ -122,6 +123,13 @@ public void createLabels(KnativeConfig config, BuildProducer<KubernetesLabelBuil
});
}

@BuildStep
public void createNamespace(KnativeConfig config, BuildProducer<KubernetesNamespaceBuildItem> namespace) {
config.getNamespace().ifPresent(n -> {
namespace.produce(new KubernetesNamespaceBuildItem(KNATIVE, n));
});
}

@BuildStep
public List<ConfiguratorBuildItem> createConfigurators(KnativeConfig config, List<KubernetesPortBuildItem> ports) {
List<ConfiguratorBuildItem> result = new ArrayList<>();
Expand All @@ -141,6 +149,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
PackageConfig packageConfig,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
Optional<KubernetesClientCapabilityBuildItem> kubernetesClientConfiguration,
List<KubernetesNamespaceBuildItem> namespaces,
List<KubernetesAnnotationBuildItem> annotations,
List<KubernetesLabelBuildItem> labels,
List<KubernetesEnvBuildItem> envs,
Expand All @@ -165,10 +174,15 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
}

String name = ResourceNameUtil.getResourceName(config, applicationInfo);
Optional<KubernetesNamespaceBuildItem> namespace = namespaces.stream()
.filter(n -> KNATIVE.equals(n.getTarget()))
.findFirst();

Optional<Project> project = KubernetesCommonHelper.createProject(applicationInfo, customProjectRoot, outputTarget,
packageConfig);
Optional<Port> port = KubernetesCommonHelper.getPort(ports, config, "http");
result.addAll(KubernetesCommonHelper.createDecorators(project, KNATIVE, name, config,

result.addAll(KubernetesCommonHelper.createDecorators(project, KNATIVE, name, namespace, config,
metricsConfiguration, kubernetesClientConfiguration, annotations,
labels, image, command, port, livenessPath, readinessPath, startupProbePath,
roles, clusterRoles, serviceAccounts, roleBindings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.kubernetes.spi.KubernetesJobBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesNamespaceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesProbePortNameBuildItem;
import io.quarkus.kubernetes.spi.KubernetesRoleBindingBuildItem;
Expand Down Expand Up @@ -234,6 +235,7 @@ public static void printMessageAboutPortsThatCantChange(String target, List<Kube
* Creates the common decorator build items.
*/
public static List<DecoratorBuildItem> createDecorators(Optional<Project> project, String target, String name,
Optional<KubernetesNamespaceBuildItem> namespace,
PlatformConfiguration config,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
Optional<KubernetesClientCapabilityBuildItem> kubernetesClientConfiguration,
Expand All @@ -254,7 +256,7 @@ public static List<DecoratorBuildItem> createDecorators(Optional<Project> projec
result.addAll(createLabelDecorators(project, target, name, config, labels));
result.addAll(createAnnotationDecorators(project, target, name, config, metricsConfiguration, annotations, port));
result.addAll(createPodDecorators(project, target, name, config));
result.addAll(createContainerDecorators(project, target, name, config));
result.addAll(createContainerDecorators(project, target, name, namespace, config));
result.addAll(createMountAndVolumeDecorators(project, target, name, config));
result.addAll(createAppConfigVolumeAndEnvDecorators(project, target, name, config));

Expand Down Expand Up @@ -811,15 +813,17 @@ public void andThenVisit(ContainerBuilder builder, ObjectMeta meta) {
*
* @param target The deployment target (e.g. kubernetes, openshift, knative)
* @param name The name of the resource to accept the configuration
* param namespace The optional namespace to apply to the resource
* @param config The {@link PlatformConfiguration} instance
*/
private static List<DecoratorBuildItem> createContainerDecorators(Optional<Project> project, String target, String name,
Optional<KubernetesNamespaceBuildItem> namespace,
PlatformConfiguration config) {
List<DecoratorBuildItem> result = new ArrayList<>();
if (config.getNamespace().isPresent()) {
result.add(new DecoratorBuildItem(target, new AddNamespaceDecorator(config.getNamespace().get())));
result.add(new DecoratorBuildItem(target, new AddNamespaceToSubjectDecorator(config.getNamespace().get())));
}
namespace.ifPresent(n -> {
result.add(new DecoratorBuildItem(target, new AddNamespaceDecorator(n.getNamespace())));
result.add(new DecoratorBuildItem(target, new AddNamespaceToSubjectDecorator(n.getNamespace())));
});

config.getWorkingDir().ifPresent(w -> {
result.add(new DecoratorBuildItem(target, new ApplyWorkingDirDecorator(name, w)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.kubernetes.spi.KubernetesJobBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesNamespaceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesProbePortNameBuildItem;
import io.quarkus.kubernetes.spi.KubernetesResourceMetadataBuildItem;
Expand Down Expand Up @@ -139,6 +140,13 @@ public void createLabels(OpenshiftConfig config, BuildProducer<KubernetesLabelBu
labels.produce(new KubernetesLabelBuildItem(KubernetesLabelBuildItem.CommonLabels.MANAGED_BY, "quarkus", OPENSHIFT));
}

@BuildStep
public void createNamespace(OpenshiftConfig config, BuildProducer<KubernetesNamespaceBuildItem> namespace) {
config.getNamespace().ifPresent(n -> {
namespace.produce(new KubernetesNamespaceBuildItem(OPENSHIFT, n));
});
}

@BuildStep
public List<ConfiguratorBuildItem> createConfigurators(ApplicationInfoBuildItem applicationInfo,
OpenshiftConfig config, Capabilities capabilities, Optional<ContainerImageInfoBuildItem> image,
Expand Down Expand Up @@ -185,6 +193,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
Capabilities capabilities,
List<KubernetesInitContainerBuildItem> initContainers,
List<KubernetesJobBuildItem> jobs,
List<KubernetesNamespaceBuildItem> namespaces,
List<KubernetesAnnotationBuildItem> annotations,
List<KubernetesLabelBuildItem> labels,
List<KubernetesEnvBuildItem> envs,
Expand Down Expand Up @@ -214,7 +223,10 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
Optional<Project> project = KubernetesCommonHelper.createProject(applicationInfo, customProjectRoot, outputTarget,
packageConfig);
Optional<Port> port = KubernetesCommonHelper.getPort(ports, config, config.route.targetPort);
result.addAll(KubernetesCommonHelper.createDecorators(project, OPENSHIFT, name, config,
Optional<KubernetesNamespaceBuildItem> namespace = namespaces.stream()
.filter(n -> OPENSHIFT.equals(n.getTarget()))
.findFirst();
result.addAll(KubernetesCommonHelper.createDecorators(project, OPENSHIFT, name, namespace, config,
metricsConfiguration, kubernetesClientConfiguration,
annotations, labels, image, command,
port, livenessPath, readinessPath, startupPath, roles, clusterRoles, serviceAccounts, roleBindings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import io.quarkus.kubernetes.spi.KubernetesInitContainerBuildItem;
import io.quarkus.kubernetes.spi.KubernetesJobBuildItem;
import io.quarkus.kubernetes.spi.KubernetesLabelBuildItem;
import io.quarkus.kubernetes.spi.KubernetesNamespaceBuildItem;
import io.quarkus.kubernetes.spi.KubernetesPortBuildItem;
import io.quarkus.kubernetes.spi.KubernetesProbePortNameBuildItem;
import io.quarkus.kubernetes.spi.KubernetesResourceMetadataBuildItem;
Expand Down Expand Up @@ -112,6 +113,13 @@ public void createLabels(KubernetesConfig config, BuildProducer<KubernetesLabelB
labels.produce(new KubernetesLabelBuildItem(KubernetesLabelBuildItem.CommonLabels.MANAGED_BY, "quarkus", KUBERNETES));
}

@BuildStep
public void createNamespace(KubernetesConfig config, BuildProducer<KubernetesNamespaceBuildItem> namespace) {
config.getNamespace().ifPresent(n -> {
namespace.produce(new KubernetesNamespaceBuildItem(KUBERNETES, n));
});
}

@BuildStep
public List<ConfiguratorBuildItem> createConfigurators(KubernetesConfig config, List<KubernetesPortBuildItem> ports) {
List<ConfiguratorBuildItem> result = new ArrayList<>();
Expand All @@ -136,6 +144,7 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
PackageConfig packageConfig,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
Optional<KubernetesClientCapabilityBuildItem> kubernetesClientConfiguration,
List<KubernetesNamespaceBuildItem> namespaces,
List<KubernetesJobBuildItem> jobs,
List<KubernetesInitContainerBuildItem> initContainers,
List<KubernetesAnnotationBuildItem> annotations,
Expand All @@ -157,11 +166,14 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
return result;
}
final String name = ResourceNameUtil.getResourceName(config, applicationInfo);
Optional<KubernetesNamespaceBuildItem> namespace = namespaces.stream()
.filter(n -> KUBERNETES.equals(n.getTarget()))
.findFirst();

Optional<Project> project = KubernetesCommonHelper.createProject(applicationInfo, customProjectRoot, outputTarget,
packageConfig);
Optional<Port> port = KubernetesCommonHelper.getPort(ports, config);
result.addAll(KubernetesCommonHelper.createDecorators(project, KUBERNETES, name, config,
result.addAll(KubernetesCommonHelper.createDecorators(project, KUBERNETES, name, namespace, config,
metricsConfiguration, kubernetesClientConfiguration, annotations, labels, image, command, port,
livenessPath, readinessPath, startupPath,
roles, clusterRoles, serviceAccounts, roleBindings));
Expand Down

0 comments on commit f5275ea

Please sign in to comment.