Search in sources :

Example 1 with TypedVisitor

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) RouteBuilder(io.fabric8.openshift.api.model.RouteBuilder)

Example 2 with TypedVisitor

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);
                    }
                });
            }
        }
    }
}
Also used : TypedVisitor(io.fabric8.kubernetes.api.builder.TypedVisitor) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetSpec(io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 3 with TypedVisitor

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();
            }
        }
    });
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) JSONArray(org.json.JSONArray) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) PersistentVolumeClaimVolumeSource(io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSource) Container(io.fabric8.kubernetes.api.model.Container) JSONObject(org.json.JSONObject) Volume(io.fabric8.kubernetes.api.model.Volume) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

PodSpec (io.fabric8.kubernetes.api.model.PodSpec)2 TypedVisitor (io.fabric8.kubernetes.api.builder.TypedVisitor)1 Container (io.fabric8.kubernetes.api.model.Container)1 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)1 PersistentVolumeClaimVolumeSource (io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSource)1 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)1 PodTemplateSpecBuilder (io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder)1 Volume (io.fabric8.kubernetes.api.model.Volume)1 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)1 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)1 DeploymentSpec (io.fabric8.kubernetes.api.model.extensions.DeploymentSpec)1 StatefulSetBuilder (io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder)1 StatefulSetSpec (io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec)1 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)1 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)1 RouteBuilder (io.fabric8.openshift.api.model.RouteBuilder)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1