Skip to content

Commit 03acf18

Browse files
committed
resolving some identified bugs
1 parent b4d6ee9 commit 03acf18

File tree

8 files changed

+124
-17
lines changed

8 files changed

+124
-17
lines changed

src/api/generate_name.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
bp = Blueprint("names", __name__)
88

99

10-
@bp.route("/generate_name")
10+
@bp.route("/")
1111
def generate_name():
1212
starts_with = request.args.get("starts_with")
1313
name_choices = names
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
apiVersion: source.toolkit.fluxcd.io/v1beta1
3+
kind: GitRepository
4+
metadata:
5+
name: credentials-copy
6+
namespace: ${SERVICE_NAME}-istio-svc
7+
spec:
8+
interval: 1m0s
9+
ref:
10+
branch: main
11+
url: https://github.com/runyontr/credentials-copy
12+
---
13+
apiVersion: helm.toolkit.fluxcd.io/v2beta1
14+
kind: HelmRelease
15+
metadata:
16+
name: credentials
17+
namespace: ${SERVICE_NAME}-istio-svc
18+
spec:
19+
chart:
20+
spec:
21+
chart: chart
22+
sourceRef:
23+
kind: GitRepository
24+
name: credentials-copy
25+
interval: 1m0s

src/api/manifests/helm-release.yaml.tmpl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@ spec:
1919
- name: bigbang
2020
namespace: bigbang
2121
valuesFrom:
22-
- kind: Secret
23-
name: common-bb
24-
valuesKey: values.yaml
25-
- kind: ConfigMap
26-
name: common
22+
- kind: ConfigMap
23+
name: common

src/api/manifests/kustomization.yaml.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
namespace: ${SERVICE_NAME}-istio-svc
2+
13
resources:
24
- namespace.yaml
35
- repository.yaml
46
- helm-release.yaml
7+
- copy-credentials.yaml
58

69
configMapGenerator:
710
- name: common

src/api/manifests/repository.yaml.tmpl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ spec:
1010
interval: 1m
1111
url: https://github.com/csemissionplatformops/big-bang-istio-service.git
1212
ref:
13-
branch: main
14-
tag: 0.0.12
13+
branch: main

src/hooks/postdeploy.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def set_env_from_azd():
2222
os.environ[key] = value
2323

2424
def get_services_to_deploy(yaml_template) -> dict:
25+
"""
26+
Parses the yaml file and returns a dictionary containing the services to deploy.
27+
"""
2528
template_file = os.path.join(CWD, yaml_template)
2629

2730
with open(template_file, "r", encoding="utf-8") as stream:
@@ -37,6 +40,9 @@ def get_services_to_deploy(yaml_template) -> dict:
3740
print("Error loading yaml file: ", ex)
3841

3942
def pre_req_assertions(deployment_template):
43+
"""
44+
Asserts that the required environment variables and files exist.
45+
"""
4046
azure_dir = os.path.join(CWD, ".azure")
4147
template_file = os.path.join(CWD, deployment_template)
4248

@@ -53,6 +59,15 @@ def pre_req_assertions(deployment_template):
5359
"The template_dir folder does not exist. Please run 'azd init'"
5460
)
5561

