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

Commit

Permalink
Disable sidecar behavior by default
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed May 23, 2019
1 parent c9285f4 commit 1bad3dc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ public static void mergeSimpleFields(Object targetValues, Object defaultValues)
}

public static String mergePodSpec(PodSpecBuilder builder, PodSpec defaultPodSpec, String defaultName) {
return mergePodSpec(builder, defaultPodSpec, defaultName, false);
}

public static String mergePodSpec(PodSpecBuilder builder, PodSpec defaultPodSpec, String defaultName, boolean sidecarEnabled) {
String defaultApplicationContainerName = null;
List<Container> containers = builder.buildContainers();
List<Container> defaultContainers = defaultPodSpec.getContainers();
Expand All @@ -690,20 +694,30 @@ public static String 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 = null;
for (Container fragmentContainer : containers) {
if (fragmentContainer.getName() == null || fragmentContainer.getName().equals(defaultContainer.getName())) {
container = fragmentContainer;
defaultApplicationContainerName = defaultContainer.getName();
break;
if(sidecarEnabled) { // Consider container as sidecar
for (Container fragmentContainer : containers) {
if (fragmentContainer.getName() == null || fragmentContainer.getName().equals(defaultContainer.getName())) {
container = fragmentContainer;
defaultApplicationContainerName = defaultContainer.getName();
break;
}
}
if (container == null) {
container = new Container();
containers.add(container);
}
} else { // Old behavior
if (idx < containers.size()) {
container = containers.get(idx);
} else {
container = new Container();
containers.add(container);
}
}

if (container == null) {
container = new Container();
containers.add(container);
}
mergeSimpleFields(container, defaultContainer);
List<EnvVar> defaultEnv = defaultContainer.getEnv();
if (defaultEnv != null) {
Expand All @@ -726,6 +740,7 @@ public static String mergePodSpec(PodSpecBuilder builder, PodSpec defaultPodSpec
if (container.getSecurityContext() == null) {
container.setSecurityContext(defaultContainer.getSecurityContext());
}
idx++;
}
builder.withContainers(containers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ protected Boolean getTrimImageInContainerSpecFlag(Boolean defaultValue) {
}
}

protected Boolean getSidecarFlag(Boolean defaultValue) {
if (getContext().getProperty("fabric8.sidecar") != null) {
return Boolean.parseBoolean(getContext().getProperty("fabric8.sidecar").toString());
} else {
return defaultValue;
}
}

/**
* This method overrides the ImagePullPolicy value by the value provided in
* XML config.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void visit(StatefulSetBuilder statefulSetBuilder) {
builder.accept(new TypedVisitor<PodSpecBuilder>() {
@Override
public void visit(PodSpecBuilder builder) {
String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name, getSidecarFlag(false));
if(defaultApplicationContainerName != null) {
setProcessingInstruction(NEED_IMAGECHANGE_TRIGGERS, Collections.singletonList(defaultApplicationContainerName));
}
Expand All @@ -120,7 +120,7 @@ public void visit(DeploymentBuilder deploymentBuilder) {
builder.accept(new TypedVisitor<PodSpecBuilder>() {
@Override
public void visit(PodSpecBuilder builder) {
String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name, getSidecarFlag(false));
if(defaultApplicationContainerName != null) {
setProcessingInstruction(NEED_IMAGECHANGE_TRIGGERS, Collections.singletonList(defaultApplicationContainerName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public void visit(DeploymentConfigSpecBuilder builder) {
PodSpec podSpec = template.getSpec();
Objects.requireNonNull(podSpec, "No PodSpec for PodTemplate:" + template);
List<Container> containers = podSpec.getContainers();
containerToImageMap = containers.stream().collect(Collectors.toMap(Container::getName, Container::getImage));
for(Container container : containers) {
if(container.getName() != null && container.getImage() != null) {
containerToImageMap.put(container.getName(), container.getImage());
}
}
}
// add a new image change trigger for the build stream
if (containerToImageMap.size() != 0) {
Expand Down
4 changes: 2 additions & 2 deletions it/src/it/sidecar/invoker.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
#

invoker.goals.1=clean fabric8:resource
invoker.mavenOpts=-Dfabric8.verbose -Dfabric8.mode=kubernetes
invoker.mavenOpts=-Dfabric8.verbose -Dfabric8.mode=kubernetes -Dfabric8.sidecar=true
#-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005
invoker.debug=false
invoker.debug=false
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public class ResourceMojo extends AbstractFabric8Mojo {
@Parameter(property = "fabric8.namespace")
private String namespace;

@Parameter(property = "fabric8.sidecar", defaultValue = "false")
private Boolean sidecar;

@Parameter(property = "fabric8.skipHealthCheck", defaultValue = "false")
private Boolean skipHealthCheck;

Expand Down

0 comments on commit 1bad3dc

Please sign in to comment.