Skip to content

Commit

Permalink
refactor file tree layout
Browse files Browse the repository at this point in the history
  • Loading branch information
usrbinkat committed Sep 20, 2024
1 parent 6c50c32 commit 28bf6ed
Show file tree
Hide file tree
Showing 55 changed files with 125 additions and 82 deletions.
112 changes: 79 additions & 33 deletions pulumi/__main__.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,81 @@
# __main__.py

# Import General Purpose Libraries
from typing import List, Dict, Any

# Import Pulumi Libraries
import pulumi
from src.lib.init import initialize_pulumi
from src.lib.config import export_results
from src.lib.deploy_module import deploy_module

# Pulumi Initialization
init = initialize_pulumi()

# Retrieve initialized resources
config = init["config"]
k8s_provider = init["k8s_provider"]
versions = init["versions"]
configurations = init["configurations"]
default_versions = init["default_versions"]
global_depends_on = init["global_depends_on"]
compliance_config = init["compliance_config"]

# List of modules to deploy
modules_to_deploy = ["cert_manager", "kubevirt"]

# Deploy each module
for module_name in modules_to_deploy:
deploy_module(
module_name=module_name,
config=config,
default_versions=default_versions,
global_depends_on=global_depends_on,
k8s_provider=k8s_provider,
versions=versions,
configurations=configurations,
)

# Export Component Metadata Outputs: - Versions - Configurations
export_results(versions, configurations, compliance_config)
from pulumi_kubernetes import Provider

# Import Local Libraries
from core.init import initialize_pulumi
from core.config import export_results
from core.deploy_module import deploy_module


def main():
"""
Main entry point for the Kargo Kubevirt Pulumi IaC.
Initializes the Pulumi program, configures resources, and deploys specified modules.
"""
try:
# Initialize Pulumi resources
init = initialize_pulumi()

# Retrieve initialized resources
config = init["config"]
k8s_provider = init["k8s_provider"]
versions = init["versions"]
configurations = init["configurations"]
default_versions = init["default_versions"]
global_depends_on = init["global_depends_on"]
compliance_config = init["compliance_config"]

# Define the list of modules to deploy
modules_to_deploy = ["cert_manager", "kubevirt"]

# Deploy each module
deploy_modules(modules_to_deploy, config, default_versions, global_depends_on, k8s_provider, versions, configurations)

# Export results
export_results(versions, configurations, compliance_config)

except Exception as e:
pulumi.log.error(f"Deployment failed: {str(e)}")
raise

def deploy_modules(
modules: List[str],
config: pulumi.Config,
default_versions: Dict[str, Any],
global_depends_on: List[pulumi.Resource],
k8s_provider: Provider,
versions: Dict[str, str],
configurations: Dict[str, Dict[str, Any]]
) -> None:
"""
Deploy the specified modules.
Args:
modules (list): List of module names to deploy.
config (pulumi.Config): The Pulumi configuration object.
default_versions (dict): Default versions for modules.
global_depends_on (list): Global dependencies.
k8s_provider (Provider): Kubernetes provider object.
versions (dict): Dictionary to store versions of deployed modules.
configurations (dict): Dictionary to store configurations of deployed modules.
"""
for module_name in modules:
pulumi.log.info(f"Deploying module: {module_name}")
deploy_module(
module_name=module_name,
config=config,
default_versions=default_versions,
global_depends_on=global_depends_on,
k8s_provider=k8s_provider,
versions=versions,
configurations=configurations,
)

if __name__ == "__main__":
main()
File renamed without changes.
9 changes: 1 addition & 8 deletions pulumi/src/lib/compliance.py → pulumi/core/compliance.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# src/lib/compliance.py

import json
from typing import Dict
from .types import ComplianceConfig
from .utils import sanitize_label_value

# src/lib/compliance.py
# core/compliance.py

import json
from typing import Dict
Expand Down
2 changes: 1 addition & 1 deletion pulumi/src/lib/config.py → pulumi/core/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# src/lib/config.py
# core/config.py
# Description: Module Configuration Parsing & Loading

"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# src/lib/deploy_module.py
# core/deploy_module.py

import inspect
import pulumi
import pulumi_kubernetes as k8s
from typing import Any, Dict, List
from src.lib.introspection import discover_config_class, discover_deploy_function
from src.lib.config import get_module_config
from .introspection import discover_config_class, discover_deploy_function
from .config import get_module_config

