mirror of
https://github.com/TwiN/gatus.git
synced 2025-02-16 18:21:07 +01:00
Merge discovery package into k8s package
This commit is contained in:
parent
799c3e9187
commit
fff34fff58
@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/TwinProduction/gatus/alerting"
|
"github.com/TwinProduction/gatus/alerting"
|
||||||
"github.com/TwinProduction/gatus/alerting/provider"
|
"github.com/TwinProduction/gatus/alerting/provider"
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
"github.com/TwinProduction/gatus/discovery"
|
|
||||||
"github.com/TwinProduction/gatus/k8s"
|
"github.com/TwinProduction/gatus/k8s"
|
||||||
"github.com/TwinProduction/gatus/security"
|
"github.com/TwinProduction/gatus/security"
|
||||||
"gopkg.in/yaml.v2"
|
"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 {
|
if config == nil || config.Services == nil || len(config.Services) == 0 {
|
||||||
err = ErrNoServiceInConfig
|
err = ErrNoServiceInConfig
|
||||||
} else {
|
} else {
|
||||||
|
// Note that the functions below may panic, and this is on purpose to prevent Gatus from starting with
|
||||||
|
// invalid configurations
|
||||||
validateAlertingConfig(config)
|
validateAlertingConfig(config)
|
||||||
validateSecurityConfig(config)
|
validateSecurityConfig(config)
|
||||||
validateServicesConfig(config)
|
validateServicesConfig(config)
|
||||||
@ -131,7 +132,10 @@ func parseAndValidateConfigBytes(yamlBytes []byte) (config *Config, err error) {
|
|||||||
|
|
||||||
func validateKubernetesConfig(config *Config) {
|
func validateKubernetesConfig(config *Config) {
|
||||||
if config.Kubernetes != nil && config.Kubernetes.AutoDiscover {
|
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...)
|
config.Services = append(config.Services, discoveredServices...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
|
||||||
}
|
|
@ -12,23 +12,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewClient(clusterMode string) *kubernetes.Clientset {
|
func NewClient(clusterMode string) *kubernetes.Clientset {
|
||||||
|
|
||||||
var kubeConfig *rest.Config
|
var kubeConfig *rest.Config
|
||||||
|
|
||||||
switch clusterMode {
|
switch clusterMode {
|
||||||
case "in":
|
case "in":
|
||||||
kubeConfig = getInclusterConfig()
|
kubeConfig = getInClusterConfig()
|
||||||
case "out":
|
case "out":
|
||||||
kubeConfig = getOutClusterConfig()
|
kubeConfig = getOutClusterConfig()
|
||||||
default:
|
default:
|
||||||
panic("invalid cluster mode")
|
panic("invalid cluster mode")
|
||||||
}
|
}
|
||||||
|
|
||||||
clientset, err := kubernetes.NewForConfig(kubeConfig)
|
clientset, err := kubernetes.NewForConfig(kubeConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientset
|
return clientset
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,20 +43,17 @@ func getOutClusterConfig() *rest.Config {
|
|||||||
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
|
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
|
||||||
}
|
}
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
|
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
func getInclusterConfig() *rest.Config {
|
func getInClusterConfig() *rest.Config {
|
||||||
config, err := rest.InClusterConfig()
|
config, err := rest.InClusterConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
@ -2,30 +2,32 @@ package k8s
|
|||||||
|
|
||||||
import "github.com/TwinProduction/gatus/core"
|
import "github.com/TwinProduction/gatus/core"
|
||||||
|
|
||||||
//Config for Kubernetes auto-discovery
|
// Config for Kubernetes auto-discovery
|
||||||
type Config struct {
|
type Config struct {
|
||||||
//AutoDiscover to discover services to monitor
|
// AutoDiscover to discover services to monitor
|
||||||
AutoDiscover bool `yaml:"auto-discover"`
|
AutoDiscover bool `yaml:"auto-discover"`
|
||||||
|
|
||||||
//ServiceTemplate Template for auto disocovered services
|
// ServiceTemplate Template for auto disocovered services
|
||||||
ServiceTemplate core.Service `yaml:"service-template"`
|
ServiceTemplate core.Service `yaml:"service-template"`
|
||||||
|
|
||||||
//ExcludeSuffix Ignore services with this suffix
|
// ExcludeSuffix Ignore services with this suffix
|
||||||
ExcludeSuffix []string `yaml:"exclude-suffix"`
|
ExcludeSuffix []string `yaml:"isExcluded-suffix"`
|
||||||
|
|
||||||
//ClusterMode to authenticate with kubernetes
|
// ClusterMode to authenticate with kubernetes
|
||||||
ClusterMode string `yaml:"cluster-mode"`
|
ClusterMode string `yaml:"cluster-mode"`
|
||||||
|
|
||||||
//Namespaces from which to discover services
|
// Namespaces from which to discover services
|
||||||
Namespaces []Namespace `yaml:"namespaces"`
|
Namespaces []NamespaceConfig `yaml:"namespaces"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//Namespace level config
|
// NamespaceConfig level config
|
||||||
type Namespace struct {
|
type NamespaceConfig struct {
|
||||||
//Name of namespace
|
// Name of namespace
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
//ServiceSuffix to append to service name
|
|
||||||
|
// ServiceSuffix to append to service name
|
||||||
ServiceSuffix string `yaml:"service-suffix"`
|
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"`
|
HealthAPI string `yaml:"health-api"`
|
||||||
}
|
}
|
||||||
|
42
k8s/discovery.go
Normal file
42
k8s/discovery.go
Normal 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
|
||||||
|
}
|
14
k8s/k8s.go
14
k8s/k8s.go
@ -1,20 +1,16 @@
|
|||||||
package k8s
|
package k8s
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
)
|
)
|
||||||
|
|
||||||
//GetServices return List of Services from given namespace
|
// GetKubernetesServices return List of Services from given namespace
|
||||||
func GetServices(client *kubernetes.Clientset, ns string) []corev1.Service {
|
func GetKubernetesServices(client *kubernetes.Clientset, ns string) ([]corev1.Service, error) {
|
||||||
options := metav1.ListOptions{}
|
services, err := client.CoreV1().Services(ns).List(metav1.ListOptions{})
|
||||||
svcs, err := client.CoreV1().Services(ns).List(options)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Discovery] : Error getting Services Err: %v", err)
|
return nil, err
|
||||||
return []corev1.Service{}
|
|
||||||
}
|
}
|
||||||
return svcs.Items
|
return services.Items, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user