-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
365 lines (311 loc) · 12.4 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package client
import (
"context"
"fmt"
"net"
"net/url"
"os"
"reflect"
"strings"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/yaml"
applicationv1alpha1 "github.com/giantswarm/apiextensions-application/api/v1alpha1"
orgv1alpha1 "github.com/giantswarm/organization-operator/api/v1alpha1"
helmclient "github.com/mittwald/go-helm-client"
corev1 "k8s.io/api/core/v1"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api/v1"
capi "sigs.k8s.io/cluster-api/api/v1beta1"
kubeadm "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
cr "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/clustertest/pkg/application"
)
// Client extends the client from controller-runtime
type Client struct {
cr.Client
config *rest.Config
clusterName string
}
// New creates a new Kubernetes client for the provided kubeconfig file
//
// The client is an extension of the client from controller-runtime and provides some additional helper functions.
// The creation of the client doesn't confirm connectivity to the cluster and REST discovery is set to lazy discovery
// so the client can be created while the cluster is still being set up.
func New(kubeconfigPath string) (*Client, error) {
return NewWithContext(kubeconfigPath, "")
}
// NewFromRawKubeconfig is like New but takes in the string contents of a Kubeconfig and creates a client for it
//
// The client is an extension of the client from controller-runtime and provides some additional helper functions.
// The creation of the client doesn't confirm connectivity to the cluster and REST discovery is set to lazy discovery
// so the client can be created while the cluster is still being set up.
func NewFromRawKubeconfig(kubeconfig string) (*Client, error) {
clusterName, err := getClusterNameFromKubeConfig([]byte(kubeconfig), "")
if err != nil {
return nil, fmt.Errorf("failed to get cluster name - %v", err)
}
clientConfig, err := clientcmd.NewClientConfigFromBytes([]byte(kubeconfig))
if err != nil {
return nil, fmt.Errorf("failed to create config - %v", err)
}
restConfig, err := clientConfig.ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to create rest config - %v", err)
}
return newClient(restConfig, clusterName)
}
// NewFromSecret create a new Kubernetes client from a cluster kubeconfig found in a secret on the MC.
// This function may return a Not Found error if the kubeconfig secret is not found on the cluster.
//
// The client is an extension of the client from controller-runtime and provides some additional helper functions.
// The creation of the client doesn't confirm connectivity to the cluster and REST discovery is set to lazy discovery
// so the client can be created while the cluster is still being set up.
func NewFromSecret(ctx context.Context, kubeClient *Client, clusterName string, namespace string) (*Client, error) {
kubeconfig, err := kubeClient.GetClusterKubeConfig(ctx, clusterName, namespace)
if err != nil {
return nil, err
}
return NewFromRawKubeconfig(string(kubeconfig))
}
// NewWithContext creates a new Kubernetes client for the provided kubeconfig file and changes the current context to the provided value
//
// The client is an extension of the client from controller-runtime and provides some additional helper functions.
// The creation of the client doesn't confirm connectivity to the cluster and REST discovery is set to lazy discovery
// so the client can be created while the cluster is still being set up.
func NewWithContext(kubeconfigPath string, contextName string) (*Client, error) {
if kubeconfigPath == "" {
return nil, fmt.Errorf("a kubeconfig file must be provided")
}
data, err := os.ReadFile(kubeconfigPath)
if err != nil {
return nil, fmt.Errorf("failed to create context from kubeconfig file '%s' - %v", kubeconfigPath, err)
}
clusterName, err := getClusterNameFromKubeConfig(data, contextName)
if err != nil {
return nil, fmt.Errorf("failed to get cluster name - %v", err)
}
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
&clientcmd.ConfigOverrides{
CurrentContext: contextName,
},
).ClientConfig()
if err != nil {
return nil, fmt.Errorf("failed to create config - %v", err)
}
return newClient(cfg, clusterName)
}
func newClient(config *rest.Config, clusterName string) (*Client, error) {
mapper, err := apiutil.NewDynamicRESTMapper(config, apiutil.WithLazyDiscovery)
if err != nil {
return nil, fmt.Errorf("failed to create new dynamic client - %v", err)
}
client, err := cr.New(config, cr.Options{Scheme: scheme.Scheme, Mapper: mapper})
if err != nil {
return nil, fmt.Errorf("failed to create new client - %v", err)
}
// Add known CRDs to scheme
_ = applicationv1alpha1.AddToScheme(client.Scheme())
_ = orgv1alpha1.AddToScheme(client.Scheme())
_ = capi.AddToScheme(client.Scheme())
_ = kubeadm.AddToScheme(client.Scheme())
return &Client{
Client: client,
config: config,
clusterName: clusterName,
}, nil
}
// getClusterNameFromKubeConfig gets the cluster name of the cluster selected for the provided context.
// The cluster name is the human-friendly name found in the KubeConfig.
// If an empty `contextName` is provided it will use the `CurrentContext` from the provided KubeConfig.
func getClusterNameFromKubeConfig(data []byte, contextName string) (string, error) {
if len(data) == 0 {
return "", fmt.Errorf("Empty kubeconfig provided")
}
kubeconfig := clientcmdapi.Config{}
err := yaml.Unmarshal(data, &kubeconfig)
if err != nil {
return "", err
}
// Default to the context in the kubeconfig if not provided
if contextName == "" {
contextName = kubeconfig.CurrentContext
}
clusterName := ""
for _, context := range kubeconfig.Contexts {
if context.Name == contextName {
clusterName = context.Context.Cluster
break
}
}
return clusterName, nil
}
// CheckConnection attempts to connect to the clusters API server and returns an error if not successful.
// A successful connection is defined as a valid response from the api-server but not necessarily a success response.
// For example, both a "Not Found" and a "Forbidden" response from the server is still a valid, working connection to
// the cluster and doesn't cause this function to return an error.
func (c *Client) CheckConnection() error {
var ns corev1.NamespaceList
err := c.List(context.Background(), &ns)
if isSuccessfulConnectionError(err) {
// The API server did return but with a known error.
// For now, we consider this a successful connection to the cluster.
return nil
}
return err
}
// GetClusterKubeConfig retrieves the Kubeconfig from the secret associated with the provided cluster name.
//
// The server hostname used in the kubeconfig is modified to use the DNS name if it is found to be using an IP address.
func (c *Client) GetClusterKubeConfig(ctx context.Context, clusterName string, clusterNamespace string) (string, error) {
var kubeconfigSecret corev1.Secret
err := c.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("%s-kubeconfig", clusterName), Namespace: clusterNamespace}, &kubeconfigSecret)
if err != nil {
return "", err
}
if len(kubeconfigSecret.Data["value"]) == 0 {
return "", fmt.Errorf("kubeconfig secret found for data not populated")
}
kubeconfig := clientcmdapi.Config{}
err = yaml.Unmarshal(kubeconfigSecret.Data["value"], &kubeconfig)
if err != nil {
return "", err
}
for i := range kubeconfig.Clusters {
kubecluster := &kubeconfig.Clusters[i]
u, err := url.Parse(kubecluster.Cluster.Server)
if err != nil {
return "", err
}
if c.needsToUpdateServerHostname(u.Hostname()) {
// We need to build up the hostname from the base domain and cluster name
var clusterValuesCM corev1.ConfigMap
err := c.Get(ctx, types.NamespacedName{Name: fmt.Sprintf("%s-cluster-values", clusterName), Namespace: clusterNamespace}, &clusterValuesCM)
if err != nil {
return "", err
}
var clusterValues struct {
BaseDomain string `yaml:"baseDomain"`
}
err = yaml.Unmarshal([]byte(clusterValuesCM.Data["values"]), &clusterValues)
if err != nil {
return "", err
}
kubecluster.Cluster.Server = fmt.Sprintf("https://api.%s:%s", clusterValues.BaseDomain, u.Port())
}
}
kc, err := yaml.Marshal(kubeconfig)
if err != nil {
return "", err
}
return string(kc), nil
}
// needsToUpdateServerHostname returns true when the server address needs to be updated so that we can reach the server through our VPN.
// Currently, there are two scenarios where this happens
// - CAPA: the server is a hostname pointing to an AWS ELB hostname
// - CAPG: the server is an IP
func (c *Client) needsToUpdateServerHostname(hostname string) bool {
return net.ParseIP(hostname) != nil || strings.Contains(hostname, "elb.amazonaws.com")
}
// GetHelmValues retrieves the helm values of a Helm release in the provided
// name and namespace and it will Unmarshal the values into the provided values
// struct.
func (c *Client) GetHelmValues(name, namespace string, values interface{}) error {
rv := reflect.ValueOf(values)
if rv.Kind() != reflect.Pointer || rv.IsNil() {
return fmt.Errorf("values must be a pointer, instead got %v", reflect.TypeOf(values))
}
helmClient, err := c.getHelmClient(namespace)
if err != nil {
return err
}
rawValues, err := helmClient.GetReleaseValues(name, true)
if err != nil {
return err
}
yamlValues, err := yaml.Marshal(rawValues)
if err != nil {
return err
}
return yaml.Unmarshal(yamlValues, values)
}
func (c *Client) getHelmClient(releaseNamespace string) (helmclient.Client, error) {
opt := &helmclient.RestConfClientOptions{
Options: &helmclient.Options{
Namespace: releaseNamespace,
},
RestConfig: c.config,
}
helmClient, err := helmclient.NewClientFromRestConf(opt)
if err != nil {
return nil, fmt.Errorf("failed to create helm client: %w", err)
}
return helmClient, nil
}
// CreateOrUpdate attempts first to create the object given but if an AlreadyExists error
// is returned it instead updates the existing resource.
func (c *Client) CreateOrUpdate(ctx context.Context, obj cr.Object) error {
existingObj := unstructured.Unstructured{}
existingObj.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())
err := c.Get(ctx, cr.ObjectKeyFromObject(obj), &existingObj)
switch {
case err == nil:
// Update:
obj.SetResourceVersion(existingObj.GetResourceVersion())
obj.SetUID(existingObj.GetUID())
return c.Patch(ctx, obj, cr.MergeFrom(existingObj.DeepCopy()))
case errors.IsNotFound(err):
// Create:
return c.Create(ctx, obj)
default:
return err
}
}
// DeployApp takes an Application and applies its manifests to the cluster in the correct order,
// ensuring the ConfigMap is made available first.
func (c *Client) DeployApp(ctx context.Context, app application.Application) error {
appCR, configMap, err := app.Build()
if err != nil {
return err
}
return c.DeployAppManifests(ctx, appCR, configMap)
}
// DeployAppManifests takes an applications App CR and ConfigMap manifests and ensures
// they are applied in the correct order, with the ConfigMap being added first.
func (c *Client) DeployAppManifests(ctx context.Context, appCR *applicationv1alpha1.App, configMap *corev1.ConfigMap) error {
if err := c.CreateOrUpdate(ctx, configMap); err != nil {
return fmt.Errorf("failed to apply cluster configmap: %v", err)
}
if err := c.CreateOrUpdate(ctx, appCR); err != nil {
return fmt.Errorf("failed to apply cluster app CR: %v", err)
}
return nil
}
// DeleteApp removes an App CR and its ConfigMap from the cluster
func (c *Client) DeleteApp(ctx context.Context, app application.Application) error {
appCR, configMap, err := app.Build()
if err != nil {
return err
}
if err := c.Delete(ctx, appCR); err != nil {
return fmt.Errorf("failed to delete app CR: %v", err)
}
if err := c.Delete(ctx, configMap); err != nil {
return fmt.Errorf("failed to delete app CR: %v", err)
}
return nil
}
// GetAPIServerEndpoint returns the full URL for the API server
func (c *Client) GetAPIServerEndpoint() string {
return c.config.Host
}
// GetClusterName returns the friendly name of the Cluster from the KubeConfig
func (c *Client) GetClusterName() string {
return c.clusterName
}