Skip to content

Commit

Permalink
feature(schema): add networkv2 schema
Browse files Browse the repository at this point in the history
Adds networkv2 schema definition file, removes skipping of schema
validation for networkv2, and adds unit tests for same.
  • Loading branch information
catmsred committed Feb 21, 2024
1 parent 05eac8b commit 6d5ce4b
Show file tree
Hide file tree
Showing 3 changed files with 591 additions and 35 deletions.
56 changes: 35 additions & 21 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
# 3. Add the new version definition to versions.schema.cloud-config.json
USERDATA_SCHEMA_FILE = "schema-cloud-config-v1.json"
NETWORK_CONFIG_V1_SCHEMA_FILE = "schema-network-config-v1.json"
NETWORK_CONFIG_V2_SCHEMA_FILE = "schema-network-config-v2.json"

_YAML_MAP = {True: "true", False: "false", None: "null"}
SCHEMA_DOC_TMPL = """
Expand Down Expand Up @@ -160,6 +161,8 @@ class SchemaType(Enum):

CLOUD_CONFIG = "cloud-config"
NETWORK_CONFIG = "network-config"
NETWORK_CONFIG_V1 = "network-config-v1"
NETWORK_CONFIG_V2 = "network-config-v2"


# Placeholders for versioned schema and schema file locations.
Expand All @@ -169,8 +172,14 @@ class SchemaType(Enum):
"latest": USERDATA_SCHEMA_FILE,
},
SchemaType.NETWORK_CONFIG: {
"latest": NETWORK_CONFIG_V2_SCHEMA_FILE,
},
SchemaType.NETWORK_CONFIG_V1: {
"latest": NETWORK_CONFIG_V1_SCHEMA_FILE,
},
SchemaType.NETWORK_CONFIG_V2: {
"latest": NETWORK_CONFIG_V2_SCHEMA_FILE,
},
}


Expand Down Expand Up @@ -699,7 +708,8 @@ def validate_cloudconfig_schema(
for the cloud config module (config.cc_*). If None, validate against
global schema.
@param schema_type: Optional SchemaType.
One of: SchemaType.CLOUD_CONFIG or SchemaType.NETWORK_CONFIG.
One of: SchemaType.CLOUD_CONFIG or SchemaType.NETWORK_CONFIG_V1 or
SchemaType.NETWORK_CONFIG_V2
Default: SchemaType.CLOUD_CONFIG
@param strict: Boolean, when True raise SchemaValidationErrors instead of
logging warnings.
Expand All @@ -714,18 +724,22 @@ def validate_cloudconfig_schema(
against the provided schema.
@raises: RuntimeError when provided config sourced from YAML is not a dict.
@raises: ValueError on invalid schema_type not in CLOUD_CONFIG or
NETWORK_CONFIG
NETWORK_CONFIG_V1 or NETWORK_CONFIG_V2
"""
if schema_type == SchemaType.NETWORK_CONFIG:
if network_schema_version(config) == 2:
if netplan_validate_network_schema(
network_config=config, strict=strict, log_details=log_details
):
# Schema was validated by netplan
return True
# network-config schema version 2 but no netplan.
# TODO(add JSON schema definition for network version 2)
return False
network_version = network_schema_version(cloudconfig)
if network_version == 2:
schema_type = SchemaType.NETWORK_CONFIG_V2
elif network_version == 1:
schema_type = SchemaType.NETWORK_CONFIG_V1
schema = get_schema(schema_type)

if schema_type == SchemaType.NETWORK_CONFIG_V2:
if netplan_validate_network_schema(
network_config=config, strict=strict, log_details=log_details
):
# Schema was validated by netplan
return True

if schema is None:
schema = get_schema(schema_type)
Expand Down Expand Up @@ -1121,22 +1135,22 @@ def validate_cloudconfig_file(
return False
network_version = network_schema_version(cloudconfig)
if network_version == 2:
schema_type = SchemaType.NETWORK_CONFIG_V2
if netplan_validate_network_schema(
network_config=cloudconfig, strict=True, annotate=annotate
):
return True # schema validation performed by netplan
if network_version != 1:
# Validation requires JSON schema definition in
# cloudinit/config/schemas/schema-network-config-v1.json
print(
"Skipping network-config schema validation."
" No network schema for version:"
f" {network_schema_version(cloudconfig)}"
)
return False
elif network_version == 1:
schema_type = SchemaType.NETWORK_CONFIG_V1
# refresh schema since NETWORK_CONFIG defaults to V2
schema = get_schema(schema_type)
try:
if not validate_cloudconfig_schema(
cloudconfig, schema=schema, strict=True, log_deprecations=False
cloudconfig,
schema=schema,
schema_type=schema_type,
strict=True,
log_deprecations=False,
):
print(
f"Skipping {schema_type.value} schema validation."
Expand Down
Loading

0 comments on commit 6d5ce4b

Please sign in to comment.