Skip to content

Commit

Permalink
add dry run and output (#36)
Browse files Browse the repository at this point in the history
* add dry run and output

* Code review updates
  • Loading branch information
mmulholla authored Jul 12, 2023
1 parent b5b55ca commit 8544f34
Show file tree
Hide file tree
Showing 23 changed files with 865 additions and 174 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/pr-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ jobs:
- name: Lint python
run: make lint

- name: run dry-run
run: make test-dry-run

- name: run test
run: make test-local
run: make test-local-no-setup

- name: run test with users
run: make test-users

- name: run test with output
run: make test-output

11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ test-released:
test-users: setup-test create-users
$(PYTHON_VENV_DIR)/bin/primazatest -u -i $(OUTPUT_DIR)/users -p $(PYTHON_VENV_DIR) -e $(WORKER_CONFIG_FILE) -f $(PRIMAZA_CONFIG_FILE) -c $(KUBE_KIND_CLUSTER_JOIN_NAME) -m $(KUBE_KIND_CLUSTER_TENANT_NAME) -a $(APPLICATION_AGENT_CONFIG_FILE) -s $(SERVICE_AGENT_CONFIG_FILE)

.PHONY: test-dry-run
test-dry-run: setup-test
$(PYTHON_VENV_DIR)/bin/primazatest -d -p $(PYTHON_VENV_DIR) -e $(WORKER_CONFIG_FILE) -f $(PRIMAZA_CONFIG_FILE) -c $(KUBE_KIND_CLUSTER_JOIN_NAME) -m $(KUBE_KIND_CLUSTER_TENANT_NAME) -a $(APPLICATION_AGENT_CONFIG_FILE) -s $(SERVICE_AGENT_CONFIG_FILE)

.PHONY test-local-no-setup:
$(PYTHON_VENV_DIR)/bin/primazatest -p $(PYTHON_VENV_DIR) -e $(WORKER_CONFIG_FILE) -f $(PRIMAZA_CONFIG_FILE) -c $(KUBE_KIND_CLUSTER_JOIN_NAME) -m $(KUBE_KIND_CLUSTER_TENANT_NAME) -a $(APPLICATION_AGENT_CONFIG_FILE) -s $(SERVICE_AGENT_CONFIG_FILE)

.PHONY: test-output
test-output: setup-test
$(PYTHON_VENV_DIR)/bin/primazatest -o -p $(PYTHON_VENV_DIR) -e $(WORKER_CONFIG_FILE) -f $(PRIMAZA_CONFIG_FILE) -c $(KUBE_KIND_CLUSTER_JOIN_NAME) -m $(KUBE_KIND_CLUSTER_TENANT_NAME) -a $(APPLICATION_AGENT_CONFIG_FILE) -s $(SERVICE_AGENT_CONFIG_FILE)

.PHONY: create-users
create-users: primazactl
-rm -rf $(OUTPUT_DIR)/users
Expand Down
125 changes: 111 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,26 @@ Primazactl help is organized in a hierarchy with contextual help available for d

### Create tenant help
```
usage: primazactl create tenant [-h] [-x] [-f CONFIG] [-v VERSION] [-c CONTEXT] [-k KUBECONFIG] tenant
usage: primazactl create tenant [-h] [-x] [-y {client,server,none}] [-o {yaml,none}] [-f CONFIG] [-v VERSION] [-c CONTEXT] [-k KUBECONFIG] [tenant]
positional arguments:
tenant tenant to create. Default: primaza-system
options:
-h, --help show this help message and exit
-x, --verbose Set for verbose output
-y {client,server,none}, --dry-run {client,server,none}
Set for dry run (default: none)
-o {yaml,none}, --output {yaml,none}
Set to get output of resources which are created (default: none).
-f CONFIG, --config CONFIG
primaza config file. Takes precedence over --version
-v VERSION, --version VERSION
Version of primaza to use, default: latest. Ignored if --config is set.
-c CONTEXT, --context CONTEXT
name of cluster, as it appears in kubeconfig, on which to create the tenant, default: current kubeconfig context
-k KUBECONFIG, --kubeconfig KUBECONFIG
path to kubeconfig file, default: KUBECONFIG environment variable if set, otherwise /Users/martinmulholland/.kube/config
path to kubeconfig file, default: KUBECONFIG environment variable if set, otherwise /<home directory>/.kube/config
```
### Create tenant options
- `--config CONFIG`:
Expand All @@ -170,7 +174,20 @@ options:
- see: [releases](https://github.com/primaza/primazactl/releases) for available versions.
- Ignored if a config file is set.
- defaults to the version used to build primazactl.

- `--output yaml`
- Outputs the manifests of the resources that are created.
- The content will be as used for creating the resource.
- Use with `--dry-run client` to get output without creating resources.
- Default is `none` - no output is produced.
- `--dry-run {server,client,none}`
- If set to `server`
- Resources will be created with dry-run and will not be persisted.
- Output provides the outcome for each resource created.
- If set to `client`
- No output produced.
- Use in conjunction with `--output--` to get output without creating resources.
- Default: none - resources are persisted.

## Delete tenant command

### Delete tenant help
Expand Down Expand Up @@ -205,11 +222,16 @@ Notes:

### Join cluster help
```
usage: primazactl join cluster [-h] [-x] [-f CONFIG] [-v VERSION] [-c CONTEXT] [-k KUBECONFIG] -d CLUSTER_ENVIRONMENT -e ENVIRONMENT [-l MAIN_KUBECONFIG] [-m TENANT_CONTEXT] [-t TENANT]
usage: primazactl join cluster [-h] [-x] [-y {client,server,none}] [-o {yaml,none}] [-f CONFIG] [-v VERSION] [-c CONTEXT] [-k KUBECONFIG] -d
CLUSTER_ENVIRONMENT -e ENVIRONMENT [-l TENANT_KUBECONFIG] [-m TENANT_CONTEXT] [-t TENANT]
options:
-h, --help show this help message and exit
-x, --verbose Set for verbose output
-y {client,server,none}, --dry-run {client,server,none}
Set for dry run (default: none)
-o {yaml,none}, --output {yaml,none}
Set to get output of resources which are created (default: none).
-f CONFIG, --config CONFIG
primaza config file. Takes precedence over --version
-v VERSION, --version VERSION
Expand All @@ -222,8 +244,9 @@ options:
name to use for the ClusterEnvironment that will be created in Primaza
-e ENVIRONMENT, --environment ENVIRONMENT
the Environment that will be associated to the ClusterEnvironment
-l MAIN_KUBECONFIG, --tenant-kubeconfig MAIN_KUBECONFIG
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise /<home directory>/.kube/config
-l TENANT_KUBECONFIG, --tenant-kubeconfig TENANT_KUBECONFIG
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise
/<home directory>/.kube/config
-m TENANT_CONTEXT, --tenant-context TENANT_CONTEXT
name of cluster, as it appears in kubeconfig, on which primaza tenant was created. Default: current kubeconfig context
-t TENANT, --tenant TENANT
Expand Down Expand Up @@ -266,6 +289,19 @@ options:
- `--tenant tenant`
- Tenant used for the join.
- Default is `primaza-system`.
- `--output yaml`
- Outputs the manifests of the resources that are created.
- The content will be as used for creating the resource.
- Use with `--dry-run client` to get output without creating resources.
- Default is `none` - no output is produced.
- `--dry-run {server,client,none}`
- If set to `server`
- Resources will be created with dry-run and will not be persisted.
- Output provides the outcome for each resource created.
- If set to `client`
- No output produced.
- Use in conjunction with `--output--` to get output without creating resources.
- Default: none - resources are persisted.


## Create application namespace command
Expand All @@ -275,18 +311,25 @@ Notes:

### Create application-namespace help
```
usage: primazactl create service-namespace [-h] [-x] -d CLUSTER_ENVIRONMENT [-c CONTEXT] [-m TENANT_CONTEXT] [-f CONFIG] [-t TENANT] [-v VERSION] namespace
usage: primazactl create application-namespace [-h] [-x] [-y {client,server,none}] [-o {yaml,none}] -d CLUSTER_ENVIRONMENT [-c CONTEXT]
[-m TENANT_CONTEXT] [-f CONFIG] [-t TENANT] [-v VERSION] [-k KUBECONFIG] [-l TENANT_KUBECONFIG]
namespace
positional arguments:
namespace namespace to create
options:
-h, --help show this help message and exit
-x, --verbose Set for verbose output
-y {client,server,none}, --dry-run {client,server,none}
Set for dry run (default: none)
-o {yaml,none}, --output {yaml,none}
Set to get output of resources which are created (default: none).
-d CLUSTER_ENVIRONMENT, --cluster-environment CLUSTER_ENVIRONMENT
name to use for the ClusterEnvironment that will be created in Primaza
-c CONTEXT, --context CONTEXT
name of cluster, as it appears in kubeconfig, on which to create the service or application namespace, default: current kubeconfig context
name of cluster, as it appears in kubeconfig, on which to create the service or application namespace, default: current
kubeconfig context
-m TENANT_CONTEXT, --tenant-context TENANT_CONTEXT
name of cluster, as it appears in kubeconfig, on which Primaza tenant was created. Default: current kubeconfig context
-f CONFIG, --config CONFIG
Expand All @@ -296,7 +339,7 @@ options:
-v VERSION, --version VERSION
Version of primaza to use, default: latest. Ignored if --config is set.
-k KUBECONFIG, --kubeconfig KUBECONFIG
path to kubeconfig file, default: KUBECONFIG environment variable if set, otherwise /Users/martinmulholland/.kube/config
path to kubeconfig file, default: KUBECONFIG environment variable if set, otherwise /<home directory>/.kube/config
-l TENANT_KUBECONFIG, --tenant-kubeconfig TENANT_KUBECONFIG
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise
/<home directory>/.kube/config
Expand Down Expand Up @@ -335,6 +378,20 @@ options:
- path to kubeconfig file for the tenant
- default: KUBECONFIG environment variable if set, otherwise
/<home directory>/.kube/config
- `--output yaml`
- Outputs the manifests of the resources that are created.
- The content will be as used for creating the resource.
- Use with `--dry-run client` to get output without creating resources.
- Default is `none` - no output is produced.
- `--dry-run {server,client,none}`
- If set to `server`
- Resources will be created with dry-run and will not be persisted.
- Output provides the outcome for each resource created.
- If set to `client`
- No output produced.
- Use in conjunction with `--output--` to get output without creating resources.
- Default: none - resources are persisted.



## Create service namespace command
Expand All @@ -345,15 +402,29 @@ Notes:

### Create service-namespace help:
```
usage: primazactl worker create service-namespace [-h] [-x] -d CLUSTER_ENVIRONMENT [-c CONTEXT] [-m TENANT_CONTEXT] [-f CONFIG]
usage: primazactl create service-namespace [-h] [-x] [-y {client,server,none}] [-o {yaml,none}] -d CLUSTER_ENVIRONMENT [-c CONTEXT] [-m TENANT_CONTEXT]
[-f CONFIG] [-t TENANT] [-v VERSION] [-k KUBECONFIG] [-l TENANT_KUBECONFIG]
namespace
positional arguments:
namespace namespace to create
options:
-h, --help show this help message and exit
-x, --verbose Set for verbose output
-y {client,server,none}, --dry-run {client,server,none}
Set for dry run (default: none)
-o {yaml,none}, --output {yaml,none}
Set to get output of resources which are created (default: none).
-d CLUSTER_ENVIRONMENT, --cluster-environment CLUSTER_ENVIRONMENT
name to use for the ClusterEnvironment that will be created in Primaza
-c CONTEXT, --context CONTEXT
name of cluster, as it appears in kubeconfig, on which to create the service or application namespace, default: current kubeconfig context
name of cluster, as it appears in kubeconfig, on which to create the service or application namespace, default: current
kubeconfig context
-m TENANT_CONTEXT, --tenant-context TENANT_CONTEXT
name of cluster, as it appears in kubeconfig, on which Primaza tenant was created. Default: current kubeconfig context
-f CONFIG, --config CONFIG
Expand All @@ -363,10 +434,10 @@ options:
-v VERSION, --version VERSION
Version of primaza to use, default: latest. Ignored if --config is set.
-k KUBECONFIG, --kubeconfig KUBECONFIG
path to kubeconfig file, default: KUBECONFIG environment variable if set, otherwise /Users/martinmulholland/.kube/config
path to kubeconfig file, default: KUBECONFIG environment variable if set, otherwise /<home directory>/.kube/config
-l TENANT_KUBECONFIG, --tenant-kubeconfig TENANT_KUBECONFIG
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise
/<homedirectory>/.kube/config
/<home directory>/.kube/config
```

### Create service-namespace options:
Expand Down Expand Up @@ -398,7 +469,21 @@ options:
- The cluster specified for worker join does not have to be the current context.
- `--tenant-kubeconfig TENANT_KUBECONFIG`
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise
/<home directory>/.kube/config
/<home directory>/.kube/config
- `--output yaml`
- Outputs the manifests of the resources that are created.
- The content will be as used for creating the resource.
- Use with `--dry-run client` to get output without creating resources.
- Default is `none` - no output is produced.
- `--dry-run {server,client,none}`
- If set to `server`
- Resources will be created with dry-run and will not be persisted.
- Output provides the outcome for each resource created.
- If set to `client`
- No output produced.
- Use in conjunction with `--output--` to get output without creating resources.
- Default: none - resources are persisted.


# Testing

Expand All @@ -415,6 +500,18 @@ options:
- run the test: `out/venv3/bin/primazatest -u`
- src script is `scripts/src/primazatest/runtest.sh`
- requires inputs: python virtual environment directory, the primaza configuration file and the cluster names.
- To run the test for dry-run run `make test-dry-run`
- This will:
- run `make setup-test`
- run the test: `out/venv3/bin/primazatest -d`
- src script is `scripts/src/primazatest/runtest.sh`
- requires inputs: python virtual environment directory, the primaza configuration file and the cluster names.
- To run the test for output of resources `make test-output`
- This will:
- run `make setup-test`
- run the test: `out/venv3/bin/primazatest -o`
- src script is `scripts/src/primazatest/runtest.sh`
- requires inputs: python virtual environment directory, the primaza configuration file and the cluster names.
- To set up the test environment run `make setup-test`
- This will run in order:
- `make clean`
Expand Down
13 changes: 11 additions & 2 deletions scripts/src/primazactl/cmd/create/namespace/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from primazactl.version import __primaza_version__
from .constants import SERVICE, APPLICATION
from primazactl.utils.kubeconfig import from_env
from primazactl.utils import settings


def add_args_namespace(parser: argparse.ArgumentParser, type):
Expand Down Expand Up @@ -96,7 +97,7 @@ def add_args_namespace(parser: argparse.ArgumentParser, type):

def __create_namespace(args, type):
try:

settings.set(args)
main = MainCluster(context=args.tenant_context,
namespace=args.tenant,
kubeconfig_path=args.tenant_kubeconfig,
Expand Down Expand Up @@ -132,7 +133,15 @@ def __create_namespace(args, type):
worker.create_namespaced_kubeconfig_secret(kcfg, args.tenant)

namespace.check()
print(f"{type} namespace primaza-{type} was successfully created")

if settings.output_active():
settings.output()
elif settings.dry_run_active():
print(f"dry run create {type} namespace "
f"{args.namespace} completed.")
else:
print(f"{type} namespace {args.namespace} "
f"was successfully created")

except Exception as e:
print(traceback.format_exc())
Expand Down
13 changes: 11 additions & 2 deletions scripts/src/primazactl/cmd/create/tenant/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from primazactl.primazamain.constants import DEFAULT_TENANT
from primazactl.primazamain.maincluster import MainCluster
from primazactl.types import kubernetes_name
from primazactl.utils import settings


def add_create_tenant(
Expand Down Expand Up @@ -32,16 +33,24 @@ def add_args_tenant(parser: argparse.ArgumentParser):

def create_tenant(args):
try:
settings.set(args)
MainCluster(
args.context,
args.tenant,
args.kubeconfig,
args.config,
args.version).install_primaza()
print("Primaza main installed")

if settings.output_active():
settings.output()
elif settings.dry_run_active():
print("Dry run Primaza tenant install complete")
else:
print("Primaza tenant installed")

except Exception as e:
if args.verbose:
print(traceback.format_exc())
print(f"\nAn exception occurred executing main install: {e}",
print(f"\nAn exception occurred executing tenant install: {e}",
file=sys.stderr)
raise e
9 changes: 8 additions & 1 deletion scripts/src/primazactl/cmd/join/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from primazactl.utils.kubeconfig import from_env
from primazactl.primazamain.constants import DEFAULT_TENANT
from primazactl.version import __primaza_version__
from primazactl.utils import settings


def add_group(parser: argparse.ArgumentParser, parents=[]):
Expand Down Expand Up @@ -118,6 +119,7 @@ def add_args_join(parser: argparse.ArgumentParser):
def join_cluster(args):

try:
settings.set(args)
main = MainCluster(
context=args.tenant_context,
namespace=args.tenant,
Expand All @@ -137,7 +139,12 @@ def join_cluster(args):
tenant=args.tenant,
).install_worker()

print("Install and configure worker completed")
if settings.output_active():
settings.output()
elif settings.dry_run_active():
print("Dry run worker join completed")
else:
print("Worker join completed")
except Exception as e:
print(traceback.format_exc())
print(f"\nAn exception occurred executing the "
Expand Down
Loading

0 comments on commit 8544f34

Please sign in to comment.