diff --git a/pkg/operator/ceph/cluster/mgr/config.go b/pkg/operator/ceph/cluster/mgr/config.go index 15cac9f4f..a7e28e355 100644 --- a/pkg/operator/ceph/cluster/mgr/config.go +++ b/pkg/operator/ceph/cluster/mgr/config.go @@ -26,7 +26,9 @@ import ( ) const ( - keyringTemplate = ` + // Ports below 1024 require extra privileges for binding inside a pod + minPortWithoutPrivileges = 1024 + keyringTemplate = ` [mgr.%s] key = %s caps mon = "allow profile mgr" @@ -42,19 +44,39 @@ type mgrConfig struct { DataPathMap *config.DataPathMap // location to store data in container } -func (c *Cluster) dashboardPort() int { +// dashboardInternalPort gets the port to be used by the service targetPort +// and the container ports on the mgr pod. +// If the port is greater than 1024, for backward compatibility the port and +// targetPort should be the same value. If the port is less than 1024, +// the internal port must use a higher port number. In that case, the internal +// port will be the default port numbers and only the public port will be +// the desired port in the cluster CR. +func (c *Cluster) dashboardInternalPort() int { + port := c.dashboardPublicPort() + if port <= minPortWithoutPrivileges { + // If the port is less than the allowed range, set it back to the default + return c.dashboardDefaultPort() + } + return port +} + +// dashboardPublicPort is the desired port to be exposed on the service +func (c *Cluster) dashboardPublicPort() int { if c.spec.Dashboard.Port == 0 { - // default port for HTTP/HTTPS - if c.spec.Dashboard.SSL { - return dashboardPortHTTPS - } else { - return dashboardPortHTTP - } + return c.dashboardDefaultPort() } // crd validates port >= 0 return c.spec.Dashboard.Port } +func (c *Cluster) dashboardDefaultPort() int { + // default port for HTTP/HTTPS + if c.spec.Dashboard.SSL { + return dashboardPortHTTPS + } + return dashboardPortHTTP +} + func (c *Cluster) generateKeyring(m *mgrConfig) (string, error) { user := fmt.Sprintf("mgr.%s", m.DaemonID) access := []string{"mon", "allow profile mgr", "mds", "allow *", "osd", "allow *"} diff --git a/pkg/operator/ceph/cluster/mgr/dashboard.go b/pkg/operator/ceph/cluster/mgr/dashboard.go index 2b1c69ed3..7abf6c25a 100644 --- a/pkg/operator/ceph/cluster/mgr/dashboard.go +++ b/pkg/operator/ceph/cluster/mgr/dashboard.go @@ -129,7 +129,7 @@ func (c *Cluster) configureDashboardModuleSettings(daemonID string) (bool, error hasChanged = hasChanged || changed // server port - port := strconv.Itoa(c.dashboardPort()) + port := strconv.Itoa(c.dashboardInternalPort()) changed, err = monStore.SetIfChanged(daemonID, "mgr/dashboard/server_port", port) if err != nil { return false, err diff --git a/pkg/operator/ceph/cluster/mgr/dashboard_test.go b/pkg/operator/ceph/cluster/mgr/dashboard_test.go index bea62ca63..965be9a6e 100644 --- a/pkg/operator/ceph/cluster/mgr/dashboard_test.go +++ b/pkg/operator/ceph/cluster/mgr/dashboard_test.go @@ -119,7 +119,7 @@ func TestStartSecureDashboard(t *testing.T) { } c := &Cluster{clusterInfo: clusterInfo, context: &clusterd.Context{Clientset: clientset, Executor: executor}, spec: cephv1.ClusterSpec{ - Dashboard: cephv1.DashboardSpec{Port: dashboardPortHTTP, Enabled: true, SSL: true}, + Dashboard: cephv1.DashboardSpec{Port: 443, Enabled: true, SSL: true}, CephVersion: cephv1.CephVersionSpec{Image: "quay.io/ceph/ceph:v15"}, }, } @@ -143,6 +143,8 @@ func TestStartSecureDashboard(t *testing.T) { svc, err := c.context.Clientset.CoreV1().Services(clusterInfo.Namespace).Get(ctx, "rook-ceph-mgr-dashboard", metav1.GetOptions{}) assert.Nil(t, err) assert.NotNil(t, svc) + assert.Equal(t, 443, int(svc.Spec.Ports[0].Port)) + assert.Equal(t, 8443, int(svc.Spec.Ports[0].TargetPort.IntVal)) // disable the dashboard c.spec.Dashboard.Enabled = false @@ -157,4 +159,28 @@ func TestStartSecureDashboard(t *testing.T) { assert.NotNil(t, err) assert.True(t, kerrors.IsNotFound(err)) assert.Nil(t, svc) + + // Set the port to something over 1024 and confirm the port and targetPort are the same + c.spec.Dashboard.Enabled = true + c.spec.Dashboard.Port = 1025 + err = c.configureDashboardService("a") + assert.Nil(t, err) + + svc, err = c.context.Clientset.CoreV1().Services(clusterInfo.Namespace).Get(ctx, "rook-ceph-mgr-dashboard", metav1.GetOptions{}) + assert.Nil(t, err) + assert.NotNil(t, svc) + assert.Equal(t, 1025, int(svc.Spec.Ports[0].Port)) + assert.Equal(t, 1025, int(svc.Spec.Ports[0].TargetPort.IntVal)) + + // Fall back to the default port + c.spec.Dashboard.Enabled = true + c.spec.Dashboard.Port = 0 + err = c.configureDashboardService("a") + assert.Nil(t, err) + + svc, err = c.context.Clientset.CoreV1().Services(clusterInfo.Namespace).Get(ctx, "rook-ceph-mgr-dashboard", metav1.GetOptions{}) + assert.Nil(t, err) + assert.NotNil(t, svc) + assert.Equal(t, 8443, int(svc.Spec.Ports[0].Port)) + assert.Equal(t, 8443, int(svc.Spec.Ports[0].TargetPort.IntVal)) } diff --git a/pkg/operator/ceph/cluster/mgr/spec.go b/pkg/operator/ceph/cluster/mgr/spec.go index c7d282f05..78aa7b9d9 100644 --- a/pkg/operator/ceph/cluster/mgr/spec.go +++ b/pkg/operator/ceph/cluster/mgr/spec.go @@ -32,6 +32,7 @@ import ( apps "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" ) const ( @@ -176,7 +177,7 @@ func (c *Cluster) makeMgrDaemonContainer(mgrConfig *mgrConfig) v1.Container { }, { Name: "dashboard", - ContainerPort: int32(c.dashboardPort()), + ContainerPort: int32(c.dashboardInternalPort()), Protocol: v1.ProtocolTCP, }, }, @@ -310,8 +311,11 @@ func (c *Cluster) makeDashboardService(name, activeDaemon string) (*v1.Service, Type: v1.ServiceTypeClusterIP, Ports: []v1.ServicePort{ { - Name: portName, - Port: int32(c.dashboardPort()), + Name: portName, + Port: int32(c.dashboardPublicPort()), + TargetPort: intstr.IntOrString{ + IntVal: int32(c.dashboardInternalPort()), + }, Protocol: v1.ProtocolTCP, }, },