Tiltfile 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- mode: Python -*-
  2. kubectl_cmd = "kubectl"
  3. # verify kubectl command exists
  4. if str(local("command -v " + kubectl_cmd + " || true", quiet = True)) == "":
  5. fail("Required command '" + kubectl_cmd + "' not found in PATH")
  6. # set defaults
  7. settings = {
  8. "debug": {
  9. "enabled": False,
  10. },
  11. }
  12. # merge default settings with user defined settings
  13. tilt_file = "./tilt-settings.yaml" if os.path.exists("./tilt-settings.yaml") else "./tilt-settings.json"
  14. settings.update(read_yaml(
  15. tilt_file,
  16. default = {},
  17. ))
  18. # set up the development environment
  19. # Split the YAML into CRDs and other resources
  20. objects = decode_yaml_stream(read_file('bin/deploy/manifests/external-secrets.yaml'))
  21. crds = []
  22. other_resources = []
  23. for o in objects:
  24. if o.get('kind') == 'CustomResourceDefinition':
  25. crds.append(o)
  26. else:
  27. other_resources.append(o)
  28. # Process deployments for development
  29. for o in other_resources:
  30. if o.get('kind') == 'Deployment' and o.get('metadata').get('name') in ['external-secrets-cert-controller', 'external-secrets', 'external-secrets-webhook']:
  31. o['spec']['template']['spec']['containers'][0]['securityContext'] = {'runAsNonRoot': False, 'readOnlyRootFilesystem': False}
  32. o['spec']['template']['spec']['containers'][0]['imagePullPolicy'] = 'Always'
  33. if settings.get('debug').get('enabled') and o.get('metadata').get('name') == 'external-secrets':
  34. o['spec']['template']['spec']['containers'][0]['ports'] = [{'containerPort': 30000}]
  35. # Create the directory
  36. local('mkdir -p .tilt-tmp')
  37. # Apply CRDs with server-side apply (handles large CRDs)
  38. if crds:
  39. crd_yaml = encode_yaml_stream(crds)
  40. local('cat > .tilt-tmp/external-secrets-crds.yaml', stdin=crd_yaml)
  41. local_resource(
  42. 'apply-crds',
  43. 'kubectl apply --server-side -f .tilt-tmp/external-secrets-crds.yaml',
  44. deps=['bin/deploy/manifests/external-secrets.yaml']
  45. )
  46. # Use regular k8s_yaml for deployments (Tilt will handle image substitution)
  47. if other_resources:
  48. deployments_yaml = encode_yaml_stream(other_resources)
  49. local('cat > .tilt-tmp/external-secrets-deployments.yaml', stdin=deployments_yaml)
  50. k8s_yaml('.tilt-tmp/external-secrets-deployments.yaml')
  51. load('ext://restart_process', 'docker_build_with_restart')
  52. # enable hot reloading by doing the following:
  53. # - locally build the whole project
  54. # - create a docker imagine using tilt's hot-swap wrapper
  55. # - push that container to the local tilt registry
  56. # Once done, rebuilding now should be a lot faster since only the relevant
  57. # binary is rebuilt and the hot swat wrapper takes care of the rest.
  58. gcflags = ''
  59. build_command_prefix = ''
  60. binary_deps = [
  61. "main.go",
  62. "go.mod",
  63. "go.sum",
  64. "apis",
  65. "cmd",
  66. "pkg",
  67. ]
  68. if settings.get('debug').get('enabled'):
  69. gcflags = '-N -l'
  70. build_command_prefix = 'make dlv && '
  71. binary_deps += [
  72. "Makefile",
  73. "hack/tools/go.mod",
  74. "hack/tools/go.sum",
  75. ]
  76. buildtags = settings.get('buildtags', 'all_providers')
  77. local_resource(
  78. 'external-secret-binary',
  79. "{prefix}CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags '{buildtags}' -gcflags '{gcflags}' -v -o bin/external-secrets ./".format(prefix=build_command_prefix, buildtags=buildtags, gcflags=gcflags),
  80. deps = binary_deps,
  81. )
  82. # Build the docker image for our controller. We use a specific Dockerfile
  83. # since tilt can't run on a scratch container.
  84. # `only` here is important, otherwise, the container will get updated
  85. # on _any_ file change. We only want to monitor the binary.
  86. # If debugging is enabled, we switch to a different docker file using
  87. # the delve port.
  88. entrypoint = ['/external-secrets']
  89. dockerfile = 'tilt.dockerfile'
  90. if settings.get('debug').get('enabled'):
  91. k8s_resource('external-secrets', port_forwards=[
  92. port_forward(30000, 30000, 'debugger'),
  93. ])
  94. entrypoint = ['/dlv', '--listen=:30000', '--api-version=2', '--continue=true', '--accept-multiclient=true', '--headless=true', 'exec', '/external-secrets', '--']
  95. dockerfile = 'tilt.debug.dockerfile'
  96. docker_build_with_restart(
  97. 'ghcr.io/external-secrets/external-secrets',
  98. '.',
  99. dockerfile = dockerfile,
  100. entrypoint = entrypoint,
  101. only=[
  102. './bin',
  103. ],
  104. live_update = [
  105. sync('./bin/external-secrets', '/external-secrets'),
  106. ],
  107. )