Skip to content
Published on

Helmプラグインとテスト戦略: 拡張性と品質保証

Authors

1. プラグインシステム

1.1 プラグイン構造

# plugin.yaml
name: 'my-plugin'
version: '1.0.0'
usage: 'A custom Helm plugin'
command: '$HELM_PLUGIN_DIR/bin/my-plugin'
hooks:
  install: '$HELM_PLUGIN_DIR/scripts/install.sh'
  update: '$HELM_PLUGIN_DIR/scripts/update.sh'

1.2 主要プラグイン

helm-diff: アップグレード前の変更プレビュー

helm plugin install https://github.com/databus23/helm-diff
helm diff upgrade my-release ./my-chart -f values.yaml
helm diff revision my-release 2 3

helm-secrets: シークレット管理

helm plugin install https://github.com/jkroepke/helm-secrets
helm secrets install my-release ./my-chart -f secrets.yaml

helm-unittest: チャートユニットテスト

helm plugin install https://github.com/helm-unittest/helm-unittest
helm unittest ./my-chart

2. チャートテスト

2.1 helm test

リリース後にクラスタ内で実行される組み込みテストメカニズム:

# templates/tests/test-connection.yaml
apiVersion: v1
kind: Pod
metadata:
  name: {{ include "my-chart.fullname" . }}-test-connection
  annotations:
    "helm.sh/hook": test
spec:
  restartPolicy: Never
  containers:
    - name: wget
      image: busybox
      command: ['wget']
      args: ['{{ include "my-chart.fullname" . }}:{{ .Values.service.port }}']

2.2 helm-unittest

クラスタなしでローカル実行されるユニットテスト:

# tests/deployment_test.yaml
suite: test deployment
templates:
  - deployment.yaml
tests:
  - it: should create deployment with correct replicas
    set:
      replicaCount: 3
    asserts:
      - isKind:
          of: Deployment
      - equal:
          path: spec.replicas
          value: 3

  - it: should set correct image
    set:
      image:
        repository: nginx
        tag: '1.25'
    asserts:
      - equal:
          path: spec.template.spec.containers[0].image
          value: 'nginx:1.25'

2.3 ct(chart-testing)ツール

CI/CDパイプラインでのチャート変更検出とテスト自動化:

ct list-changed --target-branch main
ct lint --target-branch main
ct lint-and-install --target-branch main

3. リンティング

helm lint ./my-chart --strict
helm lint ./my-chart -f production-values.yaml
helm template my-release ./my-chart | kubeconform -strict -kubernetes-version 1.29.0

4. スキーマ検証

{
  "$schema": "https://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["replicaCount", "image"],
  "properties": {
    "replicaCount": { "type": "integer", "minimum": 1, "maximum": 100 },
    "image": {
      "type": "object",
      "required": ["repository"],
      "properties": {
        "repository": { "type": "string" },
        "tag": { "type": "string", "default": "latest" },
        "pullPolicy": { "type": "string", "enum": ["Always", "IfNotPresent", "Never"] }
      }
    }
  }
}

スキーマはhelm installhelm upgradehelm linthelm templateの実行時に自動検証されます。


5. OCIレジストリ

helm package ./my-chart
helm push my-chart-1.0.0.tgz oci://ghcr.io/myorg/charts
helm install my-release oci://ghcr.io/myorg/charts/my-chart --version 1.0.0

6. チャートの署名と検証

helm package --sign --key 'my-key' --keyring ~/.gnupg/secring.gpg ./my-chart
helm verify my-chart-1.0.0.tgz
helm install my-release my-chart-1.0.0.tgz --verify

7. まとめ

Helmの拡張性と品質保証:

  1. プラグインシステム: helm-diff、helm-secrets、helm-unittestで機能拡張
  2. 多層テスト: helm test(統合)、unittest(ユニット)、ct(CI/CD)の組み合わせ
  3. リンティング: helm lint、yamllint、kubeconformによる多角的検証
  4. スキーマ検証: values.schema.jsonで入力値の妥当性を保証
  5. OCIレジストリ: コンテナイメージと同じワークフローでチャートをデプロイ
  6. 署名/検証: チャートの整合性と出所の証明