gashirar's blog

ウイスキーがすき/美味しいものがすき/k8sがすき

KubernetesのManifestから関連図を作るツールを作った

はじめに

KubernetesのManifestからResource同士のつながりを表示するツールを作りました。 (ただしくはPlantUMLのコード生成で、図の生成はPlantUMLにおまかせ)

KubernetesでPlantUMLなのでkuml

github.com

サンプル

入力

今回は下記にあるManifestをもとに図を生成してみます。

kuml/example/application at master · gashirar/kuml · GitHub

出力

ディレクトリまたはYAMLを指定してコマンドを実行すると

kuml example/application

下記のようなPlantUMLのコードが生成されます。

@startuml
rectangle "kind: ConfigMap\nname: adapter-app-properties" as default_ConfigMap_adapter_app_properties
rectangle "kind: ConfigMap\nname: adapter-infra-properties" as default_ConfigMap_adapter_infra_properties
rectangle "kind: ConfigMap\nname: application-app-properties" as default_ConfigMap_application_app_properties
rectangle "kind: ConfigMap\nname: application-infra-properties" as default_ConfigMap_application_infra_properties
rectangle "kind: Deployment\nname: sample-deployment" as default_Deployment_sample_deployment
rectangle "kind: HorizontalPodAutoscaler\nname: sample-horizontalpodautoscaler" as default_HorizontalPodAutoscaler_sample_horizontalpodautoscaler
rectangle "kind: Ingress\nname: sample-ingress" as default_Ingress_sample_ingress
rectangle "kind: PodDisruptionBudget\nname: sample-poddisruptionbudget" as default_PodDisruptionBudget_sample_poddisruptionbudget
rectangle "kind: Pod\nname: sample-deployment" as default_Pod_sample_deployment
rectangle "kind: ReplicaSet\nname: sample-deployment" as default_ReplicaSet_sample_deployment
rectangle "kind: Service\nname: sample-service" as default_Service_sample_service
default_Deployment_sample_deployment -DOWN-> default_ReplicaSet_sample_deployment : ""
default_ReplicaSet_sample_deployment -DOWN-> default_Pod_sample_deployment : ""
default_Pod_sample_deployment -DOWN-> default_ConfigMap_adapter_app_properties : ""
default_Pod_sample_deployment -DOWN-> default_ConfigMap_adapter_infra_properties : ""
default_Pod_sample_deployment -DOWN-> default_ConfigMap_application_app_properties : ""
default_Pod_sample_deployment -DOWN-> default_ConfigMap_application_infra_properties : ""
default_Service_sample_service -RIGHT-> default_Pod_sample_deployment : ""
default_Ingress_sample_ingress -RIGHT-> default_Service_sample_service : ""
default_Ingress_sample_ingress -RIGHT-> default_Service_sample_service : ""
default_PodDisruptionBudget_sample_poddisruptionbudget -LEFT-> default_Pod_sample_deployment : ""
default_HorizontalPodAutoscaler_sample_horizontalpodautoscaler -LEFT-> default_Deployment_sample_deployment : ""
@enduml

あとは適当な生成ツールにかけると、下記のような画像が生成されます。
おつかれさまでした。

f:id:gashirar:20200520094711p:plain

実装の方針

細かい話はgithubのソースを見てもらえば。かなり泥臭くResource同士の関連をPlantUMLに落とし込んでます。(DeploymentSpecの中にあるPodSpecのcontainersに指定された・・・みたいなことを泥臭く)

Controllerでもっとスマートに似たようなことをやっているはずなのでそれを参考にしようかと思ったのですが、Go力が及ばず地道にやっ(てしまっ)ています。
CustomResourceDefinitionは未対応。どうやってやるのがスマートだろう。

まとめ

Kubernetesを初めて触るような方だと、PodとConfigMapやServiceとの紐づけなどの理解が進むかもしれないです。試しに遊んでみてもらえると嬉しいです。
ただ、今回作ったツールで図示できる程度の関連は一週間くらいYAMLとたわむれていればすぐに理解して身につくような気もするので需要は少ない気も。

あとは実際にkubectl applyしてWeaveScopeとかOctantとかでみるのも良いですね。

github.com

github.com

まぁコード書くのが楽しかったのでOKということでここはひとつ。