Search in sources :

Example 96 with Config

use of io.fabric8.agent.model.Config in project fabric8 by fabric8io.

the class WebHooks method createGogsWebhook.

/**
 * Creates a webook in the given gogs repo for the user and password if the webhook does not already exist
 */
public static boolean createGogsWebhook(GitRepoClient repoClient, Logger log, String gogsUser, String repoName, String webhookUrl, String webhookSecret) throws JsonProcessingException {
    if (repoClient == null) {
        log.info("Cannot create Gogs webhooks as no Gogs service could be found or created");
        return false;
    }
    String gogsAddress = repoClient.getAddress();
    log.info("Querying webhooks in gogs at address: " + gogsAddress + " for user " + gogsUser + " repoName: " + repoName);
    RepositoryDTO repository = repoClient.getRepository(gogsUser, repoName);
    if (repository == null) {
        log.info("No repository found for user: " + gogsUser + " repo: " + repoName + " so cannot create any web hooks");
    // return false;
    }
    List<WebHookDTO> webhooks = repoClient.getWebhooks(gogsUser, repoName);
    for (WebHookDTO webhook : webhooks) {
        String url = null;
        WebhookConfig config = webhook.getConfig();
        if (config != null) {
            url = config.getUrl();
            if (Objects.equal(webhookUrl, url)) {
                log.info("Already has webhook for: " + url + " so not creating again");
                return false;
            }
            log.info("Ignoring webhook " + url + " from: " + toJson(config));
        }
    }
    CreateWebhookDTO createWebhook = new CreateWebhookDTO();
    createWebhook.setType("gogs");
    WebhookConfig config = createWebhook.getConfig();
    config.setUrl(webhookUrl);
    config.setSecret(webhookSecret);
    WebHookDTO webhook = repoClient.createWebhook(gogsUser, repoName, createWebhook);
    if (log.isDebugEnabled()) {
        log.debug("Got created web hook: " + toJson(webhook));
    }
    log.info("Created webhook for " + webhookUrl + " for user: " + gogsUser + " repoName: " + repoName + " on gogs URL: " + gogsAddress);
    return true;
}
Also used : WebhookConfig(io.fabric8.repo.git.WebhookConfig) WebHookDTO(io.fabric8.repo.git.WebHookDTO) RepositoryDTO(io.fabric8.repo.git.RepositoryDTO) CreateWebhookDTO(io.fabric8.repo.git.CreateWebhookDTO)

Example 97 with Config

use of io.fabric8.agent.model.Config 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 98 with Config

use of io.fabric8.agent.model.Config in project fabric8 by fabric8io.

the class KubernetesConfigAdminBridge method deleteConfig.

private void deleteConfig(ConfigMap map) {
    String pid = map.getMetadata().getLabels().get(pidLabel);
    String[] p = parsePid(pid);
    try {
        Map<String, String> configMapData = map.getData();
        Configuration config = getConfiguration(configAdmin.get(), pid, p[0], p[1]);
        if (configMapData != null) {
            boolean merge = configMapData.containsKey(FABRIC8_CONFIG_MERGE) ? Boolean.valueOf(configMapData.get(FABRIC8_CONFIG_MERGE)) : configMerge;
            if (!merge) {
                LOGGER.debug("Delete configuration {}", config.getPid());
                config.delete();
            }
        }
    } catch (Exception e) {
        LOGGER.warn("", e);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 99 with Config

use of io.fabric8.agent.model.Config 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 100 with Config

use of io.fabric8.agent.model.Config in project strimzi by strimzi.

the class TopicSerializationTest method testJsonSerializationRoundTrip.

@Test
public void testJsonSerializationRoundTrip() throws UnsupportedEncodingException {
    Topic.Builder builder = new Topic.Builder();
    builder.withTopicName("tom");
    builder.withMapName("bob");
    builder.withNumReplicas((short) 1);
    builder.withNumPartitions(2);
    builder.withConfigEntry("foo", "bar");
    Topic wroteTopic = builder.build();
    byte[] bytes = TopicSerialization.toJson(wroteTopic);
    String json = new String(bytes, "UTF-8");
    assertEquals("{\"map-name\":\"bob\"," + "\"topic-name\":\"tom\"," + "\"partitions\":2," + "\"replicas\":1," + "\"config\":{\"foo\":\"bar\"}" + "}", json);
    Topic readTopic = TopicSerialization.fromJson(bytes);
    assertEquals(wroteTopic, readTopic);
}
Also used : ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)128 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)44 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)43 IOException (java.io.IOException)41 HashMap (java.util.HashMap)40 File (java.io.File)31 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)28 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)28 Map (java.util.Map)28 RunImageConfiguration (io.fabric8.maven.docker.config.RunImageConfiguration)24 ArrayList (java.util.ArrayList)24 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)23 AbstractConfigHandlerTest (io.fabric8.maven.docker.config.handler.AbstractConfigHandlerTest)21 Expectations (mockit.Expectations)20 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)19 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)17 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)16 VolumeConfig (io.fabric8.maven.core.config.VolumeConfig)15 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)14 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)12