forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
411 lines (368 loc) · 13.6 KB
/
controller.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rancher
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/exec"
"path"
"strings"
"time"
"go.uber.org/zap"
rancherclient "github.com/kubermatic/kubermatic/api/pkg/controller/seed-controller-manager/rancher/client"
predicateutil "github.com/kubermatic/kubermatic/api/pkg/controller/util/predicate"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
kubermaticv1helper "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1/helper"
"github.com/kubermatic/kubermatic/api/pkg/resources"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
kubeapierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
ControllerName = "kubernatic_rancher_controller"
RancherUsername = "admin"
RancherAdminSecret = "rancher-admin-secret"
// keep the linter happy
// trueStr = "true"
rancherRandPasswordLength = 16
)
// KubeconfigProvider provides functionality to get a clusters admin kubeconfig
type KubeconfigProvider interface {
GetAdminKubeconfig(c *kubermaticv1.Cluster) ([]byte, error)
}
type fileHandlingDone func()
type Reconciler struct {
log *zap.SugaredLogger
ctrlruntimeclient.Client
KubeconfigProvider KubeconfigProvider
}
var (
rancherClient *rancherclient.Client
)
func Add(
mgr manager.Manager,
log *zap.SugaredLogger,
kubeconfigProvider KubeconfigProvider,
) error {
log = log.Named(ControllerName)
reconciler := &Reconciler{
log: log,
Client: mgr.GetClient(),
KubeconfigProvider: kubeconfigProvider,
}
c, err := controller.New(ControllerName, mgr, controller.Options{
Reconciler: reconciler,
})
if err != nil {
return fmt.Errorf("failed to create controller: %v", err)
}
predicates := predicateutil.ByName(resources.RancherStatefulSetName)
if err := c.Watch(&source.Kind{Type: &appsv1.StatefulSet{}}, &handler.EnqueueRequestForObject{}, predicates); err != nil {
return fmt.Errorf("failed to create watch for statefulsets: %v", err)
}
return nil
}
func (r *Reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log := r.log.With("request", request)
log.Debug("Processing")
statefulSet := &appsv1.StatefulSet{}
if err := r.Get(ctx, request.NamespacedName, statefulSet); err != nil {
if kubeapierrors.IsNotFound(err) {
log.Debugw("couldn't find statefulSet", zap.Error(err))
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("failed to get rancher statefulset: %v", err)
}
result, err := r.reconcile(ctx, log, statefulSet)
if result == nil {
result = &reconcile.Result{}
}
if err != nil {
clusterName := strings.ReplaceAll(statefulSet.Namespace, "cluster-", "")
log.Errorf("failed to reconcile %s: %v", clusterName, zap.Error(err))
return *result, fmt.Errorf("failed to reconcile %s: %v", clusterName, err)
}
return *result, nil
}
func (r *Reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, statefulSet *appsv1.StatefulSet) (*reconcile.Result, error) {
if statefulSet.DeletionTimestamp != nil {
return nil, nil
}
cluster := &kubermaticv1.Cluster{}
if err := r.Get(ctx, types.NamespacedName{Name: strings.ReplaceAll(statefulSet.Namespace, "cluster-", "")}, cluster); err != nil {
log.Debugw("can't find cluster", zap.Error(err))
return nil, nil
}
// rancher integration is disabled
if flag := cluster.Spec.Features[kubermaticv1.ClusterFeatureRancherIntegration]; !flag {
return nil, nil
}
// cluster is already imported
if cluster.Status.HasConditionValue(kubermaticv1.ClusterConditionRancherInitialized, corev1.ConditionTrue) &&
cluster.Status.HasConditionValue(kubermaticv1.ClusterConditionRancherClusterImported, corev1.ConditionTrue) {
return nil, nil
}
// wait for statefulSet to be ready
if statefulSet.Status.ReadyReplicas == 0 {
return &reconcile.Result{RequeueAfter: 10 * time.Second}, nil
}
if statefulSet.Annotations == nil {
statefulSet.Annotations = make(map[string]string)
}
// initialize rancher statefulSet
if !cluster.Status.HasConditionValue(kubermaticv1.ClusterConditionRancherInitialized, corev1.ConditionTrue) {
if err := r.initRancherServer(ctx, log, statefulSet); err != nil {
log.Errorw("failed to initialize Rancher Server", zap.Error(err))
return &reconcile.Result{}, err
}
kubermaticv1helper.SetClusterCondition(cluster, kubermaticv1.ClusterConditionRancherInitialized, corev1.ConditionTrue, "", "Rancher server initialized successfully")
if err := r.Update(ctx, cluster); err != nil {
return &reconcile.Result{}, err
}
log.Infow("rancher server statefulSet initialized successfully")
}
// Setup the user cluster
rancherRegToken, err := r.rancherClusterWithRegistrationToken(ctx, cluster.Status.NamespaceName, cluster.Spec.HumanReadableName)
if err != nil {
log.Errorw("failed to create rancher cluster:", zap.Error(err))
return nil, err
}
if rancherRegToken != nil {
if err := r.applyRancherRegstrationCommand(ctx, log, cluster, rancherRegToken); err != nil {
log.Errorw("failed to apply rancher regstration command", zap.Error(err))
return nil, err
}
kubermaticv1helper.SetClusterCondition(cluster, kubermaticv1.ClusterConditionRancherClusterImported, corev1.ConditionTrue, "", "Rancher cluster imported successfully")
if err := r.Update(ctx, cluster); err != nil {
return nil, err
}
}
return nil, nil
}
func (r *Reconciler) initRancherServer(ctx context.Context, log *zap.SugaredLogger, statefulSet *appsv1.StatefulSet) error {
client, err := r.getRancherClient(ctx, statefulSet.Namespace)
if err != nil {
return fmt.Errorf("failed to get rancher client: %v", err)
}
users, err := client.ListUsers(map[string]string{"username": RancherUsername})
if err != nil {
return fmt.Errorf("failed to get user list: %v", err)
}
initSecret, err := r.getRancherInitSecret(ctx, statefulSet.Namespace)
if err != nil {
return fmt.Errorf("failed to get rancher admin secret [%s] : %v", RancherAdminSecret, err)
}
for _, user := range users.Data {
if user.Username == RancherUsername {
err = client.SetUserPassword(&user, &rancherclient.SetPasswordInput{NewPassword: string(initSecret.Data["password"])})
if err != nil {
return fmt.Errorf("failed to set rancher user password: %v", err)
}
break
}
}
return nil
}
func (r *Reconciler) rancherClusterWithRegistrationToken(ctx context.Context, clusterNamespace, clusterName string) (*rancherclient.ClusterRegistrationToken, error) {
client, err := r.getRancherClient(ctx, clusterNamespace)
if err != nil {
return nil, fmt.Errorf("failed to get rancher client: %v", err)
}
clusterList, err := client.ListClusters(map[string]string{"name": clusterName})
if err != nil {
return nil, fmt.Errorf("failed to list rancher clusters: %v", err)
}
rancherCluster := &rancherclient.Cluster{Name: clusterName}
if len(clusterList.Data) != 0 {
rancherCluster = &clusterList.Data[0]
} else {
rancherCluster, err = client.CreateImportedCluster(rancherCluster)
if err != nil {
return nil, fmt.Errorf("failed to create rancher imported cluster: %v", err)
}
}
if !isRancherClusterProvisioned(rancherCluster) {
token := &rancherclient.ClusterRegistrationToken{ClusterID: rancherCluster.ID}
token, err = client.CreateClusterRegistrationToken(token)
if err != nil {
return nil, fmt.Errorf("failed to create rancher cluster registration token: %v", err)
}
return token, nil
}
return nil, nil
}
func (r *Reconciler) getRancherClient(ctx context.Context, namespace string) (*rancherclient.Client, error) {
if rancherClient != nil {
return rancherClient, nil
}
address, err := r.getRancherServerURL(ctx, namespace)
if err != nil {
return nil, fmt.Errorf("can't get rancher service URL: %v", err)
}
initSecret, err := r.getRancherInitSecret(ctx, namespace)
if err != nil {
return nil, fmt.Errorf("failed to get rancher secret: %v", err)
}
opts := rancherclient.Options{
Endpoint: address,
AccessKey: RancherUsername,
SecretKey: string(initSecret.Data["password"]),
Insecure: true,
}
rancherClient, err = rancherclient.New(opts)
if err != nil {
r.log.Debugw("faild to login updated credentials:", zap.Error(err))
// fall back to the default password
opts.SecretKey = "admin"
rancherClient, err = rancherclient.New(opts)
if err != nil {
return nil, fmt.Errorf("failed to loging to rancher server: %v", err)
}
}
return rancherClient, nil
}
func (r *Reconciler) getRancherServerURL(ctx context.Context, namespace string) (string, error) {
service := &corev1.Service{}
if err := r.Get(ctx, types.NamespacedName{Name: resources.RancherServerServiceName, Namespace: namespace}, service); err != nil {
return "", err
}
var port int32
for _, svcPort := range service.Spec.Ports {
if svcPort.Name == "https" {
port = svcPort.NodePort
break
}
}
if port == 0 {
return "", fmt.Errorf("Can't find rancher server service nodeport")
}
cluster := &kubermaticv1.Cluster{}
if err := r.Get(ctx, types.NamespacedName{Name: strings.ReplaceAll(service.Namespace, "cluster-", "")}, cluster); err != nil {
return "", fmt.Errorf("failed to get cluster: %v", err)
}
return fmt.Sprintf("https://%s:%d", cluster.Address.ExternalName, port), nil
}
func (r *Reconciler) getRancherInitSecret(ctx context.Context, namespace string) (*corev1.Secret, error) {
secret := &corev1.Secret{}
err := r.Get(ctx, types.NamespacedName{Name: RancherAdminSecret, Namespace: namespace}, secret)
if err != nil && !kubeapierrors.IsNotFound(err) {
return nil, fmt.Errorf("failed to get secret: %v", err)
}
if secret.Name != "" {
return secret, nil
}
secret.Name = RancherAdminSecret
secret.Namespace = namespace
secret.Data = map[string][]byte{
"password": []byte(randString()),
"user": []byte("admin"),
}
err = r.Create(ctx, secret)
return secret, err
}
func (r *Reconciler) applyRancherRegstrationCommand(ctx context.Context, log *zap.SugaredLogger, cluster *kubermaticv1.Cluster, regToken *rancherclient.ClusterRegistrationToken) error {
client := getHTTPClient(true)
resp, err := client.Get(regToken.ManifestURL)
if err != nil {
return fmt.Errorf("failed to get HTTP client: %v", err)
}
defer resp.Body.Close()
manifest, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read http response: %v", err)
}
kubeconfigFilename, kubeconfigDone, err := r.writeAdminKubeconfig(log, cluster)
if err != nil {
return fmt.Errorf("failed to write the admin kubeconfig to the local filesystem: %v", err)
}
defer kubeconfigDone()
cmd := getApplyCommand(ctx, kubeconfigFilename)
buffer := bytes.Buffer{}
buffer.Write(manifest)
cmd.Stdin = &buffer
_, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run rancher kubectl apply command: %v", err)
}
return nil
}
func (r *Reconciler) writeAdminKubeconfig(log *zap.SugaredLogger, cluster *kubermaticv1.Cluster) (string, fileHandlingDone, error) {
// Write kubeconfig to disk
kubeconfig, err := r.KubeconfigProvider.GetAdminKubeconfig(cluster)
if err != nil {
return "", nil, fmt.Errorf("failed to get admin kubeconfig for cluster %s: %v", cluster.Name, err)
}
kubeconfigFilename := path.Join("/tmp", fmt.Sprintf("cluster-%s-rancher-kubeconfig", cluster.Name))
if err := ioutil.WriteFile(kubeconfigFilename, kubeconfig, 0644); err != nil {
return "", nil, fmt.Errorf("failed to write admin kubeconfig for cluster %s: %v", cluster.Name, err)
}
log.Debugw("Wrote admin kubeconfig", "file", kubeconfigFilename)
return kubeconfigFilename, getFileDeleteFinalizer(log, kubeconfigFilename), nil
}
func getFileDeleteFinalizer(log *zap.SugaredLogger, filename string) fileHandlingDone {
return func() {
if err := os.RemoveAll(filename); err != nil {
log.Errorw("Failed to delete file", zap.Error(err), "file", filename)
}
}
}
func getApplyCommand(ctx context.Context, kubeconfigFilename string) *exec.Cmd {
return exec.CommandContext(ctx, "kubectl", "--kubeconfig", kubeconfigFilename, "apply", "-f", "-")
}
func getHTTPClient(insecure bool) http.Client {
tr := http.DefaultTransport
if insecure {
tr.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
return http.Client{
Transport: tr,
}
}
func isRancherClusterProvisioned(cluster *rancherclient.Cluster) bool {
if cluster == nil || cluster.Conditions == nil {
return false
}
for _, condition := range cluster.Conditions {
if condition.Type == "Provisioned" && condition.Status == "True" {
return true
}
}
return false
}
func randString() string {
rand.Seed(time.Now().UnixNano())
charset := "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"0123456789" +
"!@#$%^&*()"
b := make([]byte, rancherRandPasswordLength)
for i := 0; i < rancherRandPasswordLength; i++ {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}