Merge discovery package into k8s package

This commit is contained in:
TwinProduction 2020-11-11 14:46:19 -05:00
parent 799c3e9187
commit fff34fff58
6 changed files with 70 additions and 74 deletions

View File

@ -9,7 +9,6 @@ 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"
@ -121,6 +120,8 @@ func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) {
if config == nil || config.Services == nil || len(config.Services) == 0 {
err = ErrNoServiceInConfig
} else {
// Note that the functions below may panic, and this is on purpose to prevent Gatus from starting with
// invalid configurations
validateAlertingConfig(config)
validateSecurityConfig(config)
validateServicesConfig(config)
@ -131,7 +132,10 @@ func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) {
func validateKubernetesConfig(config *Config) {
if config.Kubernetes != nil && config.Kubernetes.AutoDiscover {
discoveredServices := discovery.GetServices(config.Kubernetes)
discoveredServices, err := k8s.DiscoverServices(config.Kubernetes)
if err != nil {
panic(err)
}
config.Services = append(config.Services, discoveredServices...)
}
}

View File

@ -1,41 +0,0 @@
package discovery
import (
"fmt"
"strings"
"github.com/TwinProduction/gatus/core"
"github.com/TwinProduction/gatus/k8s"
)
//GetServices return discovered service
func GetServices(kubernetesConfig *k8s.Config) []*core.Service {
client := k8s.NewClient(kubernetesConfig.ClusterMode)
svcs := make([]*core.Service, 0)
for _, ns := range kubernetesConfig.Namespaces {
services := k8s.GetServices(client, ns.Name)
for _, s := range services {
if exclude(kubernetesConfig.ExcludeSuffix, s.Name) {
continue
}
svc := core.Service{
Name: s.Name,
URL: fmt.Sprintf("http://%s%s/%s", s.Name, ns.ServiceSuffix, ns.HealthAPI),
Interval: kubernetesConfig.ServiceTemplate.Interval,
Conditions: kubernetesConfig.ServiceTemplate.Conditions,
}
svcs = append(svcs, &svc)
}
}
return svcs
}
func exclude(excludeList []string, name string) bool {
for _, x := range excludeList {
if strings.HasSuffix(name, x) {
return true
}
}
return false
}

View File

@ -12,23 +12,19 @@ import (
)
func NewClient(clusterMode string) *kubernetes.Clientset {
var kubeConfig *rest.Config
switch clusterMode {
case "in":
kubeConfig = getInclusterConfig()
kubeConfig = getInClusterConfig()
case "out":
kubeConfig = getOutClusterConfig()
default:
panic("invalid cluster mode")
}
clientset, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
panic(err.Error())
}
return clientset
}
@ -47,20 +43,17 @@ func getOutClusterConfig() *rest.Config {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
return config
}
func getInclusterConfig() *rest.Config {
func getInClusterConfig() *rest.Config {
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
return config
}

View File

@ -2,30 +2,32 @@ package k8s
import "github.com/TwinProduction/gatus/core"
//Config for Kubernetes auto-discovery
// Config for Kubernetes auto-discovery
type Config struct {
//AutoDiscover to discover services to monitor
// AutoDiscover to discover services to monitor
AutoDiscover bool `yaml:"auto-discover"`
//ServiceTemplate Template for auto disocovered services
// ServiceTemplate Template for auto disocovered services
ServiceTemplate core.Service `yaml:"service-template"`
//ExcludeSuffix Ignore services with this suffix
ExcludeSuffix []string `yaml:"exclude-suffix"`
// ExcludeSuffix Ignore services with this suffix
ExcludeSuffix []string `yaml:"isExcluded-suffix"`
//ClusterMode to authenticate with kubernetes
// ClusterMode to authenticate with kubernetes
ClusterMode string `yaml:"cluster-mode"`
//Namespaces from which to discover services
Namespaces []Namespace `yaml:"namespaces"`
// Namespaces from which to discover services
Namespaces []NamespaceConfig `yaml:"namespaces"`
}
//Namespace level config
type Namespace struct {
//Name of namespace
// NamespaceConfig level config
type NamespaceConfig struct {
// Name of namespace
Name string `yaml:"name"`
//ServiceSuffix to append to service name
// ServiceSuffix to append to service name
ServiceSuffix string `yaml:"service-suffix"`
//HealthAPI URI to append to service to reach health check API
// HealthAPI URI to append to service to reach health check API
HealthAPI string `yaml:"health-api"`
}

42
k8s/discovery.go Normal file
View File

@ -0,0 +1,42 @@
package k8s
import (
"fmt"
"strings"
"github.com/TwinProduction/gatus/core"
)
// DiscoverServices return discovered services
func DiscoverServices(kubernetesConfig *Config) ([]*core.Service, error) {
client := NewClient(kubernetesConfig.ClusterMode)
services := make([]*core.Service, 0)
for _, ns := range kubernetesConfig.Namespaces {
kubernetesServices, err := GetKubernetesServices(client, ns.Name)
if err != nil {
return nil, err
}
for _, s := range kubernetesServices {
if isExcluded(kubernetesConfig.ExcludeSuffix, s.Name) {
continue
}
services = append(services, &core.Service{
Name: s.Name,
URL: fmt.Sprintf("http://%s%s/%s", s.Name, ns.ServiceSuffix, ns.HealthAPI),
Interval: kubernetesConfig.ServiceTemplate.Interval,
Conditions: kubernetesConfig.ServiceTemplate.Conditions,
})
}
}
return services, nil
}
// TODO: don't uselessly allocate new things here, just move this inside the DiscoverServices function
func isExcluded(excludeList []string, name string) bool {
for _, x := range excludeList {
if strings.HasSuffix(name, x) {
return true
}
}
return false
}

View File

@ -1,20 +1,16 @@
package k8s
import (
"log"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
//GetServices return List of Services from given namespace
func GetServices(client *kubernetes.Clientset, ns string) []corev1.Service {
options := metav1.ListOptions{}
svcs, err := client.CoreV1().Services(ns).List(options)
// GetKubernetesServices return List of Services from given namespace
func GetKubernetesServices(client *kubernetes.Clientset, ns string) ([]corev1.Service, error) {
services, err := client.CoreV1().Services(ns).List(metav1.ListOptions{})
if err != nil {
log.Printf("[Discovery] : Error getting Services Err: %v", err)
return []corev1.Service{}
return nil, err
}
return svcs.Items
return services.Items, nil
}