Skip to content

Commit

Permalink
refactor: use a common DeploymnetResourceKind enum
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Mar 4, 2024
1 parent 607ece4 commit ca63e5c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> requiredTargets) {
this.kind = kind;
this.apiGroup = apiGroup;
this.apiVersion = apiVersion;
this.requiredTargets = requiredTargets;
}

public boolean isAvailalbleOn(String target) {
return requiredTargets.isEmpty() || requiredTargets.contains(target);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
*/
Expand All @@ -59,7 +43,7 @@ public enum DeploymentResourceKind {
* Supported values are 'StatefulSet', 'Job', 'CronJob' and 'Deployment' defaulting to the latter.
*/
@ConfigItem
Optional<KubernetesConfig.DeploymentResourceKind> deploymentKind;
Optional<DeploymentResourceKind> deploymentKind;

/**
* The namespace the generated resources should belong to.
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,15 @@ public List<DecoratorBuildItem> 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)));
}

Expand Down

0 comments on commit ca63e5c

Please sign in to comment.