关于 devops-maintainability-checker
devops-maintainability-checker 是用于检查基础设施代码可维护性的技能,面向编写和审查 Terraform/OpenTofu 配置的工程师。它依据格式、命名、变量说明、重复代码和版本约束等规则生成检查报告。
主要功能
- 01检查资源与变量命名
- 02验证代码格式
- 03检查变量描述
- 04识别重复值与 locals 使用
- 05检查版本约束
适用场景
- 01审查基础设施代码的可维护性
- 02在提交前检查 Terraform 配置
- 03定位缺少说明或版本约束的配置
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ majesticlabs-dev/ majestic-marketplace --skill devops-maintainability-checker安装后在 Claude Code 中输入/devops-maintainability-checker确认已加载。
详细介绍
DevOps 可维护性检查器
验证基础设施代码是否遵循可维护模式。
可维护性检查清单
| 检查项 | 良好 | 不良 |
|---|---|---|
| 资源命名 | 统一使用 ${project}-${env}-${type} | 随意或不一致 |
| 变量命名 | 描述清晰且有默认值 | 名称晦涩,没有描述 |
| 代码格式 | 通过 tofu fmt 检查 | 缩进不一致 |
| DRY 原则 | 重复值使用本地值 | 重复硬编码值 |
| 版本约束 | 固定为 ~> X.Y | 未固定或使用精确版本 |
验证命令
# Format check
tofu fmt -check -recursive 2>&1 || echo "FAIL: Needs formatting"
# Variable descriptions
grep -L "description" variables.tf && echo "WARN: Variables missing descriptions"
# Locals usage (should have some)
grep -c "local\." *.tf | awk -F: '$2 < 3 {print "WARN: Underusing locals in "$1}'
# Hardcoded values (potential DRY violations)
grep -E '^\s+(region|zone|size)\s*=\s*"[^$]' *.tf
# Provider version constraints
grep -E "version\s*=\s*\"[0-9]" *.tf | grep -v "~>" && echo "WARN: Exact versions, use ~>"
命名约定
资源名称:
# GOOD
resource "aws_instance" "web" {
tags = {
Name = "${var.project}-${var.environment}-web"
}
}
# BAD
resource "aws_instance" "instance1" {
tags = {
Name = "my-server"
}
}
变量名称:
# GOOD
variable "database_instance_class" {
description = "RDS instance class for the database"
type = string
default = "db.t3.micro"
}
# BAD
variable "db_class" {
type = string
}
DRY 模式
使用本地值:
locals {
common_tags = {
Project = var.project
Environment = var.environment
ManagedBy = "terraform"
}
name_prefix = "${var.project}-${var.environment}"
}
resource "aws_instance" "web" {
tags = merge(local.common_tags, {
Name = "${local.name_prefix}-web"
})
}
避免重复:
# BAD - hardcoded everywhere
resource "aws_instance" "web" {
tags = { Project = "myapp", Environment = "prod" }
}
resource "aws_instance" "api" {
tags = { Project = "myapp", Environment = "prod" }
}
# GOOD - use locals
resource "aws_instance" "web" {
tags = local.common_tags
}
版本约束
# GOOD - allows patch updates
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
# BAD - exact version (breaks updates)
version = "5.31.0"
# BAD - no constraint (unpredictable)
version = ">= 5.0"
报告格式
MAINTAINABILITY SCORE: X/10
Formatting: PASS/FAIL
Variable Descriptions: X/Y documented
Locals Usage: GOOD/UNDERUSED
Naming Consistency: CONSISTENT/INCONSISTENT
Version Constraints: PROPER/NEEDS FIX
Issues Found:
- [ ] Run `tofu fmt -recursive`
- [ ] Add descriptions to variables: X, Y, Z
- [ ] Extract repeated value "us-east-1" to local
- [ ] Change exact version to ~> constraint
评论
支持 Markdown