Skip to content

Commit

Permalink
K8s client lb cleanup 1 (#1625)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Apr 4, 2024
1 parent 8ac1241 commit bebd3fe
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 483 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@
import org.springframework.cloud.kubernetes.commons.discovery.DefaultKubernetesServiceInstance;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesServiceInstance;
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> {

private KubernetesLoadBalancerProperties properties;
private final KubernetesLoadBalancerProperties properties;

private final KubernetesDiscoveryProperties discoveryProperties;

private KubernetesDiscoveryProperties discoveryProperties;
private final ServicePortSecureResolver resolver;

public KubernetesClientServiceInstanceMapper(KubernetesLoadBalancerProperties properties,
KubernetesDiscoveryProperties discoveryProperties) {
this.properties = properties;
this.discoveryProperties = discoveryProperties;
resolver = new ServicePortSecureResolver(discoveryProperties);
}

@Override
Expand All @@ -66,10 +74,11 @@ else if (ports.size() > 1 && StringUtils.hasText(this.properties.getPortName()))
if (port == null) {
return null;
}
final String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(),
String host = KubernetesServiceInstanceMapper.createHost(service.getMetadata().getName(),
service.getMetadata().getNamespace(), properties.getClusterDomain());
final boolean secure = KubernetesServiceInstanceMapper.isSecure(service.getMetadata().getLabels(),
service.getMetadata().getAnnotations(), port.getName(), port.getPort());

boolean secure = secure(port, service);

return new DefaultKubernetesServiceInstance(meta.getUid(), meta.getName(), host, port.getPort(),
getServiceMetadata(service), secure);
}
Expand All @@ -78,17 +87,24 @@ 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 = KubernetesServiceInstanceMapper
.getMapWithPrefixedKeys(service.getMetadata().getLabels(), metadataProps.labelsPrefix());
Map<String, String> labelMetadata = keysWithPrefix(service.getMetadata().getLabels(),
metadataProps.labelsPrefix());
serviceMetadata.putAll(labelMetadata);
}
if (metadataProps.addAnnotations()) {
Map<String, String> annotationMetadata = KubernetesServiceInstanceMapper
.getMapWithPrefixedKeys(service.getMetadata().getAnnotations(), metadataProps.annotationsPrefix());
Map<String, String> annotationMetadata = keysWithPrefix(service.getMetadata().getAnnotations(),
metadataProps.annotationsPrefix());
serviceMetadata.putAll(annotationMetadata);
}

return serviceMetadata;
}

private boolean secure(V1ServicePort port, V1Service service) {
V1ObjectMeta metadata = service.getMetadata();
ServicePortNameAndNumber portNameAndNumber = new ServicePortNameAndNumber(port.getPort(), port.getName());
Input input = new Input(portNameAndNumber, metadata.getName(), metadata.getLabels(), metadata.getAnnotations());
return resolver.resolve(input);
}

}

This file was deleted.

This file was deleted.

Loading

0 comments on commit bebd3fe

Please sign in to comment.