Skip to content

Kubernetes ManifestからInfrastructure Adapterへの変換

既存のKubernetes ManifestをInfrastructure Adapterに変換する方法を解説します。

本機能によって、0からInfrastructure Adapterを書かずに、既に動作確認ができているKubernetes Manifestを活用して、Qmonus Value StreamのCI/CDパイプラインを素早く作り上げることが可能です。

本ガイドで紹介するコマンドの詳細は、CLIリファレンスを参照してください。

また、Adapter作成の準備に従って、cue.mod/module.cueを使ったモジュール定義ができているリポジトリで作業をしてください。

ImportコマンドによるInfrastructure Adapterの生成

Qmonus Value Stream CLIのImportコマンドを実行して、Kubernetes ManifestからInfrastructure Adapterを作成します。以下のとおり、入力Manifestのファイルまたはディレクトリパス、および出力するInfrastructure Adapterファイル名を指定して実行します。

bash
qvsctl adapter import -f ${inputFileOrDir} -o ${outputFileOrDir}

例えば、manifest.yamlを読み込み、.valuestream/local/web/main.cueを作成するためには、以下のとおり実行します。

bash
qvsctl adapter import -f manifest.yaml -o .valuestream/local/web/main.cue

ここで生成したInfrastructure Adapterは、以下のように読み出したManifestをそれぞれresource0resource1resource2、・・・、とIDをインクリメントしてリソースIDとして付与します。

cue
package REPLACE_ME

DesignPattern: {
  name:        "REPLACE_ME"
  description: "REPLACE_ME"
  resources: {
    app: {
      resource0: ...  // インポートしたManifest#1
      resource1: ...  // インポートしたManifest#2
      resource2: ...  // インポートしたManifest#3
      ...
    }
  }
}

例えば、podを1つ定義したKubernetes Manifestを変換した場合、以下のようなInfrastructure Adapterが生成されます。

cue
package REPLACE_ME

DesignPattern: {
	name:        "REPLACE_ME"
	description: "REPLACE_ME"
	resources: {
		app: {
			resource0: {
				apiVersion: "v1"
				kind:       "Pod"
				metadata: {
					name:      "sample-pod"
					namespace: "test-namespace"
				}
				spec: {
					containers: [{
						name:  "nginx"
						image: "nginx:latest"
						ports: [{
							containerPort: 80
						}]
					}]
				}
			}
		}
	}
}

Infrastructure Adapterのパッケージ名を付与

前手順で作成されたInfrastructure Adapterはパッケージ名がREPLACE_MEとなっています。ここで、REPLACE_MEを置換して、任意のパッケージ名を付与します。 また、nameフィールドとdescriptionフィールドの値についても同様にREPLACE_MEとなっているので、それぞれ任意のInfrastructure Adapterの名前、説明文に置換します。

例えば、パッケージ名をweb、Infrastructure Adapterの名前も同様にweb、説明文をSample Nginx Serverとしたい場合、以下のように編集します。

bash
vi .valuestream/local/web/main.cue
cue
package web                           // パッケージ名

DesignPattern: {
  name:        "web"                  // Infrastructure Adapter short name
  description: "Sample Nginx Server"  // Infrastructure Adapterの説明
  resources:   ...                    // インポートされたManifest
}

作業しているリポジトリのモジュールパスがgithub.com/qmonus/sample-moduleである場合、上記例で作成したInfrastructure Adapterのインポートパスはgithub.com/qmonus/sample-module/local/webとなります。 詳しくは、Cloud Native Adapterのパッケージ仕様を参照してください。

また、上記例で置換したnameフィールドとdescriptionフィールドについての詳細は、Infrastructure Adapterの基本要素を参照してください。

Infrastructure Adapterが受け取るパラメータを設定

Importコマンドで作成したInfrastructure Adapterは、パラメータを受け付けないようになっています。 parametersフィールドを宣言することで、Infrastructure Adapterにパラメータを設定し、任意の値を受け取れるようにします。

以下の例では、Infrastructure Adapterのパラメータとしてk8sNamespaceimageNameを受け取り、podのnamespace名にk8sNamespaceの値が、コンテナのイメージ名にimageNameの値が渡るようにしています。

diff
package web

DesignPattern: {
	name:        "web"
	description: "Sample Nginx Server"
+	parameters: {
+		k8sNamespace: string
+		imageName: string
+	}
	resources: {
		app: {
			resource0: {
				apiVersion: "v1"
				kind:       "Pod"
				metadata: {
					name:      "sample-pod"
-					namespace: "test-namespace"
+					namespace: parameters.k8sNamespace
				}
				spec: {
					containers: [{
						name:  "nginx"
-						image: "nginx:latest"
+						image: parameters.imageName
						ports: [{
							containerPort: 80
						}]
					}]
				}
			}
		}
	}
}

以上で、Kubernetes ManifestからInfrastructure Adapterへの変換が完了しました。 変換したInfrastructure Adapterがコンパイルできることを確認する場合は、作成したInfrastructure Adapterのコンパイルの手順を参照してください。