Search in sources :

Example 61 with Entry

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

the class SessionListener method generateSecrets.

private Set<Secret> generateSecrets(KubernetesClient client, Session session, ObjectMeta meta) {
    Set<Secret> secrets = new HashSet<>();
    Map<String, String> annotations = meta.getAnnotations();
    if (annotations != null && !annotations.isEmpty()) {
        for (Map.Entry<String, String> entry : annotations.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (SecretKeys.isSecretKey(key)) {
                SecretKeys keyType = SecretKeys.fromValue(key);
                for (String name : Secrets.getNames(value)) {
                    Map<String, String> data = new HashMap<>();
                    Secret secret = null;
                    try {
                        secret = client.secrets().inNamespace(session.getNamespace()).withName(name).get();
                    } catch (Exception e) {
                    // ignore - probably doesn't exist
                    }
                    if (secret == null) {
                        for (String c : Secrets.getContents(value, name)) {
                            data.put(c, keyType.generate());
                        }
                        secret = client.secrets().inNamespace(session.getNamespace()).createNew().withNewMetadata().withName(name).endMetadata().withData(data).done();
                        secrets.add(secret);
                    }
                }
            }
        }
    }
    return secrets;
}
Also used : Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString) SecretKeys(io.fabric8.arquillian.utils.SecretKeys) MultiException(io.fabric8.utils.MultiException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 62 with Entry

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

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 63 with Entry

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

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 64 with Entry

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

the class YamlTest method testParseYaml.

@Test
public void testParseYaml() throws Exception {
    String basedir = System.getProperty("basedir", ".");
    File file = new File(basedir, "src/test/resources/fabric8.yml");
    assertThat(file).exists();
    ProjectConfig config = ProjectConfigs.parseProjectConfig(file);
    System.out.println("Parsed: " + config);
    assertThat(config.getChatRoom()).isEqualTo("myroom");
    assertThat(config.getIssueProjectName()).isEqualTo("foo");
    assertThat(config.getPipeline()).isEqualTo("maven/CanaryReleaseThenStage.groovy");
    LinkedHashMap<String, String> environments = config.getEnvironments();
    // lets assert that things are in the correct order...
    List<Pair<String, String>> expectedEnvironents = Arrays.asList(new Pair<String, String>("Testing", "gogsadmin-james4-testing"), new Pair<String, String>("Staging", "gogsadmin-james4-staging"), new Pair<String, String>("Production", "gogsadmin-james4-prod"));
    int idx = 0;
    Set<Map.Entry<String, String>> entries = environments.entrySet();
    for (Map.Entry<String, String> entry : entries) {
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println("Found environment " + key + " = " + value);
        Pair<String, String> actual = new Pair<>(key, value);
        assertTrue("Too many entries - found unexpected value: " + actual, expectedEnvironents.size() > idx);
        Pair<String, String> expected = expectedEnvironents.get(idx++);
        assertEquals("environment " + idx, expected, actual);
    }
}
Also used : File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(io.fabric8.utils.Pair) Test(org.junit.Test)

Example 65 with Entry

use of io.fabric8.maven.docker.config.CopyConfiguration.Entry in project strimzi by strimzi.

the class TopicSerialization method topicConfigFromConfigMapString.

@SuppressWarnings("unchecked")
private static Map<String, String> topicConfigFromConfigMapString(ConfigMap cm) {
    Map<String, String> mapData = cm.getData();
    String value = mapData.get(CM_KEY_CONFIG);
    Map<?, ?> result;
    if (value == null || value.isEmpty()) {
        result = Collections.emptyMap();
    } else {
        try {
            ObjectMapper mapper = objectMapper();
            result = mapper.readValue(new StringReader(value) {

                @Override
                public String toString() {
                    return "'config' key of 'data' section of ConfigMap '" + cm.getMetadata().getName() + "' in namespace '" + cm.getMetadata().getNamespace() + "'";
                }
            }, Map.class);
        } catch (IOException e) {
            throw new InvalidConfigMapException(cm, "ConfigMap's 'data' section has invalid key '" + CM_KEY_CONFIG + "': " + (e.getMessage() != null ? e.getMessage() : e.toString()));
        }
    }
    Set<String> supportedConfigs = getSupportedTopicConfigs();
    for (Map.Entry<?, ?> entry : result.entrySet()) {
        Object key = entry.getKey();
        String msg = null;
        if (!(key instanceof String)) {
            msg = "The must be of type String, not of type " + key.getClass();
        }
        Object v = entry.getValue();
        if (v == null) {
            msg = "The value corresponding to the key must have a String value, not null";
        } else if (!(v instanceof String)) {
            msg = "The value corresponding to the key must have a String value, not a value of type " + v.getClass();
        }
        if (!supportedConfigs.contains(key)) {
            msg = "The allowed configs keys are " + supportedConfigs;
        }
        if (msg != null) {
            throw new InvalidConfigMapException(cm, "ConfigMap's 'data' section has invalid key '" + CM_KEY_CONFIG + "': The key '" + key + "' of the topic config is invalid: " + msg);
        }
    }
    return (Map<String, String>) result;
}
Also used : StringReader(java.io.StringReader) IOException(java.io.IOException) HashMap(java.util.HashMap) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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