Kubernetes設定用のYAML Schemaを生成する

KubernetesリソースJSONをYAML Schema定義に変換する方法を学びます。Deployment、Service、ConfigMapスキーマ、リソース制限、ラベル/セレクター、カスタムリソース定義(CRD)を解説します。

Real-World Configs

詳細な説明

Kubernetes設定からYAML Schemaへ

KubernetesリソースはYAMLで定義され、厳密な構造ルールに従います。サンプルKubernetesリソースJSONをYAML Schemaに変換することで、カスタムリソースとHelmチャート値の検証レイヤーを作成します。

Kubernetes DeploymentのJSON例

{
  "apiVersion": "apps/v1",
  "kind": "Deployment",
  "metadata": {
    "name": "web-app",
    "namespace": "production",
    "labels": {
      "app": "web-app",
      "env": "production"
    }
  },
  "spec": {
    "replicas": 3,
    "selector": {
      "matchLabels": {
        "app": "web-app"
      }
    },
    "template": {
      "spec": {
        "containers": [
          {
            "name": "web",
            "image": "myapp:2.0.0",
            "ports": [
              { "containerPort": 8080 }
            ],
            "resources": {
              "requests": {
                "cpu": "100m",
                "memory": "128Mi"
              },
              "limits": {
                "cpu": "500m",
                "memory": "512Mi"
              }
            }
          }
        ]
      }
    }
  }
}

生成されるYAML Schema(簡略版)

type: object
properties:
  apiVersion:
    type: string
    const: "apps/v1"
  kind:
    type: string
    const: Deployment
  metadata:
    type: object
    properties:
      name:
        type: string
        pattern: "^[a-z0-9][a-z0-9-]*[a-z0-9]$"
        maxLength: 253
      namespace:
        type: string
      labels:
        type: object
        patternProperties:
          "^[a-zA-Z0-9][a-zA-Z0-9._-]*$":
            type: string
    required:
      - name
  spec:
    type: object
    properties:
      replicas:
        type: integer
        minimum: 0
        default: 1
      selector:
        type: object
        properties:
          matchLabels:
            type: object
            patternProperties:
              ".*":
                type: string
        required:
          - matchLabels
      template:
        type: object
        properties:
          spec:
            $ref: "#/definitions/PodSpec"
    required:
      - replicas
      - selector
      - template
required:
  - apiVersion
  - kind
  - metadata
  - spec

definitions:
  PodSpec:
    type: object
    properties:
      containers:
        type: array
        items:
          $ref: "#/definitions/Container"
        minItems: 1
  Container:
    type: object
    properties:
      name:
        type: string
      image:
        type: string
        pattern: "^[a-z0-9][a-z0-9._/-]*:[a-zA-Z0-9._-]+$"
      ports:
        type: array
        items:
          type: object
          properties:
            containerPort:
              type: integer
              minimum: 1
              maximum: 65535
      resources:
        $ref: "#/definitions/ResourceRequirements"
    required:
      - name
      - image
  ResourceRequirements:
    type: object
    properties:
      requests:
        type: object
        properties:
          cpu:
            type: string
            pattern: "^\\d+m?$"
          memory:
            type: string
            pattern: "^\\d+(Ki|Mi|Gi|Ti)?$"
      limits:
        type: object
        properties:
          cpu:
            type: string
          memory:
            type: string

Helm values.yamlスキーマ

Helm 3はチャート値の検証にvalues.schema.jsonをサポートしています。デフォルト値をYAMLスキーマに変換することで、この検証が自動的に作成されます:

# values.schema.yaml
type: object
properties:
  replicaCount:
    type: integer
    minimum: 1
  image:
    type: object
    properties:
      repository:
        type: string
      tag:
        type: string
      pullPolicy:
        type: string
        enum: [Always, IfNotPresent, Never]

カスタムリソース定義(CRD)

Kubernetes CRDはOpenAPI v3スキーマを使用してカスタムリソースを検証します。同じJSON-to-YAML-Schema変換は、CRDのspec.validation.openAPIV3Schemaに適したスキーマを生成します。

ユースケース

プラットフォームエンジニアリングチームは、組織の標準に従わなければならない内部Kubernetesテンプレートとhelmチャートを作成します。リファレンスデプロイメントから生成されたYAMLスキーマは、ローカル開発とCIチェック中に、リソース制限の欠落、無効なラベル形式、不正なAPIバージョンなどの一般的なエラーを検出します。

試してみる — JSON to YAML Schema

フルツールを開く