mgr: add hostNetwork field to Manager spec

Add optinal hostnetwork field for Ceph Mgr to control whether the
mgr pods to run in host network. If unset, this defaults to
cluster level Network.HostNetwork value. This particulary useful
when user wants to restrict ceph mgr pod from exposing metrics
port in all host interfaces.

Signed-off-by: Sunnatillo <sunnat.samadov@est.tech>
(cherry picked from commit b9a4685f65)
This commit is contained in:
Sunnatillo authored and Mergify committed 2025-11-06 16:29:17 +00:00
1 parent 8269cf60ae
commit 638cc3a503
7 files changed
+107 -2

No files matched your search

+12
View File
@@ -9113,6 +9113,18 @@ bool
<p>Modules is the list of ceph manager modules to enable/disable</p>
</td>
</tr>
<tr>
<td>
<code>hostNetwork</code><br/>
<em>
bool
</em>
</td>
<td>
<em>(Optional)</em>
<p>Whether host networking is enabled for the Ceph Mgr. If not set, the network settings from CephCluster.spec.networking will be applied.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="ceph.rook.io/v1.Migration">Migration
@@ -2126,6 +2126,9 @@ spec:
maximum: 5
minimum: 0
type: integer
hostNetwork:
description: Whether host networking is enabled for the Ceph Mgr. If not set, the network settings from CephCluster.spec.networking will be applied.
type: boolean
modules:
description: Modules is the list of ceph manager modules to enable/disable
items:
+3
View File
@@ -2124,6 +2124,9 @@ spec:
maximum: 5
minimum: 0
type: integer
hostNetwork:
description: Whether host networking is enabled for the Ceph Mgr. If not set, the network settings from CephCluster.spec.networking will be applied.
type: boolean
modules:
description: Modules is the list of ceph manager modules to enable/disable
items:
+3
View File
@@ -854,6 +854,9 @@ type MgrSpec struct {
// +optional
// +nullable
Modules []Module `json:"modules,omitempty"`
// Whether host networking is enabled for the Ceph Mgr. If not set, the network settings from CephCluster.spec.networking will be applied.
// +optional
HostNetwork *bool `json:"hostNetwork,omitempty"`
}
// Module represents mgr modules that the user wants to enable or disable
@@ -3338,6 +3338,11 @@ func (in *MgrSpec) DeepCopyInto(out *MgrSpec) {
*out = make([]Module, len(*in))
copy(*out, *in)
}
if in.HostNetwork != nil {
in, out := &in.HostNetwork, &out.HostNetwork
*out = new(bool)
**out = **in
}
return
}
+12 -2
View File
@@ -48,7 +48,7 @@ func (c *Cluster) makeDeployment(mgrConfig *mgrConfig) (*apps.Deployment, error)
adminKeyringVol, _ := keyring.Volume().Admin(), keyring.VolumeMount().Admin()
volumes = append(volumes, adminKeyringVol)
}
hostNetwork := isHostNetworkEnabled(c)
podSpec := v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: mgrConfig.ResourceName,
@@ -65,7 +65,7 @@ func (c *Cluster) makeDeployment(mgrConfig *mgrConfig) (*apps.Deployment, error)
ServiceAccountName: serviceAccountName,
RestartPolicy: v1.RestartPolicyAlways,
Volumes: volumes,
HostNetwork: c.spec.Network.IsHost(),
HostNetwork: hostNetwork,
PriorityClassName: cephv1.GetMgrPriorityClassName(c.spec.PriorityClassNames),
},
}
@@ -396,3 +396,13 @@ func (c *Cluster) buildSelectorLabels(labels map[string]string) map[string]strin
selectorLabels["mgr_role"] = "active"
return selectorLabels
}
// isHostNetworkEnabled checks if hostNetwork field is set in mgr spec, if not
// gets it from Cluster Spec
func isHostNetworkEnabled(c *Cluster) bool {
hostNetwork := c.spec.Network.IsHost()
if c.spec.Mgr.HostNetwork != nil {
hostNetwork = *c.spec.Mgr.HostNetwork
}
return hostNetwork
}
@@ -191,3 +191,72 @@ func TestApplyPrometheusAnnotations(t *testing.T) {
assert.Equal(t, 1, len(c.spec.Annotations))
assert.Equal(t, 0, len(d.ObjectMeta.Annotations))
}
func TestMgrNetwork(t *testing.T) {
clientset := optest.New(t, 1)
ownerInfo := cephclient.NewMinimumOwnerInfoWithOwnerRef()
clusterInfo := &cephclient.ClusterInfo{Namespace: "ns", FSID: "myfsid", OwnerInfo: ownerInfo}
clusterInfo.SetName("test")
ptrToFalse := false
ptrToTrue := true
mgrTestConfig := mgrConfig{
DaemonID: "a",
ResourceName: "mgr-a",
DataPathMap: config.NewStatelessDaemonDataPathMap(config.MgrType, "a", "rook-ceph", "/var/lib/rook/"),
}
t.Run("cluster HostNetwork true, manager Hostnetwork false", func(t *testing.T) {
clusterSpec := cephv1.ClusterSpec{
Network: cephv1.NetworkSpec{HostNetwork: true},
Mgr: cephv1.MgrSpec{HostNetwork: &ptrToFalse},
}
c := New(&clusterd.Context{Clientset: clientset}, clusterInfo, clusterSpec, "myversion")
d, err := c.makeDeployment(&mgrTestConfig)
assert.NoError(t, err)
assert.NotNil(t, d)
assert.Equal(t, false, d.Spec.Template.Spec.HostNetwork)
})
t.Run("cluster hostNetwork true, manager hostnetwork true", func(t *testing.T) {
clusterSpec := cephv1.ClusterSpec{
Network: cephv1.NetworkSpec{HostNetwork: true},
Mgr: cephv1.MgrSpec{HostNetwork: &ptrToTrue},
}
c := New(&clusterd.Context{Clientset: clientset}, clusterInfo, clusterSpec, "myversion")
d, err := c.makeDeployment(&mgrTestConfig)
assert.NoError(t, err)
assert.NotNil(t, d)
assert.Equal(t, true, d.Spec.Template.Spec.HostNetwork)
})
t.Run("cluster hostNetwork false, manager hostnetwork empty", func(t *testing.T) {
clusterSpec := cephv1.ClusterSpec{
Network: cephv1.NetworkSpec{HostNetwork: false},
// Mgr: cephv1.MgrSpec{HostNetwork: &ptrToTrue},
}
c := New(&clusterd.Context{Clientset: clientset}, clusterInfo, clusterSpec, "myversion")
d, err := c.makeDeployment(&mgrTestConfig)
assert.NoError(t, err)
assert.NotNil(t, d)
assert.Equal(t, false, d.Spec.Template.Spec.HostNetwork)
})
t.Run("cluster hostNetwork true manager hostnetwork empty", func(t *testing.T) {
clusterSpec := cephv1.ClusterSpec{
Network: cephv1.NetworkSpec{HostNetwork: true},
// Mgr: cephv1.MgrSpec{HostNetwork: &ptrToTrue},
}
c := New(&clusterd.Context{Clientset: clientset}, clusterInfo, clusterSpec, "myversion")
d, err := c.makeDeployment(&mgrTestConfig)
assert.NoError(t, err)
assert.NotNil(t, d)
assert.Equal(t, true, d.Spec.Template.Spec.HostNetwork)
})
}