Skip to content

Commit 0a03d57

Browse files
Merge pull request #5 from silogen/refactor/better-internal-config
General deduplication of function arguments
2 parents 92c7354 + a3eb4f0 commit 0a03d57

7 files changed

Lines changed: 183 additions & 239 deletions

File tree

codehub/cli/config.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from dataclasses import dataclass
12
import os
23
from pathlib import Path
4+
from typing import Optional
35

46

57
ROOT = Path(__file__).parents[2]
@@ -34,3 +36,55 @@
3436
SLEEP_THRESHOLD = 600
3537
TIMEOUT_ERROR = "Timeout expired."
3638
CLUSTER_MAX_LEN = 14
39+
40+
41+
@dataclass
42+
class CloudState:
43+
nfs_ip: str
44+
nfs_name: str
45+
cluster_id: str
46+
hub_sa_key: str
47+
cluster_endpoint: str
48+
cluster_cert: str
49+
gcp_token: str
50+
docker_registry_hostname: str
51+
docker_image: str
52+
53+
54+
@dataclass
55+
class OAuthConfig:
56+
client_id: str
57+
client_secret: str
58+
59+
60+
@dataclass
61+
class HubConfig:
62+
admins: list[str]
63+
https: Optional[str] = None
64+
oauth_config: Optional[OAuthConfig] = None
65+
contact_email: Optional[str] = None
66+
67+
68+
@dataclass
69+
class CreateConfig:
70+
name: str
71+
admins: list[str]
72+
region: str
73+
zone: str
74+
machine_type: str
75+
76+
77+
@dataclass
78+
class UpgradeConfig:
79+
name: str
80+
hub_config: HubConfig
81+
82+
83+
@dataclass
84+
class DeployConfig:
85+
name: str
86+
region: str
87+
helm_dir: str
88+
hub_dir: str
89+
k8s_dir: str
90+
cloud_state: CloudState

codehub/cli/create.py

Lines changed: 46 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -3,102 +3,80 @@
33
import logging
44
import json
55
from codehub.cli.gcp.terraform import (
6-
TerraformOutput,
76
setup_terraform,
87
terraform_apply,
98
terraform_output,
109
)
1110
from distutils.dir_util import copy_tree
12-
from codehub.cli.config import STRUCTURE
11+
from codehub.cli.config import (
12+
STRUCTURE,
13+
CreateConfig,
14+
DeployConfig,
15+
HubConfig,
16+
UpgradeConfig,
17+
)
1318
from codehub.cli.gcp.helpers import authenticate_k8s_GKE
1419
from codehub.cli.k8s.create import create_k8s_resources
1520
from codehub.cli.helm.create import create_deploy
16-
from codehub.cli.helm.install import install_helm_chart, upgrade_helm_chart
21+
from codehub.cli.helm.install import install_helm_chart
1722
from codehub.cli.helpers import get_cloud_dir, get_latest_deployment, read_yaml
18-
from codehub.cli.manage import wait_for_hub_to_get_ready, get_ip
23+
from codehub.cli.manage import wait_for_hub_to_get_ready
1924
import kubernetes.client.rest # Add this import for the exception handling
2025

2126

22-
def create_infrastructure(name, region, zone, machine_type):
23-
_, _, k8s_dir, cloud_dir = __create_deploy_structure(name)
27+
def create_infrastructure(config: CreateConfig) -> DeployConfig:
28+
helm_dir, hub_dir, k8s_dir, cloud_dir = __create_deploy_structure(config.name)
2429

2530
setup_terraform(
26-
cluster_name=name,
27-
region=region,
28-
zone=zone,
29-
machine_type=machine_type,
31+
config=config,
3032
cloud_dir=cloud_dir,
3133
)
3234

