On LocalKubectlProxyClusterLocator, when resolving localhost, IPv4 address is placed before IPv6 address

Signed-off-by: Andres Mauricio Gomez P <andmagom@outlook.com>
This commit is contained in:
Andres Mauricio Gomez P
2024-02-06 08:38:55 -05:00
parent 078f7c427a
commit ceda378980
3 changed files with 17 additions and 3 deletions
+5
View File
@@ -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
@@ -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<ClusterDetails[]> {
const lookupResolution = await this.lookupPromise;
this.clusterDetails[0].url = `http://${lookupResolution.address}:8001`;
return this.clusterDetails;
}
}
@@ -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(