Search in sources :

Example 41 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class KubernetesConfigAdminBridge method updateConfig.

// **********************
// ConfigAdmin
// **********************
private void updateConfig(ConfigMap map) {
    Long ver = Long.parseLong(map.getMetadata().getResourceVersion());
    String pid = map.getMetadata().getLabels().get(pidLabel);
    String[] p = parsePid(pid);
    try {
        final Configuration config = getConfiguration(configAdmin.get(), pid, p[0], p[1]);
        final Map<String, String> configMapData = map.getData();
        if (configMapData == null) {
            LOGGER.debug("Ignoring configuration pid={}, (empty)", config.getPid());
            return;
        }
        final Dictionary<String, Object> props = config.getProperties();
        final Hashtable<String, Object> configAdmCfg = props != null ? new Hashtable<String, Object>() : null;
        Hashtable<String, Object> configMapCfg = new Hashtable<>();
        /*
             * If there is a key named as pid + ".cfg" (as the pid file on karaf)
             * it will be used as source of configuration instead of the content
             * of the data field. The name of the key can be changed by setting
             * the key fabric8.config.pid.cfg
             *
             * i.e.
             *   apiVersion: v1
             *   data:
             *     org.ops4j.pax.logging.cfg: |+
             *       log4j.rootLogger=DEBUG, out
             */
        String pidCfg = configMapData.get(FABRIC8_CONFIG_PID_CFG);
        if (pidCfg == null) {
            pidCfg = pid + ".cfg";
        }
        String cfgString = configMapData.get(pidCfg);
        if (Utils.isNotNullOrEmpty(cfgString)) {
            java.util.Properties cfg = new java.util.Properties();
            cfg.load(new StringReader(cfgString));
            for (Map.Entry<Object, Object> entry : cfg.entrySet()) {
                configMapCfg.put((String) entry.getKey(), entry.getValue());
            }
        } else {
            for (Map.Entry<String, String> entry : map.getData().entrySet()) {
                configMapCfg.put(entry.getKey(), entry.getValue());
            }
        }
        /*
             * Configure if mete-data should be added to the Config Admin or not
             */
        boolean meta = configMapData.containsKey(FABRIC8_CONFIG_META) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_META)) : configMeta;
        /*
             * Configure if ConfigMap data should be merge with ConfigAdmin or it
             * should override it.
             */
        boolean merge = configMapData.containsKey(FABRIC8_CONFIG_MERGE) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_MERGE)) : configMerge;
        if (configAdmCfg != null) {
            Long oldVer = (Long) props.get(FABRIC8_K8S_META_RESOURCE_VERSION);
            if (oldVer != null && (oldVer >= ver)) {
                LOGGER.debug("Ignoring configuration pid={}, oldVersion={} newVersion={} (no changes)", config.getPid(), oldVer, ver);
                return;
            }
            for (Enumeration<String> e = props.keys(); e.hasMoreElements(); ) {
                String key = e.nextElement();
                Object val = props.get(key);
                configAdmCfg.put(key, val);
            }
        }
        if (shouldUpdate(configAdmCfg, configMapCfg)) {
            LOGGER.debug("Updating configuration pid={}", config.getPid());
            if (meta) {
                configMapCfg.put(FABRIC8_PID, pid);
                configMapCfg.put(FABRIC8_K8S_META_RESOURCE_VERSION, ver);
                configMapCfg.put(FABRIC8_K8S_META_NAME, map.getMetadata().getName());
                configMapCfg.put(FABRIC8_K8S_META_NAMESPACE, map.getMetadata().getNamespace());
            }
            if (merge && configAdmCfg != null) {
                for (Map.Entry<String, Object> entry : configMapCfg.entrySet()) {
                    // Do not override ConfigAdmin meta data
                    if (!CM_META_KEYS.contains(entry.getKey())) {
                        configAdmCfg.put(entry.getKey(), entry.getValue());
                    }
                }
                configMapCfg = configAdmCfg;
            }
            config.update(configMapCfg);
        } else {
            LOGGER.debug("Ignoring configuration pid={} (no changes)", config.getPid());
        }
    } catch (Exception e) {
        LOGGER.warn("", e);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Example 42 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class SessionListener method applyConfiguration.

private boolean applyConfiguration(KubernetesClient client, Controller controller, Configuration configuration, Session session, List<KubernetesList> kubeConfigs) throws Exception {
    Logger log = session.getLogger();
    Map<Integer, Callable<Boolean>> conditions = new TreeMap<>();
    Callable<Boolean> sessionPodsReady = new SessionPodsAreReady(client, session);
    Callable<Boolean> servicesReady = new SessionServicesAreReady(client, session, configuration);
    Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator());
    for (KubernetesList c : kubeConfigs) {
        entities.addAll(enhance(session, configuration, c).getItems());
    }
    if (containsImageStreamResources(entities)) {
    // no need to use a local image registry
    // as we are using OpenShift and
    } else {
        String registry = getLocalDockerRegistry();
        if (Strings.isNotBlank(registry)) {
            log.status("Adapting resources to pull images from registry: " + registry);
            addRegistryToImageNameIfNotPresent(entities, registry);
        } else {
            log.status("No local fabric8 docker registry found");
        }
    }
    List<Object> items = new ArrayList<>();
    items.addAll(entities);
    // Ensure services are processed first.
    Collections.sort(items, new Comparator<Object>() {

        @Override
        public int compare(Object left, Object right) {
            if (left instanceof Service) {
                return -1;
            } else if (right instanceof Service) {
                return 1;
            } else {
                return 0;
            }
        }
    });
    boolean isOpenshift = client.isAdaptable(OpenShiftClient.class);
    String namespace = session.getNamespace();
    String routeDomain = null;
    if (Strings.isNotBlank(configuration.getKubernetesDomain())) {
        routeDomain = configuration.getKubernetesDomain();
    }
    preprocessEnvironment(client, controller, configuration, session);
    Set<HasMetadata> extraEntities = new TreeSet<>(new HasMetadataComparator());
    for (Object entity : items) {
        if (entity instanceof Pod) {
            Pod pod = (Pod) entity;
            log.status("Applying pod:" + getName(pod));
            Set<Secret> secrets = generateSecrets(client, session, pod.getMetadata());
            String serviceAccountName = pod.getSpec().getServiceAccountName();
            if (Strings.isNotBlank(serviceAccountName)) {
                generateServiceAccount(client, session, secrets, serviceAccountName);
            }
            controller.applyPod(pod, session.getId());
            conditions.put(1, sessionPodsReady);
        } else if (entity instanceof Service) {
            Service service = (Service) entity;
            String serviceName = getName(service);
            log.status("Applying service:" + serviceName);
            controller.applyService(service, session.getId());
            conditions.put(2, servicesReady);
            if (isOpenshift) {
                Route route = Routes.createRouteForService(routeDomain, namespace, service, log);
                if (route != null) {
                    log.status("Applying route for:" + serviceName);
                    controller.applyRoute(route, "route for " + serviceName);
                    extraEntities.add(route);
                }
            }
        } else if (entity instanceof ReplicationController) {
            ReplicationController replicationController = (ReplicationController) entity;
            log.status("Applying replication controller:" + getName(replicationController));
            Set<Secret> secrets = generateSecrets(client, session, replicationController.getSpec().getTemplate().getMetadata());
            String serviceAccountName = replicationController.getSpec().getTemplate().getSpec().getServiceAccountName();
            if (Strings.isNotBlank(serviceAccountName)) {
                generateServiceAccount(client, session, secrets, serviceAccountName);
            }
            controller.applyReplicationController(replicationController, session.getId());
            conditions.put(1, sessionPodsReady);
        } else if (entity instanceof ReplicaSet || entity instanceof Deployment || entity instanceof DeploymentConfig) {
            log.status("Applying " + entity.getClass().getSimpleName() + ".");
            controller.apply(entity, session.getId());
            conditions.put(1, sessionPodsReady);
        } else if (entity instanceof OAuthClient) {
            OAuthClient oc = (OAuthClient) entity;
            // these are global so lets create a custom one for the new namespace
            ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(oc);
            String name = metadata.getName();
            if (isOpenshift) {
                OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
                OAuthClient current = openShiftClient.oAuthClients().withName(name).get();
                boolean create = false;
                if (current == null) {
                    current = oc;
                    create = true;
                }
                boolean updated = false;
                // lets add a new redirect entry
                List<String> redirectURIs = current.getRedirectURIs();
                String namespaceSuffix = "-" + namespace;
                String redirectUri = "http://" + name + namespaceSuffix;
                if (Strings.isNotBlank(routeDomain)) {
                    redirectUri += "." + Strings.stripPrefix(routeDomain, ".");
                }
                if (!redirectURIs.contains(redirectUri)) {
                    redirectURIs.add(redirectUri);
                    updated = true;
                }
                current.setRedirectURIs(redirectURIs);
                log.status("Applying OAuthClient:" + name);
                controller.setSupportOAuthClients(true);
                if (create) {
                    openShiftClient.oAuthClients().create(current);
                } else {
                    if (updated) {
                        // TODO this should work!
                        // openShiftClient.oAuthClients().withName(name).replace(current);
                        openShiftClient.oAuthClients().withName(name).delete();
                        current.getMetadata().setResourceVersion(null);
                        openShiftClient.oAuthClients().create(current);
                    }
                }
            }
        } else if (entity instanceof HasMetadata) {
            log.status("Applying " + entity.getClass().getSimpleName() + ":" + KubernetesHelper.getName((HasMetadata) entity));
            controller.apply(entity, session.getId());
        } else if (entity != null) {
            log.status("Applying " + entity.getClass().getSimpleName() + ".");
            controller.apply(entity, session.getId());
        }
    }
    entities.addAll(extraEntities);
    // Wait until conditions are meet.
    if (!conditions.isEmpty()) {
        Callable<Boolean> compositeCondition = new CompositeCondition(conditions.values());
        WaitStrategy waitStrategy = new WaitStrategy(compositeCondition, configuration.getWaitTimeout(), configuration.getWaitPollInterval());
        if (!waitStrategy.await()) {
            log.error("Timed out waiting for pods/services!");
            return false;
        } else {
            log.status("All pods/services are currently 'running'!");
        }
    } else {
        log.warn("No pods/services/replication controllers defined in the configuration!");
    }
    return true;
}
Also used : SessionPodsAreReady(io.fabric8.arquillian.kubernetes.await.SessionPodsAreReady) OAuthClient(io.fabric8.openshift.api.model.OAuthClient) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString) Logger(io.fabric8.arquillian.kubernetes.log.Logger) Callable(java.util.concurrent.Callable) HasMetadataComparator(io.fabric8.kubernetes.internal.HasMetadataComparator) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) Route(io.fabric8.openshift.api.model.Route) SessionServicesAreReady(io.fabric8.arquillian.kubernetes.await.SessionServicesAreReady) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) WaitStrategy(io.fabric8.arquillian.kubernetes.await.WaitStrategy) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) CompositeCondition(io.fabric8.arquillian.kubernetes.await.CompositeCondition)

