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

In-cluster config support #44

Closed
hekike opened this issue Dec 13, 2016 · 2 comments
Closed

In-cluster config support #44

hekike opened this issue Dec 13, 2016 · 2 comments

Comments

@hekike
Copy link
Contributor

hekike commented Dec 13, 2016

Hi,

would you be open for adding a built-in in-cluster config?

Based on:
https://github.com/kubernetes/client-go/blob/124670e99da15091e13916f0ad4b2b2df2a39cd5/rest/config.go#L274
and
http://kubernetes.io/docs/user-guide/accessing-the-cluster/#accessing-the-api-from-a-pod

Something like:

const fs = require('fs')

const certPath = '/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 getInClusterConfig () {
  const host = process.env.KUBERNETES_SERVICE_HOST
  const port = process.env.KUBERNETES_SERVICE_PORT
  let cert
  let namespace
  let bearer

  if (!host || !port) {
    throw TypeError('Unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined')
  }

  try {
    cert = fs.readFileSync(certPath, 'utf8')
  } catch (err) {
    throw TypeError('Expected to load root CA config from ${certPath}, but got err', err)
  }

  try {
    bearer = fs.readFileSync(tokenPath, 'utf8')
  } catch (err) {
    throw TypeError('Expected to load token config from ${tokenPath}, but got err', err)
  }

  try {
    namespace = fs.readFileSync(namespacePath, 'utf8')
  } catch (err) {
    throw TypeError('Expected to load namespace config from ${namespacePath}, but got err', err)
  }

  return {
    url: `https://${host}:${port}`,
    cert,
    auth: { bearer },
    namespace
  }
}

The interface could be:

const K8Api = require('kubernetes-client');
const k8 = new K8Api.Core(K8Api.getInClusterConfig())

I'm happy to send a PR if you like the idea.
What do you think?

@hekike hekike changed the title Buil-in in-cluster config support In-cluster config support Dec 13, 2016
@silasbw
Copy link
Contributor

silasbw commented Dec 13, 2016

Yeah, I like this idea. I PR would be much appreciated.

@jcrugzz any thoughts?

@jcrugzz
Copy link
Collaborator

jcrugzz commented Dec 14, 2016

Yea this seems super convenient 👍. Would definitely take a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants