Skip to content

Commit

Permalink
small refactor of generate nfd
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan committed May 23, 2023
1 parent 8c94905 commit aa0b061
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 70 deletions.
12 changes: 8 additions & 4 deletions src/aosm/azext_aosm/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# --------------------------------------------------------------------------------------------

import json
import os
from dataclasses import asdict
from typing import Optional
from knack.log import get_logger
Expand All @@ -21,6 +22,7 @@
get_configuration,
NFConfiguration,
)
from azure.cli.core.azclierror import InvalidTemplateError, CLIInternalError


logger = get_logger(__name__)
Expand Down Expand Up @@ -104,13 +106,15 @@ def _generate_nfd(definition_type, config):
elif definition_type == CNF:
nfd_generator = CnfNfdGenerator(config)
else:
from azure.cli.core.azclierror import CLIInternalError

raise CLIInternalError(
"Generate NFD called for unrecognised definition_type. Only VNF and CNF have been implemented."
)

nfd_generator.generate_nfd()
if nfd_generator.bicep_path:
raise InvalidTemplateError(
f"ERROR: Using the existing NFD bicep template {nfd_generator.bicep_path}.\nTo generate a new NFD, delete the folder {os.path.dirname(nfd_generator.bicep_path)} and re-run this command."
)
else:
nfd_generator.generate_nfd()


def publish_definition(
Expand Down
102 changes: 47 additions & 55 deletions src/aosm/azext_aosm/generate_nfd/cnf_nfd_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tarfile
from typing import Dict, List, Any, Tuple, Optional, Iterator

import tempfile
import yaml
from azext_aosm.generate_nfd.nfd_generator_base import NFDGenerator
from jinja2 import Template, StrictUndefined
Expand Down Expand Up @@ -52,7 +53,6 @@ def __init__(self, config: CNFConfiguration):
CNF_MANIFEST_JINJA2_SOURCE_TEMPLATE,
)
self.output_folder_name = self.config.build_output_folder_name
self.tmp_folder_name = "tmp"

self.artifacts = []
self.nf_application_configurations = []
Expand All @@ -69,60 +69,55 @@ def __init__(self, config: CNFConfiguration):

def generate_nfd(self) -> None:
"""Generate a CNF NFD which comprises a group, an Artifact Manifest and an NFDV."""
# Create tmp folder.
os.mkdir(self.tmp_folder_name)

if self.bicep_path:
shutil.rmtree(self.tmp_folder_name)
raise InvalidTemplateError(
f"ERROR: Using the existing NFD bicep template {self.bicep_path}.\nPlease fix this and run the command again."
)
else:
for helm_package in self.config.helm_packages:

helm_package = HelmPackageConfig(**helm_package)
# Unpack the chart into the tmp folder
self._extract_chart(helm_package.path_to_chart)

# TODO: Validate charts

# Get schema for each chart (extract mappings and take the schema bits we need from values.schema.json)
# + Add that schema to the big schema.
self.deployment_parameter_schema["properties"].update(
self.get_chart_mapping_schema(helm_package)
)

# Get all image line matches for files in the chart.
# Do this here so we don't have to do it multiple times.
image_line_matches = self.find_pattern_matches_in_chart(
helm_package, IMAGE_LINE_REGEX
)

# Create temporary folder.
with tempfile.TemporaryDirectory() as tmpdirname:
self.tmp_folder_name = tmpdirname
try:
for helm_package in self.config.helm_packages:

helm_package = HelmPackageConfig(**helm_package)
# Unpack the chart into the tmp folder
self._extract_chart(helm_package.path_to_chart)

# TODO: Validate charts

# Get schema for each chart (extract mappings and take the schema bits we need from values.schema.json)
# + Add that schema to the big schema.
self.deployment_parameter_schema["properties"].update(
self.get_chart_mapping_schema(helm_package)
)

# Generate the NF application configuration for the chart
self.nf_application_configurations.append(
self.generate_nf_application_config(
helm_package,
image_line_matches,
self.find_pattern_matches_in_chart(
helm_package, IMAGE_PULL_SECRET_LINE_REGEX
),
# Get all image line matches for files in the chart.
# Do this here so we don't have to do it multiple times.
image_line_matches = self.find_pattern_matches_in_chart(
helm_package, IMAGE_LINE_REGEX
)
)
# Workout the list of artifacts for the chart and
# update the list for the NFD with any unique artifacts.
chart_artifacts = self.get_artifact_list(
helm_package, set(image_line_matches)
)
self.artifacts += [
a for a in chart_artifacts if a not in self.artifacts
]

self.write_nfd_bicep_file()
self.write_schema_to_file()
self.write_manifest_bicep_file()
self.copy_to_output_folder()
# Delete tmp folder
shutil.rmtree(self.tmp_folder_name)
# Generate the NF application configuration for the chart
self.nf_application_configurations.append(
self.generate_nf_application_config(
helm_package,
image_line_matches,
self.find_pattern_matches_in_chart(
helm_package, IMAGE_PULL_SECRET_LINE_REGEX
),
)
)
# Workout the list of artifacts for the chart and
# update the list for the NFD with any unique artifacts.
chart_artifacts = self.get_artifact_list(
helm_package, set(image_line_matches)
)
self.artifacts += [
a for a in chart_artifacts if a not in self.artifacts
]
self.write_nfd_bicep_file()
self.write_schema_to_file()
self.write_manifest_bicep_file()
self.copy_to_output_folder()
except InvalidTemplateError as e:
raise e

@property
def bicep_path(self) -> Optional[str]:
Expand Down Expand Up @@ -334,7 +329,6 @@ def get_chart_mapping_schema(
with open(non_def_values, "r", encoding="utf-8") as stream:
values_data = yaml.load(stream, Loader=yaml.SafeLoader)
except:
shutil.rmtree(self.tmp_folder_name)
raise InvalidTemplateError(
f"ERROR: There is no values.mappings.yaml file for the helm package '{helm_package.name}'. Please fix this and run the command again."
)
Expand All @@ -344,15 +338,13 @@ def get_chart_mapping_schema(
data = json.load(f)
schema_data = data["properties"]
except:
shutil.rmtree(self.tmp_folder_name)
raise InvalidTemplateError(
f"ERROR: There is no values.schema.json file for the helm package '{helm_package.name}'. Please fix this and run the command again."
)

try:
final_schema = self.find_deploy_params(values_data, schema_data, {})
except KeyError as e:
shutil.rmtree(self.tmp_folder_name)
raise InvalidTemplateError(
f"ERROR: Your schema and values for the helm package '{helm_package.name}' do not match. Please fix this and run the command again."
) from e
Expand Down
15 changes: 4 additions & 11 deletions src/aosm/azext_aosm/generate_nfd/vnf_bicep_nfd_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,10 @@ def __init__(self, config: VNFConfiguration):
)

def generate_nfd(self) -> None:
"""Generate a VNF NFD which comprises an group, an Artifact Manifest and a NFDV."""
if self.bicep_path:
print(f"Using the existing NFD bicep template {self.bicep_path}.")
print(
f"To generate a new NFD, delete the folder {os.path.dirname(self.bicep_path)} and re-run this command."
)
else:
self.write()

def write(self) -> None:
"""Create a bicep template for an NFD from the ARM template for the VNF."""
"""
Generate a VNF NFD which comprises an group, an Artifact Manifest and a NFDV.
Create a bicep template for an NFD from the ARM template for the VNF.
"""
logger.info(f"Generate NFD bicep template for {self.arm_template_path}")
print(f"Generate NFD bicep template for {self.arm_template_path}")

Expand Down

0 comments on commit aa0b061

Please sign in to comment.