/
kubeconfig.go
158 lines (141 loc) · 4.55 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
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
package kubeconfig
import (
"context"
"encoding/base64"
"fmt"
"github.com/giantswarm/apiextensions/v3/pkg/apis/application/v1alpha1"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
yaml "gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// KubeConfig provides functionality for connecting to remote clusters based on
// the specified kubeconfig.
type KubeConfig struct {
logger micrologger.Logger
k8sClient kubernetes.Interface
}
// New creates a new KubeConfig service.
func New(config Config) (*KubeConfig, error) {
err := config.Validate()
if err != nil {
return nil, microerror.Mask(err)
}
g := &KubeConfig{
logger: config.Logger,
k8sClient: config.K8sClient,
}
return g, nil
}
// NewRESTConfigForApp returns a Kubernetes REST config for the cluster
// configured in the kubeconfig section of the app CR.
func (k *KubeConfig) NewRESTConfigForApp(ctx context.Context, app v1alpha1.App) (*rest.Config, error) {
if inCluster(app) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, microerror.Mask(err)
}
return config, nil
}
secretName := secretName(app)
secretNamespace := secretNamespace(app)
kubeConfig, err := k.getKubeConfigFromSecret(ctx, secretName, secretNamespace)
if err != nil {
return nil, microerror.Mask(err)
}
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeConfig)
if err != nil {
return nil, microerror.Mask(err)
}
return restConfig, nil
}
// NewKubeConfigForRESTConfig returns a kubeConfig bytes for the given REST Config.
func NewKubeConfigForRESTConfig(ctx context.Context, config *rest.Config, clusterName, namespace string) ([]byte, error) {
if config == nil {
return nil, microerror.Maskf(executionFailedError, "config must not be empty")
}
if clusterName == "" {
return nil, microerror.Maskf(executionFailedError, "clusterName must not be empty")
}
kubeConfig := KubeConfigValue{
APIVersion: "v1",
Kind: "Config",
Clusters: []KubeconfigNamedCluster{
{
Name: clusterName,
Cluster: KubeconfigCluster{
Server: config.Host,
CertificateAuthorityData: base64.StdEncoding.EncodeToString(config.TLSClientConfig.CAData),
},
},
},
Contexts: []KubeconfigNamedContext{
{
Name: fmt.Sprintf("%s-context", clusterName),
Context: KubeconfigContext{
Cluster: clusterName,
Namespace: namespace,
User: fmt.Sprintf("%s-user", clusterName),
},
},
},
Users: []KubeconfigUser{
{
Name: fmt.Sprintf("%s-user", clusterName),
User: KubeconfigUserKeyPair{
ClientCertificateData: base64.StdEncoding.EncodeToString(config.CertData),
ClientKeyData: base64.StdEncoding.EncodeToString(config.KeyData),
},
},
},
CurrentContext: fmt.Sprintf("%s-context", clusterName),
}
bytes, err := yaml.Marshal(kubeConfig)
if err != nil {
return nil, microerror.Mask(err)
}
return bytes, nil
}
// NewRESTConfigForKubeConfig returns a REST Config for the given KubeConfigValue.
func NewRESTConfigForKubeConfig(ctx context.Context, kubeConfig []byte) (*rest.Config, error) {
restConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeConfig)
if err != nil {
return nil, microerror.Mask(err)
}
return restConfig, nil
}
// getKubeConfigFromSecret returns KubeConfig bytes based on the specified secret information.
func (k *KubeConfig) getKubeConfigFromSecret(ctx context.Context, secretName, secretNamespace string) ([]byte, error) {
secret, err := k.k8sClient.CoreV1().Secrets(secretNamespace).Get(ctx, secretName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil, microerror.Maskf(notFoundError, "Secret %#q in Namespace %#q", secretName, secretNamespace)
} else if _, isStatus := err.(*errors.StatusError); isStatus {
return nil, microerror.Mask(err)
} else if err != nil {
return nil, microerror.Mask(err)
}
if bytes, ok := secret.Data["kubeConfig"]; ok {
return bytes, nil
} else {
return nil, microerror.Maskf(notFoundError, "Secret %#q in Namespace %#q does not have kubeConfig key in its data", secretName, secretNamespace)
}
}
func marshal(config *KubeConfigValue) ([]byte, error) {
bytes, err := yaml.Marshal(config)
if err != nil {
return nil, microerror.Mask(err)
}
return bytes, nil
}
func unmarshal(bytes []byte) (*KubeConfigValue, error) {
var kubeConfig KubeConfigValue
err := yaml.Unmarshal(bytes, &kubeConfig)
if err != nil {
return nil, microerror.Mask(err)
}
return &kubeConfig, nil
}