Skip to content

Commit 61d8368

Browse files
authored
Move service data to commons (#1394)
1 parent a9c072b commit 61d8368

File tree

6 files changed

+488
-349
lines changed

6 files changed

+488
-349
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2019-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.HashMap;
20+
import java.util.Map;
21+
22+
import org.apache.commons.logging.LogFactory;
23+
24+
import org.springframework.core.log.LogAccessor;
25+
26+
import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.keysWithPrefix;
27+
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.NAMESPACE_METADATA_KEY;
28+
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryConstants.SERVICE_TYPE;
29+
30+
/**
31+
* @author wind57
32+
*/
33+
public final class DiscoveryClientUtils {
34+
35+
private static final LogAccessor LOG = new LogAccessor(LogFactory.getLog(DiscoveryClientUtils.class));
36+
37+
private DiscoveryClientUtils() {
38+
throw new AssertionError("no instance provided");
39+
}
40+
41+
/**
42+
* This adds the following metadata. <pre>
43+
* - labels (if requested)
44+
* - annotations (if requested)
45+
* - ports (if requested)
46+
* - namespace
47+
* - service type
48+
* </pre>
49+
*/
50+
public static Map<String, String> serviceMetadata(String serviceId, Map<String, String> serviceLabels,
51+
Map<String, String> serviceAnnotations, Map<String, String> portsData,
52+
KubernetesDiscoveryProperties properties, String namespace, String serviceType) {
53+
Map<String, String> serviceMetadata = new HashMap<>();
54+
KubernetesDiscoveryProperties.Metadata metadataProps = properties.metadata();
55+
if (metadataProps.addLabels()) {
56+
Map<String, String> labelMetadata = keysWithPrefix(serviceLabels, metadataProps.labelsPrefix());
57+
LOG.debug(() -> "Adding labels metadata: " + labelMetadata + " for serviceId: " + serviceId);
58+
serviceMetadata.putAll(labelMetadata);
59+
}
60+
if (metadataProps.addAnnotations()) {
61+
Map<String, String> annotationMetadata = keysWithPrefix(serviceAnnotations,
62+
metadataProps.annotationsPrefix());
63+
LOG.debug(() -> "Adding annotations metadata: " + annotationMetadata + " for serviceId: " + serviceId);
64+
serviceMetadata.putAll(annotationMetadata);
65+
}
66+
67+
if (metadataProps.addPorts()) {
68+
Map<String, String> portMetadata = keysWithPrefix(portsData, properties.metadata().portsPrefix());
69+
if (!portMetadata.isEmpty()) {
70+
LOG.debug(() -> "Adding port metadata: " + portMetadata + " for serviceId : " + serviceId);
71+
}
72+
serviceMetadata.putAll(portMetadata);
73+
}
74+
75+
serviceMetadata.put(NAMESPACE_METADATA_KEY, namespace);
76+
serviceMetadata.put(SERVICE_TYPE, serviceType);
77+
return serviceMetadata;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)