external: add support for config file in external mode

Now user can pass the cli flags using config file and
also command line argument,
if mentioned at both the place priority is given to
command line argument

Signed-off-by: parth-gr <partharora1010@gmail.com>
This commit is contained in:
parth-gr committed 2024-06-26 17:07:49 +05:30
1 parent e9b4c08ef9
commit 5387ef16fd
6 files changed
+117 -2

No files matched your search

+25 -1
View File
@@ -68,7 +68,8 @@ jobs:
run: |
toolbox=$(kubectl get pod -l app=rook-ceph-tools -n rook-ceph -o jsonpath='{.items[*].metadata.name}')
kubectl -n rook-ceph exec $toolbox -- mkdir -p /etc/ceph/test-data
kubectl -n rook-ceph cp tests/ceph-status-out $toolbox:/etc/ceph/test-data/
kubectl -n rook-ceph cp tests/external-cluster/ceph-status-out $toolbox:/etc/ceph/test-data/
kubectl -n rook-ceph cp tests/external-cluster/external-config.ini $toolbox:/etc/ceph/
kubectl -n rook-ceph cp deploy/examples/create-external-cluster-resources.py $toolbox:/etc/ceph
kubectl -n rook-ceph cp deploy/examples/create-external-cluster-resources-tests.py $toolbox:/etc/ceph
timeout 10 sh -c "until kubectl -n rook-ceph exec $toolbox -- python3 /etc/ceph/create-external-cluster-resources.py --rbd-data-pool-name replicapool; do echo 'waiting for script to succeed' && sleep 1; done"
@@ -267,6 +268,29 @@ jobs:
toolbox=$(kubectl get pod -l app=rook-ceph-tools -n rook-ceph -o jsonpath='{.items[*].metadata.name}')
kubectl -n rook-ceph exec $toolbox -- python3 /etc/ceph/create-external-cluster-resources.py --rbd-data-pool-name replicapool --v2-port-enable
- name: test config file flag
run: |
toolbox=$(kubectl get pod -l app=rook-ceph-tools -n rook-ceph -o jsonpath='{.items[*].metadata.name}')
# create `radosNamespace2` rados-namespace for `replicapool` rbd data-pool
kubectl -n rook-ceph exec $toolbox -- rbd namespace create replicapool/radosnamespace2
# validate `--rados-namespace`, config file value is radosnamespace2, cli arg flag is set to radosnamespace1 and no default is set
# command line arg should be used
kubectl -n rook-ceph exec $toolbox -- python3 /etc/ceph/create-external-cluster-resources.py --rbd-data-pool-name replicapool --rados-namespace radosnamespace1 --config-file /etc/ceph/external-config.ini
# validate the `rbd-data-pool-name`,config value set to replicapoolconfig, command line arg is not set and no default
# config value should be used
sed -i 's/replicapool/replicapoolconfig/' deploy/examples/pool-test.yaml
kubectl create -f deploy/examples/pool-test.yaml
kubectl wait --for='jsonpath={.status.phase}=Ready' Cephblockpool/replicapoolconfig -nrook-ceph
# create `radosNamespace2` rados-namespace for `replicapoolconfig` rbd data-pool
kubectl -n rook-ceph exec $toolbox -- rbd namespace create replicapoolconfig/radosnamespace2
kubectl -n rook-ceph exec $toolbox -- python3 /etc/ceph/create-external-cluster-resources.py --config-file /etc/ceph/external-config.ini
# validate the default `format`` flag, config file value is not set and no cmd line argument is set and default value is `Json``,
# default value should be used
kubectl -n rook-ceph exec $toolbox -- python3 /etc/ceph/create-external-cluster-resources.py --rbd-data-pool-name replicapool --config-file /etc/ceph/external-config.ini
- name: check-ownerreferences
run: tests/scripts/github-action-helper.sh check_ownerreferences
@@ -63,6 +63,32 @@ python3 create-external-cluster-resources.py --rbd-data-pool-name <pool_name> --
* `--topology-pools`: (optional) Comma-separated list of topology-constrained rbd pools
* `--topology-failure-domain-label`: (optional) K8s cluster failure domain label (example: zone, rack, or host) for the topology-pools that match the ceph domain
* `--topology-failure-domain-values`: (optional) Comma-separated list of the k8s cluster failure domain values corresponding to each of the pools in the `topology-pools` list
* `--config-file`: Path to the configuration file, Priority: command-line-args > config.ini values > default values
### Config-file
Use the config file to set the user configuration file, add the flag `--config-file` to set the file path.
Example:
`/config.ini`
```console
[Configurations]
format = bash
cephfs-filesystem-name = <filesystem-name>
rbd-data-pool-name = <pool_name>
...
```
```console
python3 create-external-cluster-resources.py --config-file /config.ini
```
!!! note
You can use both config file and other arguments at the same time
Priority: command-line-args > config.ini file values > default values
### Multi-tenancy
@@ -21,6 +21,7 @@ import argparse
import re
import subprocess
import hmac
import configparser
from hashlib import sha1 as sha
from os import linesep as LINESEP
from os import path
@@ -478,6 +479,15 @@ class RadosJSON:
+ "Upgrade flag should only be used to append new permissions to users, it shouldn't be used for changing user already applied permission, for example you shouldn't change in which pool user has access",
)
# Add command-line arguments
config_group = argP.add_argument_group("config")
config_group.add_argument(
"--config-file",
type=str,
help="Path to the configuration file, Priority: command-line-args > config.ini file values > default values",
required=False,
)
if args_to_parse:
assert (
type(args_to_parse) == list
@@ -617,10 +627,31 @@ class RadosJSON:
("-1"),
)
def parse_config_file(self, config_file):
config = configparser.ConfigParser()
config.read(config_file)
for arg in list(vars(self._arg_parser)):
# python treats flag-name as flag_name internally, so converting back to flag-name,
# so we can get those values from config file
argument = arg.replace("_", "-")
argumentValue = str(getattr(self._arg_parser, arg))
config_value = config.get("Configurations", argument, fallback=None)
# give priority to command line argument, if command line argument is not present use config.ini value,
if (
(str(sys.argv).find(argument) == -1)
and ((argumentValue is None) or (argumentValue is ""))
and ((config_value is not None) and (config_value is not ""))
):
self._arg_parser.__setattr__(arg, config_value)
return config
def __init__(self, arg_list=None):
self.out_map = {}
self._excluded_keys = set()
self._arg_parser = self.gen_arg_parser(args_to_parse=arg_list)
if self._arg_parser.config_file:
self.config = self.parse_config_file(self._arg_parser.config_file)
self._check_conflicting_options()
self.run_as_user = self._arg_parser.run_as_user
self.output_file = self._arg_parser.output
-1
View File
@@ -60,7 +60,6 @@ do.build:
@cp -r $(MANIFESTS_DIR)/monitoring $(BUILD_CONTEXT_DIR)/ceph-monitoring
@mkdir -p $(BUILD_CONTEXT_DIR)/rook-external/test-data
@cp $(MANIFESTS_DIR)/create-external-cluster-resources.* $(BUILD_CONTEXT_DIR)/rook-external/
@cp ../../tests/ceph-status-out $(BUILD_CONTEXT_DIR)/rook-external/test-data/
@cd $(BUILD_CONTEXT_DIR) && $(SED_IN_PLACE) 's|BASEIMAGE|$(BASEIMAGE)|g' Dockerfile
@if [ -z "$(BUILD_CONTAINER_IMAGE)" ]; then\
$(DOCKERCMD) build $(BUILD_ARGS) \
File renamed without changes.
@@ -0,0 +1,35 @@
[Configurations]
format =
verbose =
ceph-conf =
keyring =
run-as-user =
cluster-name =
k8s-cluster-name =
namespace =
rgw-pool-prefix =
restricted-auth-permission =
v2-port-enable =
output =
cephfs-filesystem-name =
cephfs-metadata-pool-name =
cephfs-data-pool-name =
rbd-data-pool-name = replicapoolconfig
alias-rbd-data-pool-name =
rgw-endpoint =
rgw-tls-cert-path =
rgw-skip-tls =
monitoring-endpoint =
monitoring-endpoint-port =
skip-monitoring-endpoint =
rbd-metadata-ec-pool-name =
dry-run =
rados-namespace = radosnamespace2
subvolume-group =
rgw-realm-name =
rgw-zone-name =
rgw-zonegroup-name =
topology-pool =
topology-failure-domain-label =
topology-failure-domain-values =
upgrade =