Skip to content

Commit

Permalink
Add IN_CLUSTER setting to Kubernetes driver
Browse files Browse the repository at this point in the history
The kubernetes driver defaults KUBECONFIG to ~/.kube/config when not
set. This prevents the incluster configuration from being used.

To preserve the existing behavior, I have added IN_CLUSTER which
indicates that ambient kubernetes configuration should be used. This is
intended for when the cnab process is running inside a pod.

Signed-off-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
carolynvs committed Jan 27, 2021
1 parent 4f95726 commit 2f24e74
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
30 changes: 21 additions & 9 deletions driver/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -79,6 +80,7 @@ func (k *Driver) Handles(imagetype string) bool {
// Config returns the Kubernetes driver configuration options.
func (k *Driver) Config() map[string]string {
return map[string]string{
"IN_CLUSTER": "Connect to the ambient cluster",
"KUBE_NAMESPACE": "Kubernetes namespace in which to run the invocation image",
"SERVICE_ACCOUNT": "Kubernetes service account to be mounted by the invocation image (if empty, no service account token will be mounted)",
"KUBECONFIG": "Absolute path to the kubeconfig file",
Expand All @@ -92,17 +94,27 @@ func (k *Driver) SetConfig(settings map[string]string) error {
k.Namespace = settings["KUBE_NAMESPACE"]
k.ServiceAccountName = settings["SERVICE_ACCOUNT"]

var kubeconfig string
if kpath := settings["KUBECONFIG"]; kpath != "" {
kubeconfig = kpath
} else if home := homeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
}
var conf *rest.Config
var err error
if incluster, _ := strconv.ParseBool(settings["IN_CLUSTER"]); incluster {
conf, err = rest.InClusterConfig()
if err != nil {
return errors.Wrap(err, "error retrieving in-cluster kubernetes configuration")
}
} else {
var kubeconfig string
if kpath := settings["KUBECONFIG"]; kpath != "" {
kubeconfig = kpath
} else if home := homeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
}

conf, err := clientcmd.BuildConfigFromFlags(settings["MASTER_URL"], kubeconfig)
if err != nil {
return errors.Wrapf(err, "error retrieving external kubernetes configuration using configuration:\n%v", settings)
conf, err = clientcmd.BuildConfigFromFlags(settings["MASTER_URL"], kubeconfig)
if err != nil {
return errors.Wrapf(err, "error retrieving external kubernetes configuration using configuration:\n%v", settings)
}
}

return k.setClient(conf)
}

Expand Down
13 changes: 13 additions & 0 deletions driver/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,17 @@ func TestDriver_SetConfig_Fails(t *testing.T) {
assert.Contains(t, err.Error(), "error retrieving external kubernetes configuration using configuration")
})

t.Run("use in-cluster outside cluster", func(t *testing.T) {
// Force this to fail even when the tests are run inside brigade
orig := os.Getenv("KUBERNETES_SERVICE_HOST")
os.Unsetenv("KUBERNETES_SERVICE_HOST")
defer os.Setenv("KUBERNETES_SERVICE_HOST", orig)

d := Driver{}
err := d.SetConfig(map[string]string{
"IN_CLUSTER": "true",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "error retrieving in-cluster kubernetes configuration")
})
}

0 comments on commit 2f24e74

Please sign in to comment.