Skip to content

Commit

Permalink
validate k8s response status before parsing config, fixes #113
Browse files Browse the repository at this point in the history
  • Loading branch information
mazay committed Mar 5, 2022
1 parent 8d33a0b commit 0404ef1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 4 additions & 2 deletions service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ func getConfig() *Config {
var config *Config

if inK8s && configmap != "" {
cfg := k8sGetCm(clientset, configmap)
config = readConfigString(cfg)
result, cfg := k8sGetCm(clientset, configmap)
if result {
config = readConfigString(cfg)
}
} else {
config = readConfigFile(configpath)
}
Expand Down
7 changes: 5 additions & 2 deletions service/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ func k8sWatchCm(clientset kubernetes.Interface, configmap string, reloaderChan c
}
}

func k8sGetCm(clientset kubernetes.Interface, configmap string) string {
func k8sGetCm(clientset kubernetes.Interface, configmap string) (bool, string) {
var configMap map[string]string
var result bool

ctx := context.Background()
cm := strings.Split(configmap, "/")
Expand All @@ -98,9 +99,11 @@ func k8sGetCm(clientset kubernetes.Interface, configmap string) string {

if err != nil {
logger.Errorln(err.Error())
result = false
} else {
configMap = cmObj.Data
result = true
}

return configMap["config.yml"]
return result, configMap["config.yml"]
}
2 changes: 1 addition & 1 deletion service/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sites:
{"test/mock-configmap-does-not-exist", "fail"},
}
for _, tt := range tests {
data := k8sGetCm(clientset, tt.cm)
_, data := k8sGetCm(clientset, tt.cm)
if !reflect.DeepEqual(config, data) && tt.want != "fail" {
t.Error(
"Expected:", config,
Expand Down

0 comments on commit 0404ef1

Please sign in to comment.