Skip to content

Commit

Permalink
default value option
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Jun 30, 2023
1 parent 33940ee commit fe4d841
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void createLabels(KubernetesConfig config, BuildProducer<KubernetesLabelB
public List<ConfiguratorBuildItem> createConfigurators(KubernetesConfig config,
List<KubernetesPortBuildItem> ports) {
List<ConfiguratorBuildItem> result = new ArrayList<>();
KubernetesCommonHelper.combinePorts(ports, config).values()
KubernetesCommonHelper.combinePorts(ports, config.getPorts()).values()
.forEach(value -> result.add(new ConfiguratorBuildItem(new AddPortToKubernetesConfig(value))));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void createLabels(KubernetesConfig config, BuildProducer<KubernetesLabelB
public List<ConfiguratorBuildItem> createConfigurators(KubernetesConfig config,
List<KubernetesPortBuildItem> ports) {
List<ConfiguratorBuildItem> result = new ArrayList<>();
KubernetesCommonHelper.combinePorts(ports, config).values().forEach(value -> {
KubernetesCommonHelper.combinePorts(ports, config.getPorts()).values().forEach(value -> {
result.add(new ConfiguratorBuildItem(new AddPortToKubernetesConfig(value)));
});
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
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.KubernetesConfigUtil.defaultMapIfEmpty;
import static io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem.DEFAULT_PRIORITY;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

Expand Down Expand Up @@ -102,25 +104,39 @@ public void checkKnative(ApplicationInfoBuildItem applicationInfo, KnativeConfig
}

@BuildStep
public void createAnnotations(KnativeConfig config, BuildProducer<KubernetesAnnotationBuildItem> annotations) {
config.getAnnotations().forEach((k, v) -> {
public void createAnnotations(KnativeConfig config,
KubernetesConfig kubernetesConfig,
BuildProducer<KubernetesAnnotationBuildItem> annotations) {
Map<String, String> annotationsFromConfig = config.getAnnotations();
if (annotationsFromConfig.isEmpty()) {
annotationsFromConfig = kubernetesConfig.getAnnotations();
}
annotationsFromConfig.forEach((k, v) -> {
annotations.produce(new KubernetesAnnotationBuildItem(k, v, KNATIVE));
});
}

@BuildStep
public void createLabels(KnativeConfig config, BuildProducer<KubernetesLabelBuildItem> labels,
public void createLabels(KnativeConfig config,
KubernetesConfig kubernetesConfig,
BuildProducer<KubernetesLabelBuildItem> labels,
BuildProducer<ContainerImageLabelBuildItem> imageLabels) {
config.getLabels().forEach((k, v) -> {
Map<String, String> labelsFromConfig = config.getLabels();
if (labelsFromConfig.isEmpty()) {
labelsFromConfig = kubernetesConfig.getLabels();
}
labelsFromConfig.forEach((k, v) -> {
labels.produce(new KubernetesLabelBuildItem(k, v, KNATIVE));
imageLabels.produce(new ContainerImageLabelBuildItem(k, v));
});
}

@BuildStep
public List<ConfiguratorBuildItem> createConfigurators(KnativeConfig config, List<KubernetesPortBuildItem> ports) {
public List<ConfiguratorBuildItem> createConfigurators(KnativeConfig config,
KubernetesConfig kubernetesConfig,
List<KubernetesPortBuildItem> ports) {
List<ConfiguratorBuildItem> result = new ArrayList<>();
KubernetesCommonHelper.combinePorts(ports, config).values()
KubernetesCommonHelper.combinePorts(ports, defaultMapIfEmpty(config.getPorts(), kubernetesConfig.getPorts())).values()
.stream()
// At the moment, Knative only supports single port binding: https://github.com/knative/serving/issues/8471
.filter(p -> p.getName().equals("http"))
Expand All @@ -133,6 +149,7 @@ public List<ConfiguratorBuildItem> createConfigurators(KnativeConfig config, Lis
public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applicationInfo,
OutputTargetBuildItem outputTarget,
KnativeConfig config,
KubernetesConfig kubernetesConfig,
PackageConfig packageConfig,
Optional<MetricsCapabilityBuildItem> metricsConfiguration,
Optional<KubernetesClientCapabilityBuildItem> kubernetesClientConfiguration,
Expand Down Expand Up @@ -162,7 +179,9 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
String name = ResourceNameUtil.getResourceName(config, applicationInfo);
Optional<Project> project = KubernetesCommonHelper.createProject(applicationInfo, customProjectRoot, outputTarget,
packageConfig);
Optional<Port> port = KubernetesCommonHelper.getPort(ports, config, "http");
Optional<Port> port = KubernetesCommonHelper.getPort(ports,
defaultMapIfEmpty(config.getPorts(), kubernetesConfig.getPorts()),
"http");
result.addAll(KubernetesCommonHelper.createDecorators(project, KNATIVE, name, config,
metricsConfiguration, kubernetesClientConfiguration, annotations,
labels, command, port, livenessPath, readinessPath, startupProbePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ public static Optional<Project> createProject(ApplicationInfoBuildItem app,
* Creates the configurator build items.
*/
public static Optional<Port> getPort(List<KubernetesPortBuildItem> ports, KubernetesConfig config) {
return getPort(ports, config, config.ingress.targetPort);
return getPort(ports, config.getPorts(), config.ingress.targetPort);
}

/**
* Creates the configurator build items.
*/
public static Optional<Port> getPort(List<KubernetesPortBuildItem> ports, PlatformConfiguration config, String targetPort) {
return combinePorts(ports, config).values().stream()
public static Optional<Port> getPort(List<KubernetesPortBuildItem> ports, Map<String, PortConfig> portsFromConfig,
String targetPort) {
return combinePorts(ports, portsFromConfig).values().stream()
.filter(distinct(p -> p.getName()))
.filter(p -> p.getName().equals(targetPort))
.findFirst();
Expand All @@ -162,13 +163,13 @@ public static Optional<Port> getPort(List<KubernetesPortBuildItem> ports, Platfo
* Creates the configurator build items.
*/
public static Map<String, Port> combinePorts(List<KubernetesPortBuildItem> ports,
PlatformConfiguration config) {
Map<String, PortConfig> portsFromConfig) {
Map<String, Port> allPorts = new HashMap<>();
allPorts.putAll(verifyPorts(ports).entrySet().stream()
.map(e -> new PortBuilder().withName(e.getKey()).withContainerPort(e.getValue()).build())
.collect(Collectors.toMap(Port::getName, p -> p)));

config.getPorts().entrySet().forEach(e -> {
portsFromConfig.entrySet().forEach(e -> {
String name = e.getKey();
Port configuredPort = PortConverter.convert(e);
Port buildItemPort = allPorts.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public static Map<String, Object> toMap(PlatformConfiguration... platformConfigu
return result;
}

public static <T> Map<String, T> defaultMapIfEmpty(Map<String, T> map, Map<String, T> defaultMap) {
return map == null || map.isEmpty() ? defaultMap : map;
}

public static boolean managementPortIsEnabled() {
return ConfigProvider.getConfig().getOptionalValue("quarkus.management.enabled", Boolean.class).orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ public static enum DeploymentResourceKind {
/**
* The name of the group this component belongs too
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.part-of}")
Optional<String> partOf;

/**
* The name of the application. This value will be used for naming Kubernetes
* resources like: 'Deployment', 'Service' and so on...
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.name}")
Optional<String> name;

/**
* The version of the application.
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.version}")
Optional<String> version;

/**
Expand All @@ -100,7 +100,7 @@ public static enum DeploymentResourceKind {
* (see https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#context
* for more details).
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.namespace}")
Optional<String> namespace;

/**
Expand All @@ -120,31 +120,31 @@ public static enum DeploymentResourceKind {
* This is a very useful way to have manifests of successive builds of the same
* application differ - thus ensuring that Kubernetes will apply the updated resources
*/
@ConfigItem(defaultValue = "true")
@ConfigItem(defaultValue = "${quarkus.kubernetes.add-build-timestamp}")
boolean addBuildTimestamp;

/**
* Working directory
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.working-dir}")
Optional<String> workingDir;

/**
* The commands
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.command}")
Optional<List<String>> command;

/**
* The arguments
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.arguments}")
Optional<List<String>> arguments;

/**
* The service account
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.service-account}")
Optional<String> serviceAccount;

/**
Expand All @@ -156,31 +156,31 @@ public static enum DeploymentResourceKind {
/**
* The number of desired pods
*/
@ConfigItem(defaultValue = "1")
@ConfigItem(defaultValue = "${quarkus.kubernetes.replicas}")
Integer replicas;

/**
* The type of service that will be generated for the application
*/
@ConfigItem(defaultValue = "ClusterIP")
@ConfigItem(defaultValue = "${quarkus.kubernetes.service-type}")
ServiceType serviceType;

/**
* The nodePort to set when serviceType is set to nodePort
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.node-port}")
OptionalInt nodePort;

/**
* Image pull policy
*/
@ConfigItem(defaultValue = "Always")
@ConfigItem(defaultValue = "${quarkus.kubernetes.image-pull-policy}")
ImagePullPolicy imagePullPolicy;

/**
* The image pull secret
*/
@ConfigItem
@ConfigItem(defaultValue = "${quarkus.kubernetes.image-pull-secrets}")
Optional<List<String>> imagePullSecrets;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static io.quarkus.kubernetes.deployment.Constants.ROUTE;
import static io.quarkus.kubernetes.deployment.Constants.STARTUP_PROBE;
import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.MANAGEMENT_PORT_NAME;
import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.defaultMapIfEmpty;
import static io.quarkus.kubernetes.deployment.KubernetesConfigUtil.managementPortIsEnabled;
import static io.quarkus.kubernetes.deployment.OpenshiftConfig.OpenshiftFlavor.v3;
import static io.quarkus.kubernetes.spi.KubernetesDeploymentTargetBuildItem.DEFAULT_PRIORITY;
Expand Down Expand Up @@ -124,16 +125,24 @@ public void populateInternalRegistry(OpenshiftConfig openshiftConfig, ContainerI
}

@BuildStep
public void createAnnotations(OpenshiftConfig config, BuildProducer<KubernetesAnnotationBuildItem> annotations) {
config.getAnnotations().forEach((k, v) -> {
public void createAnnotations(OpenshiftConfig config,
KubernetesConfig kubernetesConfig,
BuildProducer<KubernetesAnnotationBuildItem> annotations) {
Map<String, String> annotationsFromConfig = config.getAnnotations();
if (annotationsFromConfig.isEmpty()) {
annotationsFromConfig = kubernetesConfig.getAnnotations();
}
annotationsFromConfig.forEach((k, v) -> {
annotations.produce(new KubernetesAnnotationBuildItem(k, v, OPENSHIFT));
});
}

@BuildStep
public void createLabels(OpenshiftConfig config, BuildProducer<KubernetesLabelBuildItem> labels,
public void createLabels(OpenshiftConfig config,
KubernetesConfig kubernetesConfig,
BuildProducer<KubernetesLabelBuildItem> labels,
BuildProducer<ContainerImageLabelBuildItem> imageLabels) {
config.getLabels().forEach((k, v) -> {
defaultMapIfEmpty(config.getLabels(), kubernetesConfig.getLabels()).forEach((k, v) -> {
labels.produce(new KubernetesLabelBuildItem(k, v, OPENSHIFT));
imageLabels.produce(new ContainerImageLabelBuildItem(k, v));
});
Expand All @@ -142,14 +151,17 @@ public void createLabels(OpenshiftConfig config, BuildProducer<KubernetesLabelBu

@BuildStep
public List<ConfiguratorBuildItem> createConfigurators(ApplicationInfoBuildItem applicationInfo,
OpenshiftConfig config, Capabilities capabilities, Optional<ContainerImageInfoBuildItem> image,
OpenshiftConfig config,
KubernetesConfig kubernetesConfig,
Capabilities capabilities,
Optional<ContainerImageInfoBuildItem> image,
List<KubernetesPortBuildItem> ports) {

List<ConfiguratorBuildItem> result = new ArrayList<>();

KubernetesCommonHelper.combinePorts(ports, config).values().forEach(value -> {
result.add(new ConfiguratorBuildItem(new AddPortToOpenshiftConfig(value)));
});
KubernetesCommonHelper.combinePorts(ports, defaultMapIfEmpty(config.getPorts(), kubernetesConfig.getPorts()))
.values()
.forEach(value -> result.add(new ConfiguratorBuildItem(new AddPortToOpenshiftConfig(value))));

result.add(new ConfiguratorBuildItem(new ApplyOpenshiftRouteConfigurator(config.route)));

Expand Down Expand Up @@ -179,6 +191,7 @@ public List<ConfiguratorBuildItem> createConfigurators(ApplicationInfoBuildItem
public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applicationInfo,
OutputTargetBuildItem outputTarget,
OpenshiftConfig config,
KubernetesConfig kubernetesConfig,
ContainerImageConfig containerImageConfig,
Optional<FallbackContainerImageRegistryBuildItem> fallbackRegistry,
PackageConfig packageConfig,
Expand Down Expand Up @@ -215,7 +228,9 @@ 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);
Optional<Port> port = KubernetesCommonHelper.getPort(ports,
defaultMapIfEmpty(config.getPorts(), kubernetesConfig.getPorts()),
config.route.targetPort);
result.addAll(KubernetesCommonHelper.createDecorators(project, OPENSHIFT, name, config,
metricsConfiguration, kubernetesClientConfiguration,
annotations, labels, command,
Expand Down Expand Up @@ -322,18 +337,18 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
}

// Probe port handling
result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, LIVENESS_PROBE, config.livenessProbe,
result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, LIVENESS_PROBE, config.getLivenessProbe(),
portName,
ports,
config.ports));
result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, READINESS_PROBE, config.readinessProbe,
config.getPorts()));
result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, READINESS_PROBE, config.getReadinessProbe(),
portName,
ports,
config.ports));
result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, STARTUP_PROBE, config.startupProbe,
config.getPorts()));
result.add(KubernetesCommonHelper.createProbeHttpPortDecorator(name, OPENSHIFT, STARTUP_PROBE, config.getStartupProbe(),
portName,
ports,
config.ports));
config.getPorts()));

// Handle non-openshift builds
if (deploymentKind == DeploymentResourceKind.DeploymentConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void createLabels(KubernetesConfig config, BuildProducer<KubernetesLabelB
@BuildStep
public List<ConfiguratorBuildItem> createConfigurators(KubernetesConfig config, List<KubernetesPortBuildItem> ports) {
List<ConfiguratorBuildItem> result = new ArrayList<>();
KubernetesCommonHelper.combinePorts(ports, config).values().forEach(value -> {
KubernetesCommonHelper.combinePorts(ports, config.getPorts()).values().forEach(value -> {
result.add(new ConfiguratorBuildItem(new AddPortToKubernetesConfig(value)));
});
if (config.ingress != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
quarkus.kubernetes.deployment-target=openshift
quarkus.openshift.flavor=v4
quarkus.openshift.labels."app.openshift.io/runtime"=test
quarkus.kubernetes.labels."app.openshift.io/runtime"=test

0 comments on commit fe4d841

Please sign in to comment.