From c247e8c0f707ea9d408c2f13534ccb9928b6ca62 Mon Sep 17 00:00:00 2001 From: Andrey Laguta <13280256+laevilgenius@users.noreply.github.com> Date: Fri, 20 Jul 2018 00:12:58 +0300 Subject: [PATCH] Add urlPrefix setting for ceph dashboard Signed-off-by: Andrey Laguta <13280256+laevilgenius@users.noreply.github.com> --- Documentation/ceph-cluster-crd.md | 1 + Documentation/ceph-dashboard.md | 10 ++++++ cluster/examples/kubernetes/ceph/cluster.yaml | 2 ++ .../examples/kubernetes/ceph/operator.yaml | 2 ++ pkg/apis/ceph.rook.io/v1beta1/types.go | 2 ++ pkg/daemon/ceph/client/mgr.go | 36 +++++++++++++++++++ pkg/operator/ceph/cluster/cluster.go | 4 +++ pkg/operator/ceph/cluster/mgr/dashboard.go | 26 +++++++++++--- pkg/operator/ceph/cluster/mgr/mgr_test.go | 5 +++ 9 files changed, 83 insertions(+), 5 deletions(-) mode change 100644 => 100755 Documentation/ceph-cluster-crd.md mode change 100644 => 100755 Documentation/ceph-dashboard.md mode change 100644 => 100755 cluster/examples/kubernetes/ceph/cluster.yaml mode change 100644 => 100755 cluster/examples/kubernetes/ceph/operator.yaml mode change 100644 => 100755 pkg/apis/ceph.rook.io/v1beta1/types.go mode change 100644 => 100755 pkg/daemon/ceph/client/mgr.go mode change 100644 => 100755 pkg/operator/ceph/cluster/cluster.go mode change 100644 => 100755 pkg/operator/ceph/cluster/mgr/dashboard.go mode change 100644 => 100755 pkg/operator/ceph/cluster/mgr/mgr_test.go diff --git a/Documentation/ceph-cluster-crd.md b/Documentation/ceph-cluster-crd.md old mode 100644 new mode 100755 index 036851ea2..a3774eae8 --- a/Documentation/ceph-cluster-crd.md +++ b/Documentation/ceph-cluster-crd.md @@ -52,6 +52,7 @@ Settings can be specified at the global level to apply to the cluster as a whole If this value is empty, each pod will get an ephemeral directory to store their config files that is tied to the lifetime of the pod running on that node. More details can be found in the Kubernetes [empty dir docs](https://kubernetes.io/docs/concepts/storage/volumes/#emptydir). - `dashboard`: Settings for the Ceph dashboard. To view the dashboard in your browser see the [dashboard guide](ceph-dashboard.md). - `enabled`: Whether to enable the dashboard to view cluster status + - `urlPrefix`: Allows to serve the dashboard under a subpath (useful when you are accessing the dashboard via a reverse proxy) - `serviceAccount`: The service account under which the OSD pods will run that will give access to ConfigMaps in the cluster's namespace. If not set, the default of `rook-ceph-cluster` will be used. - `network`: The network settings for the cluster - `hostNetwork`: uses network of the hosts instead of using the SDN below the containers. diff --git a/Documentation/ceph-dashboard.md b/Documentation/ceph-dashboard.md old mode 100644 new mode 100755 index 7363bc8f9..070c32c11 --- a/Documentation/ceph-dashboard.md +++ b/Documentation/ceph-dashboard.md @@ -49,6 +49,16 @@ To retrieve the generated password, you can run the following: kubectl -n rook-ceph get secret rook-ceph-dashboard-password -o yaml | grep "password:" | awk '{print $2}' | base64 --decode ``` +## Configure the Dashboard + +If you are accessing the dashboard via a reverse proxy, you may wish to serve it under a URL prefix. +To get the dashboard to use hyperlinks that include your prefix, you can set the `urlPrefix` setting: +```yaml + spec: + dashboard: + urlPrefix: /ceph-dashboard +``` + ## Viewing the Dashboard External to the Cluster Commonly you will want to view the dashboard from outside the cluster. For example, on a development machine with the diff --git a/cluster/examples/kubernetes/ceph/cluster.yaml b/cluster/examples/kubernetes/ceph/cluster.yaml old mode 100644 new mode 100755 index 8e7049782..76a60d7d4 --- a/cluster/examples/kubernetes/ceph/cluster.yaml +++ b/cluster/examples/kubernetes/ceph/cluster.yaml @@ -78,6 +78,8 @@ spec: # enable the ceph dashboard for viewing cluster status dashboard: enabled: true + # serve the dashboard under a subpath (useful when you are accessing the dashboard via a reverse proxy) + # urlPrefix: /ceph-dashboard network: # toggle to use hostNetwork hostNetwork: false diff --git a/cluster/examples/kubernetes/ceph/operator.yaml b/cluster/examples/kubernetes/ceph/operator.yaml old mode 100644 new mode 100755 index 9eb17551f..d8b91679f --- a/cluster/examples/kubernetes/ceph/operator.yaml +++ b/cluster/examples/kubernetes/ceph/operator.yaml @@ -36,6 +36,8 @@ spec: properties: enabled: type: boolean + urlPrefix: + type: string dataDirHostPath: pattern: ^/(\S+) type: string diff --git a/pkg/apis/ceph.rook.io/v1beta1/types.go b/pkg/apis/ceph.rook.io/v1beta1/types.go old mode 100644 new mode 100755 index c7224680f..94fc44d40 --- a/pkg/apis/ceph.rook.io/v1beta1/types.go +++ b/pkg/apis/ceph.rook.io/v1beta1/types.go @@ -92,6 +92,8 @@ type CephVersionSpec struct { type DashboardSpec struct { // Whether to enable the dashboard Enabled bool `json:"enabled,omitempty"` + // A prefix for all URLs to use the dashboard with a reverse proxy + UrlPrefix string `json:"urlPrefix,omitempty"` } type ClusterStatus struct { diff --git a/pkg/daemon/ceph/client/mgr.go b/pkg/daemon/ceph/client/mgr.go old mode 100644 new mode 100755 index 42cd7d429..0912e4d37 --- a/pkg/daemon/ceph/client/mgr.go +++ b/pkg/daemon/ceph/client/mgr.go @@ -2,7 +2,9 @@ package client import ( "fmt" + "strings" + cephv1beta1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1beta1" "github.com/rook/rook/pkg/clusterd" ) @@ -16,6 +18,40 @@ func MgrDisableModule(context *clusterd.Context, clusterName, name string) error return enableModule(context, clusterName, name, false, "disable") } +func MgrSetConfig(context *clusterd.Context, clusterName, cephVersionName, key, val string) (bool, error) { + var getArgs, setArgs []string + + if cephVersionName == cephv1beta1.Luminous || cephVersionName == "" { + getArgs = append(getArgs, "config-key", "get", key) + if val == "" { + setArgs = append(setArgs, "config-key", "del", key) + } else { + setArgs = append(setArgs, "config-key", "set", key, val) + } + } else { + getArgs = append(getArgs, "config", "get", "mgr.", key) + if val == "" { + setArgs = append(setArgs, "config", "rm", "mgr", key) + } else { + setArgs = append(setArgs, "config", "set", "mgr", key, val) + } + } + + // Retrieve previous value to monitor changes + var prevVal string + buf, err := ExecuteCephCommand(context, clusterName, getArgs) + if err == nil { + prevVal = strings.TrimSpace(string(buf)) + } + + if _, err := ExecuteCephCommand(context, clusterName, setArgs); err != nil { + return false, fmt.Errorf("failed to set mgr config key %s to \"%s\": %+v", key, val, err) + } + + hasChanged := prevVal != val + return hasChanged, nil +} + func enableModule(context *clusterd.Context, clusterName, name string, force bool, action string) error { args := []string{"mgr", "module", action, name} if force { diff --git a/pkg/operator/ceph/cluster/cluster.go b/pkg/operator/ceph/cluster/cluster.go old mode 100644 new mode 100755 index b8094d419..d920f2c9c --- a/pkg/operator/ceph/cluster/cluster.go +++ b/pkg/operator/ceph/cluster/cluster.go @@ -267,6 +267,10 @@ func clusterChanged(oldCluster, newCluster cephv1beta1.ClusterSpec, clusterRef * logger.Infof("dashboard enabled has changed from %t to %t", oldCluster.Dashboard.Enabled, newCluster.Dashboard.Enabled) changeFound = true } + if oldCluster.Dashboard.UrlPrefix != newCluster.Dashboard.UrlPrefix { + logger.Infof("dashboard url prefix has changed from \"%s\" to \"%s\"", oldCluster.Dashboard.UrlPrefix, newCluster.Dashboard.UrlPrefix) + changeFound = true + } if oldCluster.Mon.Count != newCluster.Mon.Count { logger.Infof("number of mons have changed from %d to %d. The health check will update the mons...", oldCluster.Mon.Count, newCluster.Mon.Count) diff --git a/pkg/operator/ceph/cluster/mgr/dashboard.go b/pkg/operator/ceph/cluster/mgr/dashboard.go old mode 100644 new mode 100755 index 3d3572e6a..061bfd1b3 --- a/pkg/operator/ceph/cluster/mgr/dashboard.go +++ b/pkg/operator/ceph/cluster/mgr/dashboard.go @@ -53,8 +53,8 @@ func init() { func (c *Cluster) configureDashboard(port int) error { // enable or disable the dashboard module - if err := c.configureDashboardModule(); err != nil { - return fmt.Errorf("failed to enable mgr dashboard module. %+v", err) + if err := c.toggleDashboardModule(); err != nil { + return err } dashboardService := c.makeDashboardService(appName, port) @@ -80,15 +80,19 @@ func (c *Cluster) configureDashboard(port int) error { } // Ceph docs about the dashboard module: http://docs.ceph.com/docs/luminous/mgr/dashboard/ -func (c *Cluster) configureDashboardModule() error { +func (c *Cluster) toggleDashboardModule() 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.initializeDashboard(); err != nil { + if err := c.initializeSecureDashboard(); err != nil { return fmt.Errorf("failed to initialize dashboard. %+v", err) } + + if err := c.configureDashboardModule(); 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) @@ -97,7 +101,19 @@ func (c *Cluster) configureDashboardModule() error { return nil } -func (c *Cluster) initializeDashboard() error { +func (c *Cluster) configureDashboardModule() error { + hasChanged, err := client.MgrSetConfig(c.context, c.Namespace, c.cephVersion.Name, "mgr/dashboard/url_prefix", c.dashboard.UrlPrefix) + if err != nil { + return err + } + if hasChanged { + logger.Infof("dashboard config has changed") + return c.restartDashboard() + } + return nil +} + +func (c *Cluster) initializeSecureDashboard() error { if c.cephVersion.Name == cephv1beta1.Luminous || c.cephVersion.Name == "" { logger.Infof("skipping cert and user configuration on luminous") return nil diff --git a/pkg/operator/ceph/cluster/mgr/mgr_test.go b/pkg/operator/ceph/cluster/mgr/mgr_test.go old mode 100644 new mode 100755 index 109042d41..fcf0b49cb --- a/pkg/operator/ceph/cluster/mgr/mgr_test.go +++ b/pkg/operator/ceph/cluster/mgr/mgr_test.go @@ -53,6 +53,11 @@ func TestStartMGR(t *testing.T) { assert.Nil(t, err) validateStart(t, c) + c.dashboard.UrlPrefix = "/test" + err = c.Start() + assert.Nil(t, err) + validateStart(t, c) + // starting again with more replicas c.Replicas = 3 c.dashboard.Enabled = false