2020-10-30 16:30:03 +01:00
|
|
|
package k8s
|
|
|
|
|
|
|
|
import (
|
2020-12-25 06:07:18 +01:00
|
|
|
"context"
|
2020-10-30 16:30:03 +01:00
|
|
|
"flag"
|
2020-11-11 21:10:44 +01:00
|
|
|
"fmt"
|
2020-10-30 16:30:03 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2020-11-12 00:05:18 +01:00
|
|
|
"github.com/TwinProduction/gatus/k8stest"
|
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-10-30 16:30:03 +01:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
2021-02-20 02:34:35 +01:00
|
|
|
// KubernetesClientAPI is a minimal interface for interacting with Kubernetes
|
2020-11-12 00:05:18 +01:00
|
|
|
// Created mostly to make mocking the Kubernetes client easier
|
2021-02-20 02:34:35 +01:00
|
|
|
type KubernetesClientAPI interface {
|
2020-11-12 00:05:18 +01:00
|
|
|
GetServices(namespace string) ([]v1.Service, error)
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:34:35 +01:00
|
|
|
// KubernetesClient is a working implementation of KubernetesClientAPI
|
2020-11-12 00:05:18 +01:00
|
|
|
type KubernetesClient struct {
|
|
|
|
client *kubernetes.Clientset
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServices returns a list of services for a given namespace
|
|
|
|
func (k *KubernetesClient) GetServices(namespace string) ([]v1.Service, error) {
|
2020-12-25 06:07:18 +01:00
|
|
|
services, err := k.client.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{})
|
2020-11-12 00:05:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return services.Items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKubernetesClient creates a KubernetesClient
|
|
|
|
func NewKubernetesClient(client *kubernetes.Clientset) *KubernetesClient {
|
|
|
|
return &KubernetesClient{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:10:44 +01:00
|
|
|
// NewClient creates a Kubernetes client for the given ClusterMode
|
2021-02-20 02:34:35 +01:00
|
|
|
func NewClient(clusterMode ClusterMode) (KubernetesClientAPI, error) {
|
2020-10-30 16:30:03 +01:00
|
|
|
var kubeConfig *rest.Config
|
2020-11-11 21:10:44 +01:00
|
|
|
var err error
|
2020-10-30 16:30:03 +01:00
|
|
|
switch clusterMode {
|
2020-11-11 21:10:44 +01:00
|
|
|
case ClusterModeIn:
|
2020-11-12 00:05:18 +01:00
|
|
|
kubeConfig, err = rest.InClusterConfig()
|
2020-11-11 21:10:44 +01:00
|
|
|
case ClusterModeOut:
|
|
|
|
kubeConfig, err = getOutClusterConfig()
|
2020-11-12 00:05:18 +01:00
|
|
|
case ClusterModeMock:
|
|
|
|
return k8stest.GetMockedKubernetesClient(), nil
|
2020-10-30 16:30:03 +01:00
|
|
|
default:
|
2020-11-11 21:10:44 +01:00
|
|
|
return nil, fmt.Errorf("invalid cluster mode, try '%s' or '%s'", ClusterModeIn, ClusterModeOut)
|
2020-10-30 16:30:03 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-11-11 21:10:44 +01:00
|
|
|
return nil, fmt.Errorf("unable to get cluster config for mode '%s': %s", clusterMode, err.Error())
|
2020-10-30 16:30:03 +01:00
|
|
|
}
|
2020-11-12 00:05:18 +01:00
|
|
|
client, err := kubernetes.NewForConfig(kubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewKubernetesClient(client), nil
|
2020-10-30 16:30:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func homeDir() string {
|
|
|
|
if h := os.Getenv("HOME"); h != "" {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
return os.Getenv("USERPROFILE") // windows
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:10:44 +01:00
|
|
|
func getOutClusterConfig() (*rest.Config, error) {
|
|
|
|
var kubeConfig *string
|
2020-10-30 16:30:03 +01:00
|
|
|
if home := homeDir(); home != "" {
|
2020-11-11 21:10:44 +01:00
|
|
|
kubeConfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
|
2020-10-30 16:30:03 +01:00
|
|
|
} else {
|
2020-11-11 21:10:44 +01:00
|
|
|
kubeConfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
|
2020-10-30 16:30:03 +01:00
|
|
|
}
|
|
|
|
flag.Parse()
|
2020-11-11 21:10:44 +01:00
|
|
|
return clientcmd.BuildConfigFromFlags("", *kubeConfig)
|
2020-10-30 16:30:03 +01:00
|
|
|
}
|