Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package org.springframework.cloud.kubernetes.commons.discovery;

import java.io.Serializable;
import java.net.URI;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS;
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY;
Expand All @@ -34,9 +37,10 @@
* @param namespace the namespace of the service.
* @param cluster the cluster the service resides in.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public record DefaultKubernetesServiceInstance(String instanceId, String serviceId, String host, int port,
Map<String, String> metadata, boolean secure, String namespace, String cluster,
Map<String, Map<String, String>> podMetadata) implements KubernetesServiceInstance {
Map<String, Map<String, String>> podMetadata) implements KubernetesServiceInstance, Serializable {

@Override
public String getInstanceId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.kubernetes.commons.discovery;

import java.io.Serializable;
import java.net.URI;
import java.util.Map;

Expand All @@ -28,7 +29,7 @@
* @author wind57
*/
public record KubernetesExternalNameServiceInstance(String serviceId, String host, String instanceId,
Map<String, String> metadata) implements ServiceInstance {
Map<String, String> metadata) implements ServiceInstance, Serializable {

@Override
public String getServiceId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ void testResolveProfileSpecificSix() {

Profiles profiles = Mockito.mock(Profiles.class);
ConfigDataLocation configDataLocation = ConfigDataLocation.of("kubernetes:abc");
storePropertiesResolver.resolveProfileSpecific(RESOLVER_CONTEXT,
configDataLocation, profiles);
storePropertiesResolver.resolveProfileSpecific(RESOLVER_CONTEXT, configDataLocation, profiles);

// on the other hand, @Default will be picked here
Assertions.assertThat(storePropertiesResolver.configMapConfigProperties.enabled()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright 2019-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.kubernetes.commons.discovery;

import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import org.springframework.boot.test.json.BasicJsonTester;
import org.springframework.cloud.client.ServiceInstance;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author wind57
*/
class ServiceInstanceSerializationTests {

private static final BasicJsonTester BASIC_JSON_TESTER = new BasicJsonTester(
ServiceInstanceSerializationTests.class);

/**
* <pre>
* {
* "instanceId": "instanceId",
* "serviceId": "serviceId",
* "host": "host",
* "port": 8080,
* "metadata": {
* "k8s_namespace": "spring-k8s"
* },
* "secure": true,
* "namespace": "namespace",
* "cluster": "cluster",
* "podMetadata": {
* "pod_root": {
* "pod_key": "pod_value"
* }
* },
* "scheme": "https",
* "uri": "https://host:8080"
* }
* </pre>
*/
@Test
void defaultKubernetesServiceInstanceSerializationTest() throws JsonProcessingException {

DefaultKubernetesServiceInstance instance = new DefaultKubernetesServiceInstance("instanceId", "serviceId",
"host", 8080, Map.of("k8s_namespace", "spring-k8s"), true, "namespace", "cluster",
Map.of("pod_root", Map.of("pod_key", "pod_value")));

String serialized = new ObjectMapper().writeValueAsString(instance);
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.instanceId")
.isEqualTo("instanceId");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.serviceId")
.isEqualTo("serviceId");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.host").isEqualTo("host");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathNumberValue("$.port").isEqualTo(8080);
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.metadata")
.containsExactlyInAnyOrderEntriesOf(Map.of("k8s_namespace", "spring-k8s"));
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathBooleanValue("$.secure").isTrue();
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.namespace")
.isEqualTo("namespace");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.cluster").isEqualTo("cluster");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.podMetadata")
.containsExactlyInAnyOrderEntriesOf(Map.of("pod_root", Map.of("pod_key", "pod_value")));
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.scheme").isEqualTo("https");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.uri")
.isEqualTo("https://host:8080");
}

@Test
void defaultKubernetesServiceInstanceDeserializationTest() throws JsonProcessingException {

String serialized = """
{
"instanceId": "instanceId",
"serviceId": "serviceId",
"host": "host",
"port": 8080,
"metadata": {
"k8s_namespace": "spring-k8s"
},
"secure": true,
"namespace": "namespace",
"cluster": "cluster",
"podMetadata": {
"pod_root": {
"pod_key": "pod_value"
}
},
"scheme": "https",
"uri": "https://host:8080"
}
""";

ServiceInstance deserialized = new ObjectMapper().readValue(serialized, DefaultKubernetesServiceInstance.class);
assertThat(deserialized.getInstanceId()).isEqualTo("instanceId");
assertThat(deserialized.getServiceId()).isEqualTo("serviceId");
assertThat(deserialized.getScheme()).isEqualTo("https");
assertThat(deserialized.getHost()).isEqualTo("host");
assertThat(deserialized.getPort()).isEqualTo(8080);
assertThat(deserialized.getUri().toASCIIString()).isEqualTo("https://host:8080");
assertThat(deserialized.getMetadata())
.containsExactlyInAnyOrderEntriesOf(Map.of("k8s_namespace", "spring-k8s"));
}

@Test
void externalNameServiceInstanceSerializationTest() throws JsonProcessingException {
KubernetesExternalNameServiceInstance instance = new KubernetesExternalNameServiceInstance("serviceId", "host",
"instanceId", Map.of("a", "b"));
String serialized = new ObjectMapper().writeValueAsString(instance);

assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.serviceId")
.isEqualTo("serviceId");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.host").isEqualTo("host");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathStringValue("$.instanceId")
.isEqualTo("instanceId");
assertThat(BASIC_JSON_TESTER.from(serialized)).extractingJsonPathMapValue("$.metadata")
.containsExactlyInAnyOrderEntriesOf(Map.of("a", "b"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import reactor.core.publisher.Flux;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
Expand Down Expand Up @@ -47,15 +46,13 @@ public String description() {
}

@Override
@Cacheable("serviceinstances")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should start with all services being non-cacheable and I will work in reverse, to add cacheable separate clients.

Removing this option is a breaking change, but adding separate clients on top, would be not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets separate removing the cachable annotations into its own PR to keep things a bit cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done.

public Flux<ServiceInstance> getInstances(String serviceId) {
return webClient.get()
.uri("/apps/" + serviceId)
.exchangeToFlux(clientResponse -> clientResponse.bodyToFlux(DefaultKubernetesServiceInstance.class));
}

@Override
@Cacheable("services")
public Flux<String> getServices() {
return webClient.get()
.uri("/apps")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ static void assertBlockingConfiguration(CapturedOutput output, int port) {

assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue(BLOCKING_STATUS).isEqualTo("UP");

assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathStringValue(BLOCKING_STATUS).isEqualTo("UP");

assertThat(BASIC_JSON_TESTER.from(healthResult)).extractingJsonPathArrayValue(BLOCKING_SERVICES)
.containsExactlyInAnyOrder("kubernetes", "service-wiremock");

Expand Down
Loading