Java Code Examples for io.fabric8.kubernetes.api.model.ConfigMap#getData()

The following examples show how to use io.fabric8.kubernetes.api.model.ConfigMap#getData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigMapEnricherTest.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void should_materialize_file_content_from_annotation() throws Exception {
    final ConfigMap baseConfigMap = createAnnotationConfigMap("test-application.properties", "src/test/resources/test-application.properties");
    final KubernetesListBuilder builder = new KubernetesListBuilder()
            .addToConfigMapItems(baseConfigMap);
    new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);

    final ConfigMap configMap = (ConfigMap) builder.buildFirstItem();

    final Map<String, String> data = configMap.getData();
    assertThat(data)
            .containsEntry("test-application.properties", readFileContentsAsString("src/test/resources/test-application.properties"));

    final Map<String, String> annotations = configMap.getMetadata().getAnnotations();
    assertThat(annotations)
            .isEmpty();
}
 
Example 2
Source File: ConfigMapEnricherTest.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void should_materialize_binary_file_content_from_annotation() throws Exception {
    final ConfigMap baseConfigMap = createAnnotationConfigMap("test.bin", "src/test/resources/test.bin");
    final KubernetesListBuilder builder = new KubernetesListBuilder()
            .addToConfigMapItems(baseConfigMap);
    new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);

    final ConfigMap configMap = (ConfigMap) builder.buildFirstItem();

    final Map<String, String> data = configMap.getData();
    assertThat(data)
            .isEmpty();

    final Map<String, String> binaryData = configMap.getBinaryData();
    assertThat(binaryData)
            .containsEntry("test.bin", "wA==");

    final Map<String, String> annotations = configMap.getMetadata().getAnnotations();
    assertThat(annotations)
            .isEmpty();
}
 
Example 3
Source File: ConfigMapTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromResourceWithFileConfigMap() throws InterruptedException {
  KubernetesClient client = server.getClient();
  ConfigMap configMap = client.configMaps().load(getClass().getResourceAsStream("/test-application-properties-config-map.yml")).get();
  assertEquals("cfg1", configMap.getMetadata().getName());

  Map<String, String> data = (Map<String, String>) configMap.getData();
  String result = data.get("application.properties");
  String[] p1 = result.split("\n");
  Map<String,String> keys = new HashMap<>();
  for (int i = 0; i < p1.length; i += 1) {
    String[] p2 = p1[i].split(": ");
    keys.put(p2[0],p2[1]);
  }
  assertEquals("gouda",keys.get("cheese"));
  assertEquals("bar",keys.get("foo"));
}
 
Example 4
Source File: HadoopConfMountDecoratorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testHadoopConfConfigMap() throws IOException {
	setHadoopConfDirEnv();
	generateHadoopConfFileItems();

	final List<HasMetadata> additionalResources = hadoopConfMountDecorator.buildAccompanyingKubernetesResources();
	assertEquals(1, additionalResources.size());

	final ConfigMap resultConfigMap = (ConfigMap) additionalResources.get(0);

	assertEquals(Constants.API_VERSION, resultConfigMap.getApiVersion());
	assertEquals(HadoopConfMountDecorator.getHadoopConfConfigMapName(CLUSTER_ID),
		resultConfigMap.getMetadata().getName());
	assertEquals(getCommonLabels(), resultConfigMap.getMetadata().getLabels());

	Map<String, String> resultDatas = resultConfigMap.getData();
	assertEquals("some data", resultDatas.get("core-site.xml"));
	assertEquals("some data", resultDatas.get("hdfs-site.xml"));
}
 
