You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2021/03/30 00:31:09 UTC

[GitHub] [skywalking] wankai123 commented on a change in pull request #6650: Add arg `namespace` to func `retagByK8sMeta`, rebuild the relationship between pod and service by labels

wankai123 commented on a change in pull request #6650:
URL: https://github.com/apache/skywalking/pull/6650#discussion_r603699765



##########
File path: oap-server/analyzer/meter-analyzer/src/main/java/org/apache/skywalking/oap/meter/analyzer/k8s/K8sInfoRegistry.java
##########
@@ -154,71 +155,76 @@
         });
     }
 
-    private void addPod(final V1Pod pod) {
-        ofNullable(pod.getStatus()).ifPresent(
-            status -> ofNullable(status.getPodIP()).ifPresent(
-                ip -> ipPodMap.put(ip, pod))
+    protected void addService(final V1Service service) {
+        ofNullable(service.getMetadata()).ifPresent(
+            metadata -> nameServiceMap.put(metadata.getName() + SEPARATOR + metadata.getNamespace(), service)
         );
-
         recompose();
     }
 
-    private void removePod(final V1Pod pod) {
-        ofNullable(pod.getStatus()).ifPresent(
-            status -> ipPodMap.remove(status.getPodIP())
-        );
-        ofNullable(pod.getMetadata()).ifPresent(
-            metadata -> podServiceMap.remove(pod.getMetadata().getName())
+    protected void removeService(final V1Service service) {
+        ofNullable(service.getMetadata()).ifPresent(
+            metadata -> nameServiceMap.remove(metadata.getName() + SEPARATOR + metadata.getNamespace())
         );
+        recompose();
     }
 
-    private void addEndpoints(final V1Endpoints endpoints) {
-        V1ObjectMeta endpointsMetadata = endpoints.getMetadata();
-        if (isNull(endpointsMetadata)) {
-            log.error("Endpoints metadata is null: {}", endpoints);
-            return;
-        }
-
-        final String namespace = endpointsMetadata.getNamespace();
-        final String name = endpointsMetadata.getName();
-
-        ofNullable(endpoints.getSubsets()).ifPresent(subsets -> subsets.forEach(
-            subset -> ofNullable(subset.getAddresses()).ifPresent(addresses -> addresses.forEach(
-                address -> ipServiceMap.put(address.getIp(), name + "." + namespace)
-            ))
-        ));
+    protected void addPod(final V1Pod pod) {
+        ofNullable(pod.getMetadata()).ifPresent(
+            metadata -> namePodMap.put(metadata.getName() + SEPARATOR + metadata.getNamespace(), pod));
 
         recompose();
     }
 
-    private void removeEndpoints(final V1Endpoints endpoints) {
-        ofNullable(endpoints.getSubsets()).ifPresent(subsets -> subsets.forEach(
-            subset -> ofNullable(subset.getAddresses()).ifPresent(addresses -> addresses.forEach(
-                address -> ipServiceMap.remove(address.getIp())
-            ))
-        ));
-        recompose();
+    protected void removePod(final V1Pod pod) {
+        ofNullable(pod.getMetadata()).ifPresent(
+            metadata -> namePodMap.remove(metadata.getName() + SEPARATOR + metadata.getNamespace()));
+
+        ofNullable(pod.getMetadata()).ifPresent(
+            metadata -> podServiceMap.remove(metadata.getName() + SEPARATOR + metadata.getNamespace()));
     }
 
     private void recompose() {
-        ipPodMap.forEach((ip, pod) -> {
-            final String namespaceService = ipServiceMap.get(ip);
-            if (isNullOrEmpty(namespaceService)) {
-                podServiceMap.remove(ip);
-                return;
-            }
-
-            final V1ObjectMeta podMetadata = pod.getMetadata();
-            if (isNull(podMetadata)) {
-                log.warn("Pod metadata is null, {}", pod);
-                return;
-            }
-
-            podServiceMap.put(pod.getMetadata().getName(), namespaceService);
+        namePodMap.forEach((podName, pod) -> {
+            nameServiceMap.forEach((serviceName, service) -> {
+                if (isNull(pod.getMetadata()) || isNull(service.getMetadata()) || isNull(service.getSpec())) {
+                    return;
+                }
+
+                Map<String, String> selector = service.getSpec().getSelector();

Review comment:
       ignore,if `service.getSpec()` is null, the process would return in the above code.
   
   ![image](https://user-images.githubusercontent.com/16773043/112915980-347e0b00-9132-11eb-9623-851308ead283.png)
   
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org