Skip to content

Commit

Permalink
Merge pull request #39219 from gsmet/3.8.2-backports-2
Browse files Browse the repository at this point in the history
3.8.2 backports 2
  • Loading branch information
gsmet committed Mar 6, 2024
2 parents 16c11df + e8fed18 commit 002a0ed
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 92 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<opentelemetry.version>1.32.0</opentelemetry.version>
<opentelemetry-alpha.version>1.32.0-alpha</opentelemetry-alpha.version>
<opentelemetry-semconv.version>1.21.0-alpha</opentelemetry-semconv.version> <!-- keep in sync with opentelemetry-java-instrumentation in the alpha bom-->
<quarkus-http.version>5.2.0.Final</quarkus-http.version>
<quarkus-http.version>5.2.1.Final</quarkus-http.version>
<micrometer.version>1.12.2</micrometer.version><!-- keep in sync with hdrhistogram -->
<hdrhistogram.version>2.1.12</hdrhistogram.version><!-- keep in sync with micrometer -->
<google-auth.version>0.22.0</google-auth.version>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/getting-started-testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ public class Profiles {
public static class SingleTag implements QuarkusTestProfile {
@Override
public Set<String> tags() {
return List.of("test1");
return Set.of("test1");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
import io.quarkus.kubernetes.deployment.DeploymentResourceKind;
import io.quarkus.kubernetes.deployment.OpenshiftConfig;
import io.quarkus.kubernetes.deployment.OpenshiftConfig.DeploymentResourceKind;
import io.quarkus.kubernetes.deployment.ResourceNameUtil;
import io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem;
import io.quarkus.kubernetes.spi.KubernetesResourceMetadataBuildItem;
Expand All @@ -22,13 +22,13 @@ public void checkOpenshift(ApplicationInfoBuildItem applicationInfo, Capabilitie
DeploymentResourceKind deploymentResourceKind = config.getDeploymentResourceKind(capabilities);
deploymentTargets
.produce(
new KubernetesDeploymentTargetBuildItem(OPENSHIFT, deploymentResourceKind.kind,
deploymentResourceKind.apiGroup,
deploymentResourceKind.apiVersion, true,
new KubernetesDeploymentTargetBuildItem(OPENSHIFT, deploymentResourceKind.getKind(),
deploymentResourceKind.getGroup(),
deploymentResourceKind.getVersion(), true,
config.getDeployStrategy()));

String name = ResourceNameUtil.getResourceName(config, applicationInfo);
resourceMeta.produce(new KubernetesResourceMetadataBuildItem(OPENSHIFT, deploymentResourceKind.apiGroup,
deploymentResourceKind.apiVersion, deploymentResourceKind.kind, name));
resourceMeta.produce(new KubernetesResourceMetadataBuildItem(OPENSHIFT, deploymentResourceKind.getGroup(),
deploymentResourceKind.getVersion(), deploymentResourceKind.getKind(), name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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;

import io.dekorate.utils.Strings;
import io.fabric8.kubernetes.api.model.HasMetadata;

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);

private final String kind;
private final String group;
private final String version;
private final Set<String> requiredTargets;

DeploymentResourceKind(String kind, String group, String version, String... requiredTargets) {
this(kind, group, version, Set.of(requiredTargets));
}

DeploymentResourceKind(String kind, String group, String version, Set<String> requiredTargets) {
this.kind = kind;
this.group = group;
this.version = version;
this.requiredTargets = requiredTargets;
}

public static final DeploymentResourceKind find(String apiGroup, String apiVersion, String kind) {
for (DeploymentResourceKind deploymentResourceKind : DeploymentResourceKind.values()) {
if (deploymentResourceKind.kind.equals(kind) && deploymentResourceKind.group.equals(apiGroup)
&& deploymentResourceKind.version.equals(apiVersion)) {
return deploymentResourceKind;
}
}
String apiGroupVersion = Strings.isNullOrEmpty(apiGroup) ? apiVersion : apiGroup + "/" + apiVersion;
throw new IllegalArgumentException("Could not find DeploymentResourceKind for " + apiGroupVersion + " " + kind);
}

public boolean isAvailalbleOn(String target) {
return requiredTargets.isEmpty() || requiredTargets.contains(target);
}

public boolean matches(HasMetadata resource) {
String resourceKind = HasMetadata.getKind(resource.getClass());
String resourceVersion = HasMetadata.getApiVersion(resource.getClass());
return resourceKind.equals(getKind()) && resourceVersion.equals(getApiVersion());
}

public String getKind() {
return kind;
}

public String getGroup() {
return group;
}

public String getVersion() {
return version;
}

public Set<String> getRequiredTargets() {
return requiredTargets;
}

public String getApiVersion() {
return group + "/" + version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

public class DeploymentTargetEntry {
private final String name;
private final String kind;
private final DeploymentResourceKind deploymentResourceKind;
private final int priority;
private final DeployStrategy deployStrategy;

public DeploymentTargetEntry(String name, String kind, int priority, DeployStrategy deployStrategy) {
public DeploymentTargetEntry(String name, DeploymentResourceKind kind, int priority, DeployStrategy deployStrategy) {
this.name = name;
this.kind = kind;
this.deploymentResourceKind = kind;
this.priority = priority;
this.deployStrategy = deployStrategy;
}
Expand All @@ -19,8 +19,8 @@ public String getName() {
return name;
}

public String getKind() {
return kind;
public DeploymentResourceKind getDeploymentResourceKind() {
return deploymentResourceKind;
}

public int getPriority() {
Expand Down
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
Expand Up @@ -56,6 +56,7 @@
import io.quarkus.kubernetes.spi.KubernetesDeploymentClusterBuildItem;
import io.quarkus.kubernetes.spi.KubernetesOptionalResourceDefinitionBuildItem;
import io.quarkus.kubernetes.spi.KubernetesOutputDirectoryBuildItem;
import io.quarkus.logging.Log;

public class KubernetesDeployer {

Expand Down Expand Up @@ -198,16 +199,31 @@ private DeploymentResultBuildItem deploy(DeploymentTargetEntry deploymentTarget,

try (FileInputStream fis = new FileInputStream(manifest)) {
KubernetesList list = Serialization.unmarshalAsList(fis);

Optional<GenericKubernetesResource> conflictingResource = findConflictingResource(client, deploymentTarget,
list.getItems());
if (conflictingResource.isPresent()) {
String messsage = "Skipping deployment of " + deploymentTarget.getDeploymentResourceKind() + " "
+ conflictingResource.get().getMetadata().getName() + " because a "
+ conflictingResource.get().getKind() + " with the same name exists.";
log.warn(messsage);
Log.warn("This may occur when switching deployment targets, or when the default deployment target is changed.");
Log.warn("Please remove conflicting resource and try again.");
throw new IllegalStateException(messsage);
}

list.getItems().stream().filter(distinctByResourceKey()).forEach(i -> {
deployResource(deploymentTarget, client, i, optionalResourceDefinitions);
log.info("Applied: " + i.getKind() + " " + i.getMetadata().getName() + ".");
});

printExposeInformation(client, list, openshiftConfig, applicationInfo);

HasMetadata m = list.getItems().stream().filter(r -> r.getKind().equals(deploymentTarget.getKind()))
HasMetadata m = list.getItems().stream()
.filter(r -> deploymentTarget.getDeploymentResourceKind().matches(r))
.findFirst().orElseThrow(() -> new IllegalStateException(
"No " + deploymentTarget.getKind() + " found under: " + manifest.getAbsolutePath()));
"No " + deploymentTarget.getDeploymentResourceKind() + " found under: "
+ manifest.getAbsolutePath()));
return new DeploymentResultBuildItem(m.getMetadata().getName(), m.getMetadata().getLabels());
} catch (FileNotFoundException e) {
throw new IllegalStateException("Can't find generated kubernetes manifest: " + manifest.getAbsolutePath());
Expand Down Expand Up @@ -255,6 +271,35 @@ private void deployResource(DeploymentTargetEntry deploymentTarget, KubernetesCl
}
}

private Optional<GenericKubernetesResource> findConflictingResource(KubernetesClient clinet,
DeploymentTargetEntry deploymentTarget, List<HasMetadata> generated) {
HasMetadata deploymentResource = generated.stream()
.filter(r -> deploymentTarget.getDeploymentResourceKind().matches(r))
.findFirst()
.orElseThrow(() -> new IllegalStateException(
"No " + deploymentTarget.getDeploymentResourceKind() + " found under: " + deploymentTarget.getName()));
String name = deploymentResource.getMetadata().getName();

for (DeploymentResourceKind deploymentKind : DeploymentResourceKind.values()) {
if (deploymentKind.matches(deploymentResource)) {
continue;
}
try {
GenericKubernetesResource resource = clinet
.genericKubernetesResources(deploymentKind.getApiVersion(), deploymentKind.getKind()).withName(name)
.get();
if (resource != null) {
Log.warn("Found conflicting resource:" + resource.getApiVersion() + "/" + resource.getKind() + ":"
+ resource.getMetadata().getName());
return Optional.of(resource);
}
} catch (KubernetesClientException e) {
// ignore
}
}
return Optional.empty();
}

private void deleteResource(HasMetadata metadata, Resource<?> r) {
r.delete();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public EnabledKubernetesDeploymentTargetsBuildItem enabledKubernetesDeploymentTa
List<DeploymentTargetEntry> entries = new ArrayList<>(mergedDeploymentTargets.size());
for (KubernetesDeploymentTargetBuildItem deploymentTarget : mergedDeploymentTargets) {
if (deploymentTarget.isEnabled()) {
entries.add(new DeploymentTargetEntry(deploymentTarget.getName(),
deploymentTarget.getKind(), deploymentTarget.getPriority(),
deploymentTarget.getDeployStrategy()));
DeploymentResourceKind deploymentResourceKind = DeploymentResourceKind.find(deploymentTarget.getGroup(),
deploymentTarget.getVersion(), deploymentTarget.getKind());
entries.add(new DeploymentTargetEntry(deploymentTarget.getName(), deploymentResourceKind,
deploymentTarget.getPriority(), deploymentTarget.getDeployStrategy()));
}
}
return new EnabledKubernetesDeploymentTargetsBuildItem(entries);
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;
}
}
Loading

0 comments on commit 002a0ed

Please sign in to comment.