Search in sources :

Example 1 with KubernetesNamespaceProvider

use of org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider in project spring-cloud-kubernetes by spring-cloud.

the class Fabric8SecretsPropertySourceLocatorTests method locateShouldThrowExceptionOnFailureWhenFailFastIsEnabled.

@Test
public void locateShouldThrowExceptionOnFailureWhenFailFastIsEnabled() {
    final String name = "my-config";
    final String namespace = "default";
    final String path = String.format("/api/v1/namespaces/%s/secrets/%s", namespace, name);
    mockServer.expect().withPath(path).andReturn(500, "Internal Server Error").once();
    SecretsConfigProperties configMapConfigProperties = new SecretsConfigProperties();
    configMapConfigProperties.setName(name);
    configMapConfigProperties.setNamespace(namespace);
    configMapConfigProperties.setEnableApi(true);
    configMapConfigProperties.setFailFast(true);
    Fabric8SecretsPropertySourceLocator locator = new Fabric8SecretsPropertySourceLocator(mockClient, configMapConfigProperties, new KubernetesNamespaceProvider(new MockEnvironment()));
    assertThatThrownBy(() -> locator.locate(new MockEnvironment())).isInstanceOf(IllegalStateException.class).hasMessage("Unable to read Secret with name '" + name + "' or labels [{}] in namespace '" + namespace + "'");
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) SecretsConfigProperties(org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties) KubernetesNamespaceProvider(org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider) Test(org.junit.jupiter.api.Test)

Example 2 with KubernetesNamespaceProvider

use of org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider 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 3 with KubernetesNamespaceProvider

use of org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider in project spring-cloud-kubernetes by spring-cloud.

the class KubernetesClientServicesListSupplierTests method getListAllNamespaces.

