Skip to content

Commit c2b991d

Browse files
committed
Merge branch '3.0.x'
2 parents c6d0342 + ce9db7b commit c2b991d

File tree

13 files changed

+759
-434
lines changed

13 files changed

+759
-434
lines changed

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/discovery/DiscoveryClientUtils.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@
2020
import java.util.LinkedHashMap;
2121
import java.util.Map;
2222
import java.util.Optional;
23+
import java.util.function.Function;
24+
import java.util.function.Supplier;
2325
import java.util.stream.Collectors;
2426

27+
import jakarta.annotation.Nullable;
2528
import org.apache.commons.logging.LogFactory;
2629

30+
import org.springframework.cloud.client.ServiceInstance;
2731
import org.springframework.core.log.LogAccessor;
2832
import org.springframework.util.StringUtils;
2933

3034
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix;
35+
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.EXTERNAL_NAME;
3136
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTP;
3237
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.HTTPS;
3338
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY;
@@ -130,6 +135,32 @@ public static ServicePortNameAndNumber endpointsPort(LinkedHashMap<String, Integ
130135
}
131136
}
132137

138+
public static ServiceInstance serviceInstance(@Nullable ServicePortSecureResolver servicePortSecureResolver,
139+
ServiceMetadataForServiceInstance serviceMetadataForServiceInstance,
140+
Supplier<InstanceIdHostPodName> instanceIdAndHost,
141+
Function<String, PodLabelsAndAnnotations> podLabelsAndMetadata, ServicePortNameAndNumber portData,
142+
String serviceId, Map<String, String> serviceMetadata, String namespace,
143+
KubernetesDiscoveryProperties properties) {
144+
145+
InstanceIdHostPodName data = instanceIdAndHost.get();
146+
147+
boolean secured;
148+
if (servicePortSecureResolver == null) {
149+
secured = false;
150+
}
151+
else {
152+
secured = servicePortSecureResolver.resolve(new ServicePortSecureResolver.Input(portData,
153+
serviceMetadataForServiceInstance.name(), serviceMetadataForServiceInstance.labels(),
154+
serviceMetadataForServiceInstance.annotations()));
155+
}
156+
157+
Map<String, Map<String, String>> podMetadata = podMetadata(data.podName(), serviceMetadata, properties,
158+
podLabelsAndMetadata);
159+
160+
return new DefaultKubernetesServiceInstance(data.instanceId(), serviceId, data.host(), portData.portNumber(),
161+
serviceMetadata, secured, namespace, null, podMetadata);
162+
}
163+
133164
/**
134165
* take primary-port-name from service label "PRIMARY_PORT_NAME_LABEL_KEY" if it
135166
* exists, otherwise from KubernetesDiscoveryProperties if it exists, otherwise null.
@@ -154,6 +185,32 @@ static String primaryPortName(KubernetesDiscoveryProperties properties, Map<Stri
154185
return primaryPortName;
155186
}
156187

188+
static Map<String, Map<String, String>> podMetadata(String podName, Map<String, String> serviceMetadata,
189+
KubernetesDiscoveryProperties properties, Function<String, PodLabelsAndAnnotations> podLabelsAndMetadata) {
190+
if (!EXTERNAL_NAME.equals(serviceMetadata.get(SERVICE_TYPE))) {
191+
if (properties.metadata().addPodLabels() || properties.metadata().addPodAnnotations()) {
192+
193+
if (podName != null) {
194+
PodLabelsAndAnnotations both = podLabelsAndMetadata.apply(podName);
195+
Map<String, Map<String, String>> result = new HashMap<>();
196+
if (properties.metadata().addPodLabels() && !both.labels().isEmpty()) {
197+
result.put("labels", both.labels());
198+
}
199+
200+
if (properties.metadata().addPodAnnotations() && !both.annotations().isEmpty()) {
201+
result.put("annotations", both.annotations());
202+
}
203+
204+
LOG.debug(() -> "adding podMetadata : " + result + " from pod : " + podName);
205+
return result;
206+
}
207+
208+
}
209+
}
210+
211+
return Map.of();
212+
}
213+
157214
private static Optional<ServicePortNameAndNumber> fromMap(Map<String, Integer> existingPorts, String key,
158215
String message) {
159216
Integer fromPrimaryPortName = existingPorts.get(key);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.commons.discovery;
18+
19+
/**
20+
* computes instanceId, host and podName. All needed when calculating ServiceInstance.
21+
* @author wind57
22+
*/
23+
public record InstanceIdHostPodName(String instanceId, String host, String podName) {
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.commons.discovery;
18+
19+
import java.util.Map;
20+
21+
/**
22+
* Holds pod labels and annotations.
23+
* @author wind57
24+
*/
25+
public record PodLabelsAndAnnotations(Map<String, String> labels, Map<String, String> annotations) {
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.commons.discovery;
18+
19+
import java.util.Map;
20+
21+
/**
22+
* Holds service name, labels and annotations.
23+
*
24+
* @author wind57
25+
*
26+
*/
27+
public record ServiceMetadataForServiceInstance(String name, Map<String, String> labels,
28+
Map<String, String> annotations) {
29+
}

0 commit comments

Comments
 (0)