Skip to content

Commit

Permalink
Remove kind specific code (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Ilario <[email protected]>
  • Loading branch information
filariow committed Jul 13, 2023
1 parent 8544f34 commit 317ab12
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 77 deletions.
59 changes: 32 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Primazactl help is organized in a hierarchy with contextual help available for d
- enables primaza tenant to access the namespace
- creates two service accounts for the service-namespace to access kubernetes resources based on two different roles.
- provides join cluster service account with access to the namespace

## Create tenant command

### Create tenant help
Expand Down Expand Up @@ -187,7 +187,7 @@ options:
- 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 @@ -222,8 +222,8 @@ Notes:

### Join cluster help
```
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]
usage: primazactl join cluster [-h] [-x] [-y {client,server,none}] [-o {yaml,none}] [-f CONFIG] [-v VERSION] [-c CONTEXT] [-k KUBECONFIG] [-u INTERNAL_URL] -d CLUSTER_ENVIRONMENT -e ENVIRONMENT [-l TENANT_KUBECONFIG] [-m TENANT_CONTEXT]
[-t TENANT]
options:
-h, --help show this help message and exit
Expand All @@ -240,13 +240,14 @@ options:
name of cluster, as it appears in kubeconfig, to join, default: current kubeconfig context
-k KUBECONFIG, --kubeconfig KUBECONFIG
path to kubeconfig file, default: KUBECONFIG environment variable if set, otherwise /<home directory>/.kube/config
-u INTERNAL_URL, --internal-url INTERNAL_URL
the url used by Primaza's Control Plane to reach the joined cluster
-d CLUSTER_ENVIRONMENT, --cluster-environment CLUSTER_ENVIRONMENT
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 TENANT_KUBECONFIG, --tenant-kubeconfig TENANT_KUBECONFIG
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise
/<home directory>/.kube/config
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 All @@ -267,6 +268,8 @@ options:
- Set the environment variable `KIND_CLUSTER_JOIN_NAME` before running make to overwrite the name of the cluster created.
- If using kind, prepend `kind-` to the cluster name.
- Can use the same cluster as used for main install.
- `-internal-url INTERNAL_URL`
- the url that will be used by the Control Plane to reach the joined cluster
- `--kubeconfig KUBECONFIG`
- The kubeconfig file is not modified by primazactl.
- The cluster specified for worker join does not have to be the current context.
Expand Down Expand Up @@ -302,7 +305,7 @@ options:
- 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 @@ -311,8 +314,8 @@ Notes:

### Create application-namespace help
```
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]
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] [-u TENANT_INTERNAL_URL] [-v VERSION] [-k KUBECONFIG]
[-l TENANT_KUBECONFIG]
namespace
positional arguments:
Expand All @@ -328,21 +331,21 @@ options:
-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
Config file containing agent roles
-t TENANT, --tenant TENANT
tenant to use. Default: primaza-system
-u TENANT_INTERNAL_URL, --tenant-internal-url TENANT_INTERNAL_URL
Internal URL for the cluster on which Primaza's Control Plane is running
-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 /<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
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise /<home directory>/.kube/config
```

### Create application-namespace options:
Expand All @@ -362,8 +365,10 @@ options:
- Run `make config` from the repository
- The config will be created: `out/config/application_agent_config_latest.yaml`
- `--tenant TENANT`
- tenant to use.
- Default is `primaza-system`.
- tenant to use.
- Default is `primaza-system`.
- `--tenant-internal-url TENANT_INTERNAL_URL`
- The URL the Application Agent will use to contact the cluster on which Primaza's Control Plane is running
- `--version VERSION`
- Specify the version of manifests to use.
- see: [releases](https://github.com/primaza/primazactl/releases) for available versions.
Expand Down Expand Up @@ -392,18 +397,17 @@ options:
- Use in conjunction with `--output--` to get output without creating resources.
- Default: none - resources are persisted.



## Create service namespace command

Notes:
- requires join cluster to be completed.


### Create service-namespace help:
```
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]
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] [-u TENANT_INTERNAL_URL] [-v VERSION] [-k KUBECONFIG]
[-l TENANT_KUBECONFIG]
namespace
positional arguments:
Expand All @@ -416,28 +420,24 @@ options:
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
Config file containing agent roles
-t TENANT, --tenant TENANT
tenant to use. Default: primaza-system
-u TENANT_INTERNAL_URL, --tenant-internal-url TENANT_INTERNAL_URL
Internal URL for the cluster on which Primaza's Control Plane is running
-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 /<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
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise /<home directory>/.kube/config
```

### Create service-namespace options:
Expand Down Expand Up @@ -467,6 +467,11 @@ options:
- `--kubeconfig KUBECONFIG`
- The kubeconfig file is not modified by primazactl.
- The cluster specified for worker join does not have to be the current context.
- `--tenant TENANT`
- tenant to use.
- Default is `primaza-system`.
- `--tenant-internal-url TENANT_INTERNAL_URL`
- The URL the Application Agent will use to contact the cluster on which Primaza's Control Plane is running
- `--tenant-kubeconfig TENANT_KUBECONFIG`
path to kubeconfig file for the tenant, default: KUBECONFIG environment variable if set, otherwise
/<home directory>/.kube/config
Expand Down
15 changes: 13 additions & 2 deletions scripts/src/primazactl/cmd/create/namespace/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ def add_args_namespace(parser: argparse.ArgumentParser, type):
help=f"tenant to use. Default: {DEFAULT_TENANT}",
default=DEFAULT_TENANT)

parser.add_argument(
"-u", "--tenant-internal-url",
dest="tenant_internal_url",
type=str,
required=False,
help="Internal URL for the cluster \
on which Primaza's Control Plane is running",
default=None)

parser.add_argument(
"-v", "--version",
dest="version",
Expand Down Expand Up @@ -102,7 +111,9 @@ def __create_namespace(args, type):
namespace=args.tenant,
kubeconfig_path=args.tenant_kubeconfig,
config_file=None,
version=None,)
version=None,
internal_url=args.tenant_internal_url,
)

worker = WorkerCluster(
primaza_main=main,
Expand All @@ -117,7 +128,7 @@ def __create_namespace(args, type):

main_user = main.create_primaza_identity(
args.cluster_environment)
kcfg = main.get_kubeconfig(main_user, args.context)
kcfg = main.get_kubeconfig(main_user)

namespace = WorkerNamespace(type,
args.namespace,
Expand Down
10 changes: 10 additions & 0 deletions scripts/src/primazactl/cmd/join/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ def add_args_join(parser: argparse.ArgumentParser):
type=existing_file,
default=from_env())

parser.add_argument(
"-u", "--internal-url",
dest="internal_url",
required=False,
help="the url used by Primaza's Control Plane to \
reach the joined cluster",
type=str,
default=None)

# main
parser.add_argument(
"-d", "--cluster-environment",
Expand Down Expand Up @@ -137,6 +146,7 @@ def join_cluster(args):
environment=args.environment,
cluster_environment=args.cluster_environment,
tenant=args.tenant,
internal_url=args.internal_url,
).install_worker()

if settings.output_active():
Expand Down
53 changes: 18 additions & 35 deletions scripts/src/primazactl/primaza/primazacluster.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import yaml
import uuid
from typing import Dict
from kubernetes import client
from primazactl.utils import logger
from primazactl.utils.command import Command
from primazactl.identity.kubeidentity import KubeIdentity
from primazactl.kube.secret import Secret
from primazactl.kube.role import Role
Expand All @@ -25,19 +23,28 @@ class PrimazaCluster(object):
config_file: str = None
cluster_environment: str = None
tenant: str = None
internal_url: str | None = None

def __init__(self, namespace, context,
user, user_type,
kubeconfig_path, config_file,
cluster_environment,
tenant):
def __init__(
self,
namespace: str,
context: str,
user: str,
user_type: str,
kubeconfig_path: str,
config_file: str,
cluster_environment: str,
tenant: str,
internal_url: str | None,
):
self.namespace = namespace
self.context = context
self.user = user
self.user_type = user_type if user_type else user
self.config_file = config_file
self.cluster_environment = cluster_environment
self.tenant = tenant
self.internal_url = internal_url

self.kube_config_file = kubeconfig_path \
if kubeconfig_path is not None \
Expand All @@ -46,34 +53,10 @@ def __init__(self, namespace, context,
kcw = KubeConfigWrapper(context, self.kube_config_file)
self.kubeconfig = kcw.get_kube_config_for_cluster()

def get_updated_server_url(self):
logger.log_entry()
cluster = f'{self.context.replace("kind-","")}'
control_plane = f'{cluster}-control-plane'
out, err = Command().run(f"docker inspect {control_plane}")
if err != 0:
raise RuntimeError("\n[ERROR] error getting data from docker:"
f"{control_plane} : {err}")

docker_data = yaml.safe_load(out)
try:
networks = docker_data[0]["NetworkSettings"]["Networks"]
ipaddr = networks["kind"]["IPAddress"]
logger.log_info(f"new cluster url: https://{ipaddr}:6443")
return f"https://{ipaddr}:6443"
except KeyError:
logger.log_info("new cluster url not found")
return ""

def get_kubeconfig(self, identity: KubeIdentity,
other_context) -> Dict:
logger.log_entry(f"id: {identity.sa_name}, "
f"other_context: {other_context}")
server_url = self.get_updated_server_url() \
if self.context != other_context \
else None

return identity.get_kubeconfig(self.kubeconfig, server_url)
def get_kubeconfig(self, identity: KubeIdentity) -> Dict:
logger.log_entry(f"id: {identity.sa_name}")

return identity.get_kubeconfig(self.kubeconfig, self.internal_url)

def create_identity(self, sa_name: str, key_name: str) -> KubeIdentity:
logger.log_entry()
Expand Down
6 changes: 4 additions & 2 deletions scripts/src/primazactl/primazamain/maincluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def __init__(
namespace: str | None,
kubeconfig_path: str | None,
config_file: str | None,
version: str | None):
version: str | None,
internal_url: str | None = None):

self.kube_config_file = kubeconfig_path

Expand All @@ -36,7 +37,8 @@ def __init__(
kubeconfig_path,
config_file,
None,
namespace)
namespace,
internal_url)

self.primaza_version = version

Expand Down
7 changes: 4 additions & 3 deletions scripts/src/primazactl/primazaworker/workercluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
environment: str,
cluster_environment: str,
tenant: str,
internal_url: str | None = None,
):

sa_name, _ = names.get_identity_names(tenant, cluster_environment)
Expand All @@ -38,7 +39,8 @@ def __init__(
kubeconfig_file,
config_file,
cluster_environment,
tenant)
tenant,
internal_url)

self.primaza_main = primaza_main
self.environment = environment
Expand Down Expand Up @@ -75,8 +77,7 @@ def install_worker(self):
identity = self.create_identity(sa_name, key_name)

logger.log_info("Create cluster context secret in main")
cc_kubeconfig = self.get_kubeconfig(identity,
self.primaza_main.context)
cc_kubeconfig = self.get_kubeconfig(identity)

logger.log_info("Create cluster environment in main")
secret_name = names.get_kube_secret_name(self.cluster_environment)
Expand Down
5 changes: 3 additions & 2 deletions scripts/src/primazactl/primazaworker/workernamespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def __init__(self, type,
kubeconfig_file,
role_config,
cluster_environment,
worker.tenant)
worker.tenant,
None)

self.main = main
self.worker = worker
Expand Down Expand Up @@ -78,7 +79,7 @@ def create(self):
self.namespace)

# Get kubeconfig with secret from service accounf
kc = self.main.get_kubeconfig(main_identity, self.context)
kc = self.main.get_kubeconfig(main_identity)

# - in the created namespace, create the Secret
# 'primaza-auth-$CLUSTER_ENVIRONMENT' the Worker key
Expand Down
Loading

0 comments on commit 317ab12

Please sign in to comment.