diff --git a/config/config.go b/config/config.go index 36d46cf4..94abbc42 100644 --- a/config/config.go +++ b/config/config.go @@ -9,6 +9,7 @@ import ( "github.com/TwinProduction/gatus/alerting" "github.com/TwinProduction/gatus/alerting/provider" "github.com/TwinProduction/gatus/core" + "github.com/TwinProduction/gatus/discovery" "github.com/TwinProduction/gatus/k8s" "github.com/TwinProduction/gatus/security" "gopkg.in/yaml.v2" @@ -123,10 +124,18 @@ func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) { validateAlertingConfig(config) validateSecurityConfig(config) validateServicesConfig(config) + validateKubernetesConfig(config) } return } +func validateKubernetesConfig(config *Config) { + if config.Kubernetes != nil && config.Kubernetes.AutoDiscover { + discoveredServices := discovery.GetServices(config.Kubernetes) + config.Services = append(config.Services, discoveredServices...) + } +} + func validateServicesConfig(config *Config) { for _, service := range config.Services { if config.Debug { diff --git a/discovery/discovery.go b/discovery/discovery.go index 133486c3..03b0a2ad 100644 --- a/discovery/discovery.go +++ b/discovery/discovery.go @@ -4,27 +4,26 @@ import ( "fmt" "strings" - "github.com/TwinProduction/gatus/config" "github.com/TwinProduction/gatus/core" "github.com/TwinProduction/gatus/k8s" ) //GetServices return discovered service -func GetServices(cfg *config.Config) []*core.Service { - client := k8s.NewClient(cfg.Kubernetes.ClusterMode) +func GetServices(kubernetesConfig *k8s.Config) []*core.Service { + client := k8s.NewClient(kubernetesConfig.ClusterMode) svcs := make([]*core.Service, 0) - for _, ns := range cfg.Kubernetes.Namespaces { + for _, ns := range kubernetesConfig.Namespaces { services := k8s.GetServices(client, ns.Name) - for _, s := range services { - if exclude(cfg.Kubernetes.ExcludeSuffix, s.Name) { + if exclude(kubernetesConfig.ExcludeSuffix, s.Name) { continue } - svc := core.Service{Name: s.Name, + svc := core.Service{ + Name: s.Name, URL: fmt.Sprintf("http://%s%s/%s", s.Name, ns.ServiceSuffix, ns.HealthAPI), - Interval: cfg.Kubernetes.ServiceTemplate.Interval, - Conditions: cfg.Kubernetes.ServiceTemplate.Conditions, + Interval: kubernetesConfig.ServiceTemplate.Interval, + Conditions: kubernetesConfig.ServiceTemplate.Conditions, } svcs = append(svcs, &svc) } diff --git a/main.go b/main.go index eb55ef42..0d3ffcb9 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( "time" "github.com/TwinProduction/gatus/config" - "github.com/TwinProduction/gatus/discovery" "github.com/TwinProduction/gatus/security" "github.com/TwinProduction/gatus/watchdog" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -26,10 +25,6 @@ var ( func main() { cfg := loadConfiguration() - if cfg.Kubernetes.AutoDiscover { - discoveredServices := discovery.GetServices(cfg) - cfg.Services = append(cfg.Services, discoveredServices...) - } resultsHandler := serviceResultsHandler if cfg.Security != nil && cfg.Security.IsValid() { resultsHandler = security.Handler(serviceResultsHandler, cfg.Security)