From 0191094eb880be064e56070221cbdac05491e0b2 Mon Sep 17 00:00:00 2001 From: Ioannis Canellos Date: Wed, 21 Feb 2024 13:17:31 +0200 Subject: [PATCH] refactor: use a common DeploymnetResourceKind enum (cherry picked from commit ca63e5c856ff3b1716f337ca40eebfa7d2f8eddb) --- .../deployment/DeploymentResourceKind.java | 51 +++++++++++++++++++ .../deployment/KubernetesConfig.java | 27 ++-------- .../deployment/OpenshiftConfig.java | 33 +----------- .../deployment/OpenshiftProcessor.java | 1 - .../VanillaKubernetesProcessor.java | 11 ++-- 5 files changed, 62 insertions(+), 61 deletions(-) create mode 100644 extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/DeploymentResourceKind.java diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/DeploymentResourceKind.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/DeploymentResourceKind.java new file mode 100644 index 0000000000000..b624cb18d8f5f --- /dev/null +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/DeploymentResourceKind.java @@ -0,0 +1,51 @@ +package io.quarkus.kubernetes.deployment; + +import static io.quarkus.kubernetes.deployment.Constants.BATCH_GROUP; +import static io.quarkus.kubernetes.deployment.Constants.BATCH_VERSION; +import static io.quarkus.kubernetes.deployment.Constants.CRONJOB; +import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT; +import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_CONFIG; +import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_CONFIG_GROUP; +import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_CONFIG_VERSION; +import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_GROUP; +import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_VERSION; +import static io.quarkus.kubernetes.deployment.Constants.JOB; +import static io.quarkus.kubernetes.deployment.Constants.KNATIVE; +import static io.quarkus.kubernetes.deployment.Constants.KNATIVE_SERVICE; +import static io.quarkus.kubernetes.deployment.Constants.KNATIVE_SERVICE_GROUP; +import static io.quarkus.kubernetes.deployment.Constants.KNATIVE_SERVICE_VERSION; +import static io.quarkus.kubernetes.deployment.Constants.OPENSHIFT; +import static io.quarkus.kubernetes.deployment.Constants.STATEFULSET; + +import java.util.Set; + +public enum DeploymentResourceKind { + + Deployment(DEPLOYMENT, DEPLOYMENT_GROUP, DEPLOYMENT_VERSION), + @Deprecated(since = "OpenShift 4.14") + DeploymentConfig(DEPLOYMENT_CONFIG, DEPLOYMENT_CONFIG_GROUP, DEPLOYMENT_CONFIG_VERSION, OPENSHIFT), + StatefulSet(STATEFULSET, DEPLOYMENT_GROUP, DEPLOYMENT_VERSION), + Job(JOB, BATCH_GROUP, BATCH_VERSION), + CronJob(CRONJOB, BATCH_GROUP, BATCH_VERSION), + KnativeService(KNATIVE_SERVICE, KNATIVE_SERVICE_GROUP, KNATIVE_SERVICE_VERSION, KNATIVE); + + public final String kind; + public final String apiGroup; + public final String apiVersion; + public final Set requiredTargets; + + DeploymentResourceKind(String kind, String apiGroup, String apiVersion, String... requiredTargets) { + this(kind, apiGroup, apiVersion, Set.of(requiredTargets)); + } + + DeploymentResourceKind(String kind, String apiGroup, String apiVersion, Set requiredTargets) { + this.kind = kind; + this.apiGroup = apiGroup; + this.apiVersion = apiVersion; + this.requiredTargets = requiredTargets; + } + + public boolean isAvailalbleOn(String target) { + return requiredTargets.isEmpty() || requiredTargets.contains(target); + } +} diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java index 6d6e1022707f7..ebab056064d48 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/KubernetesConfig.java @@ -1,9 +1,6 @@ package io.quarkus.kubernetes.deployment; -import static io.quarkus.kubernetes.deployment.Constants.CRONJOB; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT; -import static io.quarkus.kubernetes.deployment.Constants.JOB; -import static io.quarkus.kubernetes.deployment.Constants.STATEFULSET; +import static io.quarkus.kubernetes.deployment.Constants.KUBERNETES; import java.util.Collections; import java.util.List; @@ -23,19 +20,6 @@ @ConfigRoot public class KubernetesConfig implements PlatformConfiguration { - public enum DeploymentResourceKind { - Deployment(DEPLOYMENT), - StatefulSet(STATEFULSET), - Job(JOB), - CronJob(CRONJOB); - - final String kind; - - DeploymentResourceKind(String kind) { - this.kind = kind; - } - } - /** * The name of the group this component belongs too */ @@ -59,7 +43,7 @@ public enum DeploymentResourceKind { * Supported values are 'StatefulSet', 'Job', 'CronJob' and 'Deployment' defaulting to the latter. */ @ConfigItem - Optional deploymentKind; + Optional deploymentKind; /** * The namespace the generated resources should belong to. @@ -625,13 +609,12 @@ public RbacConfig getRbacConfig() { return rbac; } - public KubernetesConfig.DeploymentResourceKind getDeploymentResourceKind(Capabilities capabilities) { + public DeploymentResourceKind getDeploymentResourceKind(Capabilities capabilities) { if (deploymentKind.isPresent()) { - return deploymentKind.get(); + return deploymentKind.filter(k -> k.isAvailalbleOn(KUBERNETES)).get(); } else if (capabilities.isPresent(Capability.PICOCLI)) { - return KubernetesConfig.DeploymentResourceKind.Job; + return DeploymentResourceKind.Job; } - return DeploymentResourceKind.Deployment; } diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java index 5d89ccba2a2d7..afdd1b33e9a3d 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java @@ -1,19 +1,8 @@ package io.quarkus.kubernetes.deployment; -import static io.quarkus.kubernetes.deployment.Constants.BATCH_GROUP; -import static io.quarkus.kubernetes.deployment.Constants.BATCH_VERSION; -import static io.quarkus.kubernetes.deployment.Constants.CRONJOB; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_CONFIG; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_CONFIG_GROUP; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_CONFIG_VERSION; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_GROUP; -import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_VERSION; -import static io.quarkus.kubernetes.deployment.Constants.JOB; import static io.quarkus.kubernetes.deployment.Constants.OPENSHIFT; import static io.quarkus.kubernetes.deployment.Constants.S2I; -import static io.quarkus.kubernetes.deployment.Constants.STATEFULSET; import java.util.Collections; import java.util.List; @@ -39,25 +28,6 @@ public static enum OpenshiftFlavor { v4; } - public static enum DeploymentResourceKind { - Deployment(DEPLOYMENT, DEPLOYMENT_GROUP, DEPLOYMENT_VERSION), - @Deprecated(since = "OpenShift 4.14") - DeploymentConfig(DEPLOYMENT_CONFIG, DEPLOYMENT_CONFIG_GROUP, DEPLOYMENT_CONFIG_VERSION), - StatefulSet(STATEFULSET, DEPLOYMENT_GROUP, DEPLOYMENT_VERSION), - Job(JOB, BATCH_GROUP, BATCH_VERSION), - CronJob(CRONJOB, BATCH_GROUP, BATCH_VERSION); - - public final String kind; - public final String apiGroup; - public final String apiVersion; - - DeploymentResourceKind(String kind, String apiGroup, String apiVersion) { - this.kind = kind; - this.apiGroup = apiGroup; - this.apiVersion = apiVersion; - } - } - /** * The OpenShift flavor / version to use. * Older versions of OpenShift have minor differences in the labels and fields they support. @@ -652,11 +622,10 @@ public static boolean isOpenshiftBuildEnabled(ContainerImageConfig containerImag public DeploymentResourceKind getDeploymentResourceKind(Capabilities capabilities) { if (deploymentKind.isPresent()) { - return deploymentKind.get(); + return deploymentKind.filter(k -> k.isAvailalbleOn(OPENSHIFT)).get(); } else if (capabilities.isPresent(Capability.PICOCLI)) { return DeploymentResourceKind.Job; } - return (flavor == OpenshiftFlavor.v3) ? DeploymentResourceKind.DeploymentConfig : DeploymentResourceKind.Deployment; } } diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java index f49540aed39ef..711780e984679 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java @@ -56,7 +56,6 @@ import io.quarkus.deployment.pkg.PackageConfig; import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem; import io.quarkus.kubernetes.client.spi.KubernetesClientCapabilityBuildItem; -import io.quarkus.kubernetes.deployment.OpenshiftConfig.DeploymentResourceKind; import io.quarkus.kubernetes.spi.ConfiguratorBuildItem; import io.quarkus.kubernetes.spi.CustomProjectRootBuildItem; import io.quarkus.kubernetes.spi.DecoratorBuildItem; diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java index 04d6f0c5d1859..0f8ccc06c4580 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/VanillaKubernetesProcessor.java @@ -166,16 +166,15 @@ public List createDecorators(ApplicationInfoBuildItem applic livenessPath, readinessPath, startupPath, roles, clusterRoles, serviceAccounts, roleBindings)); - KubernetesConfig.DeploymentResourceKind deploymentKind = config.getDeploymentResourceKind(capabilities); - if (deploymentKind != KubernetesConfig.DeploymentResourceKind.Deployment) { + DeploymentResourceKind deploymentKind = config.getDeploymentResourceKind(capabilities); + if (deploymentKind != DeploymentResourceKind.Deployment) { result.add(new DecoratorBuildItem(KUBERNETES, new RemoveDeploymentResourceDecorator(name))); } - - if (deploymentKind == KubernetesConfig.DeploymentResourceKind.StatefulSet) { + if (deploymentKind == DeploymentResourceKind.StatefulSet) { result.add(new DecoratorBuildItem(KUBERNETES, new AddStatefulSetResourceDecorator(name, config))); - } else if (deploymentKind == KubernetesConfig.DeploymentResourceKind.Job) { + } else if (deploymentKind == DeploymentResourceKind.Job) { result.add(new DecoratorBuildItem(KUBERNETES, new AddJobResourceDecorator(name, config.job))); - } else if (deploymentKind == KubernetesConfig.DeploymentResourceKind.CronJob) { + } else if (deploymentKind == DeploymentResourceKind.CronJob) { result.add(new DecoratorBuildItem(KUBERNETES, new AddCronJobResourceDecorator(name, config.cronJob))); }