Fix duplicate config maps (#844)

This commit is contained in:
erabii
2021-08-05 14:49:43 -04:00
committed by GitHub
parent 9b54b71872
commit 5c13b9bd63
3 changed files with 44 additions and 10 deletions

View File

@@ -16,7 +16,7 @@
package org.springframework.cloud.kubernetes.client.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.github.tomakehurst.wiremock.WireMockServer;
@@ -95,7 +95,7 @@ class KubernetesClientConfigMapPropertySourceLocatorTests {
configMapConfigProperties.setName("bootstrap-640");
KubernetesClientProperties kubernetesClientProperties = new KubernetesClientProperties();
kubernetesClientProperties.setNamespace("default");
PropertySource propertySource = new KubernetesClientConfigMapPropertySourceLocator(api,
PropertySource<?> propertySource = new KubernetesClientConfigMapPropertySourceLocator(api,
configMapConfigProperties, kubernetesClientProperties).locate(new MockEnvironment());
assertThat(propertySource.containsProperty("spring.cloud.kubernetes.configuration.watcher.refreshDelay"))
.isTrue();
@@ -108,15 +108,14 @@ class KubernetesClientConfigMapPropertySourceLocatorTests {
.willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(PROPERTIES_CONFIGMAP_LIST))));
ConfigMapConfigProperties configMapConfigProperties = new ConfigMapConfigProperties();
configMapConfigProperties.setName("fake-name");
ConfigMapConfigProperties.Source source1 = new ConfigMapConfigProperties.Source();
source1.setName("bootstrap-640");
source1.setNamespace("default");
List<ConfigMapConfigProperties.Source> sources = new ArrayList<>();
sources.add(source1);
ConfigMapConfigProperties.Source source = new ConfigMapConfigProperties.Source();
source.setName("bootstrap-640");
source.setNamespace("default");
List<ConfigMapConfigProperties.Source> sources = Collections.singletonList(source);
configMapConfigProperties.setSources(sources);
KubernetesClientProperties kubernetesClientProperties = new KubernetesClientProperties();
kubernetesClientProperties.setNamespace("dev");
PropertySource propertySource = new KubernetesClientConfigMapPropertySourceLocator(api,
PropertySource<?> propertySource = new KubernetesClientConfigMapPropertySourceLocator(api,
configMapConfigProperties, kubernetesClientProperties).locate(new MockEnvironment());
assertThat(propertySource.containsProperty("spring.cloud.kubernetes.configuration.watcher.refreshDelay"))
.isTrue();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.springframework.cloud.kubernetes.commons.config;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -132,6 +133,23 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties {
return new NormalizedSource(normalizedName, normalizedNamespace);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Source other = (Source) o;
return Objects.equals(this.name, other.name) && Objects.equals(this.namespace, other.namespace);
}
@Override
public int hashCode() {
return Objects.hash(name, namespace);
}
}
public static class NormalizedSource {
@@ -158,6 +176,23 @@ public class ConfigMapConfigProperties extends AbstractConfigProperties {
return "{ config-map name : '" + name + "', namespace : '" + namespace + "' }";
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
NormalizedSource other = (NormalizedSource) o;
return Objects.equals(this.name, other.name) && Objects.equals(this.namespace, other.namespace);
}
@Override
public int hashCode() {
return Objects.hash(name, namespace);
}
}
}

View File

@@ -85,7 +85,7 @@ public class MultipleConfigMapsTests {
.addToData(data).done();
}
// the last confimap defined in 'multiplecms.yml' has the highest priority, so
// the last configmap defined in 'multiplecms.yml' has the highest priority, so
// the common property defined in all configmaps is taken from the last one defined
@Test
public void testCommonMessage() {