|
| 1 | + |
| 2 | +## Checks |
| 3 | + |
| 4 | +The validation system uses a modular check architecture that automatically discovers and runs validation checks based on the compute engine. |
| 5 | + |
| 6 | +### Core Components |
| 7 | + |
| 8 | +- **ValidationResult**: Represents the outcome of a single check with `name`, `success` (bool), `details`, and optional `remediation` |
| 9 | +- **ValidationCheck**: Couples a validation function with metadata (`name`, `run`, `remediation`, `only_for`) |
| 10 | +- **Check modules**: Python files in `tecton_validate/checks/` that define validation logic |
| 11 | + |
| 12 | +### How Checks Work |
| 13 | + |
| 14 | +1. **Auto-discovery**: All `.py` files in `tecton_validate/checks/` are automatically imported |
| 15 | +2. **Aggregation**: Each module's `CHECKS` list is collected into a master list |
| 16 | +3. **Filtering**: Only checks applicable to the specified `--compute-engine` are executed |
| 17 | +4. **Execution**: Each check receives CLI args, boto3 session, and Rich console for output |
| 18 | + |
| 19 | +### Adding a New Check |
| 20 | + |
| 21 | +Create a new check by adding a function and registering it in any check module: |
| 22 | + |
| 23 | +```python |
| 24 | +# In tecton_validate/checks/my_new_checks.py |
| 25 | +from tecton_validate.validation_types import ValidationCheck, ValidationResult |
| 26 | +import argparse |
| 27 | +import boto3 |
| 28 | +from rich.console import Console |
| 29 | + |
| 30 | +def _check_my_feature(args: argparse.Namespace, session: boto3.Session, console: Console) -> ValidationResult: |
| 31 | + """Check some aspect of the infrastructure.""" |
| 32 | + try: |
| 33 | + # Your validation logic here |
| 34 | + if everything_looks_good: |
| 35 | + return ValidationResult( |
| 36 | + name="My Feature Check", |
| 37 | + success=True, |
| 38 | + details="Feature is properly configured." |
| 39 | + ) |
| 40 | + else: |
| 41 | + return ValidationResult( |
| 42 | + name="My Feature Check", |
| 43 | + success=False, |
| 44 | + details="Feature misconfiguration detected.", |
| 45 | + remediation="Run 'terraform apply' to fix the configuration." |
| 46 | + ) |
| 47 | + except Exception as e: |
| 48 | + return ValidationResult( |
| 49 | + name="My Feature Check", |
| 50 | + success=False, |
| 51 | + details=f"Error during validation: {e}", |
| 52 | + remediation="Check AWS permissions and network connectivity." |
| 53 | + ) |
| 54 | + |
| 55 | +# Register the check |
| 56 | +CHECKS = [ |
| 57 | + ValidationCheck( |
| 58 | + name="My Feature Check", |
| 59 | + run=_check_my_feature, |
| 60 | + remediation="Ensure feature is enabled in your Terraform configuration.", |
| 61 | + ) |
| 62 | +] |
| 63 | +``` |
| 64 | + |
| 65 | +### Restricting Checks to Specific Compute Engines |
| 66 | + |
| 67 | +To make a check run only for certain compute engines, add an `only_for` attribute to the ValidationCheck object: |
| 68 | + |
| 69 | +```python |
| 70 | +# Check runs only for EMR |
| 71 | +MyCheck.only_for = ["emr"] |
| 72 | + |
| 73 | +# Check runs for multiple engines |
| 74 | +MyCheck.only_for = ["emr", "databricks"] |
| 75 | + |
| 76 | +# No only_for attribute = runs for all engines (default) |
| 77 | +``` |
| 78 | + |
| 79 | +Available compute engines: `"rift"`, `"emr"`, `"databricks"` |
| 80 | + |
| 81 | +### Expected Function Signature |
| 82 | + |
| 83 | +All check functions must follow this signature: |
| 84 | + |
| 85 | +```python |
| 86 | +def check_function( |
| 87 | + args: argparse.Namespace, # CLI arguments |
| 88 | + session: boto3.Session, # Configured AWS session |
| 89 | + console: Console # Rich console for output |
| 90 | +) -> ValidationResult: |
| 91 | + pass |
| 92 | +``` |
| 93 | + |
| 94 | +### Common Patterns |
| 95 | + |
| 96 | +**AWS Resource Checks:** |
| 97 | +```python |
| 98 | +def _check_s3_bucket(args, session, console): |
| 99 | + s3 = session.client("s3") |
| 100 | + bucket_name = f"tecton-{args.cluster_name}" |
| 101 | + try: |
| 102 | + s3.head_bucket(Bucket=bucket_name) |
| 103 | + return ValidationResult("S3 Bucket", True, f"Bucket {bucket_name} exists") |
| 104 | + except ClientError: |
| 105 | + return ValidationResult("S3 Bucket", False, f"Bucket {bucket_name} not found") |
| 106 | +``` |
| 107 | + |
| 108 | +**IAM Policy Validation:** |
| 109 | +```python |
| 110 | +from tecton_validate.policy_test import test_policy |
| 111 | + |
| 112 | +def _check_iam_permissions(args, session, console): |
| 113 | + result = test_policy(session, role_arn, policy_document, actions_to_test) |
| 114 | + return ValidationResult("IAM Permissions", result.success, result.details) |
| 115 | +``` |
| 116 | + |
| 117 | +**Terraform Output Integration:** |
| 118 | +```python |
| 119 | +from tecton_validate.terraform import load_terraform_outputs |
| 120 | + |
| 121 | +def _check_terraform_resource(args, session, console): |
| 122 | + if args.terraform_outputs: |
| 123 | + outputs = load_terraform_outputs(args.terraform_outputs) |
| 124 | + resource_id = outputs.get("my_resource_id") |
| 125 | + # Validate the resource exists... |
| 126 | +``` |
0 commit comments