diff --git a/src/config.ts b/src/config.ts index 67bbca1272..cf9020b08d 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,7 @@ import execa = require('execa'); import fs = require('fs'); import https = require('https'); +import net = require('net'); import path = require('path'); import yaml = require('js-yaml'); @@ -197,11 +198,17 @@ export class KubeConfig { scheme = 'http'; } + // Wrap raw IPv6 addresses in brackets. + let serverHost = host; + if (host && net.isIPv6(host)) { + serverHost = `[${host}]`; + } + this.clusters = [ { name: clusterName, caFile: `${pathPrefix}${Config.SERVICEACCOUNT_CA_PATH}`, - server: `${scheme}://${host}:${port}`, + server: `${scheme}://${serverHost}:${port}`, skipTLSVerify: false, }, ]; diff --git a/src/config_test.ts b/src/config_test.ts index 7ec14e8651..c9e5e09117 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -1073,6 +1073,32 @@ describe('KubeConfig', () => { expect(cluster.server).to.equal('http://kubernetes:80'); }); + it('should load from cluster with ipv6', () => { + const token = 'token'; + const cert = 'cert'; + mockfs({ + '/var/run/secrets/kubernetes.io/serviceaccount': { + 'ca.crt': cert, + token, + }, + }); + + process.env.KUBERNETES_SERVICE_HOST = '::1234:5678'; + process.env.KUBERNETES_SERVICE_PORT = '80'; + const kc = new KubeConfig(); + kc.loadFromDefault(); + mockfs.restore(); + delete process.env.KUBERNETES_SERVICE_HOST; + delete process.env.KUBERNETES_SERVICE_PORT; + + const cluster = kc.getCurrentCluster(); + expect(cluster).to.not.be.null; + if (!cluster) { + return; + } + expect(cluster.server).to.equal('http://[::1234:5678]:80'); + }); + it('should default to localhost', () => { const currentHome = process.env.HOME; process.env.HOME = '/non/existent';