Tiltfile.opencost 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. load('ext://helm_resource', 'helm_resource', 'helm_repo')
  2. load('ext://restart_process', 'docker_build_with_restart')
  3. load('ext://secret', 'secret_create_generic')
  4. def get_docker_platform(arch):
  5. if arch == "arm64":
  6. return "linux/arm64"
  7. else:
  8. return "linux/amd64"
  9. def get_go_arch(arch):
  10. if arch == "arm64":
  11. return "arm64"
  12. else:
  13. return "amd64"
  14. # run_opencost is encapsulated as a function to make import easier for running alongside kubecost.
  15. # The `../opencost` pattern that repeats across this function is to deal with how multiple tilt
  16. # files work - the base directory (`.`) is always relative to the Tiltfile executed and not the
  17. # directory containing this file.
  18. def run_opencost(options):
  19. docker_platform = get_docker_platform(options["arch"])
  20. go_arch = get_go_arch(options["arch"])
  21. is_cloud_integration = options["cloud_integration"] != '' and os.path.exists(options["cloud_integration"])
  22. is_service_key = options["service_key"] != '' and os.path.exists(options["service_key"])
  23. continue_flag = '--continue'
  24. if options["delve_continue"] == False:
  25. continue_flag = ''
  26. # Build and update opencost back end binary when code changes
  27. local_resource(
  28. name='build-costmodel',
  29. dir='.',
  30. cmd='CGO_ENABLED=0 GOOS=linux GOARCH='+go_arch+' go build -o ../opencost/cmd/costmodel/costmodel-tilt ../opencost/cmd/costmodel/main.go',
  31. deps=[
  32. '../opencost/cmd/costmodel/main.go',
  33. '../opencost/pkg',
  34. ],
  35. allow_parallel=True,
  36. )
  37. # Build back end docker container
  38. # If the binary is updated, update the running container and restart binary in dlv
  39. docker_build_with_restart(
  40. ref=options["docker_repo"]+'opencost-costmodel',
  41. context='../opencost',
  42. # remove --continue flag to make dlv wait until debugger is attached to start
  43. entrypoint='/go/bin/dlv exec --listen=:40000 --api-version=2 --headless=true --accept-multiclient --log '+continue_flag+' /app/main',
  44. dockerfile='../opencost/Dockerfile.debug',
  45. platform=docker_platform,
  46. build_args={'binary_path': './cmd/costmodel/costmodel-tilt'},
  47. only=[
  48. 'cmd/costmodel/costmodel-tilt',
  49. 'configs',
  50. ],
  51. live_update=[
  52. sync('../opencost/cmd/costmodel/costmodel-tilt', '/app/main'),
  53. ],
  54. )
  55. # npm install if package.json changes
  56. local_resource(
  57. name='build-npm-install',
  58. dir='../opencost/ui',
  59. cmd='npm install',
  60. deps=[
  61. '../opencost/ui/package.json',
  62. ],
  63. allow_parallel=True,
  64. )
  65. # Build FE locally when code changes
  66. local_resource(
  67. name='build-ui',
  68. dir='../opencost/ui',
  69. cmd='npx parcel build src/index.html',
  70. deps=[
  71. '../opencost/ui/src',
  72. '../opencost/ui/package.json',
  73. ],
  74. allow_parallel=True,
  75. resource_deps=['build-npm-install'],
  76. )
  77. # update container when relevant files change
  78. docker_build(
  79. ref=options["docker_repo"]+'opencost-ui',
  80. context='../opencost/ui',
  81. dockerfile='../opencost/ui/Dockerfile.cross',
  82. only=[
  83. 'dist',
  84. 'nginx.conf',
  85. 'default.nginx.conf.template',
  86. 'docker-entrypoint.sh',
  87. ],
  88. live_update=[
  89. sync('../opencost/ui/dist', '/var/www'),
  90. ],
  91. )
  92. values_set = [
  93. 'opencost.ui.image.fullImageName='+options["docker_repo"]+'opencost-ui',
  94. 'opencost.exporter.image.fullImageName='+options["docker_repo"]+'opencost-costmodel',
  95. 'opencost.prometheus.internal.namespaceName='+k8s_namespace(),
  96. 'opencost.exporter.debugPort=40000',
  97. ]
  98. if is_cloud_integration:
  99. values_set.append('opencost.cloudIntegrationSecret=cloud-integration')
  100. values_set.append('opencost.cloudCost.enabled=true')
  101. else:
  102. values_set.append('opencost.cloudCost.enabled=false')
  103. if is_cloud_integration:
  104. secret_create_generic(
  105. name='cloud-integration',
  106. namespace=k8s_namespace(),
  107. from_file=options["cloud_integration"],
  108. secret_type=None,
  109. from_env_file=None
  110. )
  111. if is_service_key:
  112. secret_create_generic(
  113. name='service-key',
  114. namespace=k8s_namespace(),
  115. from_file=options["service_key"],
  116. secret_type=None,
  117. from_env_file=None
  118. )
  119. # build yaml for deployment to k8s
  120. yaml = helm(
  121. '../opencost-helm-chart/charts/opencost',
  122. name='opencost',
  123. values=[options["helm_values"]],
  124. set=values_set
  125. )
  126. k8s_yaml(yaml) # put resulting yaml into k8s
  127. port_forwards = [
  128. options['port_costmodel']+':9003',
  129. options['port_ui']+':9090',
  130. options['port_debug']+':40000',
  131. ]
  132. k8s_resource(workload='opencost', port_forwards=port_forwards)
  133. helm_resource(
  134. name='prometheus',
  135. chart='prometheus-community/prometheus')
  136. k8s_resource(workload='prometheus', port_forwards=[options['port_prometheus']+':9090'])
  137. local_resource(
  138. name='costmodel-test',
  139. dir='../opencost',
  140. cmd='go test ./...',
  141. deps=[
  142. './pkg',
  143. ],
  144. allow_parallel=True,
  145. resource_deps=['opencost'], # run tests after build to speed up deployment
  146. )