forked from rook/rook
/
dashboard.go
executable file
·298 lines (262 loc) · 9.47 KB
/
dashboard.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
/*
Copyright 2018 The Rook Authors. 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.
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 mgr for the Ceph manager.
package mgr
import (
"context"
"fmt"
"math/rand"
"os/exec"
"strconv"
"syscall"
"time"
"github.com/rook/rook/pkg/daemon/ceph/client"
cephver "github.com/rook/rook/pkg/operator/ceph/version"
"github.com/rook/rook/pkg/operator/k8sutil"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
dashboardModuleName = "dashboard"
dashboardPortHTTPS = 8443
dashboardPortHTTP = 7000
dashboardUsername = "admin"
dashboardPasswordName = "rook-ceph-dashboard-password"
passwordLength = 10
passwordKeyName = "password"
certAlreadyConfiguredErrorCode = 5
invalidArgErrorCode = int(syscall.EINVAL)
)
var (
dashboardInitWaitTime = 5 * time.Second
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func (c *Cluster) configureDashboard(m *mgrConfig) error {
// enable or disable the dashboard module
if err := c.toggleDashboardModule(m); err != nil {
return err
}
dashboardService := c.makeDashboardService(appName, m.DashboardPort)
if c.dashboard.Enabled {
// expose the dashboard service
if _, err := c.context.Clientset.CoreV1().Services(c.Namespace).Create(dashboardService); err != nil {
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create dashboard mgr service. %+v", err)
}
logger.Infof("dashboard service already exists")
original, err := c.context.Clientset.CoreV1().Services(c.Namespace).Get(dashboardService.Name, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get dashboard service. %+v", err)
}
if original.Spec.Ports[0].Port != int32(m.DashboardPort) {
logger.Infof("dashboard port changed. updating service")
original.Spec.Ports[0].Port = int32(m.DashboardPort)
if _, err := c.context.Clientset.CoreV1().Services(c.Namespace).Update(original); err != nil {
return fmt.Errorf("failed to update dashboard mgr service. %+v", err)
}
}
} else {
logger.Infof("dashboard service started")
}
} else {
// delete the dashboard service if it exists
err := c.context.Clientset.CoreV1().Services(c.Namespace).Delete(dashboardService.Name, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to delete dashboard service. %+v", err)
}
}
return nil
}
// Ceph docs about the dashboard module: http://docs.ceph.com/docs/nautilus/mgr/dashboard/
func (c *Cluster) toggleDashboardModule(m *mgrConfig) error {
if c.dashboard.Enabled {
if err := client.MgrEnableModule(c.context, c.Namespace, dashboardModuleName, true); err != nil {
return fmt.Errorf("failed to enable mgr dashboard module. %+v", err)
}
if err := c.initializeSecureDashboard(); err != nil {
return fmt.Errorf("failed to initialize dashboard. %+v", err)
}
if err := c.configureDashboardModule(m); err != nil {
return fmt.Errorf("failed to configure mgr dashboard module. %+v", err)
}
} else {
if err := client.MgrDisableModule(c.context, c.Namespace, dashboardModuleName); err != nil {
logger.Errorf("failed to disable mgr dashboard module. %+v", err)
}
}
return nil
}
func (c *Cluster) configureDashboardModule(m *mgrConfig) error {
// url prefix
hasChanged, err := client.MgrSetConfig(c.context, c.Namespace, m.DaemonID, c.clusterInfo.CephVersion, "mgr/dashboard/url_prefix", c.dashboard.UrlPrefix, false)
if err != nil {
return err
}
// ssl support
ssl := strconv.FormatBool(c.dashboard.SSL)
changed, err := client.MgrSetConfig(c.context, c.Namespace, m.DaemonID, c.clusterInfo.CephVersion, "mgr/dashboard/ssl", ssl, false)
if err != nil {
return err
}
hasChanged = hasChanged || changed
// server port
port := strconv.Itoa(m.DashboardPort)
changed, err = client.MgrSetConfig(c.context, c.Namespace, m.DaemonID, c.clusterInfo.CephVersion, "mgr/dashboard/server_port", port, false)
if err != nil {
return err
}
hasChanged = hasChanged || changed
// SSL enabled. Needed to set specifically the ssl port setting starting with Nautilus(14.2.1)
if c.dashboard.SSL {
if c.clusterInfo.CephVersion.IsAtLeast(cephver.CephVersion{Major: 14, Minor: 2, Extra: 1}) {
changed, err = client.MgrSetConfig(c.context, c.Namespace, m.DaemonID, c.clusterInfo.CephVersion, "mgr/dashboard/ssl_server_port", port, false)
if err != nil {
return err
}
hasChanged = hasChanged || changed
}
}
if hasChanged {
logger.Infof("dashboard config has changed")
return c.restartDashboard()
}
return nil
}
func (c *Cluster) initializeSecureDashboard() error {
// we need to wait a short period after enabling the module before we can call the `ceph dashboard` commands.
time.Sleep(dashboardInitWaitTime)
password, err := c.getOrGenerateDashboardPassword()
if err != nil {
return fmt.Errorf("failed to generate a password. %+v", err)
}
if c.dashboard.SSL {
alreadyCreated, err := c.createSelfSignedCert()
if err != nil {
return fmt.Errorf("failed to create a self signed cert. %+v", err)
}
if alreadyCreated {
return nil
}
}
if err := c.setLoginCredentials(password); err != nil {
return fmt.Errorf("failed to set login creds. %+v", err)
}
return c.restartDashboard()
}
func (c *Cluster) createSelfSignedCert() (bool, error) {
// create a self-signed cert for the https connections required in mimic
args := []string{"dashboard", "create-self-signed-cert"}
// retry a few times in the case that the mgr module is not ready to accept commands
for i := 0; i < 5; i++ {
_, err := client.NewCephCommand(c.context, c.Namespace, args).RunWithTimeout(client.CmdExecuteTimeout)
if err == context.DeadlineExceeded {
logger.Infof("cert creation timed out. trying again..")
continue
}
if err != nil {
exitCode, parsed := c.exitCode(err)
if parsed {
if exitCode == certAlreadyConfiguredErrorCode {
logger.Infof("dashboard is already initialized with a cert")
return true, nil
}
if exitCode == invalidArgErrorCode {
logger.Infof("dashboard module is not ready yet. trying again...")
time.Sleep(dashboardInitWaitTime)
continue
}
}
return false, fmt.Errorf("failed to create self signed cert on mgr. %+v", err)
}
break
}
return false, nil
}
// Get the return code from the process
func getExitCode(err error) (int, bool) {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), true
}
}
return 0, false
}
func (c *Cluster) setLoginCredentials(password string) error {
// Set the login credentials. Write the command/args to the debug log so we don't write the password by default to the log.
logger.Infof("Running command: ceph dashboard set-login-credentials admin *******")
// retry a few times in the case that the mgr module is not ready to accept commands
_, err := client.ExecuteCephCommandWithRetry(func() ([]byte, error) {
args := []string{"dashboard", "set-login-credentials", dashboardUsername, password}
cmd := client.NewCephCommand(c.context, c.Namespace, args)
cmd.Debug = true
return cmd.RunWithTimeout(client.CmdExecuteTimeout)
}, c.exitCode, 5, invalidArgErrorCode, dashboardInitWaitTime)
if err != nil {
return fmt.Errorf("failed to set login creds on mgr. %+v", err)
}
return nil
}
func (c *Cluster) getOrGenerateDashboardPassword() (string, error) {
secret, err := c.context.Clientset.CoreV1().Secrets(c.Namespace).Get(dashboardPasswordName, metav1.GetOptions{})
if err == nil {
logger.Infof("the dashboard secret was already generated")
return decodeSecret(secret)
}
if !errors.IsNotFound(err) {
return "", fmt.Errorf("failed to get dashboard secret. %+v", err)
}
// Generate a password
password := generatePassword(passwordLength)
// Store the keyring in a secret
secrets := map[string][]byte{
passwordKeyName: []byte(password),
}
secret = &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: dashboardPasswordName,
Namespace: c.Namespace,
},
Data: secrets,
Type: k8sutil.RookType,
}
k8sutil.SetOwnerRef(&secret.ObjectMeta, &c.ownerRef)
_, err = c.context.Clientset.CoreV1().Secrets(c.Namespace).Create(secret)
if err != nil {
return "", fmt.Errorf("failed to save dashboard secret. %+v", err)
}
return password, nil
}
func generatePassword(length int) string {
const passwordChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
passwd := make([]byte, length)
for i := range passwd {
passwd[i] = passwordChars[rand.Intn(len(passwordChars))]
}
return string(passwd)
}
func decodeSecret(secret *v1.Secret) (string, error) {
password, ok := secret.Data[passwordKeyName]
if !ok {
return "", fmt.Errorf("password not found in secret")
}
return string(password), nil
}
func (c *Cluster) restartDashboard() error {
logger.Infof("restarting the mgr module")
client.MgrDisableModule(c.context, c.Namespace, dashboardModuleName)
client.MgrEnableModule(c.context, c.Namespace, dashboardModuleName, true)
return nil
}