Skip to content

Commit 0e1662c

Browse files
util: improve static PV handling
- Clarify static PV must not specify controllerPublishSecretRef. - Improve handling volumeHandle validation error. The related discussion: #6290 Signed-off-by: Satoru Takeuchi <satoru.takeuchi@gmail.com>
1 parent 62342db commit 0e1662c

5 files changed

Lines changed: 64 additions & 61 deletions

File tree

docs/static-pvc.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ spec:
8383
volumeMode: Filesystem
8484
```
8585
86+
> [!note]
87+
> `controllerPublishSecretRef` must not be specified for static RBD PVs.
88+
8689
### RBD Volume Attributes in PV
8790

8891
Below table explains the list of volume attributes can be set when creating a
@@ -278,6 +281,9 @@ spec:
278281
volumeMode: Filesystem
279282
```
280283
284+
> [!note]
285+
> `controllerPublishSecretRef` must not be specified for static CephFS PVs.
286+
281287
### Node stage secret ref in CephFS PV
282288

283289
For static CephFS PV to work, userID and userKey needs to be specified in the

internal/cephfs/controllerserver.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,21 +1155,12 @@ func (cs *ControllerServer) getServiceAccountRestriction(
11551155
req *csi.ControllerPublishVolumeRequest,
11561156
) (string, error) {
11571157
volumeID := req.GetVolumeId()
1158-
secrets := req.GetSecrets()
1159-
1160-
if secrets == nil {
1161-
secretName, secretNamespace, err := util.GetControllerPublishSecretRef(volumeID, util.CephFsType)
1162-
if err != nil {
1163-
log.WarningLog(ctx, "controller publish secret not found: %v", err)
1164-
1165-
return "", nil
1166-
}
1167-
1168-
secrets, err = k8s.GetSecret(secretName, secretNamespace)
1169-
if err != nil {
1170-
return "", status.Errorf(codes.Internal,
1171-
"failed to get controller publish secret from k8s: %v", err)
1172-
}
1158+
secrets, skip, err := util.GetControllerPublishSecrets(ctx, req.GetSecrets(), volumeID, util.CephFsType)
1159+
if skip {
1160+
return "", nil
1161+
}
1162+
if err != nil {
1163+
return "", status.Errorf(codes.Internal, "%v", err)
11731164
}
11741165

11751166
volOptions, _, err := store.NewVolumeOptionsFromVolID(ctx, volumeID, nil, secrets, cs.ClusterName)
@@ -1228,21 +1219,12 @@ func (cs *ControllerServer) ControllerUnpublishVolume(
12281219
}
12291220
defer cs.VolumeLocks.Release(volumeId)
12301221

1231-
secrets := req.GetSecrets()
1232-
if secrets == nil {
1233-
secretName, secretNamespace, err := util.GetControllerPublishSecretRef(volumeId, util.CephFsType)
1234-
if err != nil {
1235-
log.WarningLog(ctx, "controller publish secret not found: %v", err)
1236-
1237-
// If the secret is not found, return success to not break for older PVs
1238-
// without controller-publish secrets.
1239-
return &csi.ControllerUnpublishVolumeResponse{}, nil
1240-
}
1241-
1242-
secrets, err = k8s.GetSecret(secretName, secretNamespace)
1243-
if err != nil {
1244-
return nil, fmt.Errorf("failed to get controller publish secret from k8s: %w", err)
1245-
}
1222+
secrets, skip, err := util.GetControllerPublishSecrets(ctx, req.GetSecrets(), volumeId, util.CephFsType)
1223+
if skip {
1224+
return &csi.ControllerUnpublishVolumeResponse{}, nil
1225+
}
1226+
if err != nil {
1227+
return nil, status.Errorf(codes.Internal, "%v", err)
12461228
}
12471229

12481230
volOptions, _, err := store.NewVolumeOptionsFromVolID(ctx, volumeId, nil, secrets, cs.ClusterName)

internal/rbd/controllerserver.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,21 +1755,12 @@ func (cs *ControllerServer) getServiceAccountRestriction(
17551755
req *csi.ControllerPublishVolumeRequest,
17561756
) (string, error) {
17571757
volumeID := req.GetVolumeId()
1758-
secrets := req.GetSecrets()
1759-
1760-
if secrets == nil {
1761-
secretName, secretNamespace, err := util.GetControllerPublishSecretRef(volumeID, util.RBDType)
1762-
if err != nil {
1763-
log.WarningLog(ctx, "controller publish secret not found: %v", err)
1764-
1765-
return "", nil
1766-
}
1767-
1768-
secrets, err = k8s.GetSecret(secretName, secretNamespace)
1769-
if err != nil {
1770-
return "", status.Errorf(codes.Internal,
1771-
"failed to get controller publish secret from k8s: %v", err)
1772-
}
1758+
secrets, skip, err := util.GetControllerPublishSecrets(ctx, req.GetSecrets(), volumeID, util.RBDType)
1759+
if skip {
1760+
return "", nil
1761+
}
1762+
if err != nil {
1763+
return "", status.Errorf(codes.Internal, "%v", err)
17731764
}
17741765

17751766
cr, err := util.NewUserCredentials(secrets)
@@ -1827,21 +1818,12 @@ func (cs *ControllerServer) ControllerUnpublishVolume(
18271818
}
18281819
defer cs.VolumeLocks.Release(volumeId)
18291820

1830-
secrets := req.GetSecrets()
1831-
if secrets == nil {
1832-
secretName, secretNamespace, err := util.GetControllerPublishSecretRef(volumeId, util.RBDType)
1833-
if err != nil {
1834-
log.WarningLog(ctx, "controller publish secret not found: %v", err)
1835-
1836-
// If the secret is not found, return success to not break for older PVs
1837-
// without controller-publish secrets.
1838-
return &csi.ControllerUnpublishVolumeResponse{}, nil
1839-
}
1840-
1841-
secrets, err = k8s.GetSecret(secretName, secretNamespace)
1842-
if err != nil {
1843-
return nil, fmt.Errorf("failed to get controller publish secret from k8s: %w", err)
1844-
}
1821+
secrets, skip, err := util.GetControllerPublishSecrets(ctx, req.GetSecrets(), volumeId, util.RBDType)
1822+
if skip {
1823+
return &csi.ControllerUnpublishVolumeResponse{}, nil
1824+
}
1825+
if err != nil {
1826+
return nil, status.Errorf(codes.Internal, "%v", err)
18451827
}
18461828

18471829
credentials, err := util.NewAdminCredentials(secrets)

internal/util/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ var (
3636
ErrMissingConfigForMonitor = errors.New("missing configuration of cluster ID for monitor")
3737
// ErrConfigNotFound is returned when no configuration is found for a cluster ID.
3838
ErrConfigNotFound = errors.New("missing configuration for cluster ID")
39+
// ErrInvalidVolID is returned when the volume ID cannot be decomposed into a CSI identifier.
40+
ErrInvalidVolID = errors.New("invalid volume ID")
3941
)

internal/util/util.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
mount "k8s.io/mount-utils"
3535

3636
"github.com/ceph/ceph-csi/internal/util/k8s"
37+
"github.com/ceph/ceph-csi/internal/util/log"
3738
)
3839

3940
// Driver types to identify type of driver running.
@@ -349,7 +350,8 @@ func GetControllerPublishSecretRef(volumeId, driverType string) (string, string,
349350
)
350351
err := vi.DecomposeCSIID(volumeId)
351352
if err != nil {
352-
return secretName, secretNamespace, fmt.Errorf("failed to decode volume ID (%s): %w", volumeId, err)
353+
return secretName, secretNamespace, fmt.Errorf("failed to decode volume ID (%s): %w",
354+
volumeId, errors.Join(ErrInvalidVolID, err))
353355
}
354356

355357
secretName, secretNamespace, err = getControllerPublishSecretRef(vi.ClusterID, driverType)
@@ -395,6 +397,35 @@ func GetControllerPublishSecretRef(volumeId, driverType string) (string, string,
395397
return secretName, secretNamespace, nil
396398
}
397399

400+
// GetControllerPublishSecrets resolves secrets for a controller publish/unpublish operation.
401+
// If reqSecrets is non-nil, return it. Otherwise secrets are fetched from the CSI config.
402+
// When the second return value is true the caller should return early with no error.
403+
func GetControllerPublishSecrets(
404+
ctx context.Context,
405+
reqSecrets map[string]string,
406+
volumeID, driverType string,
407+
) (map[string]string, bool, error) {
408+
if reqSecrets != nil {
409+
return reqSecrets, false, nil
410+
}
411+
secretName, secretNamespace, err := GetControllerPublishSecretRef(volumeID, driverType)
412+
if errors.Is(err, ErrInvalidVolID) || errors.Is(err, ErrConfigNotFound) {
413+
// Possibly the volume is a static/older volume. In this case, we have nothing to do.
414+
// Even if it's not a static/older volume, we should skip handling it because
415+
// we have no way to process handling anyway.
416+
log.WarningLog(ctx, "should skip handling this volume: %v", err)
417+
return nil, true, nil
418+
}
419+
if err != nil {
420+
return nil, false, fmt.Errorf("failed to get controller publish secret ref: %w", err)
421+
}
422+
secrets, err := k8s.GetSecret(secretName, secretNamespace)
423+
if err != nil {
424+
return nil, false, fmt.Errorf("failed to get controller publish secret from k8s: %w", err)
425+
}
426+
return secrets, false, nil
427+
}
428+
398429
func getControllerPublishSecretRef(clusterId, driverType string) (string, string, error) {
399430
var (
400431
err error

0 commit comments

Comments
 (0)