Skip to content

Commit

Permalink
feat(config): support multiple config files in KUBECONFIG (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacopoDaeli authored and silasbw committed Sep 10, 2018
1 parent aa1b8ff commit d76a84f
Show file tree
Hide file tree
Showing 8 changed files with 837 additions and 770 deletions.
53 changes: 35 additions & 18 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const merge = require('deepmerge');

const caPath = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt';
const tokenPath = '/var/run/secrets/kubernetes.io/serviceaccount/token';
const namespacePath = '/var/run/secrets/kubernetes.io/serviceaccount/namespace';

function defaultConfigPath() {
function defaultConfigPaths() {
if (process.env.KUBECONFIG) {
// From https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable
// KUBECONFIG can support multiple config files.
// If multiple config files in KUBECONFIG, then return the first one.
const delimiter = process.platform === 'win32' ? ';' : ':';
const configPaths = process.env.KUBECONFIG.split(delimiter);
return configPaths[0];
return process.env.KUBECONFIG.split(delimiter);
}
const homeDir = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
return path.join(homeDir, '.kube', 'config');
return [path.join(homeDir, '.kube', 'config')];
}

/**
Expand Down Expand Up @@ -62,8 +61,11 @@ module.exports.getInCluster = getInCluster;
function fromKubeconfig(kubeconfig, current) {
if (!kubeconfig) kubeconfig = loadKubeconfig();
// if kubeconfig is provided as a path to the yaml file,
// or array of paths to the yaml files,
// automatically load it.
if (typeof kubeconfig === 'string') kubeconfig = loadKubeconfig(kubeconfig);
if (typeof kubeconfig === 'string' || Array.isArray(kubeconfig)) {
kubeconfig = loadKubeconfig(kubeconfig);
}

current = current || kubeconfig['current-context'];
const context = kubeconfig.contexts
Expand Down Expand Up @@ -159,27 +161,42 @@ module.exports.fromKubeconfig = fromKubeconfig;
function mapCertificates(cfgPath, config) {
const configDir = path.dirname(cfgPath);

config.clusters.filter(cluster => cluster.cluster['certificate-authority']).forEach(cluster => {
cluster.cluster['certificate-authority'] = path.resolve(configDir, cluster.cluster['certificate-authority']);
});
if (config.clusters) {
config.clusters.filter(cluster => cluster.cluster['certificate-authority']).forEach(cluster => {
cluster.cluster['certificate-authority'] = path.resolve(configDir, cluster.cluster['certificate-authority']);
});
}

config.users.filter(user => user.user['client-certificate']).forEach(user => {
user.user['client-certificate'] = path.resolve(configDir, user.user['client-certificate']);
});
if (config.users) {
config.users.filter(user => user.user['client-certificate']).forEach(user => {
user.user['client-certificate'] = path.resolve(configDir, user.user['client-certificate']);
});

config.users.filter(user => user.user['client-key']).forEach(user => {
user.user['client-key'] = path.resolve(configDir, user.user['client-key']);
});
config.users.filter(user => user.user['client-key']).forEach(user => {
user.user['client-key'] = path.resolve(configDir, user.user['client-key']);
});
}

return config;
}

function loadKubeconfig(cfgPath) {
cfgPath = cfgPath || defaultConfigPath();
let cfgPaths;

if (!cfgPath) {
cfgPaths = defaultConfigPaths();
} else if (Array.isArray(cfgPath)) {
cfgPaths = cfgPath;
} else {
cfgPaths = [cfgPath];
}

const config = yaml.safeLoad(fs.readFileSync(cfgPath));
const configs = cfgPaths.map(cfgPath => {
const config = yaml.safeLoad(fs.readFileSync(cfgPath));
return mapCertificates(cfgPath, config);
});

return mapCertificates(cfgPath, config);
return merge.all(configs);
}

module.exports.loadKubeconfig = loadKubeconfig;
Loading

0 comments on commit d76a84f

Please sign in to comment.