|
43 | 43 | import prettytable |
44 | 44 | PARSER = argparse.ArgumentParser() |
45 | 45 | ARGS=None # just so that the linter is happy, ARGS is global already. |
| 46 | +DEFAULT_CSI_DIRECTORY_SHARDS = 11 |
46 | 47 |
|
47 | 48 | # -p pvc-test -k /home/.kube/config -n default -rn rook-ceph |
48 | 49 | PARSER.add_argument("-p", "--pvcname", default="", help="PVC name") |
|
66 | 67 | help="configmap name which holds the cephcsi configuration") |
67 | 68 | PARSER.add_argument("-cmn", "--configmapnamespace", default="default", |
68 | 69 | help="namespace where configmap exists") |
| 70 | +PARSER.add_argument("--instanceid", default="default", |
| 71 | + help="ceph-csi instance ID") |
69 | 72 |
|
70 | 73 |
|
71 | 74 | def list_pvc_vol_name_mapping(arg): |
@@ -187,34 +190,11 @@ def check_pv_name_in_rados(arg, image_id, pvc_name, pool_name, is_rbd): |
187 | 190 | validate pvc information in rados |
188 | 191 | """ |
189 | 192 | omapkey = f'csi.volume.{pvc_name}' |
190 | | - cmd = ['rados', 'getomapval', 'csi.volumes.default', |
191 | | - omapkey, "--pool", pool_name] |
192 | | - if not arg.userkey: |
193 | | - cmd += ["--id", arg.userid, "--key", arg.userkey] |
194 | | - if not is_rbd: |
195 | | - cmd += ["--namespace", "csi"] |
196 | | - if arg.toolboxdeployed is True: |
197 | | - kube = get_cmd_prefix(arg) |
198 | | - cmd = kube + cmd |
199 | | - with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as out: |
200 | | - stdout, stderr = out.communicate() |
201 | | - |
202 | | - if stderr is not None: |
203 | | - return False |
204 | | - name = b'' |
205 | | - lines = [x.strip() for x in stdout.split(b"\n")] |
206 | | - for line in lines: |
207 | | - if b' ' not in line: |
208 | | - continue |
209 | | - if b'value' in line and b'bytes' in line: |
210 | | - continue |
211 | | - part = re.findall(br'[A-Za-z0-9\-]+', line) |
212 | | - if part: |
213 | | - name += part[-1] |
214 | | - if name.decode() != image_id: |
| 193 | + omapobj = f'csi.volumes.{arg.instanceid}' |
| 194 | + name = get_val_from_csi_directory(omapobj, omapkey, pool_name, is_rbd) |
| 195 | + if name != image_id: |
215 | 196 | if arg.debug: |
216 | | - decoded_name = name.decode() |
217 | | - print(f"expected image Id {image_id} found Id in rados {decoded_name}") |
| 197 | + print(f"expected image Id {image_id} found Id in rados {name}") |
218 | 198 | return False |
219 | 199 | return True |
220 | 200 |
|
@@ -643,15 +623,43 @@ def get_val_from_omap(obj, key, pool, is_rbd, regex=br'[A-Za-z0-9\-]+'): |
643 | 623 |
|
644 | 624 | return result.decode() |
645 | 625 |
|
| 626 | +def fnv32a(key): |
| 627 | + """ |
| 628 | + Returns the 32-bit FNV-1a hash for a string. |
| 629 | + """ |
| 630 | + hval = 0x811c9dc5 |
| 631 | + for byte in key.encode(): |
| 632 | + hval ^= byte |
| 633 | + hval = (hval * 0x01000193) & 0xffffffff |
| 634 | + return hval |
| 635 | + |
| 636 | +def get_csi_directory_shard(base_oid, key): |
| 637 | + """ |
| 638 | + Returns the sharded csiDirectory object for a key. |
| 639 | + """ |
| 640 | + shard = fnv32a(key) % DEFAULT_CSI_DIRECTORY_SHARDS |
| 641 | + return f"{base_oid}.{shard}" |
| 642 | + |
| 643 | +def get_val_from_csi_directory(base_oid, key, pool, is_rbd): |
| 644 | + """ |
| 645 | + Retrieve a value from sharded csiDirectory with legacy oid fallback. |
| 646 | + """ |
| 647 | + shard_oid = get_csi_directory_shard(base_oid, key) |
| 648 | + val = get_val_from_omap(shard_oid, key, pool, is_rbd) |
| 649 | + if val: |
| 650 | + return val |
| 651 | + |
| 652 | + return get_val_from_omap(base_oid, key, pool, is_rbd) |
| 653 | + |
646 | 654 | def check_snap_content_name_in_rados(snap_uuid, snap_content_name, pool, is_rbd): |
647 | 655 | """ |
648 | | - Validates if snapshot content name is listed in csi.snaps.default omap |
| 656 | + Validates if snapshot content name is listed in csi.snaps omap |
649 | 657 | """ |
650 | 658 | snap_content_name = snap_content_name.replace("snapcontent", "snapshot") |
651 | | - omap_obj = "csi.snaps.default" |
| 659 | + omap_obj = f"csi.snaps.{ARGS.instanceid}" |
652 | 660 | omap_key = f'csi.snap.{snap_content_name}' |
653 | 661 |
|
654 | | - val = get_val_from_omap(omap_obj, omap_key, pool, is_rbd) |
| 662 | + val = get_val_from_csi_directory(omap_obj, omap_key, pool, is_rbd) |
655 | 663 | if val != snap_uuid: |
656 | 664 | if ARGS.debug: |
657 | 665 | print(f"expected image Id {snap_uuid} found Id in rados {val}") |
|
0 commit comments