wu-sheng commented on a change in pull request #6650:
URL: https://github.com/apache/skywalking/pull/6650#discussion_r603705114



##########
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:
       > Thanks @wankai123, this is quite clean and a good example for 
development while improving the tool. I don't know why infer complained but it 
makes a good test case and eventual regression test.
   
   ASF Infra complained? I am not sure why. Involving this process usually is 
determined by PMC.




-- 
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:
[email protected]


Reply via email to