use of io.fabric8.kubernetes.api.builder.TypedVisitor in project fabric8-maven-plugin by fabric8io.
the class OpenShiftRouteEnricher method hasRoute.
/**
* Returns true if we already have a route created for the given name
*/
private boolean hasRoute(final KubernetesListBuilder listBuilder, final String name) {
final AtomicBoolean answer = new AtomicBoolean(false);
listBuilder.accept(new TypedVisitor<RouteBuilder>() {
@Override
public void visit(RouteBuilder builder) {
ObjectMeta metadata = builder.getMetadata();
if (metadata != null && name.equals(metadata.getName())) {
answer.set(true);
}
}
});
return answer.get();
}
use of io.fabric8.kubernetes.api.builder.TypedVisitor in project fabric8-maven-plugin by fabric8io.
the class DefaultControllerEnricher method addMissingResources.
@Override
public void addMissingResources(KubernetesListBuilder builder) {
final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getProject()));
final ResourceConfig config = new ResourceConfig.Builder().controllerName(name).imagePullPolicy(getConfig(Config.pullPolicy)).withReplicas(Configs.asInt(getConfig(Config.replicaCount))).build();
final List<ImageConfiguration> images = getImages();
// Check if at least a replica set is added. If not add a default one
if (!KubernetesResourceUtil.checkForKind(builder, POD_CONTROLLER_KINDS)) {
// At least one image must be present, otherwise the resulting config will be invalid
if (!Lists.isNullOrEmpty(images)) {
String type = getConfig(Config.type);
if ("deployment".equalsIgnoreCase(type)) {
log.info("Adding a default Deployment");
builder.addToDeploymentItems(deployHandler.getDeployment(config, images));
} else if ("statefulSet".equalsIgnoreCase(type)) {
log.info("Adding a default StatefulSet");
builder.addToStatefulSetItems(statefulSetHandler.getStatefulSet(config, images));
} else if ("daemonSet".equalsIgnoreCase(type)) {
log.info("Adding a default DaemonSet");
builder.addToDaemonSetItems(daemonSetHandler.getDaemonSet(config, images));
} else if ("replicaSet".equalsIgnoreCase(type)) {
log.info("Adding a default ReplicaSet");
builder.addToReplicaSetItems(rsHandler.getReplicaSet(config, images));
} else if ("replicationController".equalsIgnoreCase(type)) {
log.info("Adding a default ReplicationController");
builder.addToReplicationControllerItems(rcHandler.getReplicationController(config, images));
} else if ("job".equalsIgnoreCase(type)) {
log.info("Adding a default Job");
builder.addToJobItems(jobHandler.getJob(config, images));
}
}
} else if (KubernetesResourceUtil.checkForKind(builder, "StatefulSet")) {
final StatefulSetSpec spec = statefulSetHandler.getStatefulSet(config, images).getSpec();
if (spec != null) {
builder.accept(new TypedVisitor<StatefulSetBuilder>() {
@Override
public void visit(StatefulSetBuilder statefulSetBuilder) {
statefulSetBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
mergeStatefulSetSpec(statefulSetBuilder, spec);
}
});
if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
final PodSpec podSpec = spec.getTemplate().getSpec();
builder.accept(new TypedVisitor<PodSpecBuilder>() {
@Override
public void visit(PodSpecBuilder builder) {
KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
}
});
}
}
} else {
final DeploymentSpec spec = deployHandler.getDeployment(config, images).getSpec();
if (spec != null) {
builder.accept(new TypedVisitor<DeploymentBuilder>() {
@Override
public void visit(DeploymentBuilder deploymentBuilder) {
deploymentBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
mergeDeploymentSpec(deploymentBuilder, spec);
}
});
if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
final PodSpec podSpec = spec.getTemplate().getSpec();
builder.accept(new TypedVisitor<PodSpecBuilder>() {
@Override
public void visit(PodSpecBuilder builder) {
KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
}
});
}
}
}
}
use of io.fabric8.kubernetes.api.builder.TypedVisitor in project fabric8-maven-plugin by fabric8io.
the class VolumePermissionEnricher method adapt.
@Override
public void adapt(KubernetesListBuilder builder) {
builder.accept(new TypedVisitor<PodTemplateSpecBuilder>() {
@Override
public void visit(PodTemplateSpecBuilder builder) {
PodSpec podSpec = builder.buildSpec();
if (podSpec == null) {
return;
}
if (!checkForPvc(podSpec)) {
return;
}
List<Container> containers = podSpec.getContainers();
if (containers == null || containers.isEmpty()) {
return;
}
log.verbose("Adding init container for changing persistent volumes access mode to %s", getConfig(Config.permission));
if (!initContainerHandler.hasInitContainer(builder, ENRICHER_NAME)) {
initContainerHandler.appendInitContainer(builder, createPvInitContainer(podSpec));
}
}
private boolean checkForPvc(PodSpec podSpec) {
List<Volume> volumes = podSpec.getVolumes();
if (volumes != null) {
for (Volume volume : volumes) {
PersistentVolumeClaimVolumeSource persistentVolumeClaim = volume.getPersistentVolumeClaim();
if (persistentVolumeClaim != null) {
return true;
}
}
}
return false;
}
private JSONObject createPvInitContainer(PodSpec podSpec) {
Map<String, String> mountPoints = extractMountPoints(podSpec);
JSONObject entry = new JSONObject();
entry.put("name", ENRICHER_NAME);
entry.put("image", "busybox");
entry.put("imagePullPolicy", "IfNotPresent");
entry.put("command", createChmodCommandArray(mountPoints));
entry.put("volumeMounts", createMounts(mountPoints));
return entry;
}
private JSONArray createChmodCommandArray(Map<String, String> mountPoints) {
JSONArray ret = new JSONArray();
ret.put("chmod");
ret.put(getConfig(Config.permission));
Set<String> uniqueNames = new LinkedHashSet<>(mountPoints.values());
for (String name : uniqueNames) {
ret.put(name);
}
return ret;
}
private JSONArray createMounts(Map<String, String> mountPoints) {
JSONArray ret = new JSONArray();
for (Map.Entry<String, String> entry : mountPoints.entrySet()) {
JSONObject mount = new JSONObject();
mount.put("name", entry.getKey());
mount.put("mountPath", entry.getValue());
ret.put(mount);
}
return ret;
}
private Map<String, String> extractMountPoints(PodSpec podSpec) {
Map<String, String> nameToMount = new LinkedHashMap<>();
List<Volume> volumes = podSpec.getVolumes();
if (volumes != null) {
for (Volume volume : volumes) {
PersistentVolumeClaimVolumeSource persistentVolumeClaim = volume.getPersistentVolumeClaim();
if (persistentVolumeClaim != null) {
String name = volume.getName();
String mountPath = getMountPath(podSpec.getContainers(), name);
nameToMount.put(name, mountPath);
}
}
}
return nameToMount;
}
private String getMountPath(List<Container> containers, String name) {
for (Container container : containers) {
List<VolumeMount> volumeMounts = container.getVolumeMounts();
if (volumeMounts != null) {
for (VolumeMount volumeMount : volumeMounts) {
if (name.equals(volumeMount.getName())) {
return volumeMount.getMountPath();
}
}
}
}
throw new IllegalArgumentException("No matching volume mount found for volume " + name);
}
});
builder.accept(new TypedVisitor<PersistentVolumeClaimBuilder>() {
@Override
public void visit(PersistentVolumeClaimBuilder pvcBuilder) {
// lets ensure we have a default storage class so that PVs will get dynamically created OOTB
if (pvcBuilder.buildMetadata() == null) {
pvcBuilder.withNewMetadata().endMetadata();
}
String storageClass = getConfig(Config.defaultStorageClass);
if (Strings.isNotBlank(storageClass) && !pvcBuilder.buildMetadata().getAnnotations().containsKey(VOLUME_STORAGE_CLASS_ANNOTATION)) {
pvcBuilder.editMetadata().addToAnnotations(VOLUME_STORAGE_CLASS_ANNOTATION, storageClass).endMetadata();
}
}
});
}
Aggregations