2024-07-24 22:10:57 +05:30
|
|
|
/*
|
|
|
|
|
Copyright 2024 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 csi
|
|
|
|
|
|
|
|
|
|
import (
|
2024-11-20 17:09:06 +05:30
|
|
|
"os"
|
2025-08-25 11:55:00 -06:00
|
|
|
"strings"
|
2024-11-20 17:09:06 +05:30
|
|
|
|
2024-07-24 22:10:57 +05:30
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
|
|
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
|
|
|
|
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
|
2025-08-25 11:55:00 -06:00
|
|
|
"github.com/rook/rook/pkg/operator/ceph/cluster/osd/topology"
|
2026-01-09 15:54:24 -07:00
|
|
|
cephver "github.com/rook/rook/pkg/operator/ceph/version"
|
2024-11-20 17:09:06 +05:30
|
|
|
"github.com/rook/rook/pkg/operator/k8sutil"
|
2024-07-24 22:10:57 +05:30
|
|
|
|
2025-07-10 12:36:57 +05:30
|
|
|
csiopv1 "github.com/ceph/ceph-csi-operator/api/v1"
|
2024-07-24 22:10:57 +05:30
|
|
|
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func CreateUpdateCephConnection(c client.Client, clusterInfo *cephclient.ClusterInfo, clusterSpec cephv1.ClusterSpec) error {
|
2025-08-07 12:06:50 -06:00
|
|
|
// Create a cephconnection CR in the namespace of the operator, with the namespace of the cephcluster CR
|
2025-07-10 12:36:57 +05:30
|
|
|
csiCephConnection := &csiopv1.CephConnection{}
|
2025-08-07 12:06:50 -06:00
|
|
|
csiCephConnection.Name = clusterInfo.NamespacedName().Namespace
|
2024-11-20 17:09:06 +05:30
|
|
|
csiCephConnection.Namespace = os.Getenv(k8sutil.PodNamespaceEnvVar)
|
2025-08-07 12:06:50 -06:00
|
|
|
logger.Infof("Configuring ceph connection CR %q in namespace %q", csiCephConnection.Name, csiCephConnection.Namespace)
|
2024-07-24 22:10:57 +05:30
|
|
|
|
2025-08-11 07:31:52 +02:00
|
|
|
spec, err := generateCephConnSpec(c, clusterInfo, clusterSpec)
|
2024-07-24 22:10:57 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to set ceph connection CR %q in namespace %q", csiCephConnection.Name, clusterInfo.Namespace)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = c.Get(clusterInfo.Context, types.NamespacedName{Name: csiCephConnection.Name, Namespace: csiCephConnection.Namespace}, csiCephConnection)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if kerrors.IsNotFound(err) {
|
|
|
|
|
csiCephConnection.Spec = spec
|
|
|
|
|
err = c.Create(clusterInfo.Context, csiCephConnection)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to create ceph connection CR %q", csiCephConnection.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Infof("Successfully created ceph connection CR %q", csiCephConnection.Name)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return errors.Wrap(err, "failed to get ceph connection CR")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
csiCephConnection.Spec = spec
|
|
|
|
|
err = c.Update(clusterInfo.Context, csiCephConnection)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrapf(err, "failed to update ceph connection CR %q", csiCephConnection.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Infof("Successfully updated ceph connection CR %q", csiCephConnection.Name)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-11 07:31:52 +02:00
|
|
|
func generateCephConnSpec(c client.Client, clusterInfo *cephclient.ClusterInfo, clusterSpec cephv1.ClusterSpec) (csiopv1.CephConnectionSpec, error) {
|
|
|
|
|
csiClusterConnSpec := csiopv1.CephConnectionSpec{}
|
|
|
|
|
|
2026-01-09 15:54:24 -07:00
|
|
|
if ReadAffinityEnabled(clusterSpec.CSI.ReadAffinity.Enabled, clusterInfo.CephVersion) {
|
2025-08-25 11:55:00 -06:00
|
|
|
crushLabels := clusterSpec.CSI.ReadAffinity.CrushLocationLabels
|
|
|
|
|
if len(crushLabels) == 0 {
|
|
|
|
|
logger.Debug("using default crush topology labels")
|
|
|
|
|
crushLabels = strings.Split(topology.GetDefaultTopologyLabels(), ",")
|
|
|
|
|
}
|
2025-08-11 07:31:52 +02:00
|
|
|
csiClusterConnSpec.ReadAffinity = &csiopv1.ReadAffinitySpec{
|
2025-08-25 11:55:00 -06:00
|
|
|
CrushLocationLabels: crushLabels,
|
2024-08-07 16:17:03 +05:30
|
|
|
}
|
2024-07-24 22:10:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cephRBDMirrorList := &cephv1.CephRBDMirrorList{}
|
|
|
|
|
err := c.List(clusterInfo.Context, cephRBDMirrorList, &client.ListOptions{Namespace: clusterInfo.Namespace})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return csiClusterConnSpec, errors.Wrapf(err, "failed to list CephRBDMirror resource")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(cephRBDMirrorList.Items) == 0 {
|
|
|
|
|
logger.Debug("no ceph CephRBDMirror found")
|
|
|
|
|
} else {
|
|
|
|
|
// Currently, only single RBD mirror is supported
|
|
|
|
|
csiClusterConnSpec.RbdMirrorDaemonCount = cephRBDMirrorList.Items[0].Spec.Count
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-26 16:41:53 +05:30
|
|
|
csiClusterConnSpec.Monitors = MonEndpoints(clusterInfo.AllMonitors(), clusterSpec.RequireMsgr2())
|
2024-07-24 22:10:57 +05:30
|
|
|
return csiClusterConnSpec, nil
|
|
|
|
|
}
|
2026-01-09 15:54:24 -07:00
|
|
|
|
|
|
|
|
func ReadAffinityEnabled(desiredEnabled bool, version cephver.CephVersion) bool {
|
|
|
|
|
if !desiredEnabled {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
logger.Debugf("checking if read affinity is supported for Ceph version %q", version.String())
|
|
|
|
|
// Read affinity causes corruption on Ceph v20.2.0, so always disable it for that version
|
|
|
|
|
if version.Major == 20 && version.Minor == 2 && version.Extra == 0 {
|
|
|
|
|
logger.Warningf("read affinity disabled for Ceph version v20.2.0")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|