2020import java .util .LinkedHashMap ;
2121import java .util .Map ;
2222import java .util .Optional ;
23+ import java .util .function .Function ;
24+ import java .util .function .Supplier ;
2325import java .util .stream .Collectors ;
2426
27+ import jakarta .annotation .Nullable ;
2528import org .apache .commons .logging .LogFactory ;
2629
30+ import org .springframework .cloud .client .ServiceInstance ;
2731import org .springframework .core .log .LogAccessor ;
2832import org .springframework .util .StringUtils ;
2933
3034import static org .springframework .cloud .kubernetes .commons .config .ConfigUtils .keysWithPrefix ;
35+ import static org .springframework .cloud .kubernetes .commons .discovery .KubernetesDiscoveryConstants .EXTERNAL_NAME ;
3136import static org .springframework .cloud .kubernetes .commons .discovery .KubernetesDiscoveryConstants .HTTP ;
3237import static org .springframework .cloud .kubernetes .commons .discovery .KubernetesDiscoveryConstants .HTTPS ;
3338import 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 );
0 commit comments