What is the best way to detect whether a golang app is running inside a cluster or outside of one?
if _, inCluster := os.LookupEnv("KUBERNETES_SERVICE_HOST"); inCluster == true {
Moreover, I need a clientset, which in the cluster I think I can get like so:
config, err := restClient.InClusterConfig()
client.RestConfig = config
if err != nil {
return nil, err
}
client.Clientset, err = coreClient.NewForConfig(config)
if err != nil {
return nil, err
}
But outside of the cluster, I think I can get it like this:
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := cmdClient.BuildConfigFromFlags("", *kubeconfig)
client.RestConfig = config
if err != nil {
return nil, err
}
client.Clientset, err = coreClient.NewForConfig(config)
if err != nil {
return nil, err
}
But maybe there is some better way, or a universal config/discovery thing that would be better. Looking for any guidance, criticism or heckling you may provide.
Thanks!
Best,
Steve