Skip to content

Commit

Permalink
Merge pull request #20 from kurtosis-tech/lporoli/tilt
Browse files Browse the repository at this point in the history
feat: Tilt integration
  • Loading branch information
leoporoli authored Aug 28, 2024
2 parents 461a4f6 + 1d61b27 commit fe1a679
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 411 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.direnv
branch-name.nix
.idea
.DS_Store
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

17 changes: 0 additions & 17 deletions .idea/dataSources.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/new-obd.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

30 changes: 11 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ minikube tunnel

## Development Guide

This doc explains how to build and run the OnlineBoutique source code locally using the `skaffold` command-line tool.
This doc explains how to build and run the OnlineBoutique source code locally using the `tilt` command-line tool.

### Prerequisites

- [Docker for Desktop](https://www.docker.com/products/docker-desktop).
- [Docker for Desktop](https://www.docker.com/products/docker-desktop)
- kubectl (can be installed via `gcloud components install kubectl`)
- [skaffold **1.27+**](https://skaffold.dev/docs/install/) (latest version recommended), a tool that builds and deploys Docker images in bulk.
- [tilt **0.22.2+**](https://docs.tilt.dev/install.html) (latest version recommended)
- [Minikube](https://minikube.sigs.k8s.io/docs/start/) (optional - see Local Cluster)

### Local Cluster
Expand All @@ -78,22 +78,14 @@ This doc explains how to build and run the OnlineBoutique source code locally us

2. Run `kubectl get nodes` to verify you're connected to the respective control plane.
3. Run `skaffold run` (first time will be slow, it can take ~20 minutes).
This will build and deploy the application. If you need to rebuild the images
automatically as you refactor the code, run `skaffold dev` command.
4. Run `kubectl get pods` to verify the Pods are ready and running.
5. Access the web frontend through your browser
- **Minikube** requires you to run a command to access the frontend service:
```shell
minikube service frontend-external
```
- **Docker For Desktop** should automatically provide the frontend at http://localhost:80
3. Two options:
1. Run `sudo tilt up`.
To deploy the app using the `./release/obd-kardinal.yaml` file, with Kardinal annotations, in the cluster. Take into account that it will use the container images defined in the YAML, it will try to pull them from the cloud. The sudo privileges are necessary in order to port-forward the port "80"
2. Run `sudo tilt up -- --build frontend --build productcatalogservice`.
To deploy the app using the `./release/obd-kardinal.yaml` file and also create a new 'dev' flow with dev images version for the services specified with the `build` flag (valid values: 'frontend', 'cartservice', 'productcatalogservice', convine these as you want).
Edit the source code and check the changes in the dev URL, `Tilt` will trigger the hot-reload for it
## Cleanup
If you've deployed the application with `skaffold run` command, you can run
`skaffold delete` to clean up the deployed resources.
If you've deployed the application with `tilt up` command, you can run
`tilt down --delete-namespaces` to clean up the deployed resources, the `--delete-namespaces` flas is important because otherwise it won't delete the namespace.
102 changes: 102 additions & 0 deletions Tiltfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# constants
FRONTEND_SERVICE_NAME='frontend'
CARTSERVICE_SERVICE_NAME='cartservice'
PRODUCTCATALOG_SERVICE_NAME='productcatalogservice'
DEV_IMAGE_SUFFIX='-dev-image'
FRONTEND_REF=FRONTEND_SERVICE_NAME + DEV_IMAGE_SUFFIX
CARTSERVICE_REF=CARTSERVICE_SERVICE_NAME + DEV_IMAGE_SUFFIX
PRODUCTCATALOG_REF=PRODUCTCATALOG_SERVICE_NAME + DEV_IMAGE_SUFFIX

# enforces a minimum Tilt version
version_settings(constraint='>=0.22.2')

# check if Kardinal is installed
load('./dev/require-tool', 'require_tool')
require_tool('kardinal', 'Kardinal CLI is not installed, visit https://github.com/kurtosis-tech/kardinal#installation to install it')

# define and get the arguments
config.define_string_list('build')
arguments = config.parse()

# get the 'build' argument
build_arg = []
if 'build' in arguments:
build_arg = arguments['build']

build_arg_len = len(build_arg)

# validate the 'build' argument
valid_build_arguments = [FRONTEND_SERVICE_NAME, CARTSERVICE_SERVICE_NAME, PRODUCTCATALOG_SERVICE_NAME]
for bu_arg in build_arg:
if bu_arg not in valid_build_arguments:
fail('build argument {} is not valid. Valid arguments are: {}'.format(bu_arg, valid_build_arguments))

# enforces to use this Tilt with Minikube or Docker desktop contexts to avoid deploying this stuff in cloud servers
allow_k8s_contexts(['minikube', 'docker-desktop'])

if k8s_context() == 'minikube':
#This allows Tilt to build and push images directly to Minikube's Docker daemon
print('Building container images with Minikube')
local('eval $(minikube docker-env)')

# set the env var to use the local Kontrol
local('export KARDINAL_CLI_DEV_MODE=TRUE')

# clean current flows before creating new ones, it's just for the tilt up cmd
if config.tilt_subcommand == 'up':
local(['kardinal', 'flow', 'delete', '', '--all'])

# executing kardinal deploy
local(['kardinal', 'deploy', '-k', './release/obd-kardinal.yaml'])

if build_arg_len > 0:
# execute the kardinal flow create for the services we want to build
cmd_list = ['kardinal', 'flow', 'create']

for index, value in enumerate(build_arg):
service_name = value
service_image = value + DEV_IMAGE_SUFFIX
if index == 0:
cmd_list.extend([service_name, service_image])
else:
cmd_list.append('-s')
add_sv_flag = '{}={}'.format(service_name, service_image)
cmd_list.append(add_sv_flag)

# now execute the create flow command
local(cmd_list)

# CART SERVICE
if 'cartservice' in build_arg:
docker_build(
CARTSERVICE_REF,
context='./src/cartservice',
dockerfile='./src/cartservice/Dockerfile',
)

# PRODUCT CATALOG SERVICE
if 'productcatalogservice' in build_arg:
docker_build(
PRODUCTCATALOG_REF,
context='./src/productcatalogservice',
dockerfile='./src/productcatalogservice/Dockerfile',
)

# FRONTEND
if 'frontend' in build_arg:
docker_build(
FRONTEND_REF,
context='./src',
dockerfile='./src/frontend.dockerfile',
)

kardinal_topology_yaml = local(['kardinal', 'topology', 'print-manifest', '--add-trace-router'], quiet=True)
kardinal_topology_yaml_str = str(kardinal_topology_yaml)

if kardinal_topology_yaml_str != '':
k8s_yaml(kardinal_topology_yaml, allow_duplicates = True)

local_resource(
name='ingress-gateway-port-forward',
serve_cmd=['kubectl', 'port-forward', 'service/istio-ingressgateway', '80:80', '-n', 'istio-system']
)
15 changes: 15 additions & 0 deletions dev/require-tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# block Tiltfile execution if missing required tool (e.g. Helm)
def require_tool(tool, msg=None):
tool = shlex.quote(tool)
if not msg:
msg = '%s is required but was not found in PATH' % tool
local(
command='command -v %s >/dev/null 2>&1 || { echo >&2 "%s"; exit 1; }' % (tool, msg),
command_bat=[
"powershell.exe",
"-Noninteractive",
"-Command",
'& {{if (!(Get-Command %s -ErrorAction SilentlyContinue)) {{ Write-Error "%s"; exit 1 }}}}' % (tool, msg),
],
quiet=True,
)
80 changes: 0 additions & 80 deletions kubernetes-manifests/cartservice.yaml

This file was deleted.

88 changes: 0 additions & 88 deletions kubernetes-manifests/frontend.yaml

This file was deleted.

Loading

0 comments on commit fe1a679

Please sign in to comment.