This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
client.go
84 lines (73 loc) · 3.13 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Collection of shared utils for initializing kubernetes clients within flyteadmin.
package flytek8s
import (
"context"
"os"
"github.com/flyteorg/flyteadmin/pkg/errors"
"google.golang.org/grpc/codes"
runtimeInterfaces "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flytestdlib/logger"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // to overcome gke auth provider issue
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Reads secret values from paths specified in the config to initialize a Kubernetes rest client Config.
func RemoteClusterConfig(host string, auth runtimeInterfaces.Auth) (*restclient.Config, error) {
tokenString, err := auth.GetToken()
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "Failed to get auth token: %+v", err)
}
caCert, err := auth.GetCA()
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "Failed to get auth CA: %+v", err)
}
tlsClientConfig := restclient.TLSClientConfig{}
tlsClientConfig.CAData = caCert
return &restclient.Config{
Host: host,
TLSClientConfig: tlsClientConfig,
BearerToken: tokenString,
}, nil
}
func GetRestClientConfigForCluster(cluster runtimeInterfaces.ClusterConfig) (*restclient.Config, error) {
kubeConfiguration, err := RemoteClusterConfig(cluster.Endpoint, cluster.Auth)
if err != nil {
return nil, err
}
logger.Debugf(context.Background(), "successfully loaded kube configuration from %v", cluster)
return kubeConfiguration, nil
}
// Initializes a config using a variety of configurable or default fallback options that can be passed to a Kubernetes client on
// initialization.
func GetRestClientConfig(kubeConfig, master string,
k8sCluster *runtimeInterfaces.ClusterConfig) (*restclient.Config, error) {
var kubeConfiguration *restclient.Config
var err error
if kubeConfig != "" {
// ExpandEnv allows using $HOME in the path and it will automatically map to the right OS's user home
kubeConfigPath := os.ExpandEnv(kubeConfig)
kubeConfiguration, err = clientcmd.BuildConfigFromFlags(master, kubeConfigPath)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "Error building kubeconfig: %v", err)
}
logger.Debugf(context.Background(), "successfully loaded kube config from %s", kubeConfig)
} else if k8sCluster != nil {
return GetRestClientConfigForCluster(*k8sCluster)
} else {
kubeConfiguration, err = restclient.InClusterConfig()
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "Cannot get incluster kubeconfig : %v", err.Error())
}
logger.Debug(context.Background(), "successfully loaded kube configuration from in cluster config")
}
return kubeConfiguration, nil
}
// Initializes a kubernetes Client which performs CRD operations on Kubernetes objects
func NewKubeClient(kubeConfig, master string, k8sCluster *runtimeInterfaces.ClusterConfig) (client.Client, error) {
kubeConfiguration, err := GetRestClientConfig(kubeConfig, master, k8sCluster)
if err != nil {
return nil, err
}
return client.New(kubeConfiguration, client.Options{})
}