Skip to content

Commit e30c212

Browse files
authored
KubernetesCoordinator: make self instance return real pod IP address instead of 127.0.0.1. (#13577)
1 parent c7e8896 commit e30c212

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

docs/en/changes/changes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#### OAP Server
66

7+
* KubernetesCoordinator: make self instance return real pod IP address instead of `127.0.0.1`.
8+
79
#### UI
810

911
#### Documentation

oap-server/server-cluster-plugin/cluster-kubernetes-plugin/src/main/java/org/apache/skywalking/oap/server/cluster/plugin/kubernetes/KubernetesCoordinator.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.apache.skywalking.oap.server.cluster.plugin.kubernetes;
2020

21+
import com.linecorp.armeria.client.Endpoint;
2122
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
2223
import lombok.extern.slf4j.Slf4j;
2324
import org.apache.skywalking.oap.server.core.CoreModule;
@@ -152,7 +153,16 @@ public void start() {
152153
.collect(Collectors.toList());
153154

154155
// The endpoint group will never include itself, add it.
155-
final var selfInstance = new RemoteInstance(new Address("127.0.0.1", port, true));
156+
Endpoint selfEndpoint = null;
157+
if (endpointGroup instanceof KubernetesLabelSelectorEndpointGroup) {
158+
selfEndpoint = ((KubernetesLabelSelectorEndpointGroup) endpointGroup).getSelfEndpoint();
159+
}
160+
final RemoteInstance selfInstance;
161+
if (selfEndpoint == null) {
162+
selfInstance = new RemoteInstance(new Address("127.0.0.1", port, true));
163+
} else {
164+
selfInstance = new RemoteInstance(new Address(selfEndpoint.host(), selfEndpoint.port(), true));
165+
}
156166
instances.add(selfInstance);
157167

158168
if (log.isDebugEnabled()) {

oap-server/server-cluster-plugin/cluster-kubernetes-plugin/src/main/java/org/apache/skywalking/oap/server/cluster/plugin/kubernetes/KubernetesLabelSelectorEndpointGroup.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@
2828
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
2929
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
3030
import io.fabric8.kubernetes.client.informers.cache.Lister;
31+
import java.util.ArrayList;
32+
import java.util.List;
3133
import lombok.Data;
34+
import lombok.Getter;
3235
import lombok.experimental.Accessors;
3336
import lombok.extern.slf4j.Slf4j;
3437
import org.apache.skywalking.oap.server.library.util.StringUtil;
3538

3639
import java.util.Map;
3740
import java.util.concurrent.CompletableFuture;
3841
import java.util.concurrent.ConcurrentHashMap;
39-
import java.util.stream.Collectors;
4042

4143
@Slf4j
4244
public class KubernetesLabelSelectorEndpointGroup extends DynamicEndpointGroup {
@@ -48,6 +50,8 @@ public class KubernetesLabelSelectorEndpointGroup extends DynamicEndpointGroup {
4850
private final String portName;
4951
private final SharedIndexInformer<Pod> podInformer;
5052
private final String selfUid;
53+
@Getter
54+
private volatile Endpoint selfEndpoint;
5155

5256
private KubernetesLabelSelectorEndpointGroup(Builder builder) {
5357
super(builder.selectionStrategy);
@@ -92,14 +96,26 @@ private void updateEndpoints() {
9296
}
9397
final var podLister = new Lister<>(podInformer.getIndexer());
9498
final var pods = podLister.namespace(namespace).list();
95-
96-
final var newEndpoints = pods.stream()
97-
.filter(this::isPodReady)
98-
.filter(pod -> StringUtil.isNotBlank(pod.getStatus().getPodIP()))
99-
.filter(pod -> !pod.getMetadata().getUid().equals(selfUid))
100-
.map(this::createEndpoint)
101-
.filter(endpoint -> endpoint != null)
102-
.collect(Collectors.toList());
99+
final List<Endpoint> newEndpoints = new ArrayList<>();
100+
for (Pod pod : pods) {
101+
if (!isPodReady(pod)) {
102+
continue;
103+
}
104+
if (StringUtil.isBlank(pod.getStatus().getPodIP())) {
105+
continue;
106+
}
107+
if (pod.getMetadata().getUid().equals(selfUid)) {
108+
Endpoint endpoint = createEndpoint(pod);
109+
if (endpoint != null) {
110+
selfEndpoint = endpoint;
111+
}
112+
continue;
113+
}
114+
Endpoint endpoint = createEndpoint(pod);
115+
if (endpoint != null) {
116+
newEndpoints.add(endpoint);
117+
}
118+
}
103119

104120
log.debug("Updating endpoints to: {}", newEndpoints);
105121
setEndpoints(newEndpoints);

0 commit comments

Comments
 (0)