62+
if os.environ.get("SERVICE_API_IMAGE_NAME") is None:
63+
raise ValueError(
64+
"No SERVICE_API_IMAGE_NAME environment variable found. Please set the \
65+
SERVICE_API_IMAGE_NAME environment variable."
66+
)
67+
68+
os.environ['SERVICE_API_IMAGE_REPO'] = os.environ['SERVICE_API_IMAGE_NAME'].split(':')[0]
69+
os.environ['SERVICE_API_IMAGE_TAG'] = os.environ['SERVICE_API_IMAGE_NAME'].split(':')[1]
70+
5671
def copy_manifest_dir_tree_to_repo(manifest_path, repo_path):
5772
"""
5873
Recursively searches for files ending with a .tmpl extension under manifest_path
@@ -67,8 +82,8 @@ def ignore_func(_, files):
6782
if __name__ == "__main__":
6883
print("Post-Deplouy hook running...")
6984

70-
pre_req_assertions(TEMPLATE_FILE)
7185
set_env_from_azd()
86+
pre_req_assertions(TEMPLATE_FILE)
7287
environment_name = os.environ.get("AZURE_AKS_ENVIRONMENT_NAME")
7388
git_client = GithubClient()
7489

src/hooks/wrappers/github.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
import git
55

66
class GithubClient:
7+
"""
8+
A class that provides methods for interacting with the GitHub API
9+
and performing common Git operations.
10+
"""
711
def login(self):
12+
"""
13+
Logs in to GitHub using either the GITHUB_TOKEN environment variable or the gh CLI.
14+
"""
815
gh_pat = os.environ.get("GITHUB_TOKEN")
916

1017
if gh_pat is None:
@@ -19,6 +26,9 @@ def login(self):
1926
os.environ["GITHUB_TOKEN"] = gh_pat
2027

2128
def __login_with_pat(self, gh_pat):
29+
"""
30+
Logs in to GitHub using a personal access token (PAT).
31+
"""
2232
with tempfile.TemporaryDirectory() as td_hndl:
2333
token_file = os.path.join(td_hndl, 'token')
2434
with open(token_file, 'w', encoding="utf-8") as file_hndl:
@@ -27,6 +37,9 @@ def __login_with_pat(self, gh_pat):
2737
self.__run_gh_cli_command("auth", ["login", f"--with-token < {token_file}"])
2838

2939
def clone_repo(self, repo_name, target_branch, directory):
40+
"""
41+
Clones a GitHub repository to a local directory.
42+
"""
3043
if repo_name is None:
3144
raise ValueError("repo_name cannot be None")
3245

@@ -38,6 +51,9 @@ def clone_repo(self, repo_name, target_branch, directory):
3851
print(f"Error cloning repo {repo_name}: Exception: {ex}")
3952

4053
def push_changes(self, repo_dir, commit_message):
54+
"""
55+
Pushes changes to a GitHub repository.
56+
"""
4157
if repo_dir is None:
4258
raise ValueError("repo_dir cannot be None")
4359

@@ -53,16 +69,25 @@ def push_changes(self, repo_dir, commit_message):
5369
print(f"Error pushing changes: Exception: {ex}")
5470

5571
def __run_command(self, command):
72+
"""
73+
Runs a command in the shell and returns its output.
74+
"""
5675
try:
5776
output = subprocess.check_output(command, stderr=subprocess.STDOUT, \
5877
universal_newlines=True, shell=True)
5978
return output.strip()
6079
except subprocess.CalledProcessError as ex:
6180
print(f"Error: {ex.output}")
6281
return None
63-
82+
6483
def __run_gh_cli_command(self, command, args):
84+
"""
85+
Runs a command in the gh CLI and returns its output.
86+
"""
6587
return self.__run_command(["gh", command] + args)
6688

6789
def __unset_gh_pat(self):
68-
del os.environ["GITHUB_TOKEN"]
90+
"""
91+
Unsets the GITHUB_TOKEN environment variable.
92+
"""
93+
del os.environ["GITHUB_TOKEN"]

src/hooks/wrappers/renderer.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@
33
import yaml
44

55
class RenderEngine:
6+
"""
7+
The RenderEngine class provides functionality to render K8 templates
8+
and add a service to a kustomization file.
9+
"""
610
def __init__(self, path, kustomization_path, service_name):
11+
"""
12+
Initializes a new instance of the RenderEngine class.
13+
14+
Args:
15+
path (str): The path to the directory containing the templates to render.
16+
kustomization_path (str): The path to the kustomization file.
17+
service_name (str): The name of the service to add to the kustomization file.
18+
"""
719
self.path = path
820
self.service_name = service_name
921
self.kustomization_path = kustomization_path
1022
self.remove_template_file = True
1123

1224
def __retrieve_files_to_render(self):
25+
"""
26+
Retrieves a list of files to render.
27+
28+
Returns:
29+
A list of files to render.
30+
"""
1331
files_to_render = []
1432
for root, _, files in os.walk(self.path):
1533
for file in files:
@@ -18,6 +36,9 @@ def __retrieve_files_to_render(self):
1836
return files_to_render
1937

2038
def render(self):
39+
"""
40+
Renders the templates and adds the service to the kustomization file.
41+
"""
2142
files_to_render = self.__retrieve_files_to_render()
2243
for file in files_to_render:
2344
print(f"Rendering {file}")
@@ -29,6 +50,15 @@ def render(self):
2950
self.__add_service_to_kustomization_resources()
3051

3152
def __run_command(self, command):
53+
"""
54+
Runs a shell command.
55+
56+
Args:
57+
command (str): The command to run.
58+
59+
Returns:
60+
The output of the command.
61+
"""
3262
try:
3363
output = subprocess.check_output(command, stderr=subprocess.STDOUT, \
3464
universal_newlines=True, shell=True)
@@ -38,27 +68,40 @@ def __run_command(self, command):
3868
return None
3969

4070
def __add_service_to_kustomization_resources(self):
71+
"""
72+
Adds the service to the kustomization file.
73+
"""
4174
# verify that the kustomization file exists
4275
if not os.path.exists(self.kustomization_path):
4376
raise ValueError(
4477
f"The kustomization file {self.kustomization_path} does not exist."
4578
)
4679

47-
print(f"Adding {self.service_name} to {self.kustomization_path}")
48-
4980
with open(self.kustomization_path, "r", encoding="utf-8") as r_stream:
5081
yaml_dict = yaml.safe_load(r_stream)
5182
# check if the resources key exists
5283

5384
if "resources" not in yaml_dict:
5485
raise ValueError("No resources found in kustomization yaml file.")
5586

56-
yaml_dict["resources"].append(f"./services/{self.service_name}")
57-
# Write the updated yaml to the kustomization file\
58-
with open(self.kustomization_path, "w", encoding="utf-8") as w_stream:
59-
yaml.safe_dump(yaml_dict, w_stream, default_flow_style=False, sort_keys=False)
87+
# Check if the service already exists in the resources
88+
if f"./services/{self.service_name}" not in yaml_dict["resources"]:
89+
yaml_dict["resources"].append(f"./services/{self.service_name}")
90+
# Write the updated yaml to the kustomization file\
91+
with open(self.kustomization_path, "w", encoding="utf-8") as w_stream:
92+
print(f"Adding {self.service_name} to {self.kustomization_path}")
93+
yaml.safe_dump(yaml_dict, w_stream, default_flow_style=False, sort_keys=False)
6094

6195
def __run_command(self, command):
96+
"""
97+
Runs a shell command.
98+
99+
Args:
100+
command (str): The command to run.
101+
102+
Returns:
103+
The output of the command.
104+
"""
62105
try:
63106
output = subprocess.check_output(command, stderr=subprocess.STDOUT, \
64107
universal_newlines=True, shell=True)

0 commit comments

Comments
 (0)