Skip to content

Commit

Permalink
Allow tilt provider with pre-build images
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziopandini committed Mar 11, 2024
1 parent 21a52f9 commit 7077c9c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
33 changes: 17 additions & 16 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -342,23 +342,24 @@ def enable_provider(name, debug):

port_forwards, links = get_port_forwards(debug)

build_go_binary(
context = p.get("context"),
reload_deps = p.get("live_reload_deps"),
debug = debug,
go_main = p.get("go_main", "main.go"),
binary_name = "manager",
label = label,
)
if p.get("image"):
build_go_binary(
context = p.get("context"),
reload_deps = p.get("live_reload_deps"),
debug = debug,
go_main = p.get("go_main", "main.go"),
binary_name = "manager",
label = label,
)

build_docker_image(
image = p.get("image"),
context = p.get("context"),
binary_name = "manager",
additional_docker_helper_commands = p.get("additional_docker_helper_commands", ""),
additional_docker_build_commands = p.get("additional_docker_build_commands", ""),
port_forwards = port_forwards,
)
build_docker_image(
image = p.get("image"),
context = p.get("context"),
binary_name = "manager",
additional_docker_helper_commands = p.get("additional_docker_helper_commands", ""),
additional_docker_build_commands = p.get("additional_docker_build_commands", ""),
port_forwards = port_forwards,
)

additional_objs = []
p_resources = p.get("additional_resources", [])
Expand Down
31 changes: 21 additions & 10 deletions hack/tools/internal/tilt-prepare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type tiltProvider struct {
type tiltProviderConfig struct {
Context *string `json:"context,omitempty"`
Version *string `json:"version,omitempty"`
LiveReloadDeps []string `json:"live_reload_deps,omitempty"`
ApplyProviderYaml *bool `json:"apply_provider_yaml,omitempty"`
KustomizeFolder *string `json:"kustomize_folder,omitempty"`
KustomizeOptions []string `json:"kustomize_options,omitempty"`
Expand Down Expand Up @@ -347,7 +348,13 @@ func tiltResources(ctx context.Context, ts *tiltSettings) error {
if ptr.Deref(config.ApplyProviderYaml, true) {
kustomizeFolder := path.Join(*config.Context, ptr.Deref(config.KustomizeFolder, "config/default"))
kustomizeOptions := config.KustomizeOptions
tasks[providerName] = workloadTask(providerName, "provider", "manager", "manager", ts, kustomizeFolder, kustomizeOptions, getProviderObj(config.Version))
liveReloadDeps := config.LiveReloadDeps
var debugConfig *tiltSettingsDebugConfig
if d, ok := ts.Debug[providerName]; ok {
debugConfig = &d
}
extraArgs := ts.ExtraArgs[providerName]
tasks[providerName] = workloadTask(providerName, "provider", "manager", "manager", liveReloadDeps, debugConfig, extraArgs, kustomizeFolder, kustomizeOptions, getProviderObj(config.Version))
}
}

Expand Down Expand Up @@ -686,7 +693,7 @@ func kustomizeTask(path, out string) taskFunction {
// workloadTask generates a task for creating the component yaml for a workload and saving the output on a file.
// NOTE: This task has several sub steps including running kustomize, envsubst, fixing components for debugging,
// and adding the workload resource mimicking what clusterctl init does.
func workloadTask(name, workloadType, binaryName, containerName string, ts *tiltSettings, path string, options []string, getAdditionalObject func(string, []unstructured.Unstructured) (*unstructured.Unstructured, error)) taskFunction {
func workloadTask(name, workloadType, binaryName, containerName string, liveReloadDeps []string, debugConfig *tiltSettingsDebugConfig, extraArgs tiltSettingsExtraArgs, path string, options []string, getAdditionalObject func(string, []unstructured.Unstructured) (*unstructured.Unstructured, error)) taskFunction {
return func(ctx context.Context, prefix string, errCh chan error) {
args := []string{"build"}
args = append(args, options...)
Expand Down Expand Up @@ -718,7 +725,7 @@ func workloadTask(name, workloadType, binaryName, containerName string, ts *tilt
return
}

if err := prepareWorkload(name, prefix, binaryName, containerName, objs, ts); err != nil {
if err := prepareWorkload(name, prefix, binaryName, containerName, objs, liveReloadDeps, debugConfig, extraArgs); err != nil {
errCh <- err
return
}
Expand Down Expand Up @@ -787,14 +794,18 @@ func writeIfChanged(prefix string, path string, yaml []byte) error {
// If there are extra_args given for the workload, we append those to the ones that already exist in the deployment.
// This has the affect that the appended ones will take precedence, as those are read last.
// Finally, we modify the deployment to enable prometheus metrics scraping.
func prepareWorkload(name, prefix, binaryName, containerName string, objs []unstructured.Unstructured, ts *tiltSettings) error {
func prepareWorkload(name, prefix, binaryName, containerName string, objs []unstructured.Unstructured, liveReloadDeps []string, debugConfig *tiltSettingsDebugConfig, extraArgs tiltSettingsExtraArgs) error {

Check failure on line 797 in hack/tools/internal/tilt-prepare/main.go

View workflow job for this annotation

GitHub Actions / lint (hack/tools)

unused-parameter: parameter 'name' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 797 in hack/tools/internal/tilt-prepare/main.go

View workflow job for this annotation

GitHub Actions / lint (hack/tools)

unused-parameter: parameter 'name' seems to be unused, consider removing or renaming it as _ (revive)
return updateDeployment(prefix, objs, func(deployment *appsv1.Deployment) {
for j, container := range deployment.Spec.Template.Spec.Containers {
if container.Name != containerName {
continue
}
cmd := []string{"sh", "/start.sh", "/" + binaryName}
args := append(container.Args, []string(ts.ExtraArgs[name])...)

cmd := []string{"/" + binaryName}
if len(liveReloadDeps) > 0 || debugConfig != nil {
cmd = []string{"sh", "/start.sh", "/" + binaryName}
}
args := append(container.Args, []string(extraArgs)...)

// remove securityContext for tilt live_update, see https://github.com/tilt-dev/tilt/issues/3060
container.SecurityContext = nil
Expand All @@ -805,19 +816,19 @@ func prepareWorkload(name, prefix, binaryName, containerName string, objs []unst
// alter deployment for working nicely with delve debugger;
// most specifically, configuring delve, starting the manager with profiling enabled, dropping liveness and
// readiness probes and disabling leader election.
if d, ok := ts.Debug[name]; ok {
if debugConfig != nil {
cmd = []string{"sh", "/start.sh", "/dlv", "--accept-multiclient", "--api-version=2", "--headless=true", "exec"}

if d.Port != nil && *d.Port > 0 {
if debugConfig.Port != nil && *debugConfig.Port > 0 {
cmd = append(cmd, "--listen=:30000")
}
if d.Continue != nil && *d.Continue {
if debugConfig.Continue != nil && *debugConfig.Continue {
cmd = append(cmd, "--continue")
}

cmd = append(cmd, []string{"--", "/" + binaryName}...)

if d.ProfilerPort != nil && *d.ProfilerPort > 0 {
if debugConfig.ProfilerPort != nil && *debugConfig.ProfilerPort > 0 {
args = append(args, []string{"--profiler-address=:6060"}...)
}

Expand Down

0 comments on commit 7077c9c

Please sign in to comment.