Example 5
Source File: KubernetesJobManagerFactoryTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testFlinkConfConfigMap() {
	final ConfigMap resultConfigMap = (ConfigMap) this.kubernetesJobManagerSpecification.getAccompanyingResources()
		.stream()
		.filter(x -> x instanceof ConfigMap &&
			x.getMetadata().getName().equals(FlinkConfMountDecorator.getFlinkConfConfigMapName(CLUSTER_ID)))
		.collect(Collectors.toList())
		.get(0);

	assertEquals(2, resultConfigMap.getMetadata().getLabels().size());

	final Map<String, String> resultDatas = resultConfigMap.getData();
	assertEquals(3, resultDatas.size());
	assertEquals("some data", resultDatas.get("log4j.properties"));
	assertEquals("some data", resultDatas.get("logback.xml"));
	assertTrue(resultDatas.get(FLINK_CONF_FILENAME)
		.contains(KubernetesConfigOptionsInternal.ENTRY_POINT_CLASS.key() + ": " + ENTRY_POINT_CLASS));
}
 
Example 6
Source File: KubernetesJobManagerFactoryTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testHadoopConfConfigMap() throws IOException {
	setHadoopConfDirEnv();
	generateHadoopConfFileItems();
	kubernetesJobManagerSpecification = KubernetesJobManagerFactory.buildKubernetesJobManagerSpecification(kubernetesJobManagerParameters);

	final ConfigMap resultConfigMap = (ConfigMap) kubernetesJobManagerSpecification.getAccompanyingResources()
		.stream()
		.filter(x -> x instanceof ConfigMap &&
			x.getMetadata().getName().equals(HadoopConfMountDecorator.getHadoopConfConfigMapName(CLUSTER_ID)))
		.collect(Collectors.toList())
		.get(0);

	assertEquals(2, resultConfigMap.getMetadata().getLabels().size());

	final Map<String, String> resultDatas = resultConfigMap.getData();
	assertEquals(2, resultDatas.size());
	assertEquals("some data", resultDatas.get("core-site.xml"));
	assertEquals("some data", resultDatas.get("hdfs-site.xml"));
}
 
Example 7
Source File: LeadershipController.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
private Leader extractLeader(ConfigMap configMap) {
	if (configMap == null || configMap.getData() == null) {
		return null;
	}

	Map<String, String> data = configMap.getData();
	String leaderKey = getLeaderKey();
	String leaderId = data.get(leaderKey);
	if (leaderId == null) {
		return null;
	}

	return new Leader(this.candidate.getRole(), leaderId);
}
 
Example 8
Source File: ConfigMapsTests.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigMap() {
	ConfigMap configmap = mockClient.configMaps().inNamespace("test")
			.withName(APPLICATION_NAME).get();
	HashMap<String, String> keys = (HashMap<String, String>) configmap.getData();
	assertThat("Hello ConfigMap, %s!").isEqualTo(keys.get("bean.greeting"));
}
 
Example 9
Source File: ApplyStepExecution.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
private String loadKubernetesExposeControllerCommandLine(KubernetesClient kubernetes, String commands) throws Exception {
    String namespace = System.getenv("KUBERNETES_NAMESPACE");
    if (Strings.isNullOrBlank(namespace)) {
        namespace = KubernetesHelper.getNamespace(kubernetes);
    }
    ConfigMap configMap = kubernetes.configMaps().inNamespace(namespace).withName("exposecontroller").get();
    if (configMap == null) {
        listener.getLogger().println("WARNING: no ConfigMap in namespace " + namespace + " called: exposecontroller so cannot run exposecontroller to expose Service URLs");
        return null;
    }
    String configYaml = null;
    Map<String, String> data = configMap.getData();
    if (data != null) {
        configYaml = data.get("config.yml");
    }
    if (Strings.isNullOrBlank(configYaml)) {
        throw new Exception("ConfigMap " + namespace + "/exposecontroller does not have a `config.yml` data entry");
    }
    Map map;
    try {
        map = KubernetesHelper.loadYaml(configYaml, Map.class);
    } catch (IOException e) {
        throw new Exception("Could not parse YAML in ConfigMap " + namespace + "/exposecontroller entry `config.yml`: " + e, e);
    }
    StringBuilder builder = new StringBuilder(commands);
    appendCliArgument(builder, "--http", map.get("http"));
    appendCliArgument(builder, "--exposer", map.get("exposer"));
    appendCliArgument(builder, "--domain", map.get("domain"));
    return builder.toString();
}
 
