Skip to content

Commit e76f9d3

Browse files
[Monorepo] Fix and Unify Tilt Workflow
- Added `.tilt-support` directory at the root, updated to work with both `operator-controller` and `catalogd`. - Removed the Tilt workflow from the `catalogd` directory, consolidating all configurations into the root Tilt setup. - Updated configurations to use `catalogd` from the source code instead of fetching it from `github.com/operator-framework/catalogd`. Co-authored-by: Joe Lanford <[email protected]>
1 parent 3398e48 commit e76f9d3

File tree

6 files changed

+169
-173
lines changed

6 files changed

+169
-173
lines changed

.github/workflows/catalogd-tilt.yaml

Lines changed: 0 additions & 45 deletions
This file was deleted.

.github/workflows/tilt.yaml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,20 @@ on:
66
- 'api/**'
77
- 'cmd/**'
88
- 'config/**'
9+
- 'catalogd/**'
910
- 'internal/**'
1011
- 'pkg/**'
1112
- 'Tiltfile'
13+
- '.tilt-support'
1214
merge_group:
1315

1416
jobs:
1517
tilt:
1618
runs-on: ubuntu-latest
1719
steps:
18-
- uses: actions/checkout@v4
19-
with:
20-
repository: operator-framework/tilt-support
21-
path: tilt-support
2220
- uses: actions/checkout@v4
2321
with:
2422
path: operator-controller
25-
- name: Get catalogd version
26-
id: get-catalogd-version
27-
run: |
28-
cd operator-controller
29-
echo "CATALOGD_VERSION=$(go list -mod=mod -m -f "{{.Version}}" github.com/operator-framework/catalogd)" >> "$GITHUB_OUTPUT"
30-
- uses: actions/checkout@v4
31-
with:
32-
repository: operator-framework/catalogd
33-
path: catalogd
34-
ref: "${{ steps.get-catalogd-version.outputs.CATALOGD_VERSION }}"
3523
- name: Install Go
3624
uses: actions/setup-go@v5
3725
with:

.tilt-support

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
load('ext://restart_process', 'docker_build_with_restart')
2+
load('ext://cert_manager', 'deploy_cert_manager')
3+
4+
5+
def deploy_cert_manager_if_needed():
6+
cert_manager_var = '__CERT_MANAGER__'
7+
if os.getenv(cert_manager_var) != '1':
8+
deploy_cert_manager(version="v1.15.3")
9+
os.putenv(cert_manager_var, '1')
10+
11+
12+
# Set up our build helper image that has delve in it. We use a helper so parallel image builds don't all simultaneously
13+
# install delve. Instead, they all wait for this build to complete, and then proceed in parallel.
14+
docker_build(
15+
ref='helper',
16+
context='.',
17+
build_args={'GO_VERSION': '1.23'},
18+
dockerfile_contents='''
19+
ARG GO_VERSION
20+
FROM golang:${GO_VERSION}
21+
ARG GO_VERSION
22+
RUN CGO_ENABLED=0 go install github.com/go-delve/delve/cmd/dlv@v${GO_VERSION}
23+
'''
24+
)
25+
26+
27+
def build_binary(repo, binary, deps, image, tags="", debug=True):
28+
gcflags = ''
29+
if debug:
30+
gcflags = "-gcflags 'all=-N -l'"
31+
32+
# Treat the main binary as a local resource, so we can automatically rebuild it when any of the deps change. This
33+
# builds it locally, targeting linux, so it can run in a linux container.
34+
binary_name = binary.split("/")[-1]
35+
local_resource(
36+
'{}_{}_binary'.format(repo, binary_name),
37+
cmd='''
38+
mkdir -p .tiltbuild/bin
39+
CGO_ENABLED=0 GOOS=linux go build {tags} {gcflags} -o .tiltbuild/bin/{binary_name} {binary}
40+
'''.format(repo=repo, binary_name=binary_name, binary=binary, gcflags=gcflags, tags=tags),
41+
deps=deps
42+
)
43+
44+
entrypoint = ['/{}'.format(binary_name)]
45+
if debug:
46+
entrypoint = ['/dlv', '--accept-multiclient', '--api-version=2', '--headless=true', '--listen', ':30000', 'exec', '--continue', '--'] + entrypoint
47+
48+
# Configure our image build. If the file in live_update.sync (.tiltbuild/bin/$binary) changes, Tilt
49+
# copies it to the running container and restarts it.
50+
docker_build_with_restart(
51+
# This has to match an image in the k8s_yaml we call below, so Tilt knows to use this image for our Deployment,
52+
# instead of the actual image specified in the yaml.
53+
ref='{image}:{binary_name}'.format(image=image, binary_name=binary_name),
54+
# This is the `docker build` context, and because we're only copying in the binary we've already had Tilt build
55+
# locally, we set the context to the directory containing the binary.
56+
context='.tiltbuild/bin',
57+
# We use a slimmed-down Dockerfile that only has $binary in it.
58+
dockerfile_contents='''
59+
FROM gcr.io/distroless/static:debug
60+
WORKDIR /
61+
COPY --from=helper /go/bin/dlv /
62+
COPY {} /
63+
'''.format(binary_name),
64+
# The set of files Tilt should include in the build. In this case, it's just the binary we built above.
65+
only=binary_name,
66+
# If .tiltbuild/bin/$binary changes, Tilt will copy it into the running container and restart the process.
67+
live_update=[
68+
sync('.tiltbuild/bin/{}'.format(binary_name), '/{}'.format(binary_name)),
69+
],
70+
# The command to run in the container.
71+
entrypoint=entrypoint,
72+
)
73+
74+
75+
def process_yaml(yaml):
76+
if type(yaml) == 'string':
77+
objects = read_yaml_stream(yaml)
78+
elif type(yaml) == 'blob':
79+
objects = decode_yaml_stream(yaml)
80+
else:
81+
fail('expected a string or blob, got: {}'.format(type(yaml)))
82+
83+
for o in objects:
84+
# For Tilt's live_update functionality to work, we have to run the container as root. Remove any PSA labels
85+
# to allow this.
86+
if o['kind'] == 'Namespace' and 'labels' in o['metadata']:
87+
labels_to_delete = [label for label in o['metadata']['labels'] if label.startswith('pod-security.kubernetes.io')]
88+
for label in labels_to_delete:
89+
o['metadata']['labels'].pop(label)
90+
91+
if o['kind'] != 'Deployment':
92+
# We only need to modify Deployments, so we can skip this
93+
continue
94+
95+
# For Tilt's live_update functionality to work, we have to run the container as root. Otherwise, Tilt won't
96+
# be able to untar the updated binary in the container's file system (this is how live update
97+
# works). If there are any securityContexts, remove them.
98+
if "securityContext" in o['spec']['template']['spec']:
99+
o['spec']['template']['spec'].pop('securityContext')
100+
for c in o['spec']['template']['spec']['containers']:
101+
if "securityContext" in c:
102+
c.pop('securityContext')
103+
104+
# If multiple Deployment manifests all use the same image but use different entrypoints to change the binary,
105+
# we have to adjust each Deployment to use a different image. Tilt needs each Deployment's image to be
106+
# unique. We replace the tag with what is effectively :$binary, e.g. :helm.
107+
for c in o['spec']['template']['spec']['containers']:
108+
if c['name'] == 'kube-rbac-proxy':
109+
continue
110+
111+
command = c['command'][0]
112+
if command.startswith('./'):
113+
command = command.removeprefix('./')
114+
elif command.startswith('/'):
115+
command = command.removeprefix('/')
116+
117+
image_without_tag = c['image'].rsplit(':', 1)[0]
118+
119+
# Update the image so instead of :$tag it's :$binary
120+
c['image'] = '{}:{}'.format(image_without_tag, command)
121+
122+
# Now apply all the yaml
123+
# We are using allow_duplicates=True here as both
124+
# operator-controller and catalogd will be installed in the same
125+
# namespace "olmv1-system" as of https://github.com/operator-framework/operator-controller/pull/888
126+
# and https://github.com/operator-framework/catalogd/pull/283
127+
k8s_yaml(encode_yaml_stream(objects), allow_duplicates=True)
128+
129+
130+
# data format:
131+
# {
132+
# 'image': 'quay.io/operator-framework/rukpak',
133+
# 'yaml': 'manifests/overlays/cert-manager',
134+
# 'binaries': {
135+
# 'core': 'core',
136+
# 'crdvalidator': 'crd-validation-webhook',
137+
# 'helm': 'helm-provisioner',
138+
# 'webhooks': 'rukpak-webhooks',
139+
# },
140+
# 'deps': ['api', 'cmd/binary_name', 'internal', 'pkg'],
141+
# },
142+
def deploy_repo(repo, data, tags="", debug=True):
143+
print('Deploying repo {}'.format(repo))
144+
deploy_cert_manager_if_needed()
145+
146+
local_port = data['starting_debug_port']
147+
for binary, deployment in data['binaries'].items():
148+
build_binary(repo, binary, data['deps'], data['image'], tags, debug)
149+
k8s_resource(deployment, port_forwards=['{}:30000'.format(local_port)])
150+
local_port += 1
151+
process_yaml(kustomize(data['yaml']))

