Skip to content

Commit

Permalink
Merge pull request #381 from brendandburns/fix
Browse files Browse the repository at this point in the history
Add ipv6 support.
  • Loading branch information
k8s-ci-robot authored Dec 22, 2019
2 parents 2bcb2e1 + 2f9b7c7 commit da9f3d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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,
},
];
Expand Down
26 changes: 26 additions & 0 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit da9f3d8

Please sign in to comment.