Search in sources :

Example 1 with ConfigurationUpdateStrategy

use of org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy 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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) KubernetesClientSecretsPropertySourceLocator(org.springframework.cloud.kubernetes.client.config.KubernetesClientSecretsPropertySourceLocator) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Gson(com.google.gson.Gson) JSON(io.kubernetes.client.openapi.JSON) ConfigurationUpdateStrategy(org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy) V1SecretList(io.kubernetes.client.openapi.models.V1SecretList) Watch(io.kubernetes.client.util.Watch) KubernetesNamespaceProvider(org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ClientBuilder(io.kubernetes.client.util.ClientBuilder) GsonBuilder(com.google.gson.GsonBuilder) ApiClient(io.kubernetes.client.openapi.ApiClient) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) ConfigReloadProperties(org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties) MockPropertySource(org.springframework.mock.env.MockPropertySource) V1Secret(io.kubernetes.client.openapi.models.V1Secret) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurationUpdateStrategy

use of org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy 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");
}
Also used : ConfigMapList(io.fabric8.kubernetes.api.model.ConfigMapList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) HashMap(java.util.HashMap) Resource(io.fabric8.kubernetes.client.dsl.Resource) Fabric8ConfigMapPropertySource(org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySource) ConfigurationUpdateStrategy(org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy) ConfigReloadProperties(org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties) Fabric8ConfigMapPropertySourceLocator(org.springframework.cloud.kubernetes.fabric8.config.Fabric8ConfigMapPropertySourceLocator) MockEnvironment(org.springframework.mock.env.MockEnvironment) Test(org.junit.jupiter.api.Test)

Example 3 with ConfigurationUpdateStrategy

use of org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy in project spring-cloud-kubernetes by spring-cloud.

the class KubernetesClientEventBasedConfigMapChangeDetectorTests 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();
    Map<String, String> data = new HashMap<>();
    data.put("application.properties", "spring.cloud.kubernetes.configuration.watcher.refreshDelay=0\n" + "logging.level.org.springframework.cloud.kubernetes=TRACE");
    Map<String, String> updateData = new HashMap<>();
    updateData.put("application.properties", "spring.cloud.kubernetes.configuration.watcher.refreshDelay=1\n" + "logging.level.org.springframework.cloud.kubernetes=TRACE");
    V1ConfigMap applicationConfig = new V1ConfigMap().kind("ConfigMap").metadata(new V1ObjectMeta().namespace("default").name("bar1")).data(data);
    V1ConfigMapList configMapList = new V1ConfigMapList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(applicationConfig));
    stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).inScenario("watch").whenScenarioStateIs(STARTED).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withBody(gson.toJson(configMapList))).willSetStateTo("update"));
    Watch.Response<V1ConfigMap> watchResponse = new Watch.Response<>(EventType.MODIFIED.name(), new V1ConfigMap().kind("ConfigMap").metadata(new V1ObjectMeta().namespace("default").name("bar1")).data(updateData));
    stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).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/configmaps.*")).inScenario("watch").whenScenarioStateIs("add").withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new Watch.Response<>(EventType.ADDED.name(), new V1ConfigMap().kind("ConfigMap").metadata(new V1ObjectMeta().namespace("default").name("bar3")).putDataItem("application.properties", "debug=true"))))).willSetStateTo("delete"));
    stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).inScenario("watch").whenScenarioStateIs("delete").withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new Watch.Response<>(EventType.DELETED.name(), new V1ConfigMap().kind("ConfigMap").metadata(new V1ObjectMeta().namespace("default").name("bar1")).putDataItem("application.properties", "debug=true"))))).willSetStateTo("done"));
    stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).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(KubernetesClientConfigMapPropertySource.class)).withProperty("debug", "true");
    KubernetesClientConfigMapPropertySourceLocator locator = mock(KubernetesClientConfigMapPropertySourceLocator.class);
    when(locator.locate(environment)).thenAnswer(x -> new MockPropertySource().withProperty("debug", "false"));
    KubernetesNamespaceProvider kubernetesNamespaceProvider = mock(KubernetesNamespaceProvider.class);
    when(kubernetesNamespaceProvider.getNamespace()).thenReturn("default");
    KubernetesClientEventBasedConfigMapChangeDetector changeDetector = new KubernetesClientEventBasedConfigMapChangeDetector(coreV1Api, environment, new ConfigReloadProperties(), strategy, locator, kubernetesNamespaceProvider);
    Thread controllerThread = new Thread(changeDetector::watch);
    controllerThread.setDaemon(true);
    controllerThread.start();
    await().timeout(Duration.ofSeconds(5)).until(() -> Mockito.mockingDetails(strategy).getInvocations().size() > 4);
    verify(strategy, atLeast(3)).reload();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HashMap(java.util.HashMap) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Gson(com.google.gson.Gson) JSON(io.kubernetes.client.openapi.JSON) ConfigurationUpdateStrategy(org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy) Watch(io.kubernetes.client.util.Watch) KubernetesNamespaceProvider(org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider) V1ConfigMapList(io.kubernetes.client.openapi.models.V1ConfigMapList) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) ClientBuilder(io.kubernetes.client.util.ClientBuilder) GsonBuilder(com.google.gson.GsonBuilder) ApiClient(io.kubernetes.client.openapi.ApiClient) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) ConfigReloadProperties(org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties) KubernetesClientConfigMapPropertySourceLocator(org.springframework.cloud.kubernetes.client.config.KubernetesClientConfigMapPropertySourceLocator) MockPropertySource(org.springframework.mock.env.MockPropertySource) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)3 ConfigReloadProperties (org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadProperties)3 ConfigurationUpdateStrategy (org.springframework.cloud.kubernetes.commons.config.reload.ConfigurationUpdateStrategy)3 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 ApiClient (io.kubernetes.client.openapi.ApiClient)2 JSON (io.kubernetes.client.openapi.JSON)2 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)2 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)2 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)2 ClientBuilder (io.kubernetes.client.util.ClientBuilder)2 Watch (io.kubernetes.client.util.Watch)2 HashMap (java.util.HashMap)2 OkHttpClient (okhttp3.OkHttpClient)2 KubernetesNamespaceProvider (org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider)2 MockPropertySource (org.springframework.mock.env.MockPropertySource)2 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 ConfigMapList (io.fabric8.kubernetes.api.model.ConfigMapList)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1