Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loadFromCluster to KubeConfig object, and an example of how to use. #58

Merged
merged 2 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/in-cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const k8s = require('@kubernetes/client-node');

let kc = new k8s.KubeConfig();
kc.loadFromCluster();

let k8sApi = new k8s.Core_v1Api(kc.getCurrentCluster()['server']);
k8sApi.setDefaultAuthentication(kc);

k8sApi.listNamespacedPod('default')
.then((res) => {
console.log(res.body);
})
.catch((err) => {
console.log(err);
});

2 changes: 1 addition & 1 deletion node-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kubernetes/client-node",
"version": "0.2.1",
"version": "0.4.0",
"description": "NodeJS client for kubernetes",
"repository": {
"type": "git",
Expand Down
45 changes: 45 additions & 0 deletions node-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,51 @@ export class KubeConfig {
this.users = newUsers(obj['users']);
this.currentContext = obj['current-context'];
}

public loadFromCluster() {
const host = process.env.KUBERNETES_SERVICE_HOST;
const port = process.env.KUBERNETES_SERVICE_PORT;
const clusterName = 'inCluster';
const userName = 'inClusterUser';
const contextName = 'inClusterContext';

let scheme = 'https';
if (port === '80' || port === '8080' || port === '8001') {
scheme = 'http';
}

this.clusters = [
{
name: clusterName,
caFile: Config.SERVICEACCOUNT_CA_PATH,
caData: null,
server: `${scheme}://${host}:${port}`,
skipTLSVerify: false
}
]
this.users = [
{
name: userName,
token: fs.readFileSync(Config.SERVICEACCOUNT_TOKEN_PATH).toString(),
// empty defaults, fields are required...
certData: null,
certFile: null,
keyData: null,
keyFile: null,
authProvider: null,
username: null,
password: null
}
]
this.contexts = [
{
cluster: clusterName,
user: userName,
name: contextName
}
];
this.currentContext = contextName;
}
}

export class Config {
Expand Down