Skip to content

Commit

Permalink
Merge branch 'master' into k8_upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Aergonus authored Jan 3, 2018
2 parents 970ed42 + a1ec881 commit 1f7cc77
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package kubernetes

import (
"fmt"
"os"

"github.com/golang/glog"

cfg "github.com/asobti/kube-monkey/config"
Expand All @@ -9,8 +12,49 @@ import (
"k8s.io/client-go/rest"
)

// Check if running in a cluster with service accounts
// Use case: firewall based/only cluster
func RunningInCluster() (bool) {
if _, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token"); err == nil {
return true;
} else {
return false;
}
}

func GetConfig() (*rest.Config, error) {
var kubeConfig rest.Config

// Set the Kubernetes configuration based on the environment
if RunningInCluster() {
config, err := rest.InClusterConfig()

if err != nil {
return nil, fmt.Errorf("Failed to create in-cluster config: %v.", err)
}

kubeConfig = *config
} else {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
tmpKubeConfig, err := config.ClientConfig()
if err != nil {
return nil, fmt.Errorf("Failed to load local kube config: %v", err)
}
kubeConfig = *tmpKubeConfig;
}

if apiserverHost, override := cfg.ClusterAPIServerHost(); override {
glog.V(3).Infof("API server host overriden to: %s\n", apiserverHost)
kubeConfig.Host = apiserverHost
}

return &kubeConfig, nil
}

func NewInClusterClient() (*kube.Clientset, error) {
config, err := rest.InClusterConfig()
config, err := GetConfig()
if err != nil {
glog.Errorf("failed to obtain config from InClusterConfig: %v", err)
return nil, err
Expand Down

0 comments on commit 1f7cc77

Please sign in to comment.