From ceda378980eecbe0c6de672a63441b9009db7488 Mon Sep 17 00:00:00 2001 From: Andres Mauricio Gomez P Date: Tue, 6 Feb 2024 08:38:55 -0500 Subject: [PATCH] On LocalKubectlProxyClusterLocator, when resolving localhost, IPv4 address is placed before IPv6 address Signed-off-by: Andres Mauricio Gomez P --- .changeset/wicked-dolphins-wash.md | 5 +++++ .../src/cluster-locator/LocalKubectlProxyLocator.ts | 13 +++++++++++-- .../src/service/KubernetesProxy.test.ts | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 .changeset/wicked-dolphins-wash.md diff --git a/.changeset/wicked-dolphins-wash.md b/.changeset/wicked-dolphins-wash.md new file mode 100644 index 0000000000..9258913c41 --- /dev/null +++ b/.changeset/wicked-dolphins-wash.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-kubernetes-backend': patch +--- + +On LocalKubectlProxyClusterLocator, when resolving localhost, IPv4 address is placed before IPv6 address, ignoring the order from the DNS resolver. This change is necessary since by default kubectl proxy listen on IPv4 diff --git a/plugins/kubernetes-backend/src/cluster-locator/LocalKubectlProxyLocator.ts b/plugins/kubernetes-backend/src/cluster-locator/LocalKubectlProxyLocator.ts index 9bbab42443..f825203505 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/LocalKubectlProxyLocator.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/LocalKubectlProxyLocator.ts @@ -15,18 +15,25 @@ */ import { ANNOTATION_KUBERNETES_AUTH_PROVIDER } from '@backstage/plugin-kubernetes-common'; -import { ClusterDetails, KubernetesClustersSupplier } from '../types/types'; +import { + ClusterDetails, + KubernetesClustersSupplier, +} from '@backstage/plugin-kubernetes-node'; +import dns from 'node:dns'; export class LocalKubectlProxyClusterLocator implements KubernetesClustersSupplier { private readonly clusterDetails: ClusterDetails[]; + // verbatim: when false, IPv4 addresses are placed before IPv6 addresses, ignoring the order from the DNS resolver + // By default kubectl proxy listens on 127.0.0.1 instead of [::1] + private lookupPromise = dns.promises.lookup('localhost', { verbatim: false }); public constructor() { this.clusterDetails = [ { name: 'local', - url: 'http:/localhost:8001', + url: 'http://localhost:8001', authMetadata: { [ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'localKubectlProxy', }, @@ -36,6 +43,8 @@ export class LocalKubectlProxyClusterLocator } async getClusters(): Promise { + const lookupResolution = await this.lookupPromise; + this.clusterDetails[0].url = `http://${lookupResolution.address}:8001`; return this.clusterDetails; } } diff --git a/plugins/kubernetes-backend/src/service/KubernetesProxy.test.ts b/plugins/kubernetes-backend/src/service/KubernetesProxy.test.ts index f2eb61e7f9..527cf94a6a 100644 --- a/plugins/kubernetes-backend/src/service/KubernetesProxy.test.ts +++ b/plugins/kubernetes-backend/src/service/KubernetesProxy.test.ts @@ -646,7 +646,7 @@ describe('KubernetesProxy', () => { }); worker.use( - rest.get('http://localhost:8001/api/v1/namespaces', (req, res, ctx) => { + rest.get('http://127.0.0.1:8001/api/v1/namespaces', (req, res, ctx) => { return req.headers.get('Authorization') ? res(ctx.status(401)) : res(