/
app_admin_secret.go
77 lines (67 loc) · 2.42 KB
/
app_admin_secret.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
/*
Copyright 2020,2021 Avi Zimmerman
This file is part of kvdi.
kvdi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
kvdi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with kvdi. If not, see <https://www.gnu.org/licenses/>.
*/
package app
import (
"context"
appv1 "github.com/kvdi/kvdi/apis/app/v1"
"github.com/kvdi/kvdi/pkg/util/common"
"github.com/kvdi/kvdi/pkg/util/errors"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const passwordKey = "password"
func (r *Reconciler) reconcileAdminSecret(reqLogger logr.Logger, cluster *appv1.VDICluster) (password string, err error) {
nn := types.NamespacedName{Name: cluster.GetAdminSecret(), Namespace: cluster.GetCoreNamespace()}
found := &corev1.Secret{}
if err := r.client.Get(context.TODO(), nn, found); err != nil {
if client.IgnoreNotFound(err) != nil {
return "", err
}
// We are generating a password
reqLogger.Info("Generating password and creating new admin secret", "Secret.Name", nn.Name, "Secret.Namespace", nn.Namespace)
passw, err := common.GeneratePassword(16)
if err != nil {
return "", err
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: nn.Name,
Namespace: nn.Namespace,
Labels: cluster.GetComponentLabels("admin-secret"),
OwnerReferences: cluster.OwnerReferences(),
},
Data: map[string][]byte{
passwordKey: []byte(passw),
},
}
if err := r.client.Create(context.TODO(), secret); err != nil {
return "", err
}
return passw, nil
}
existingPassw, ok := found.Data[passwordKey]
if !ok {
// delete the secret and requeue, currently migration depends on the admin
// password - but long-term this is probably not a good idea
if err := r.client.Delete(context.TODO(), found); err != nil {
return "", err
}
return "", errors.NewRequeueError("Regenerating admin secret", 3)
}
return string(existingPassw), nil
}