Skip to content

Commit

Permalink
Lint Tiltfiles with ruff and fix findings (#625)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bast <[email protected]>
  • Loading branch information
dbast authored Jan 2, 2025
1 parent 41dae53 commit bfafab4
Show file tree
Hide file tree
Showing 28 changed files with 155 additions and 148 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ repos:
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.2
rev: v0.8.4
hooks:
- id: ruff
args: [--fix]
exclude: ^.*/Tiltfile$
types_or: [tiltfile]
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1
hooks:
Expand Down
2 changes: 1 addition & 1 deletion cert_manager/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def deploy_cert_manager(registry="quay.io/jetstack", version="v1.3.1", load_to_k
else:
cert_manager_test_resources_versioned = cert_manager_test_resources.format(cert_manager_api_version='v1')

if load_to_kind == True:
if load_to_kind:
print("Loading images to kind")
# Prepull all the cert-manager images to your local environment and then load them directly into kind. This speeds up
# setup if you're repeatedly destroying and recreating your kind cluster, as it doesn't have to pull the images over
Expand Down
4 changes: 2 additions & 2 deletions configmap/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def configmap_yaml(name, namespace="", from_file=None, watch=True, from_env_file
for f in from_file:
args.extend(["--from-file", f])
if watch:
l = f.split('=')
watch_file(l[len(l)-1])
line = f.split('=')
watch_file(line[len(line)-1])
generator = True
else:
fail("Bad from_file argument: %s" % from_file)
Expand Down
2 changes: 1 addition & 1 deletion configmap/test/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ configmap_create('job-config', from_file='my-job.ini=./my-job.ini')
configmap_create('env-job-config', from_env_file='my-job.env')
configmap_from_dict('from-dict-config', inputs={"hello":"world"})

k8s_yaml('job.yaml')
k8s_yaml('job.yaml')
7 changes: 3 additions & 4 deletions file_sync_only/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def file_sync_only(image='', manifests=[], deps=["."], live_update=[], allow_dup

# sync files at first time
def _first_sync_from_liveupdate(image, live_update, k8s_resources):
for l in live_update:
if type(l) == "live_update_sync_step":
local_path, remote_path = _get_sync_params(l)
for step in live_update:
if type(step) == "live_update_sync_step":
local_path, remote_path = _get_sync_params(step)
syncname = "firstsync-%s-%s" % (image.replace("/", "_"), local_path.replace("/", "_"))
local_resource(syncname,
'touch %s' % local_path,
Expand Down Expand Up @@ -81,4 +81,3 @@ def _get_related_resources(manifests, image):
fail("found multiple image tags for %s in manifests %s" % (image, manifests))

return k8s_resources, tags_in_manifests[0]

5 changes: 2 additions & 3 deletions get_obj/Tiltfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
def get_container(decoded_yaml, container_selector):
containers=decoded_yaml["spec"]["template"]["spec"]["containers"]
found_containers=[]

for container in containers:
if container["name"] == container_selector:
return container

fail("failed to find container %s in the manifest %s" % (container_selector, containers))

def get_image(container):
return container["image"]

def remove_probe(container, probe):
if container.get(probe):
container.pop(probe)
container.pop(probe)

def get_obj(name, kind, container_selector = None, namespace = None, disable_probes = False):
"""
Expand Down
2 changes: 1 addition & 1 deletion get_obj/test/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ k8s_resource(
"8080:8080",
"9229:9229"
]
)
)
2 changes: 1 addition & 1 deletion get_obj/test/setup.Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ docker_build(
'remote-dev-test-image',
'.'
)
k8s_yaml('deployment.yaml')
k8s_yaml('deployment.yaml')
6 changes: 4 additions & 2 deletions git_resource/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ def git_checkout(repository_url, checkout_dir=None, unsafe_mode=False):

def deploy_from_dir(resource_name, directory, dockerfile='Dockerfile', namespace='default', resource_deps=[], port_forwards=[], build_callback=None, deployment_callback=None):
if deployment_callback == None:
deployment_callback = lambda resource_name, image_name, namespace: _default_deployment_callback(resource_name, image_name, namespace)
def deployment_callback(resource_name, image_name, namespace):
return _default_deployment_callback(resource_name, image_name, namespace)

if build_callback == None:
build_callback = lambda resource_name, directory, dockerfile: _default_build_callback(resource_name, directory, dockerfile=dockerfile)
def build_callback(resource_name, directory, dockerfile):
return _default_build_callback(resource_name, directory, dockerfile=dockerfile)

image_name = build_callback(resource_name, directory, dockerfile=dockerfile)
yaml = deployment_callback(resource_name, image_name, namespace)
Expand Down
2 changes: 1 addition & 1 deletion hasura/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DEFAULT_HASURA_PORT = 8080
DEFAULT_HASURA_RESOURCE_NAME = 'hasura'

def hasura_console(release_name='',
path='.',
path='.',
hasura_resource_name=None,
hasura_endpoint= 'http://localhost:8080',
hasura_secret=DEFAULT_HASURA_SECRET,
Expand Down
4 changes: 2 additions & 2 deletions helm_remote/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ def helm_remote_yaml(chart, repo_url='', repo_name='', release_name='', values=[
# str.isalnum accepts dots, which is only dangerous when slashes are allowed
# https://helm.sh/docs/chart_best_practices/conventions/#chart-names

if chart.replace('-', '').isalnum() == False or chart != chart.replace('.', ''):
if not chart.replace('-', '').isalnum() or chart != chart.replace('.', ''):
# https://helm.sh/docs/chart_best_practices/conventions/#chart-names
fail('Chart name is not valid')

if repo_name != chart and repo_name.replace('-', '').isalnum() == False or repo_name != repo_name.replace('.', ''):
if repo_name != chart and not repo_name.replace('-', '').isalnum() or repo_name != repo_name.replace('.', ''):
# https://helm.sh/docs/chart_best_practices/conventions/#chart-names
if not repo_name.startswith('oci://'):
fail('Repo name is not valid')
Expand Down
1 change: 0 additions & 1 deletion k8s_yaml_glob/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def k8s_yaml_glob(*glob_patterns, **kwargs):

for glob_pattern in glob_patterns:
script = 'import glob; print("|".join( p for p in glob.glob(\"' + glob_pattern + '")))'
cmd = python_cmd + " -c '" + script + "'"
val = str(local(command=python_cmd + " -c '" + script + "'", quiet=False)).strip()
yaml_paths = val.split("|")
k8s_yaml(yaml_paths, **kwargs)
34 changes: 17 additions & 17 deletions kubebuilder/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ def kubebuilder(DOMAIN, GROUP, VERSION, KIND, IMG='controller:latest', CONTROLLE

return encode_yaml_stream(decoded)
return data

def manifests():
return 'controller-gen ' + CONTROLLERGEN

def generate():
return 'controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./...";'

def vetfmt():
return 'go vet ./...; go fmt ./...'

# build to tilt_bin beause kubebuilder has a dockerignore for bin/
def binary():
return 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o tilt_bin/manager cmd/main.go'
Expand All @@ -43,29 +43,29 @@ def kubebuilder(DOMAIN, GROUP, VERSION, KIND, IMG='controller:latest', CONTROLLE

DIRNAME = os.path.basename(os. getcwd())
# if kubebuilder
if os.path.exists('go.mod') == False:
if not os.path.exists('go.mod'):
local("go mod init %s" % DIRNAME)
if os.path.exists('PROJECT') == False:

if not os.path.exists('PROJECT'):
local("kubebuilder init --domain %s" % DOMAIN)
if os.path.exists('api') == False:

if not os.path.exists('api'):
local("kubebuilder create api --resource --controller --group %s --version %s --kind %s" % (GROUP, VERSION, KIND))

local(manifests() + generate())

local_resource('CRD', manifests() + 'kustomize build config/crd | kubectl apply -f -', deps=["api"])

k8s_yaml(yaml())

deps = ['internal/controller', 'cmd/main.go']
deps.append('api')

local_resource('Watch&Compile', generate() + binary(), deps=deps, ignore=['*/*/zz_generated.deepcopy.go'])

local_resource('Sample YAML', 'kubectl apply -f ./config/samples', deps=["./config/samples"], resource_deps=[DIRNAME + "-controller-manager"])
docker_build_with_restart(IMG, '.',

docker_build_with_restart(IMG, '.',
dockerfile_contents=DOCKERFILE,
entrypoint='/manager',
only=['./tilt_bin/manager'],
Expand Down
14 changes: 7 additions & 7 deletions list_dependencies/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ load('ext://uibutton', 'cmd_button')
display_deps_path = os.path.join(os.getcwd(), 'deps.py')

def display_dependencies():
cmd_button(
name = 'show_deps',
resource = '(Tiltfile)',
argv = [display_deps_path],
text = 'list pending dependencies',
icon_name = 'account_tree'
)
cmd_button(
name = 'show_deps',
resource = '(Tiltfile)',
argv = [display_deps_path],
text = 'list pending dependencies',
icon_name = 'account_tree'
)
14 changes: 7 additions & 7 deletions list_port_forwards/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ load('ext://uibutton', 'cmd_button')
display_pf_script_path = os.path.join(os.getcwd(), "display_port_forwards.sh")

def display_port_forwards():
cmd_button(
name = 'which_ports',
resource = '(Tiltfile)',
argv = [display_pf_script_path],
text='List port forwards',
icon_name='travel_explore',
)
cmd_button(
name = 'which_ports',
resource = '(Tiltfile)',
argv = [display_pf_script_path],
text='List port forwards',
icon_name='travel_explore',
)
Loading

0 comments on commit bfafab4

Please sign in to comment.