From 49e0c3587bd52a30f134a2fa2a344cec1341defd Mon Sep 17 00:00:00 2001 From: Ta-Ching Chen Date: Wed, 6 Mar 2019 18:20:09 +0800 Subject: [PATCH] Fix canary config manager creation error in controller (#1105) Fixing failure of the controller startup when Prometheus is provided as env variable and it fails to parse correctly --- canaryconfigmgr/canaryConfigMgr.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/canaryconfigmgr/canaryConfigMgr.go b/canaryconfigmgr/canaryConfigMgr.go index 212a2527..73e9fd97 100644 --- a/canaryconfigmgr/canaryConfigMgr.go +++ b/canaryconfigmgr/canaryConfigMgr.go @@ -19,6 +19,7 @@ package canaryconfigmgr import ( "context" "fmt" + "net/url" "os" "strings" "time" @@ -47,19 +48,29 @@ type canaryConfigMgr struct { func MakeCanaryConfigMgr(fissionClient *crd.FissionClient, kubeClient *kubernetes.Clientset, crdClient *rest.RESTClient, prometheusSvc string) (*canaryConfigMgr, error) { if prometheusSvc == "" { + log.Info("Try to retrieve prometheus server information from environment variables") + + var prometheusSvcHost, prometheusSvcPort string // handle a case where there is a prometheus server is already installed, try to find the service from env variable envVars := os.Environ() + for _, envVar := range envVars { if strings.Contains(envVar, "PROMETHEUS_SERVER_SERVICE_HOST") { - envVarSplit := strings.Split(envVar, "=") - prometheusSvc = envVarSplit[1] + prometheusSvcHost = getEnvValue(envVar) + } else if strings.Contains(envVar, "PROMETHEUS_SERVER_SERVICE_PORT") { + prometheusSvcPort = getEnvValue(envVar) + } + + if len(prometheusSvcHost) > 0 && len(prometheusSvcPort) > 0 { break } } + prometheusSvc = fmt.Sprintf("http://%v:%v", prometheusSvcHost, prometheusSvcPort) + } - if prometheusSvc == "" { - return nil, fmt.Errorf("prometheus service not found, cant create canary config manager") - } + _, err := url.Parse(prometheusSvc) + if err != nil { + return nil, fmt.Errorf("prometheus service url not found/invalid, cant create canary config manager: %v", prometheusSvc) } promClient, err := MakePrometheusClient(prometheusSvc) @@ -390,3 +401,8 @@ func (canaryCfgMgr *canaryConfigMgr) updateCanaryConfig(oldCanaryConfig *crd.Can } canaryCfgMgr.addCanaryConfig(newCanaryConfig) } + +func getEnvValue(envVar string) string { + envVarSplit := strings.Split(envVar, "=") + return envVarSplit[1] +}