Tiltfile

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
if not os.path.exists('../tilt-support'):
2-
fail('Please clone https://github.com/operator-framework/tilt-support to ../tilt-support')
1+
load('.tilt-support', 'deploy_repo')
32

4-
load('../tilt-support/Tiltfile', 'deploy_repo')
5-
6-
config.define_string_list('repos', args=True)
7-
cfg = config.parse()
8-
repos = cfg.get('repos', ['operator-controller', 'catalogd'])
9-
10-
repo = {
3+
operator_controller = {
114
'image': 'quay.io/operator-framework/operator-controller',
125
'yaml': 'config/overlays/cert-manager',
136
'binaries': {
14-
'manager': 'operator-controller-controller-manager',
7+
'./cmd/operator-controller': 'operator-controller-controller-manager',
158
},
9+
'deps': ['api', 'cmd/operator-controller', 'internal', 'pkg', 'go.mod', 'go.sum'],
1610
'starting_debug_port': 30000,
1711
}
12+
deploy_repo('operator-controller', operator_controller, '-tags containers_image_openpgp')
13+
14+
catalogd = {
15+
'image': 'quay.io/operator-framework/catalogd',
16+
'yaml': 'catalogd/config/overlays/cert-manager',
17+
'binaries': {
18+
'./catalogd/cmd/catalogd': 'catalogd-controller-manager',
19+
},
20+
'deps': ['catalogd/api', 'catalogd/cmd/catalogd', 'catalogd/internal', 'catalogd/pkg', 'go.mod', 'go.sum'],
21+
'starting_debug_port': 20000,
22+
}
1823

19-
for r in repos:
20-
if r == 'operator-controller':
21-
deploy_repo('operator-controller', repo, '-tags containers_image_openpgp')
22-
else:
23-
include('../{}/Tiltfile'.format(r))
24+
deploy_repo('catalogd', catalogd, '-tags containers_image_openpgp')

catalogd/Tiltfile

Lines changed: 0 additions & 15 deletions
This file was deleted.

catalogd/tilt.md

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)