def deploy_module(
module_name: str,
config: pulumi.Config,
default_versions: Dict[str, Any],
global_depends_on: List[pulumi.Resource],
k8s_provider: Any,
k8s_provider: k8s.Provider,
versions: Dict[str, str],
configurations: Dict[str, Dict[str, Any]]
) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# core/helm_chart_versions.py

import requests
import logging
import yaml
Expand Down
12 changes: 6 additions & 6 deletions pulumi/src/lib/init.py → pulumi/core/init.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# src/lib/init.py
# core/init.py
# Description: Initializes Pulumi configuration, Kubernetes provider, and global resources.

import os
Expand All @@ -7,17 +7,17 @@
from pulumi_kubernetes import Provider
from typing import Dict, List, Any

from src.lib.versions import load_default_versions
from src.lib.metadata import (
from .versions import load_default_versions
from .metadata import (
collect_git_info,
generate_git_labels,
generate_git_annotations,
set_global_labels,
set_global_annotations
)
from src.lib.compliance import generate_compliance_labels, generate_compliance_annotations
from src.lib.types import ComplianceConfig
from src.lib.utils import generate_global_transformations
from .compliance import generate_compliance_labels, generate_compliance_annotations
from .types import ComplianceConfig
from .utils import generate_global_transformations

def initialize_pulumi() -> Dict[str, Any]:
"""
Expand Down
11 changes: 6 additions & 5 deletions pulumi/src/lib/introspection.py → pulumi/core/introspection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# src/lib/introspection.py
# core/introspection.py

import inspect
import importlib
from typing import Type
Expand All @@ -11,15 +12,15 @@ def discover_config_class(module_name: str) -> Type:
Returns:
Type: The configuration class.
"""
types_module = importlib.import_module(f"src.{module_name}.types")
types_module = importlib.import_module(f"modules.{module_name}.types")

# Inspect the module to find dataclasses
for name, obj in inspect.getmembers(types_module):
if inspect.isclass(obj) and hasattr(obj, "__dataclass_fields__"):
# Developers note: module config dataclass is the first dataclass in src/<module_name>/types.py
# src/<module_name>/types.py is a mandatory file and the first dataclass must exist and be the config class.
return obj
raise ValueError(f"No dataclass found in src.{module_name}.types")
raise ValueError(f"No dataclass found in modules.{module_name}.types")

def discover_deploy_function(module_name: str) -> callable:
"""
Expand All @@ -29,13 +30,13 @@ def discover_deploy_function(module_name: str) -> callable:
Returns:
callable: The deploy function.
"""
deploy_module = importlib.import_module(f"src.{module_name}.deploy")
deploy_module = importlib.import_module(f"modules.{module_name}.deploy")

# Look for a deploy function that matches the pattern deploy_<module_name>_module
function_name = f"deploy_{module_name}_module"
deploy_function = getattr(deploy_module, function_name, None)

if not deploy_function:
raise ValueError(f"No deploy function named '{function_name}' found in src.{module_name}.deploy")
raise ValueError(f"No deploy function named '{function_name}' found in modules.{module_name}.deploy")

return deploy_function
2 changes: 1 addition & 1 deletion pulumi/src/lib/metadata.py → pulumi/core/metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# src/lib/metadata.py
# core/metadata.py

import subprocess
from typing import Dict
Expand Down
2 changes: 1 addition & 1 deletion pulumi/src/lib/namespace.py → pulumi/core/namespace.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# src/lib/namespace.py
# core/namespace.py

"""
Utility functions for managing Kubernetes namespaces within the Kargo PaaS platform.
Expand Down
2 changes: 1 addition & 1 deletion pulumi/src/lib/types.py → pulumi/core/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# src/lib/types.py
# core/types.py

"""
Types and data structures used across Kargo modules.
Expand Down
2 changes: 1 addition & 1 deletion pulumi/src/lib/utils.py → pulumi/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# src/lib/utils.py
# core/utils.py
# Description: Utility functions for sanitizing compliance metadata labels.

import re
Expand Down
6 changes: 3 additions & 3 deletions pulumi/src/lib/versions.py → pulumi/core/versions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# src/lib/versions.py
# core/versions.py

import json
import os
Expand Down Expand Up @@ -89,14 +89,14 @@ def load_versions_from_url(url):
if versions_stack_name:
# Attempt to load versions from a stack-specific file (e.g., ./versions/dev.json)
current_dir = os.path.dirname(os.path.abspath(__file__))
versions_dir = os.path.join(current_dir, '..', '..', 'versions')
versions_dir = os.path.join(current_dir, '..', 'versions')
stack_versions_path = os.path.join(versions_dir, f'{stack_name}.json')
default_versions = load_versions_from_file(stack_versions_path)

# If versions are still not loaded, attempt to load from local default_versions.json
if not default_versions:
current_dir = os.path.dirname(os.path.abspath(__file__))
default_versions_path = os.path.join(current_dir, '..', '..', 'default_versions.json')
default_versions_path = os.path.join(current_dir, '..', 'default_versions.json')
default_versions = load_versions_from_file(default_versions_path)

# If still not loaded, attempt to fetch from a remote URL based on the specified channel
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# src/cert_manager/deploy.py
# modules/cert_manager/deploy.py

import pulumi
import pulumi_kubernetes as k8s
from pulumi_kubernetes.apiextensions.CustomResource import CustomResource
from typing import Optional, List, Dict, Any, Tuple

from src.lib.namespace import create_namespace
from src.lib.helm_chart_versions import get_latest_helm_chart_version
from src.cert_manager.types import CertManagerConfig
from src.lib.types import NamespaceConfig
from src.lib.metadata import get_global_annotations, get_global_labels
from core.namespace import create_namespace
from core.helm_chart_versions import get_latest_helm_chart_version
from core.types import NamespaceConfig
from core.metadata import get_global_annotations, get_global_labels
from .types import CertManagerConfig

def deploy_cert_manager_module(
config_cert_manager: CertManagerConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# src/cert_manager/types.py
# modules/cert_manager/types.py

from dataclasses import dataclass
from typing import Optional, Dict, Any
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
import yaml
import tempfile
import os
from typing import Optional, List, Tuple, Dict
from typing import Optional, List, Tuple, Dict, Any

import pulumi
import pulumi_kubernetes as k8s
from pulumi_kubernetes.apiextensions.CustomResource import CustomResource
from pulumi_kubernetes.meta.v1 import ObjectMetaArgs

from src.lib.namespace import create_namespace
from src.lib.types import NamespaceConfig
from core.types import NamespaceConfig
from core.namespace import create_namespace
from core.metadata import get_global_labels, get_global_annotations

from .types import KubeVirtConfig
from src.lib.metadata import get_global_labels, get_global_annotations

def deploy_kubevirt_module(
config_kubevirt: KubeVirtConfig,
Expand Down Expand Up @@ -45,7 +46,7 @@ def deploy_kubevirt_module(
# Now deploy KubeVirt, ensuring it depends on the namespace creation
kubevirt_version, kubevirt_operator = deploy_kubevirt(
config_kubevirt=config_kubevirt,
depends_on=namespace_resource,
depends_on=[namespace_resource],
k8s_provider=k8s_provider,
namespace_resource=namespace_resource
)
Expand Down Expand Up @@ -93,7 +94,7 @@ def deploy_kubevirt(
# Transform the YAML and set the correct namespace
transformed_yaml = _transform_yaml(kubevirt_yaml, namespace)

def kubevirt_transform(obj: dict, opts: pulumi.ResourceOptions):
def kubevirt_transform(obj: dict, opts: pulumi.ResourceOptions) -> pulumi.ResourceTransformationResult:
"""
Transformation function to add labels and annotations to Kubernetes objects
in the KubeVirt operator YAML manifests.
Expand All @@ -117,8 +118,7 @@ def kubevirt_transform(obj: dict, opts: pulumi.ResourceOptions):
obj["spec"]["template"]["metadata"].setdefault("annotations", {})
obj["spec"]["template"]["metadata"]["annotations"].update(annotations)

# Debugging: print the transformed object
#print(f"Transformed object: {obj['metadata']}")
return pulumi.ResourceTransformationResult(obj, opts)

# Write the transformed YAML to a temporary file
with tempfile.NamedTemporaryFile(delete=False, mode='w') as temp_file:
Expand Down Expand Up @@ -192,7 +192,7 @@ def kubevirt_transform(obj: dict, opts: pulumi.ResourceOptions):
return version, operator


def _transform_yaml(yaml_data, namespace: str) -> List[Dict]:
def _transform_yaml(yaml_data: Any, namespace: str) -> List[Dict]:
"""
Helper function to transform YAML to set namespace and modify resources.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# src/kubevirt/types.py
# modules/kubevirt/types.py

from dataclasses import dataclass, field
from typing import Optional, Dict, Any
import pulumi
from src.lib.metadata import get_global_labels, get_global_annotations
from core.metadata import get_global_labels, get_global_annotations

@dataclass
class KubeVirtConfig:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 28bf6ed

Please sign in to comment.