use of org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties in project spring-cloud-kubernetes by spring-cloud.
the class KubernetesClientEventBasedSecretsChangeDetectorTests method watch.
@Test
void watch() {
GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE).registerTypeAdapter(OffsetDateTime.class, new GsonOffsetDateTimeAdapter());
Gson gson = builder.create();
V1Secret dbPassword = new V1Secret().kind("Secret").metadata(new V1ObjectMeta().name("db-password")).putStringDataItem("password", Base64.getEncoder().encodeToString("p455w0rd".getBytes())).putStringDataItem("username", Base64.getEncoder().encodeToString("user".getBytes()));
V1Secret dbPasswordUpdated = new V1Secret().kind("Secret").metadata(new V1ObjectMeta().name("db-password")).putStringDataItem("password", Base64.getEncoder().encodeToString("p455w0rd2".getBytes())).putStringDataItem("username", Base64.getEncoder().encodeToString("user".getBytes()));
V1SecretList secretList = new V1SecretList().kind("SecretList").metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(dbPassword));
stubFor(get(urlMatching("^/api/v1/namespaces/default/secrets.*")).inScenario("watch").whenScenarioStateIs(STARTED).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withBody(gson.toJson(secretList))).willSetStateTo("update"));
Watch.Response<V1Secret> watchResponse = new Watch.Response<>(EventType.MODIFIED.name(), dbPasswordUpdated);
stubFor(get(urlMatching("/api/v1/namespaces/default/secrets.*")).inScenario("watch").whenScenarioStateIs("update").withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(watchResponse))).willSetStateTo("add"));
stubFor(get(urlMatching("/api/v1/namespaces/default/secrets.*")).inScenario("watch").whenScenarioStateIs("add").withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new Watch.Response<>(EventType.ADDED.name(), new V1Secret().kind("Secret").metadata(new V1ObjectMeta().name("rabbit-password")).putDataItem("rabbit-pw", Base64.getEncoder().encode("password".getBytes())))))).willSetStateTo("delete"));
stubFor(get(urlMatching("/api/v1/namespaces/default/secrets.*")).inScenario("watch").whenScenarioStateIs("delete").withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new Watch.Response<>(EventType.DELETED.name(), new V1Secret().kind("Secret").metadata(new V1ObjectMeta().name("rabbit-password")).putDataItem("rabbit-pw", Base64.getEncoder().encode("password".getBytes())))))).willSetStateTo("done"));
stubFor(get(urlMatching("/api/v1/namespaces/default/secrets.*")).inScenario("watch").whenScenarioStateIs("done").withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200)));
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + wireMockServer.port()).build();
OkHttpClient httpClient = apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
apiClient.setHttpClient(httpClient);
CoreV1Api coreV1Api = new CoreV1Api(apiClient);
ConfigurationUpdateStrategy strategy = mock(ConfigurationUpdateStrategy.class);
when(strategy.getName()).thenReturn("strategy");
KubernetesMockEnvironment environment = new KubernetesMockEnvironment(mock(KubernetesClientSecretsPropertySource.class)).withProperty("db-password", "p455w0rd");
KubernetesClientSecretsPropertySourceLocator locator = mock(KubernetesClientSecretsPropertySourceLocator.class);
when(locator.locate(environment)).thenAnswer(ignoreMe -> new MockPropertySource().withProperty("db-password", "p455w0rd2"));
ConfigReloadProperties properties = new ConfigReloadProperties();
properties.setMonitoringSecrets(true);
KubernetesNamespaceProvider kubernetesNamespaceProvider = mock(KubernetesNamespaceProvider.class);
when(kubernetesNamespaceProvider.getNamespace()).thenReturn("default");
KubernetesClientEventBasedSecretsChangeDetector changeDetector = new KubernetesClientEventBasedSecretsChangeDetector(coreV1Api, environment, properties, strategy, locator, kubernetesNamespaceProvider);
Thread controllerThread = new Thread(changeDetector::watch);
controllerThread.setDaemon(true);
controllerThread.start();
await().timeout(Duration.ofSeconds(300)).until(() -> Mockito.mockingDetails(strategy).getInvocations().size() > 4);
verify(strategy, atLeast(3)).reload();
}
use of org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties in project spring-cloud-kubernetes by spring-cloud.
the class HttpBasedConfigMapWatchChangeDetectorTests method setup.
@Before
public void setup() {
EndpointAddress fooEndpointAddress = new EndpointAddress();
fooEndpointAddress.setIp("127.0.0.1");
fooEndpointAddress.setHostname("localhost");
EndpointPort fooEndpointPort = new EndpointPort();
// fooEndpointPort.setPort(wireMockRule.port());
List<ServiceInstance> instances = new ArrayList<>();
KubernetesServiceInstance fooServiceInstance = new KubernetesServiceInstance("foo", "foo", fooEndpointAddress.getIp(), fooEndpointPort.getPort(), new HashMap<>(), false);
instances.add(fooServiceInstance);
when(reactiveDiscoveryClient.getInstances(eq("foo"))).thenReturn(Flux.fromIterable(instances));
MockEnvironment mockEnvironment = new MockEnvironment();
ConfigReloadProperties configReloadProperties = new ConfigReloadProperties();
configurationWatcherConfigurationProperties = new ConfigurationWatcherConfigurationProperties();
WebClient webClient = WebClient.builder().build();
changeDetector = new HttpBasedConfigMapWatchChangeDetector(mockEnvironment, configReloadProperties, client, updateStrategy, fabric8ConfigMapPropertySourceLocator, configurationWatcherConfigurationProperties, threadPoolTaskExecutor, webClient, reactiveDiscoveryClient);
}
use of org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties in project spring-cloud-kubernetes by spring-cloud.
the class EventBasedConfigurationChangeDetectorTests method verifyConfigChangesAccountsForBootstrapPropertySources.
@SuppressWarnings("unchecked")
@Test
public void verifyConfigChangesAccountsForBootstrapPropertySources() {
ConfigReloadProperties configReloadProperties = new ConfigReloadProperties();
MockEnvironment env = new MockEnvironment();
KubernetesClient k8sClient = mock(KubernetesClient.class);
ConfigMap configMap = new ConfigMap();
Map<String, String> data = new HashMap<>();
data.put("foo", "bar");
configMap.setData(data);
MixedOperation<ConfigMap, ConfigMapList, Resource<ConfigMap>> mixedOperation = mock(MixedOperation.class);
when(k8sClient.configMaps()).thenReturn(mixedOperation);
Resource<ConfigMap> resource = mock(Resource.class);
when(resource.get()).thenReturn(configMap);
when(mixedOperation.withName(eq("myconfigmap"))).thenReturn(resource);
when(mixedOperation.inNamespace("default")).thenReturn(mixedOperation);
when(k8sClient.getNamespace()).thenReturn("default");
Fabric8ConfigMapPropertySource fabric8ConfigMapPropertySource = new Fabric8ConfigMapPropertySource(k8sClient, "myconfigmap", "default", new MockEnvironment(), "", true, false);
env.getPropertySources().addFirst(new BootstrapPropertySource<>(fabric8ConfigMapPropertySource));
ConfigurationUpdateStrategy configurationUpdateStrategy = mock(ConfigurationUpdateStrategy.class);
Fabric8ConfigMapPropertySourceLocator configMapLocator = mock(Fabric8ConfigMapPropertySourceLocator.class);
EventBasedConfigMapChangeDetector detector = new EventBasedConfigMapChangeDetector(env, configReloadProperties, k8sClient, configurationUpdateStrategy, configMapLocator);
List<Fabric8ConfigMapPropertySource> sources = detector.findPropertySources(Fabric8ConfigMapPropertySource.class);
assertThat(sources.size()).isEqualTo(1);
assertThat(sources.get(0).getProperty("foo")).isEqualTo("bar");
}
use of org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties in project spring-cloud-kubernetes by spring-cloud.
the class BusEventBasedConfigMapWatcherChangeDetectorTests method setup.
@Before
public void setup() {
MockEnvironment mockEnvironment = new MockEnvironment();
ConfigReloadProperties configReloadProperties = new ConfigReloadProperties();
configurationWatcherConfigurationProperties = new ConfigurationWatcherConfigurationProperties();
busProperties = new BusProperties();
changeDetector = new BusEventBasedConfigMapWatcherChangeDetector(mockEnvironment, configReloadProperties, client, updateStrategy, fabric8ConfigMapPropertySourceLocator, busProperties, configurationWatcherConfigurationProperties, threadPoolTaskExecutor);
changeDetector.setApplicationEventPublisher(applicationEventPublisher);
}
use of org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties in project spring-cloud-kubernetes by spring-cloud.
the class BusEventBasedSecretsWatcherChangeDetectorTests method setup.
@Before
public void setup() {
MockEnvironment mockEnvironment = new MockEnvironment();
ConfigReloadProperties configReloadProperties = new ConfigReloadProperties();
configurationWatcherConfigurationProperties = new ConfigurationWatcherConfigurationProperties();
busProperties = new BusProperties();
changeDetector = new BusEventBasedSecretsWatcherChangeDetector(mockEnvironment, configReloadProperties, client, updateStrategy, fabric8SecretsPropertySourceLocator, busProperties, configurationWatcherConfigurationProperties, threadPoolTaskExecutor);
changeDetector.setApplicationEventPublisher(applicationEventPublisher);
}
Aggregations