Skip to content

Commit

Permalink
Merge pull request rook#14492 from rook/mergify/bp/release-1.14/pr-14484
Browse files Browse the repository at this point in the history
mgr: Properly detect if dashboard cert already exists to avoid unnecessary dashboard module restarts (backport rook#14484)
  • Loading branch information
travisn authored Jul 24, 2024
2 parents b68f4fc + f5a7f0e commit a8a05d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
47 changes: 27 additions & 20 deletions pkg/operator/ceph/cluster/mgr/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ func (c *Cluster) configureDashboardModules() error {
return nil
}

err := c.initializeSecureDashboard()
secureRequiresRestart, err := c.initializeSecureDashboard()
if err != nil {
return errors.Wrap(err, "failed to initialize dashboard")
}

hasChanged, err := c.configureDashboardModuleSettings()
configChanged, err := c.configureDashboardModuleSettings()
if err != nil {
return err
}
if hasChanged {
if secureRequiresRestart || configChanged {
logger.Info("dashboard config has changed. restarting the dashboard module")
return c.restartMgrModule(dashboardModuleName)
}
Expand Down Expand Up @@ -199,38 +199,46 @@ func (c *Cluster) configureDashboardModuleSettings() (bool, error) {
return hasChanged, nil
}

func (c *Cluster) initializeSecureDashboard() error {
func (c *Cluster) initializeSecureDashboard() (bool, error) {
// we need to wait a short period after enabling the module before we can call the `ceph dashboard` commands.
time.Sleep(dashboardInitWaitTime)
restartNeeded := false

password, err := c.getOrGenerateDashboardPassword()
if err != nil {
return errors.Wrap(err, "failed to generate a password for the ceph dashboard")
return restartNeeded, errors.Wrap(err, "failed to generate a password for the ceph dashboard")
}

if c.spec.Dashboard.SSL {
alreadyCreated, err := c.createSelfSignedCert()
if err != nil {
return errors.Wrap(err, "failed to create a self signed cert for the ceph dashboard")
return restartNeeded, errors.Wrap(err, "failed to create a self signed cert for the ceph dashboard")
}
if alreadyCreated {
return nil
}
if err := c.restartMgrModule(dashboardModuleName); err != nil {
logger.Warningf("failed to restart dashboard after generating ssl cert. %v", err)
if !alreadyCreated {
restartNeeded = true
}
}

if err := c.setLoginCredentials(password); err != nil {
return errors.Wrap(err, "failed to set login credentials for the ceph dashboard")
return restartNeeded, errors.Wrap(err, "failed to set login credentials for the ceph dashboard")
}

return nil
return restartNeeded, nil
}

func (c *Cluster) createSelfSignedCert() (bool, error) {

// Check if the cert already exists
args := []string{"config-key", "get", "mgr/dashboard/crt"}
output, err := client.NewCephCommand(c.context, c.clusterInfo, args).RunWithTimeout(exec.CephCommandsTimeout)
if err == nil && len(output) > 0 {
logger.Info("dashboard is already initialized with a cert")
return true, nil
}
logger.Debugf("dashboard cert does not appear to exist. err=%v", err)

// create a self-signed cert for the https connections
args := []string{"dashboard", "create-self-signed-cert"}
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++ {
Expand All @@ -242,20 +250,19 @@ func (c *Cluster) createSelfSignedCert() (bool, error) {
if err != nil {
exitCode, parsed := c.exitCode(err)
if parsed {
if exitCode == certAlreadyConfiguredErrorCode {
logger.Info("dashboard is already initialized with a cert")
return true, nil
}
if exitCode == invalidArgErrorCode {
logger.Info("dashboard module is not ready yet. trying again")
time.Sleep(dashboardInitWaitTime)
continue
}
} else {
return false, errors.Wrap(err, "failed to create self signed cert on mgr")
}
return false, errors.Wrap(err, "failed to create self signed cert on mgr")
}
break
logger.Info("dashboard cert created")
return false, nil
}
logger.Info("dashboard cert creation exceeded retries")
return false, nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/operator/ceph/cluster/mgr/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func TestStartSecureDashboard(t *testing.T) {
err = c.configureDashboardModules()
assert.NoError(t, err)
// the dashboard is enabled once with the new dashboard and modules
assert.Equal(t, 3, enables)
assert.Equal(t, 2, disables)
assert.Equal(t, 2, enables)
assert.Equal(t, 1, disables)
assert.Equal(t, 2, moduleRetries)

svc, err := c.context.Clientset.CoreV1().Services(clusterInfo.Namespace).Get(ctx, "rook-ceph-mgr-dashboard", metav1.GetOptions{})
Expand All @@ -152,8 +152,8 @@ func TestStartSecureDashboard(t *testing.T) {
assert.Nil(t, err)
err = c.configureDashboardModules()
assert.NoError(t, err)
assert.Equal(t, 3, enables)
assert.Equal(t, 3, disables)
assert.Equal(t, 2, enables)
assert.Equal(t, 2, disables)

svc, err = c.context.Clientset.CoreV1().Services(clusterInfo.Namespace).Get(ctx, "rook-ceph-mgr-dashboard", metav1.GetOptions{})
assert.NotNil(t, err)
Expand Down

0 comments on commit a8a05d3

Please sign in to comment.