Example 10
Source File: ApplyStepExecution.java    From kubernetes-pipeline-plugin with Apache License 2.0 5 votes vote down vote up
private String getRegistry() {
    if (Strings.isNullOrBlank(step.getRegistry())) {
        // lets try and find an external docker registry in the users home namespace pipeline config
        KubernetesClient client = getKubernetes();
        ConfigMap cm = client.configMaps().inNamespace(this.buildConfigNamespace).withName(Constants.USERS_PIPELINE_CONFIGMAP_NAME).get();
        if (cm != null){
            Map<String, String> data = cm.getData();
            if (data != null){
                String rs = data.get(Constants.EXTERNAL_DOCKER_REGISTRY_URL);
                if (Strings.isNotBlank(rs)){
                    return rs;
                }
            }
        }

        // fall back to namespace env vars to support old fabric8 version
        if (isOpenShift() && openShiftClient().supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE)) {
            if (Strings.isNotBlank(env.get(Constants.OPENSHIFT_DOCKER_REGISTRY_SERVICE_HOST))){
                return env.get(Constants.OPENSHIFT_DOCKER_REGISTRY_SERVICE_HOST) + ":" + env.get(Constants.OPENSHIFT_DOCKER_REGISTRY_SERVICE_PORT);
            }
        } else if (Strings.isNotBlank(env.get(Constants.FABRIC8_DOCKER_REGISTRY_SERVICE_HOST))) {
            return env.get(Constants.FABRIC8_DOCKER_REGISTRY_SERVICE_HOST) + ":" + env.get(Constants.FABRIC8_DOCKER_REGISTRY_SERVICE_PORT);
        }
        return null;
    } else {
        return step.getRegistry();
    }
}
 
Example 11
Source File: AbstractModel.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * @param logging The Logging to parse.
 * @param externalCm The external ConfigMap, used if Logging is an instance of ExternalLogging
 * @return The logging properties as a String in log4j/2 properties file format.
 */
public String parseLogging(Logging logging, ConfigMap externalCm) {
    if (logging instanceof InlineLogging) {
        InlineLogging inlineLogging = (InlineLogging) logging;
        OrderedProperties newSettings = getDefaultLogConfig();

        if (inlineLogging.getLoggers() != null) {
            // Inline logging as specified and some loggers are configured
            newSettings.addMapPairs(inlineLogging.getLoggers());
        }

        return createPropertiesString(newSettings);

    } else if (logging instanceof ExternalLogging) {
        if (externalCm != null && externalCm.getData() != null && externalCm.getData().containsKey(getAncillaryConfigMapKeyLogConfig())) {
            return externalCm.getData().get(getAncillaryConfigMapKeyLogConfig());
        } else {
            log.warn("ConfigMap {} with external logging configuration does not exist or doesn't contain the configuration under the {} key. Default logging settings are used.",
                    ((ExternalLogging) getLogging()).getName(),
                    getAncillaryConfigMapKeyLogConfig());
            return createPropertiesString(getDefaultLogConfig());
        }

    } else {
        log.debug("logging is not set, using default loggers");
        return createPropertiesString(getDefaultLogConfig());
    }
}
 
Example 12
Source File: ConfigMapTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromResourceConfigMap() throws InterruptedException {
  KubernetesClient client = server.getClient();
  ConfigMap configMap = client.configMaps().load(getClass().getResourceAsStream("/test-config-map.yml")).get();
  assertEquals("cfg1", configMap.getMetadata().getName());

  Map<String, String> keys = (Map<String, String>) configMap.getData();
  assertEquals("gouda",keys.get("cheese"));
  assertEquals("bar",keys.get("foo"));
}
 
