Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Apr 4, 2024
1 parent cba3a44 commit e291a92
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.cloud.kubernetes.client.loadbalancer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -25,23 +24,30 @@
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.openapi.models.V1ServicePort;

import io.kubernetes.client.openapi.models.V1ServiceSpec;
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.DiscoveryClientUtils;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.ServiceMetadata;
import org.springframework.cloud.kubernetes.commons.discovery.ServicePortNameAndNumber;
import org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesLoadBalancerProperties;
import org.springframework.cloud.kubernetes.commons.loadbalancer.KubernetesServiceInstanceMapper;
import org.springframework.util.StringUtils;

import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix;
import static org.springframework.cloud.kubernetes.commons.discovery.ServicePortSecureResolver.Input;

/**
* @author Ryan Baxter
*/
public class KubernetesClientServiceInstanceMapper implements KubernetesServiceInstanceMapper<V1Service> {

/**
* empty on purpose, load balancer implementation does not need them.
*/
private static final Map<String, Integer> PORTS_DATA = Map.of();

private final KubernetesLoadBalancerProperties properties;

private final KubernetesDiscoveryProperties discoveryProperties;
Expand Down Expand Up @@ -80,24 +86,16 @@ else if (ports.size() > 1 && StringUtils.hasText(this.properties.getPortName()))
boolean secure = secure(port, service);

return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
getServiceMetadata(service), secure);
serviceMetadata(service), secure);
}

private Map<String, String> getServiceMetadata(V1Service service) {
final Map<String, String> serviceMetadata = new HashMap<>();
KubernetesDiscoveryProperties.Metadata metadataProps = this.discoveryProperties.metadata();
if (metadataProps.addLabels()) {
Map<String, String> labelMetadata = keysWithPrefix(service.getMetadata().getLabels(),
metadataProps.labelsPrefix());
serviceMetadata.putAll(labelMetadata);
}
if (metadataProps.addAnnotations()) {
Map<String, String> annotationMetadata = keysWithPrefix(service.getMetadata().getAnnotations(),
metadataProps.annotationsPrefix());
serviceMetadata.putAll(annotationMetadata);
}
private Map<String, String> serviceMetadata(V1Service service) {
V1ObjectMeta metadata = service.getMetadata();
V1ServiceSpec serviceSpec = service.getSpec();
ServiceMetadata serviceMetadata = new ServiceMetadata(metadata.getName(), metadata.getNamespace(),
serviceSpec.getType(), metadata.getLabels(), metadata.getAnnotations());

return serviceMetadata;
return DiscoveryClientUtils.serviceInstanceMetadata(PORTS_DATA, serviceMetadata, discoveryProperties);
}

private boolean secure(V1ServicePort port, V1Service service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ void singlePortNonSecure() {
V1Service service = createService("database", "default", annotations, labels, servicePorts);

KubernetesServiceInstance serviceInstance = mapper.map(service);
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true",
"k8s_namespace", "default", "type", "V1Service");
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
"database.default.svc.cluster.local", 80, metadata, false);
assertThat(serviceInstance).isEqualTo(result);
Expand All @@ -74,7 +75,8 @@ void singlePortSecure() {
V1Service service = createService("database", "default", annotations, labels, servicePorts);

KubernetesServiceInstance serviceInstance = mapper.map(service);
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "secured", "true");
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true", "secured", "true",
"k8s_namespace", "default", "type", "V1Service");
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
"database.default.svc.cluster.local", 80, metadata, true);
assertThat(serviceInstance).isEqualTo(result);
Expand All @@ -95,7 +97,8 @@ void multiplePortsSecure() {
);
V1Service service = createService("database", "default", annotations, labels, servicePorts);

Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true");
Map<String, String> metadata = Map.of("org.springframework.cloud", "true", "beta", "true",
"k8s_namespace", "default", "type", "V1Service");
KubernetesServiceInstance serviceInstance = mapper.map(service);
DefaultKubernetesServiceInstance result = new DefaultKubernetesServiceInstance("0", "database",
"database.default.svc.cluster.local", 443, metadata, true);
Expand All @@ -108,7 +111,7 @@ private V1Service createService(String name, String namespace, Map<String, Strin
.withMetadata(new V1ObjectMetaBuilder().withName(name).withUid("0")
.withNamespace(namespace).addToAnnotations(annotations)
.addToLabels(labels).build())
.withSpec(new V1ServiceSpecBuilder().addAllToPorts(servicePorts).build()).build();
.withSpec(new V1ServiceSpecBuilder().addAllToPorts(servicePorts).withType("V1Service").build()).build();
}

}

0 comments on commit e291a92

Please sign in to comment.