Example 43 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class Example method listPods.

protected static void listPods(KubernetesClient kube) {
    System.out.println("\n\nLooking up pods");
    System.out.println("=========================================================================");
    PodList pods = kube.pods().list();
    // System.out.println("Got pods: " + pods);
    List<Pod> items = pods.getItems();
    for (Pod item : items) {
        System.out.println("Pod " + KubernetesHelper.getName(item) + " with ip: " + item.getStatus().getPodIP() + " created: " + item.getMetadata().getCreationTimestamp());
        PodSpec spec = item.getSpec();
        if (spec != null) {
            List<Container> containers = spec.getContainers();
            if (containers != null) {
                for (Container container : containers) {
                    System.out.println("Container " + container.getImage() + " " + container.getCommand() + " ports: " + container.getPorts());
                }
            }
        }
        Map<String, ContainerStatus> currentContainers = KubernetesHelper.getCurrentContainers(item);
        System.out.println("Has " + currentContainers.size() + " container(s)");
        Set<Map.Entry<String, ContainerStatus>> entries = currentContainers.entrySet();
        for (Map.Entry<String, ContainerStatus> entry : entries) {
            String id = entry.getKey();
            ContainerStatus info = entry.getValue();
            System.out.println("Current container: " + id + " info: " + info);
        }
    }
    System.out.println();
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Container(io.fabric8.kubernetes.api.model.Container) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Map(java.util.Map)

Example 44 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project fabric8 by jboss-fuse.

the class ConfigMaps method createConfigMapAnnotations.

private static Map<String, String> createConfigMapAnnotations(Session session, String status) {
    Map<String, String> annotations = new HashMap<>();
    File dir = Util.getProjectBaseDir(session);
    String gitUrl = Util.findGitUrl(session, dir);
    annotations.put(Annotations.Tests.SESSION_ID, session.getId());
    annotations.put(Annotations.Tests.TEST_SESSION_STATUS, status);
    if (Strings.isNotBlank(gitUrl)) {
        annotations.put(Annotations.Builds.GIT_URL, gitUrl);
    }
    // lets see if there's a maven generated set of pom properties
    File pomProperties = new File(dir, "target/maven-archiver/pom.properties");
    if (pomProperties.isFile()) {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream(pomProperties));
            Map<String, String> map = PropertiesHelper.toMap(properties);
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (Strings.isNotBlank(key) && Strings.isNotBlank(value)) {
                    annotations.put(Annotations.Project.PREFIX + key, value);
                }
            }
        } catch (IOException e) {
            session.getLogger().warn("Failed to load " + pomProperties + " file to annotate the namespace: " + e);
        }
    }
    return annotations;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) HashMap(java.util.HashMap) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream)

