Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Improve support for sidecar containers
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Feb 20, 2018
1 parent 533f844 commit f33dd5a
Show file tree
Hide file tree
Showing 17 changed files with 714 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ List<EnvVar> getEnvironmentVariables(Map<String, String> envVars) {
.endValueFrom()
.build());

ret.add(
new EnvVarBuilder()
.withName("FABRIC8_GENERATED")
.withValue("true")
.build());

return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,16 @@ public static void mergePodSpec(PodSpecBuilder builder, PodSpec defaultPodSpec,
if (containers == null || containers.isEmpty()) {
builder.addToContainers(defaultContainers.toArray(new Container[size]));
} else {
int idx = 0;
for (Container defaultContainer : defaultContainers) {
Container container;
if (idx < containers.size()) {
container = containers.get(idx);
} else {
Container container = null;
for (Container fragmentContainer : containers) {
if (fragmentContainer.getName() == null || fragmentContainer.getName().equals(defaultContainer.getName())) {
container = fragmentContainer;
break;
}
}

if (container == null) {
container = new Container();
containers.add(container);
}
Expand All @@ -773,15 +777,16 @@ public static void mergePodSpec(PodSpecBuilder builder, PodSpec defaultPodSpec,
if (container.getSecurityContext() == null) {
container.setSecurityContext(defaultContainer.getSecurityContext());
}
idx++;
}
builder.withContainers(containers);
}
} else if (!containers.isEmpty()) {
// lets default the container name if there's none specified in the custom yaml file
Container container = containers.get(0);
if (isNullOrBlank(container.getName())) {
container.setName(defaultName);
for (Container container : containers) {
if (isNullOrBlank(container.getName())) {
container.setName(defaultName);
break; // do it for one container only, but not necessarily the first one
}
}
builder.withContainers(containers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public void emptyEnvVarHandlerTest(){
List<EnvVar> envVars = envVarHandler.getEnvironmentVariables(env);

assertNotNull(envVars);
assertEquals(1,envVars.size());
assertEquals(2,envVars.size());
assertEquals("KUBERNETES_NAMESPACE", envVars.get(0).getName());
assertEquals("FABRIC8_GENERATED", envVars.get(1).getName());
assertTrue(envVars.contains(var4));
}

Expand All @@ -90,7 +91,7 @@ public void envVarHandlerTest(){
List<EnvVar> envVars = envVarHandler.getEnvironmentVariables(env);

assertNotNull(envVars);
assertEquals(4,envVars.size());
assertEquals(5,envVars.size());
assertTrue(envVars.contains(var1));
assertTrue(envVars.contains(var2));
assertTrue(envVars.contains(var3));
Expand All @@ -116,9 +117,10 @@ public void envVarHandlerWithoutNameTest(){
List<EnvVar> envVars = envVarHandler.getEnvironmentVariables(env);

assertNotNull(envVars);
assertEquals(1,envVars.size());
assertEquals(2,envVars.size());
assertTrue(!envVars.contains(var1));
assertEquals("KUBERNETES_NAMESPACE", envVars.get(0).getName());
assertEquals("FABRIC8_GENERATED", envVars.get(1).getName());
assertTrue(envVars.contains(var4));
}
}
20 changes: 20 additions & 0 deletions doc/src/main/asciidoc/inc/_enricher.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ and use paths `/readiness-check` for readiness check and `/health-check` for liv

These options cannot be configured.

The enricher is applied only to the default container by default. The following options can be used to override the default settings:

- `enrichAllContainers`: if true, health checks are added to all defined containers, not only the generated ones
- `enrichContainers`: comma separated list of containers to enrich with health checks

[[f8-prometheus]]
==== f8-prometheus

Expand Down Expand Up @@ -331,6 +336,10 @@ These values can be configured by the enricher in the `fabric8-maven-plugin` con
</configuration>
</plugin>

The enricher is applied only to the default container by default. The following options can be used to override the default settings:

- `enrichAllContainers`: if true, health checks are added to all defined containers, not only the generated ones
- `enrichContainers`: comma separated list of containers to enrich with health checks


[[f8-wildfly-swarm-health-check]]
Expand Down Expand Up @@ -381,6 +390,12 @@ These values can be configured by the enricher in the `fabric8-maven-plugin` con
</plugin>


The enricher is applied only to the default container by default. The following options can be used to override the default settings:

- `enrichAllContainers`: if true, health checks are added to all defined containers, not only the generated ones
- `enrichContainers`: comma separated list of containers to enrich with health checks


[[f8-vertx-health-check]]
==== f8-vertx-health-check

Expand Down Expand Up @@ -485,6 +500,11 @@ disabled, the liveness check uses `GET localhost:8080/ping`
* `vertx.health.path` set to "/ping" and `vertx.health.readiness.path` set to "/ready" => the readiness check uses
`GET localhost:8080/ready` , the liveness check uses the `GET localhost:8080/ping`

The enricher is applied only to the default container by default. The following options can be used to override the default settings:

- `enrichAllContainers`: if true, health checks are added to all defined containers, not only the generated ones
- `enrichContainers`: comma separated list of containers to enrich with health checks

[[f8-maven-scm-enricher]]
==== f8-maven-scm-enricher

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,46 @@
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.maven.core.util.Configs;
import io.fabric8.maven.core.util.KubernetesResourceUtil;

import java.util.*;

/**
* Enriches containers with health check probes.
*/
public abstract class AbstractHealthCheckEnricher extends BaseEnricher {

private enum Config implements Configs.Key {
enrichAllContainers {{ d = "false"; }},
enrichContainers {{ d = null; }};

public String def() { return d; } protected String d;
}

public AbstractHealthCheckEnricher(EnricherContext buildContext, String name) {
super(buildContext, name);
}

@Override
public void addMissingResources(KubernetesListBuilder builder) {

builder.accept(new TypedVisitor<ContainerBuilder>() {
@Override
public void visit(ContainerBuilder container) {
if (!container.hasReadinessProbe()) {
Probe probe = getReadinessProbe(container);
if (probe != null) {
log.info("Adding readiness " + describe(probe));
container.withReadinessProbe(probe);
}
for (ContainerBuilder container : getContainersToEnrich(builder)) {
if (!container.hasReadinessProbe()) {
Probe probe = getReadinessProbe(container);
if (probe != null) {
log.info("Adding readiness " + describe(probe));
container.withReadinessProbe(probe);
}
}

if (!container.hasLivenessProbe()) {
Probe probe = getLivenessProbe(container);
if (probe != null) {
log.info("Adding liveness " + describe(probe));
container.withLivenessProbe(probe);
}
if (!container.hasLivenessProbe()) {
Probe probe = getLivenessProbe(container);
if (probe != null) {
log.info("Adding liveness " + describe(probe));
container.withLivenessProbe(probe);
}
}
});
}
}

private String describe(Probe probe) {
Expand Down Expand Up @@ -80,6 +87,46 @@ private String describe(Probe probe) {
return desc.toString();
}

protected List<ContainerBuilder> getContainersToEnrich(KubernetesListBuilder builder) {
final List<ContainerBuilder> containerBuilders = new LinkedList<>();
builder.accept(new TypedVisitor<ContainerBuilder>() {
@Override
public void visit(ContainerBuilder containerBuilder) {
containerBuilders.add(containerBuilder);
}
});

boolean enrichAllContainers = "true".equalsIgnoreCase(getConfig(Config.enrichAllContainers));
String enrichContainers = getConfig(Config.enrichContainers);
Set<String> containersToEnrich = new HashSet<>();
if (enrichContainers != null) {
containersToEnrich.addAll(Arrays.asList(enrichContainers.split(",")));
}

if (enrichAllContainers) {
return containerBuilders;
} else if (!containersToEnrich.isEmpty()) {
List<ContainerBuilder> filteredContainers = new LinkedList<>();
for (ContainerBuilder container : containerBuilders) {
if (container.hasName() && containersToEnrich.contains(container.getName())) {
filteredContainers.add(container);
}
}
return filteredContainers;
} else if (containerBuilders.size() == 1) {
return containerBuilders;
} else {
// Multiple unfiltered containers, enrich only the generated ones
List<ContainerBuilder> generatedContainers = new LinkedList<>();
for (ContainerBuilder container : containerBuilders) {
if ("true".equals(KubernetesResourceUtil.getEnvVar(container.buildEnv(), "FABRIC8_GENERATED", "false"))) {
generatedContainers.add(container);
}
}
return generatedContainers;
}
}

/**
* Override this method to create a per-container readiness probe.
*/
Expand Down
Loading

0 comments on commit f33dd5a

Please sign in to comment.