Skip to content

Commit

Permalink
Validation requirements on helm names (#109)
Browse files Browse the repository at this point in the history
* Validation requirements on helm names

* lint

* markups

* lint
  • Loading branch information
sunnycarter committed Oct 9, 2023
1 parent 1f90521 commit f951dc0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
57 changes: 56 additions & 1 deletion src/aosm/azext_aosm/generate_nfd/cnf_nfd_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,69 @@ class Artifact:


@dataclass
class NFApplicationConfiguration:
class NFApplicationConfiguration: # pylint: disable=too-many-instance-attributes
name: str
chartName: str
chartVersion: str
releaseName: str
dependsOnProfile: List[str]
registryValuesPaths: List[str]
imagePullSecretsValuesPaths: List[str]
valueMappingsFile: str

def __post_init__(self):
"""Format the fields based on the NFDV validation rules."""
self._format_name()
self._format_release_name()

def _format_name(self):
"""
Format the name field.
The name should start with a alphabetic character, have alphanumeric characters
or '-' in-between and end with alphanumerc character, and be less than 64
characters long. See NfdVersionValidationHelper.cs in pez codebase
"""
# Replace any non (alphanumeric or '-') characters with '-'
self.name = re.sub("[^0-9a-zA-Z-]+", "-", self.name)
# Strip leading or trailing -
self.name = self.name.strip("-")
self.name = self.name[:64]

if not self.name:
raise InvalidTemplateError(
"The name field of the NF application configuration for helm package "
f"{self.chartName} is empty after removing invalid characters. "
"Valid characters are alphanumeric and '-'. Please fix this in the name"
" field for the helm package in your input config file."
)

def _format_release_name(self):
"""
Format release name.
It must consist of lower case alphanumeric characters, '-' or '.', and must
start and end with an alphanumeric character See
AzureArcKubernetesRuleBuilderExtensions.cs and
AzureArcKubernetesNfValidationMessage.cs in pez codebase
"""
self.releaseName = self.releaseName.lower()
# Replace any non (alphanumeric or '-' or '.') characters with '-'
self.releaseName = re.sub("[^0-9a-z-.]+", "-", self.releaseName)
# Strip leading - or .
self.releaseName = self.releaseName.strip("-")
self.releaseName = self.releaseName.strip(".")
if not self.releaseName:
raise InvalidTemplateError(
"The releaseName field of the NF application configuration for helm "
f"chart {self.chartName} is empty after formatting and removing invalid"
"characters. Valid characters are alphanumeric, -.' and '-' and the "
"releaseName must start and end with an alphanumeric character. The "
"value of this field is taken from Chart.yaml within the helm package. "
"Please fix up the helm package. Before removing invalid characters"
f", the releaseName was {self.chartName}."
)


@dataclass
class ImageInfo:
Expand Down Expand Up @@ -378,6 +432,7 @@ def _generate_nf_application_config(
name=helm_package.name,
chartName=name,
chartVersion=version,
releaseName=name,
dependsOnProfile=helm_package.depends_on,
registryValuesPaths=list(registry_values_paths),
imagePullSecretsValuesPaths=list(image_pull_secrets_values_paths),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ resource nfdv 'Microsoft.Hybridnetwork/publishers/networkfunctiondefinitiongroup
deployParametersMappingRuleProfile: {
applicationEnablement: 'Enabled'
helmMappingRuleProfile: {
releaseNamespace: '{{ configuration.chartName }}'
releaseName: '{{ configuration.chartName }}'
releaseNamespace: '{{ configuration.releaseName }}'
releaseName: '{{ configuration.releaseName }}'
helmPackageVersion: '{{ configuration.chartVersion }}'
values: string(loadJsonContent('configMappings/{{ configuration.valueMappingsFile }}'))
}
Expand Down

0 comments on commit f951dc0

Please sign in to comment.