-
Notifications
You must be signed in to change notification settings - Fork 9
/
kubeclient.go
236 lines (189 loc) · 6.3 KB
/
kubeclient.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package client
import (
"context"
"net/url"
"k8s.io/apimachinery/pkg/runtime/schema"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"namespacelabs.dev/foundation/framework/kubernetes/kubeclient"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/workspace/dirs"
fnschema "namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
var hostEnvConfigType = cfg.DefineConfigType[*HostEnv]()
type ClusterConfiguration struct {
Config clientcmdapi.Config
TokenProvider TokenProviderFunc
Ephemeral bool // Set to true if thie target cluster is ephemeral.
ProviderSpecific any // Up to an implementation to attach state if needed.
Labels []*fnschema.Label
SupportedIngressClasses []string
}
type DeferredProvider struct{}
type TokenProviderFunc func(context.Context) (string, error)
type ProviderFunc func(context.Context, cfg.Configuration) (ClusterConfiguration, error)
var (
providers = map[string]ProviderFunc{}
)
type Prepared struct {
Clientset *k8s.Clientset
RESTConfig *rest.Config
ClientConfig clientcmd.ClientConfig
HostEnv *HostEnv
Configuration ClusterConfiguration
}
func RegisterConfigurationProvider(name string, p ProviderFunc) {
providers[name] = p
}
type configResult struct {
ClientConfig clientcmd.ClientConfig
ConfigurationSource string // If set, kubeconfig was loaded from this file.
ClusterConfiguration
}
func (conf ClusterConfiguration) AsResult() *configResult {
return &configResult{
ClientConfig: clientcmd.NewDefaultClientConfig(conf.Config, nil),
ClusterConfiguration: conf,
}
}
func computeConfig(ctx context.Context, c *HostEnv, config cfg.Configuration) (*configResult, error) {
if c.Incluster {
return nil, nil
}
if c.Provider != "" {
provider := providers[c.Provider]
if provider == nil {
return nil, fnerrors.BadInputError("%s: no such kubernetes configuration provider", c.Provider)
}
result, err := provider(ctx, config)
if err != nil {
return nil, err
}
return result.AsResult(), nil
}
if c.StaticConfig != nil {
return &configResult{
ClientConfig: clientcmd.NewDefaultClientConfig(*kubeclient.MakeApiConfig(c.StaticConfig), nil),
}, nil
}
if c.GetKubeconfig() == "" {
return nil, fnerrors.New("hostEnv.Kubeconfig is required")
}
existing, err := LoadExistingConfiguration(c.GetKubeconfig(), c.GetContext())
if err != nil {
return nil, err
}
return &configResult{ClientConfig: existing, ConfigurationSource: c.GetKubeconfig()}, nil
}
func LoadExistingConfiguration(kubeConfig, contextName string) (clientcmd.ClientConfig, error) {
kubeconfig, err := dirs.ExpandHome(kubeConfig)
if err != nil {
return nil, err
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{CurrentContext: contextName}), nil
}
func obtainRESTConfig(ctx context.Context, hostEnv *HostEnv, computed *configResult) (*rest.Config, error) {
if hostEnv != nil && hostEnv.Incluster {
config, err := rest.InClusterConfig()
return config, err
}
restcfg, err := computed.ClientConfig.ClientConfig()
if err != nil {
if computed.ConfigurationSource != "" {
return nil, fnerrors.New("failed to load existing configuration from %q: %w", computed.ConfigurationSource, err)
}
return nil, fnerrors.New("failed to load kubernetes configuration: %w", err)
}
if computed.TokenProvider != nil {
token, err := computed.TokenProvider(ctx)
if err != nil {
return nil, err
}
restcfg.BearerToken = token
}
return restcfg, nil
}
func NewClient(ctx context.Context, cfg cfg.Configuration) (*Prepared, error) {
tasks.TraceCaller(ctx, console.Debug, "kubernetes.NewClient")
hostEnv, err := CheckGetHostEnv(cfg)
if err != nil {
return nil, err
}
computed, err := computeConfig(ctx, hostEnv, cfg)
if err != nil {
return nil, err
}
return NewClientFromResult(ctx, hostEnv, computed)
}
func NewClientFromResult(ctx context.Context, hostEnv *HostEnv, computed *configResult) (*Prepared, error) {
restcfg, err := obtainRESTConfig(ctx, hostEnv, computed)
if err != nil {
return nil, err
}
clientset, err := k8s.NewForConfig(restcfg)
if err != nil {
return nil, err
}
c := &Prepared{
Clientset: clientset,
RESTConfig: restcfg,
HostEnv: hostEnv,
}
if computed != nil {
c.Configuration = computed.ClusterConfiguration
c.ClientConfig = computed.ClientConfig
}
return c, nil
}
func MakeGroupVersionBasedClientAndConfig(ctx context.Context, original *rest.Config, gv schema.GroupVersion) (*rest.Config, rest.Interface, error) {
config := copyAndSetDefaults(*original, gv)
client, err := rest.RESTClientFor(config)
return config, client, err
}
func MakeGroupVersionBasedClient(ctx context.Context, original *rest.Config, gv schema.GroupVersion) (rest.Interface, error) {
_, cli, err := MakeGroupVersionBasedClientAndConfig(ctx, original, gv)
return cli, err
}
func copyAndSetDefaults(config rest.Config, gv schema.GroupVersion) *rest.Config {
config.GroupVersion = &gv
if gv.Group == "" {
config.APIPath = "/api"
} else {
config.APIPath = "/apis"
}
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
if config.UserAgent == "" {
config.UserAgent = rest.DefaultKubernetesUserAgent()
}
return &config
}
func CheckGetHostEnv(cfg cfg.Configuration) (*HostEnv, error) {
hostEnv, ok := hostEnvConfigType.CheckGet(cfg)
if !ok {
return nil, fnerrors.UsageError("Try running one `ns prepare local` or `ns prepare eks`", "%s: no kubernetes configuration available", cfg.EnvKey())
}
return hostEnv, nil
}
func IsInclusterClient(c *k8s.Clientset) bool {
config, err := rest.InClusterConfig()
if err != nil {
return false
}
url, err := url.Parse(config.Host)
if err != nil {
return false
}
// TODO is there a cleaner way?
return url.Host == c.RESTClient().Get().URL().Host
}