-
Notifications
You must be signed in to change notification settings - Fork 473
/
control_plane.go
202 lines (165 loc) · 5.63 KB
/
control_plane.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package secrets
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
clientcmdv1 "k8s.io/client-go/tools/clientcmd/api/v1"
)
// ControlPlaneSecretDataKeyCertificatePEM returns the data key inside a Secret of type ControlPlane whose value
// contains the certificate PEM.
func ControlPlaneSecretDataKeyCertificatePEM(name string) string { return name + ".crt" }
// ControlPlaneSecretDataKeyPrivateKey returns the data key inside a Secret of type ControlPlane whose value
// contains the private key PEM.
func ControlPlaneSecretDataKeyPrivateKey(name string) string { return name + ".key" }
// ControlPlaneSecretConfig is a struct which inherits from CertificateSecretConfig and is extended with a couple of additional
// properties. A control plane secret will always contain a server/client certificate and optionally a kubeconfig.
type ControlPlaneSecretConfig struct {
Name string
CertificateSecretConfig *CertificateSecretConfig
BasicAuth *BasicAuth
Token *Token
KubeConfigRequests []KubeConfigRequest
}
// KubeConfigRequest is a struct which holds information about a Kubeconfig to be generated.
type KubeConfigRequest struct {
ClusterName string
APIServerHost string
CAData []byte
}
// ControlPlane contains the certificate, and optionally the basic auth. information as well as a Kubeconfig.
type ControlPlane struct {
Name string
Certificate *Certificate
BasicAuth *BasicAuth
Token *Token
Kubeconfig []byte
}
// GetName returns the name of the secret.
func (s *ControlPlaneSecretConfig) GetName() string {
return s.Name
}
// Generate implements ConfigInterface.
func (s *ControlPlaneSecretConfig) Generate() (DataInterface, error) {
var certificate *Certificate
if s.CertificateSecretConfig != nil {
s.CertificateSecretConfig.Name = s.Name
certData, err := s.CertificateSecretConfig.GenerateCertificate()
if err != nil {
return nil, err
}
certificate = certData
}
controlPlane := &ControlPlane{
Name: s.Name,
Certificate: certificate,
BasicAuth: s.BasicAuth,
Token: s.Token,
}
if len(s.KubeConfigRequests) > 0 {
kubeconfig, err := generateKubeconfig(s, certificate)
if err != nil {
return nil, err
}
controlPlane.Kubeconfig = kubeconfig
}
return controlPlane, nil
}
// SecretData computes the data map which can be used in a Kubernetes secret.
func (c *ControlPlane) SecretData() map[string][]byte {
data := make(map[string][]byte)
if c.Certificate != nil {
if c.Certificate.CA != nil {
data[DataKeyCertificateCA] = c.Certificate.CA.CertificatePEM
}
if c.Certificate.CertificatePEM != nil && c.Certificate.PrivateKeyPEM != nil {
data[ControlPlaneSecretDataKeyPrivateKey(c.Name)] = c.Certificate.PrivateKeyPEM
data[ControlPlaneSecretDataKeyCertificatePEM(c.Name)] = c.Certificate.CertificatePEM
}
}
if c.BasicAuth != nil {
data[DataKeyUserName] = []byte(c.BasicAuth.Username)
data[DataKeyPassword] = []byte(c.BasicAuth.Password)
}
if c.Token != nil {
data[DataKeyToken] = []byte(c.Token.Token)
}
if c.Kubeconfig != nil {
data[DataKeyKubeconfig] = c.Kubeconfig
}
return data
}
func generateKubeconfig(secret *ControlPlaneSecretConfig, certificate *Certificate) ([]byte, error) {
if len(secret.KubeConfigRequests) == 0 {
return nil, fmt.Errorf("missing kubeconfig request for %q", secret.Name)
}
var (
name = secret.KubeConfigRequests[0].ClusterName
authContextName string
authInfos = []clientcmdv1.NamedAuthInfo{}
tokenContextName = fmt.Sprintf("%s-token", name)
basicAuthContextName = fmt.Sprintf("%s-basic-auth", name)
)
if certificate != nil && certificate.CertificatePEM != nil && certificate.PrivateKeyPEM != nil {
authContextName = name
} else if secret.Token != nil {
authContextName = tokenContextName
} else if secret.BasicAuth != nil {
authContextName = basicAuthContextName
}
if certificate != nil && certificate.CertificatePEM != nil && certificate.PrivateKeyPEM != nil {
authInfos = append(authInfos, clientcmdv1.NamedAuthInfo{
Name: name,
AuthInfo: clientcmdv1.AuthInfo{
ClientCertificateData: certificate.CertificatePEM,
ClientKeyData: certificate.PrivateKeyPEM,
},
})
}
if secret.Token != nil {
authInfos = append(authInfos, clientcmdv1.NamedAuthInfo{
Name: tokenContextName,
AuthInfo: clientcmdv1.AuthInfo{
Token: secret.Token.Token,
},
})
}
if secret.BasicAuth != nil {
authInfos = append(authInfos, clientcmdv1.NamedAuthInfo{
Name: basicAuthContextName,
AuthInfo: clientcmdv1.AuthInfo{
Username: secret.BasicAuth.Username,
Password: secret.BasicAuth.Password,
},
})
}
config := &clientcmdv1.Config{
CurrentContext: name,
Clusters: []clientcmdv1.NamedCluster{},
Contexts: []clientcmdv1.NamedContext{},
AuthInfos: authInfos,
}
for _, req := range secret.KubeConfigRequests {
caData := req.CAData
if caData == nil && certificate != nil && certificate.CA != nil {
caData = certificate.CA.CertificatePEM
}
config.Clusters = append(config.Clusters, clientcmdv1.NamedCluster{
Name: req.ClusterName,
Cluster: clientcmdv1.Cluster{
CertificateAuthorityData: caData,
Server: fmt.Sprintf("https://%s", req.APIServerHost),
},
})
config.Contexts = append(config.Contexts, clientcmdv1.NamedContext{
Name: req.ClusterName,
Context: clientcmdv1.Context{
Cluster: req.ClusterName,
AuthInfo: authContextName,
},
})
}
return runtime.Encode(clientcmdlatest.Codec, config)
}