feat(exporter): load kubeconfig from a new argument, or env variable

This commit is contained in:
Thibault VINCENT
2022-10-13 15:05:37 +02:00
parent 55e457a4b4
commit 895fee3bb5
2 changed files with 18 additions and 13 deletions
+16 -11
View File
@@ -40,6 +40,8 @@ func main() {
kubeEnabled := getopt.BoolLong("watch-kube-secrets", 0, "scrape kubernetes secrets and monitor them")
kubeConfig := getopt.StringLong("kubeconfig", 0, "", "Path to the kubeconfig file to use for requests. Takes precedence over the KUBECONFIG environment variable, and default path (~/.kube/config).", "path")
kubeSecretTypes := stringArrayFlag{}
getopt.FlagLong(&kubeSecretTypes, "secret-type", 's', "one or more kubernetes secret type & key to watch (e.g. \"kubernetes.io/tls:tls.crt\"")
@@ -105,18 +107,21 @@ func main() {
}
if *kubeEnabled {
err := exporter.ConnectToKubernetesCluster("")
if err != nil {
log.Warn(err)
defaultKubeConfig := path.Join(os.Getenv("HOME"), ".kube", "config")
kubeConfigEnv := os.Getenv("KUBECONFIG")
configpath := os.Getenv("KUBECONFIG")
if len(configpath) == 0 {
configpath = path.Join(os.Getenv("HOME"), ".kube/config")
}
err = exporter.ConnectToKubernetesCluster(configpath)
if err != nil {
log.Fatal(err)
}
configpath := ""
if len(*kubeConfig) > 0 {
configpath = *kubeConfig
} else if len(kubeConfigEnv) > 0 {
configpath = kubeConfigEnv
} else if _, err := os.Stat(defaultKubeConfig); err == nil {
configpath = defaultKubeConfig
}
err := exporter.ConnectToKubernetesCluster(configpath)
if err != nil {
log.Fatal(err)
}
}
+2 -2
View File
@@ -251,10 +251,10 @@ func parseKubeConfig(kubeconfigPath string) (*rest.Config, error) {
var err error
if len(kubeconfigPath) > 0 {
log.Infof("reading config from %s", kubeconfigPath)
log.Infof("using kubeconfig file: %s", kubeconfigPath)
config, err = clientcmd.BuildConfigFromFlags("", kubeconfigPath)
} else {
log.Info("fetching configuration from within the cluster")
log.Info("no kubeconfig file provided, attempting to load in-cluster configuration")
config, err = rest.InClusterConfig()
}