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
This commit is contained in:
Ta-Ching Chen
2019-03-06 15:50:09 +05:30
committed by Vishal
parent f8ad9c421a
commit 49e0c3587b
+21 -5
View File
@@ -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]
}