Example 45 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project docker-maven-plugin by fabric8io.

the class AbstractDockerMojo method execute.

/**
 * Entry point for this plugin. It will set up the helper class and then calls
 * {@link #executeInternal(ServiceHub)}
 * which must be implemented by subclass.
 *
 * @throws MojoExecutionException
 * @throws MojoFailureException
 */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (!skip) {
        boolean ansiRestore = Ansi.isEnabled();
        File output = null;
        if (outputFile != null) {
            output = new File(outputFile);
            if (output.exists()) {
                output.delete();
            }
        }
        log = new AnsiLogger(getLog(), useColorForLogging(), verbose, !settings.getInteractiveMode(), getLogPrefix(), output);
        try {
            authConfigFactory.setLog(log);
            imageConfigResolver.setLog(log);
            LogOutputSpecFactory logSpecFactory = new LogOutputSpecFactory(useColor, logStdout, logDate);
            ConfigHelper.validateExternalPropertyActivation(project, getAllImages());
            DockerAccess access = null;
            try {
                // The 'real' images configuration to use (configured images + externally resolved images)
                this.minimalApiVersion = initImageConfiguration(getBuildTimestamp());
                if (isDockerAccessRequired()) {
                    DockerAccessFactory.DockerAccessContext dockerAccessContext = getDockerAccessContext();
                    access = dockerAccessFactory.createDockerAccess(dockerAccessContext);
                }
                ServiceHub serviceHub = serviceHubFactory.createServiceHub(project, session, access, log, logSpecFactory);
                executeInternal(serviceHub);
            } catch (IOException | ExecException exp) {
                logException(exp);
                throw new MojoExecutionException(log.errorMessage(exp.getMessage()), exp);
            } catch (MojoExecutionException exp) {
                logException(exp);
                throw exp;
            } finally {
                if (access != null) {
                    access.shutdown();
                }
            }
        } finally {
            Ansi.setEnabled(ansiRestore);
            try {
                log.close();
            } catch (IOException exp) {
                logException(exp);
            }
        }
    }
}
Also used : DockerAccess(io.fabric8.maven.docker.access.DockerAccess) LogOutputSpecFactory(io.fabric8.maven.docker.log.LogOutputSpecFactory) ServiceHub(io.fabric8.maven.docker.service.ServiceHub) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecException(io.fabric8.maven.docker.access.ExecException) IOException(java.io.IOException) AnsiLogger(io.fabric8.maven.docker.util.AnsiLogger) File(java.io.File) DockerAccessFactory(io.fabric8.maven.docker.service.DockerAccessFactory)

Aggregations

Map (java.util.Map)89 HashMap (java.util.HashMap)57 IOException (java.io.IOException)31 File (java.io.File)30 ArrayList (java.util.ArrayList)26 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)16 List (java.util.List)14 Properties (java.util.Properties)14 HashSet (java.util.HashSet)10 Entry (io.fabric8.maven.docker.config.CopyConfiguration.Entry)9 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)9 Test (org.junit.Test)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 LinkedHashMap (java.util.LinkedHashMap)8 TreeMap (java.util.TreeMap)8 Resource (io.fabric8.kubernetes.client.dsl.Resource)7 FileInputStream (java.io.FileInputStream)7 Set (java.util.Set)7 Profile (io.fabric8.api.Profile)6 ProfileService (io.fabric8.api.ProfileService)5