Example 13
Source File: FlinkConfMountDecoratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigMap() throws IOException {
	KubernetesTestUtils.createTemporyFile("some data", flinkConfDir, "log4j.properties");
	KubernetesTestUtils.createTemporyFile("some data", flinkConfDir, "logback.xml");

	final List<HasMetadata> additionalResources = flinkConfMountDecorator.buildAccompanyingKubernetesResources();
	assertEquals(1, additionalResources.size());

	final ConfigMap resultConfigMap = (ConfigMap) additionalResources.get(0);

	assertEquals(Constants.API_VERSION, resultConfigMap.getApiVersion());

	assertEquals(getFlinkConfConfigMapName(CLUSTER_ID),
			resultConfigMap.getMetadata().getName());
	assertEquals(getCommonLabels(), resultConfigMap.getMetadata().getLabels());

	Map<String, String> resultDatas = resultConfigMap.getData();
	assertEquals("some data", resultDatas.get("logback.xml"));
	assertEquals("some data", resultDatas.get("log4j.properties"));

	final Configuration resultFlinkConfig = KubernetesTestUtils.loadConfigurationFromString(
		resultDatas.get(FLINK_CONF_FILENAME));
	assertThat(resultFlinkConfig.get(KubernetesConfigOptions.FLINK_CONF_DIR), is(FLINK_CONF_DIR_IN_POD));
	// The following config options should not be added to config map
	assertThat(resultFlinkConfig.get(KubernetesConfigOptions.KUBE_CONFIG_FILE), is(nullValue()));
	assertThat(resultFlinkConfig.get(DeploymentOptionsInternal.CONF_DIR), is(nullValue()));
}
 
Example 14
Source File: SshKeySecretProvisionerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void addSshKeysConfigInPod() throws Exception {
  String keyName1 = UUID.randomUUID().toString();
  String keyName2 = "default-" + UUID.randomUUID().toString();
  String keyName3 = "github.com";
  String keyName4 = UUID.randomUUID().toString();
  lenient()
      .when(sshManager.getPairs(someUser, "vcs"))
      .thenReturn(
          ImmutableList.of(
              new SshPairImpl(someUser, "vcs", keyName1, "public", "private"),
              new SshPairImpl(someUser, "vcs", keyName2, "public", "private"),
              new SshPairImpl(someUser, "vcs", keyName3, "public", "private")));

  lenient()
      .when(sshManager.getPairs(someUser, "internal"))
      .thenReturn(
          ImmutableList.of(new SshPairImpl(someUser, "internal", keyName4, "public", "private")));

  sshKeysProvisioner.provision(k8sEnv, runtimeIdentity);

  verify(podSpec, times(2)).getVolumes();
  verify(podSpec, times(2)).getContainers();

  Secret secret = k8sEnv.getSecrets().get("wksp-sshprivatekeys");
  assertNotNull(secret);
  assertEquals(secret.getType(), "opaque");

  String key1 = secret.getData().get(keyName1);
  assertNotNull(key1);
  assertEquals("private", new String(Base64.getDecoder().decode(key1)));

  String key2 = secret.getData().get(keyName2);
  assertNotNull(key2);
  assertEquals("private", new String(Base64.getDecoder().decode(key2)));

  String key3 = secret.getData().get(keyName3);
  assertNotNull(key3);
  assertEquals("private", new String(Base64.getDecoder().decode(key3)));

  String key4 = secret.getData().get(keyName3);
  assertNotNull(key3);
  assertEquals("private", new String(Base64.getDecoder().decode(key4)));

  Map<String, ConfigMap> configMaps = k8sEnv.getConfigMaps();
  assertNotNull(configMaps);
  assertTrue(configMaps.containsKey("wksp-sshconfigmap"));

  ConfigMap sshConfigMap = configMaps.get("wksp-sshconfigmap");
  assertNotNull(sshConfigMap);

  Map<String, String> mapData = sshConfigMap.getData();
  assertNotNull(mapData);
  assertTrue(mapData.containsKey("ssh_config"));

  String sshConfig = mapData.get("ssh_config");
  assertTrue(sshConfig.contains("host " + keyName1));
  assertTrue(sshConfig.contains("IdentityFile " + "/etc/ssh/private/" + keyName1));

  assertTrue(sshConfig.contains("host *"));
  assertTrue(sshConfig.contains("IdentityFile " + "/etc/ssh/private/" + keyName2));

  assertTrue(sshConfig.contains("host github.com"));
  assertTrue(sshConfig.contains("IdentityFile /etc/ssh/private/github.com"));
}