Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Helm Chart を Unit Test する / Unit Testing Helm Chart

Helm Chart を Unit Test する / Unit Testing Helm Chart

Kubernetes Meetup Tokyo #42 で発表したスライドです
https://k8sjp.connpass.com/event/214286/

Cad656ed619672b702191833dc819943?s=128

d-kuro

June 24, 2021
Tweet

Transcript

  1. Chart Λ Unit Test ͢Δ Kubernetes Meetup Tokyo #42 LT

  2. I work for @ponde_m @d-kuro

  3. Summary • Helm Chart ͷ Test ʹ͍ͭͯ • Helm Chart

    Λ Unit Test ͢ΔͨΊͷϥΠϒϥϦΛ࣮૷ͨ͠࿩ • ࣮૷ͷৄࡉ • “k8s.io” ϥΠϒϥϦΛ࢖ͬͨ Tips ͷ঺հ
  4. Testing Helm Charts --set autoscaling.enabled=false --set replicaCount=3

  5. Testing Helm Charts • Helm ʹ͸ςετػߏ͕ଘࡏ͢Δ • “helm.sh/hook: test” annotation

    Λ෇༩ͨ͠ Pod ఆٛΛ༻ҙ͢Δ • “<chart-name>/templates/tests/“ ʹ഑ஔ͢Δ • “helm install” ͨ͋͠ͱʹ “helm test” Λ࣮ߦ͢Δ • Pod ͕ exit 0 Ͱऴྃ͢Ε͹ςετ͕੒ޭͨ͠ͱΈͳ͞ΕΔ
  6. None
  7. None
  8. Testing Helm Charts • ͜ͷςετػߏ͸ͲͪΒ͔ͱ͍͑͹ Integration Test • values.yaml ʹఆٛͨ͠

    Option ͕ҙਤͨ͠௨Γʹػೳ͢Δ͔ 
 Ͳ͏͔ͳͲͷ Unit Test Ϩϕϧͷ֬ೝ͕͔ͨͬͨ͠
  9. Unit Testing Helm Charts • Go Ͱ Helm Chart Λ

    Unit Test ͢Δ • “helm.sh/helm/v3” Λ import ͯ͠ 
 “helm template” ίϚϯυͰ࢖༻͍ͯ͠Δؔ਺Λݺͼग़͢ • ϨϯμϦϯά͞Εͨ YAML ͕ string Ͱ໯͑ΔͷͰ 
 ͍͍ײ͡ʹൺֱ͢Δ
  10. Unit Testing Helm Charts • ࡞੒ͨ͠ϥΠϒϥϦ • github.com/d-kuro/helmut • “helm

    template” ίϚϯυ૬౰ͷॲཧΛ࣮ߦ͢ΔϥΠϒϥϦ • Assert ༻ͷؔ਺Λఏڙ • github.com/google/go-cmp Λ࢖༻ͨ͠ diff Λදࣔ͢Δ
  11. None
  12. None
  13. None
  14. Unit Testing Helm Charts • ྨࣅͷϥΠϒϥϦ • github.com/gruntwork-io/terratest/modules/helm • ಺෦Ͱ

    “helm tempalte —show-only” ίϚϯυΛ࣮ߦ͍ͯ͠Δ • github.com/Waterdrips/helmunit • Helm ͷύοέʔδΛ import ͢Δಉ༷ͷΞϓϩʔν • Assert ؔ਺ͳͲ͸ͳ͍
  15. Architecture map[ObjectKey]runtime.Object Rendered Manifests Exec helm template Render Convert Get

    User
  16. Exec helm template

  17. None
  18. Split Manifests

  19. None
  20. Convert from Raw Manifest to runtime.Object

  21. None
  22. Assert • ݱࡏ͸ Contains ؔ਺͔࣮͠૷͍ͯ͠ͳ͍ • Contains ؔ਺ʹ౉͞Εͨ Object Λ

    map ͔Β Get ͯ͠ 
 google/go-cmp Ͱൺֱͯ͠ߏ଄ମͷ diff Λදࣔ͢Δ • runtime.Object Λ௚઀౉͢ํ๏ͱ 
 YAML or JSON ͷ Manifest Λ౉͢ํ๏ͷ 2 ͭΛఏڙ • ౉͢ runtime.Object ͷ TypeMeta ͸ۭͳΒಈతʹઃఆ
  23. TypeMeta ͷهड़͕ෆཁʹͳΔ

  24. Get GVK from runtime.Object

  25. None
  26. Assert • Helm ʹΑ͘࢖ΘΕΔ label ΍ annotation ͳͲͷ 
 ࠩ෼Λແࢹ͢ΔͨΊͷ

    Option Λఏڙ
  27. Omit Metadata

  28. None
  29. Conclude • Helm Chart Λ Unit Test ͢ΔͨΊͷϥΠϒϥϦΛ࡞ͬͨ • github.com/d-kuro/helmut

    • ྑ͚Ε͹࢖͍ͬͯͩ͘͞
  30. Thank You for Listening