@Test
void getListAllNamespaces() {
    MockEnvironment env = new MockEnvironment();
    env.setProperty(LoadBalancerClientFactory.PROPERTY_NAME, "service1");
    KubernetesNamespaceProvider kubernetesNamespaceProvider = mock(KubernetesNamespaceProvider.class);
    when(kubernetesNamespaceProvider.getNamespace()).thenReturn("default");
    KubernetesDiscoveryProperties kubernetesDiscoveryProperties = new KubernetesDiscoveryProperties();
    kubernetesDiscoveryProperties.setAllNamespaces(true);
    CoreV1Api coreV1Api = new CoreV1Api();
    KubernetesClientServiceInstanceMapper mapper = new KubernetesClientServiceInstanceMapper(new KubernetesLoadBalancerProperties(), kubernetesDiscoveryProperties);
    KubernetesClientServicesListSupplier listSupplier = new KubernetesClientServicesListSupplier(env, mapper, kubernetesDiscoveryProperties, coreV1Api, kubernetesNamespaceProvider);
    stubFor(get(urlMatching("^/api/v1/services.*")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(SERVICE_LIST_ALL_NAMESPACE))));
    Flux<List<ServiceInstance>> instances = listSupplier.get();
    Map<String, String> metadata = new HashMap<>();
    metadata.put("org.springframework.cloud", "true");
    metadata.put("beta", "true");
    KubernetesServiceInstance service1 = new KubernetesServiceInstance("0", "service1", "service1.default.svc.cluster.local", 80, metadata, false);
    KubernetesServiceInstance service2 = new KubernetesServiceInstance("1", "service1", "service1.test.svc.cluster.local", 80, new HashMap<>(), false);
    List<ServiceInstance> services = new ArrayList<>();
    services.add(service1);
    services.add(service2);
    StepVerifier.create(instances).expectNext(services).verifyComplete();
}
Also used : KubernetesServiceInstance(org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KubernetesServiceInstance(org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance) ServiceInstance(org.springframework.cloud.client.ServiceInstance) JSON(io.kubernetes.client.openapi.JSON) KubernetesDiscoveryProperties(org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties) KubernetesLoadBalancerProperties(org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties) MockEnvironment(org.springframework.mock.env.MockEnvironment) ArrayList(java.util.ArrayList) V1ServiceList(io.kubernetes.client.openapi.models.V1ServiceList) List(java.util.List) KubernetesNamespaceProvider(org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) Test(org.junit.jupiter.api.Test)

Example 4 with KubernetesNamespaceProvider

use of org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider in project spring-cloud-kubernetes by spring-cloud.

the class KubernetesInformerReactiveDiscoveryClientTests method testDiscoveryGetInstanceAllNamespaceShouldWork.

@Test
public void testDiscoveryGetInstanceAllNamespaceShouldWork() {
    Lister<V1Service> serviceLister = setupServiceLister(testService1, testService2);
    Lister<V1Endpoints> endpointsLister = setupEndpointsLister(testEndpoints1);
    when(kubernetesDiscoveryProperties.isAllNamespaces()).thenReturn(true);
    KubernetesInformerReactiveDiscoveryClient discoveryClient = new KubernetesInformerReactiveDiscoveryClient(new KubernetesNamespaceProvider(new MockEnvironment()), sharedInformerFactory, serviceLister, endpointsLister, null, null, kubernetesDiscoveryProperties);
    StepVerifier.create(discoveryClient.getInstances("test-svc-1")).expectNext(new KubernetesServiceInstance("", "test-svc-1", "2.2.2.2", 8080, new HashMap<>(), false, "namespace1", null)).expectComplete().verify();
    verify(kubernetesDiscoveryProperties, times(2)).isAllNamespaces();
}
Also used : KubernetesServiceInstance(org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance) V1Endpoints(io.kubernetes.client.openapi.models.V1Endpoints) HashMap(java.util.HashMap) MockEnvironment(org.springframework.mock.env.MockEnvironment) V1Service(io.kubernetes.client.openapi.models.V1Service) KubernetesNamespaceProvider(org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider) Test(org.junit.Test)

Example 5 with KubernetesNamespaceProvider

use of org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider in project spring-cloud-kubernetes by spring-cloud.

the class KubernetesInformerReactiveDiscoveryClientTests method testDiscoveryGetServicesOneNamespaceShouldWork.

@Test
public void testDiscoveryGetServicesOneNamespaceShouldWork() {
    Lister<V1Service> serviceLister = setupServiceLister(testService1, testService2);
    when(kubernetesDiscoveryProperties.isAllNamespaces()).thenReturn(false);
    KubernetesNamespaceProvider kubernetesNamespaceProvider = mock(KubernetesNamespaceProvider.class);
    when(kubernetesNamespaceProvider.getNamespace()).thenReturn("namespace1");
    KubernetesInformerReactiveDiscoveryClient discoveryClient = new KubernetesInformerReactiveDiscoveryClient(kubernetesNamespaceProvider, sharedInformerFactory, serviceLister, null, null, null, kubernetesDiscoveryProperties);
    StepVerifier.create(discoveryClient.getServices()).expectNext(testService1.getMetadata().getName()).expectComplete().verify();
    verify(kubernetesDiscoveryProperties, times(1)).isAllNamespaces();
}
Also used : V1Service(io.kubernetes.client.openapi.models.V1Service) KubernetesNamespaceProvider(org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider) Test(org.junit.Test)

Aggregations

KubernetesNamespaceProvider (org.springframework.cloud.kubernetes.commons.KubernetesNamespaceProvider)23 Test (org.junit.jupiter.api.Test)19 MockEnvironment (org.springframework.mock.env.MockEnvironment)18 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)15 JSON (io.kubernetes.client.openapi.JSON)8 ConfigMapConfigProperties (org.springframework.cloud.kubernetes.commons.config.ConfigMapConfigProperties)8 SecretsConfigProperties (org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties)7 HashMap (java.util.HashMap)5 V1Service (io.kubernetes.client.openapi.models.V1Service)4 Test (org.junit.Test)4 KubernetesServiceInstance (org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance)4 ArrayList (java.util.ArrayList)3 KubernetesClientProperties (org.springframework.cloud.kubernetes.commons.KubernetesClientProperties)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 V1Endpoints (io.kubernetes.client.openapi.models.V1Endpoints)2 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)2 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)2