-
Notifications
You must be signed in to change notification settings - Fork 9
/
kubeconfig.go
73 lines (63 loc) · 2.08 KB
/
kubeconfig.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
// 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 eks
import (
"context"
"encoding/base64"
"os"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"namespacelabs.dev/foundation/internal/fnerrors"
)
func Kubeconfig(awsCluster *AwsCluster, envName string) (*clientcmdapi.Config, error) {
cluster := awsCluster.Cluster
if cluster.Name == nil {
return nil, fnerrors.BadInputError("cluster name is missing")
}
if cluster.Endpoint == nil {
return nil, fnerrors.BadInputError("cluster endpoint is missing")
}
if cluster.CertificateAuthority == nil || cluster.CertificateAuthority.Data == nil {
return nil, fnerrors.BadInputError("cluster certificateauthority is missing")
}
decoded, err := base64.StdEncoding.DecodeString(*cluster.CertificateAuthority.Data)
if err != nil {
return nil, fnerrors.BadInputError("failed to parse certificateauthority: %w", err)
}
clusterName := *cluster.Name
contextName := clusterName
return &clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{
clusterName: {
Server: *cluster.Endpoint,
CertificateAuthorityData: decoded,
},
},
Contexts: map[string]*clientcmdapi.Context{
contextName: {
Cluster: clusterName,
AuthInfo: contextName,
},
},
CurrentContext: contextName,
AuthInfos: map[string]*clientcmdapi.AuthInfo{
contextName: {
Exec: &clientcmdapi.ExecConfig{
APIVersion: "client.authentication.k8s.io/v1",
Command: os.Args[0],
Args: []string{"eks", "generate-token", "--exec_credential",
"--env", envName, clusterName},
Env: []clientcmdapi.ExecEnvVar{},
InteractiveMode: clientcmdapi.NeverExecInteractiveMode,
},
},
},
}, nil
}
func KubeconfigFromCluster(ctx context.Context, s *Session, clusterName string) (*clientcmdapi.Config, error) {
cluster, err := DescribeCluster(ctx, s, clusterName)
if err != nil {
return nil, err
}
return Kubeconfig(cluster, s.cfg.EnvKey())
}