33-
cloud_config = terraform_apply(cloud_dir=cloud_dir)
34-
__create_k8s_resources(
35-
name,
36-
deploy_dir=k8s_dir,
37-
nfs_name=cloud_config.nfs_name,
38-
nfs_ip=cloud_config.nfs_ip,
35+
cloud_state = terraform_apply(cloud_dir=cloud_dir)
36+
deploy_config = DeployConfig(
37+
config.name, config.region, helm_dir, hub_dir, k8s_dir, cloud_state
3938
)
39+
__create_k8s_resources(deploy_config)
4040

41-
return get_ip(name, k8s_dir)
41+
return deploy_config
4242

4343

44-
def create(name, admins, region, zone, machine_type):
45-
helm_dir, hub_dir, k8s_dir, cloud_dir = __create_deploy_structure(name)
44+
def create(config: CreateConfig) -> DeployConfig:
45+
deploy_config = create_infrastructure(config)
46+
hub_config = HubConfig(config.admins)
4647

47-
setup_terraform(
48-
cluster_name=name,
49-
region=region,
50-
zone=zone,
51-
machine_type=machine_type,
52-
cloud_dir=cloud_dir,
53-
)
48+
__create_k8s_resources(deploy_config)
5449

55-
cloud_state = terraform_apply(cloud_dir=cloud_dir)
56-
__create_k8s_resources(
57-
name,
58-
deploy_dir=k8s_dir,
59-
nfs_name=cloud_state.nfs_name,
60-
nfs_ip=cloud_state.nfs_ip,
61-
)
62-
63-
__install_helm_chart(
64-
name,
65-
region=region,
66-
helm_deploy_dir=helm_dir,
67-
hub_deploy_dir=hub_dir,
68-
cloud_state=cloud_state,
69-
admins=admins,
70-
)
50+
__create_deploy(deploy_config, hub_config)
51+
install_helm_chart(deploy_config, upgrade=False)
7152

72-
wait_for_hub_to_get_ready(k8s_dir)
53+
wait_for_hub_to_get_ready(deploy_config.k8s_dir)
7354

74-
return get_ip(name, k8s_dir)
55+
return deploy_config
7556

7657

77-
def upgrade(name, admins, https=None, client_id=None, client_secret=None):
78-
old_deploy_dir = get_latest_deployment(name=name)
58+
def upgrade(config: UpgradeConfig):
59+
old_deploy_dir = get_latest_deployment(config.name)
7960

8061
helm_dir, hub_dir, k8s_dir, cloud_dir = __upgrade_deploy_structure(
81-
name, old_deploy_dir
62+
config.name, old_deploy_dir
8263
)
8364
cloud_state = terraform_output(cloud_dir=cloud_dir)
8465

85-
last_helm_install = read_yaml(os.path.join(old_deploy_dir, "helm", "chart.yaml"))[
86-
"install"
66+
region = read_yaml(os.path.join(old_deploy_dir, "helm", "chart.yaml"))["install"][
67+
"region"
8768
]
69+
deploy_config = DeployConfig(
70+
config.name, region, helm_dir, hub_dir, k8s_dir, cloud_state
71+
)
8872

89-
__upgrade_helm_chart(
90-
name,
91-
region=last_helm_install["region"],
92-
helm_deploy_dir=helm_dir,
93-
hub_deploy_dir=hub_dir,
94-
cloud_state=cloud_state,
95-
admins=admins,
96-
https=https,
97-
client_id=client_id,
98-
client_secret=client_secret,
73+
__create_deploy(
74+
deploy_config,
75+
config.hub_config,
9976
)
77+
install_helm_chart(deploy_config, upgrade=True)
10078

101-
authenticate_k8s_GKE(name)
79+
authenticate_k8s_GKE(config.name)
10280
wait_for_hub_to_get_ready(k8s_dir)
10381

10482

@@ -136,10 +114,10 @@ def __upgrade_deploy_structure(name, old_deploy_dir):
136114
return helm_dir, hub_dir, k8s_dir, cloud_dir
137115

138116

139-
def __create_k8s_resources(name, deploy_dir, nfs_name, nfs_ip):
140-
authenticate_k8s_GKE(name)
117+
def __create_k8s_resources(deploy_config: DeployConfig):
118+
authenticate_k8s_GKE(deploy_config.name)
141119
try:
142-
create_k8s_resources(deploy_dir, nfs_name, nfs_ip)
120+
create_k8s_resources(deploy_config)
143121
except kubernetes.client.rest.ApiException as e:
144122
logger = logging.getLogger(__name__)
145123
# If the error is due to a resource already existing, we can continue
@@ -150,72 +128,11 @@ def __create_k8s_resources(name, deploy_dir, nfs_name, nfs_ip):
150128
raise
151129

152130

153-
def __install_helm_chart(
154-
name,
155-
region,
156-
helm_deploy_dir,
157-
hub_deploy_dir,
158-
cloud_state: TerraformOutput,
159-
admins,
160-
):
161-
__create_deploy(name, helm_deploy_dir, hub_deploy_dir, cloud_state, admins)
162-
install_helm_chart(
163-
name,
164-
region=region,
165-
helm_deploy_dir=helm_deploy_dir,
166-
hub_deploy_dir=hub_deploy_dir,
167-
)
168-
169-
170-
def __upgrade_helm_chart(
171-
name,
172-
region,
173-
helm_deploy_dir,
174-
hub_deploy_dir,
175-
cloud_state: TerraformOutput,
176-
admins,
177-
https=None,
178-
client_id=None,
179-
client_secret=None,
180-
):
181-
__create_deploy(
182-
name,
183-
helm_deploy_dir,
184-
hub_deploy_dir,
185-
cloud_state=cloud_state,
186-
admins=admins,
187-
https=https,
188-
client_id=client_id,
189-
client_secret=client_secret,
190-
)
191-
192-
upgrade_helm_chart(
193-
name, region, helm_deploy_dir=helm_deploy_dir, hub_deploy_dir=hub_deploy_dir
194-
)
195-
196-
197131
def __create_deploy(
198-
name,
199-
helm_deploy_dir,
200-
hub_deploy_dir,
201-
cloud_state: TerraformOutput,
202-
admins,
203-
https=None,
204-
client_id=None,
205-
client_secret=None,
132+
deploy_config: DeployConfig,
133+
hub_config: HubConfig,
206134
):
207-
contact_email = None
208135
with open(STRUCTURE["secrets"]["gcp"]["sa"], "rt") as f:
209-
contact_email = json.loads(f.read())["client_email"]
210-
211-
create_deploy(
212-
name,
213-
contact_email=contact_email,
214-
admins=admins,
215-
helm_deploy_dir=helm_deploy_dir,
216-
hub_deploy_dir=hub_deploy_dir,
217-
cloud_state=cloud_state,
218-
https=https,
219-
client_id=client_id,
220-
client_secret=client_secret,
221-
)
136+
hub_config.contact_email = json.loads(f.read())["client_email"]
137+
138+
create_deploy(deploy_config, hub_config)

codehub/cli/entrypoint.py

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from typing import Optional
12
import click
23
import logging
34
import sys
5+
from codehub.cli.config import CreateConfig, HubConfig, OAuthConfig, UpgradeConfig
46
from codehub.cli.create import create, upgrade, scale, create_infrastructure
57
from codehub.cli.delete import delete
68
from codehub.cli.helpers import check_commands, validate_cluster_name, check_credentials
@@ -37,15 +39,11 @@ def createcluster(name, admin, region, zone, machine_type):
3739
logger = logging.getLogger(__name__)
3840
logger.info(f"Creating cluster '{name}'")
3941

40-
admins = [i.lower() for i in admin]
42+
admins = [user.lower() for user in admin]
43+
config = CreateConfig(name, admins, region, zone, machine_type)
4144

42-
res = create(
43-
name=name,
44-
admins=admins,
45-
region=region,
46-
zone=zone,
47-
machine_type=machine_type,
48-
)
45+
deploy_config = create(config)
46+
res = get_ip(name, deploy_config.k8s_dir)
4947

5048
logger.debug(f"{create.__module__}.{create.__name__} output:")
5149
logger.debug(res)
@@ -64,32 +62,31 @@ def upgradecluster(name, admin, https=None, client_id=None, client_secret=None):
6462
logger = logging.getLogger(__name__)
6563
logger.info(f"Updating cluster '{name}'")
6664

67-
https_passed = https is not None
68-
client_id_passed = client_id is not None
69-
client_secret_passed = client_secret is not None
70-
passed_correct_args = https_passed and (client_id_passed is client_secret_passed)
71-
passed_no_args = not https_passed and not (client_id_passed or client_secret_passed)
65+
hub_config = HubConfig([i.lower() for i in admin])
7266

73-
admins = [i.lower() for i in admin]
67+
if https is not None:
68+
hub_config.https = https
69+
if client_id and client_secret:
70+
hub_config.oauth_config = OAuthConfig(
71+
client_id=client_id, client_secret=client_secret
72+
)
73+
# None or both need to be passed
74+
elif client_id or client_secret:
75+
raise ValueError(
76+
"To add oauth you need to pass the following arguments\n"
77+
+ "`--https` <host-name>\n"
78+
+ "`--client-id` <github-client-id>\n"
79+
+ "`--client-secret` <github-client-secret>"
80+
)
7481

75-
if passed_correct_args or passed_no_args:
76-
res = upgrade(
77-
name=name,
78-
admins=admins,
79-
https=https,
80-
client_id=client_id,
81-
client_secret=client_secret,
82-
)
82+
config = UpgradeConfig(name, hub_config)
8383

84-
logger.debug(f"{upgrade.__module__}.{upgrade.__name__} output:")
85-
logger.debug(res)
84+
res = upgrade(config)
8685

87-
logger.info(f"Cluster '{name}' upgraded successfully")
88-
else:
89-
logger.warning("To add oauth you need to pass the following arguments")
90-
logger.warning("`--https` <host-name>")
91-
logger.warning("`--client-id` <github-client-id>")
92-
logger.warning("`--client-secret` <github-client-secret>")
86+
logger.debug(f"{upgrade.__module__}.{upgrade.__name__} output:")
87+
logger.debug(res)
88+
89+
logger.info(f"Cluster '{name}' upgraded successfully")
9390

9491

9592
@cli.command()
@@ -153,12 +150,11 @@ def createclusterinfra(name, region, zone, machine_type):
153150
logger = logging.getLogger(__name__)
154151
logger.info(f"Creating cluster '{name}'")
155152

156-
res = create_infrastructure(
157-
name=name,
158-
region=region,
159-
zone=zone,
160-
machine_type=machine_type,
161-
)
153+
admins = []
154+
config = CreateConfig(name, admins, region, zone, machine_type)
155+
156+
deploy_config = create_infrastructure(config)
157+
res = get_ip(name, deploy_config.k8s_dir)
162158

163159
logger.debug(f"{create.__module__}.{create.__name__} output:")
164160
logger.debug(res)

0 commit comments

Comments
 (0)