From c99c4207769d49821a5e57d7f71e6ff2bc56d99f Mon Sep 17 00:00:00 2001 From: Naadir Jeewa Date: Mon, 25 Oct 2021 18:17:42 +0100 Subject: [PATCH] Tiltfile: Add remote debugging options This enables adding the following in tilt-settings.json, for example, to enable a remote debugger to be attached to the core controller on 127.0.0.1:30000 when using Tilt: ``` "debug": { "core": { "race_detector": true, "port": 30000, "profiler_port": 40000, "metrics_port": 40001 }, "kubeadm-bootstrap": { "port": 30001 }, "kubeadm-control-plane": { "port": 30002 }, "docker": { "port": 30003 }, "aws": { "port": 30004 } }, ``` Signed-off-by: Naadir Jeewa --- Makefile | 5 ++ Tiltfile | 115 ++++++++++++++++++++---- docs/book/src/developer/tilt.md | 70 ++++++++++++++- hack/tools/go.mod | 1 + hack/tools/go.sum | 23 ++++- hack/tools/tools.go | 1 + tilt_modules/extensions.json | 5 ++ tilt_modules/local_output/README.md | 27 ++++++ tilt_modules/local_output/Tiltfile | 2 + tilt_modules/local_output/test/Tiltfile | 9 ++ tilt_modules/local_output/test/test.sh | 15 ++++ 11 files changed, 252 insertions(+), 21 deletions(-) create mode 100644 tilt_modules/local_output/README.md create mode 100644 tilt_modules/local_output/Tiltfile create mode 100644 tilt_modules/local_output/test/Tiltfile create mode 100755 tilt_modules/local_output/test/test.sh diff --git a/Makefile b/Makefile index b1aa40f9d868..d15cdba635b8 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,8 @@ CAPD_DIR := $(TEST_DIR)/infrastructure/docker GO_APIDIFF_BIN := $(BIN_DIR)/go-apidiff GO_APIDIFF := $(TOOLS_DIR)/$(GO_APIDIFF_BIN) ENVSUBST_BIN := $(BIN_DIR)/envsubst +YQ_BIN := $(BIN_DIR)/yq +YQ := $(TOOLS_DIR)/$(YQ_BIN) ENVSUBST := $(TOOLS_DIR)/$(ENVSUBST_BIN) export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH) @@ -218,6 +220,9 @@ $(GO_APIDIFF): $(TOOLS_DIR)/go.mod $(ENVSUBST): $(TOOLS_DIR)/go.mod cd $(TOOLS_DIR) && go build -tags=tools -o $(ENVSUBST_BIN) github.com/drone/envsubst/v2/cmd/envsubst +$(YQ): $(TOOLS_DIR)/go.mod + cd $(TOOLS_DIR) && go build -tags=tools -o $(YQ_BIN) github.com/mikefarah/yq/v4 + $(KUSTOMIZE): # Download kustomize using hack script into tools folder. hack/ensure-kustomize.sh diff --git a/Tiltfile b/Tiltfile index dfd20f6cdb7b..ebf6682aee26 100644 --- a/Tiltfile +++ b/Tiltfile @@ -1,15 +1,21 @@ # -*- mode: Python -*- - # set defaults +load("ext://local_output", "local_output") + +version_settings(True, ">=0.22.2") envsubst_cmd = "./hack/tools/bin/envsubst" kustomize_cmd = "./hack/tools/bin/kustomize" +yq_cmd = "./hack/tools/bin/yq" +os_name = local_output("go env GOOS") +os_arch = local_output("go env GOARCH") settings = { "deploy_cert_manager": True, "preload_images_for_kind": True, "enable_providers": ["docker"], "kind_cluster_name": "kind", + "debug": {}, } # global settings @@ -138,9 +144,10 @@ tilt_helper_dockerfile_header = """ # Tilt image FROM golang:1.16.8 as tilt-helper # Support live reloading with Tilt +RUN go get github.com/go-delve/delve/cmd/dlv RUN wget --output-document /restart.sh --quiet https://raw.githubusercontent.com/windmilleng/rerun-process-wrapper/master/restart.sh && \ wget --output-document /start.sh --quiet https://raw.githubusercontent.com/windmilleng/rerun-process-wrapper/master/start.sh && \ - chmod +x /start.sh && chmod +x /restart.sh + chmod +x /start.sh && chmod +x /restart.sh && chmod +x /go/bin/dlv """ tilt_dockerfile_header = """ @@ -148,6 +155,7 @@ FROM gcr.io/distroless/base:debug as tilt WORKDIR / COPY --from=tilt-helper /start.sh . COPY --from=tilt-helper /restart.sh . +COPY --from=tilt-helper /go/bin/dlv . COPY manager . """ @@ -156,12 +164,13 @@ COPY manager . # 1. Enables a local_resource go build of the provider's manager binary # 2. Configures a docker build for the provider, with live updating of the manager binary # 3. Runs kustomize for the provider's config/default and applies it -def enable_provider(name): +def enable_provider(name, debug): p = providers.get(name) - + manager_name = p.get("manager_name") context = p.get("context") go_main = p.get("go_main", "main.go") label = p.get("label", name) + debug_port = int(debug.get("port", 0)) # Prefix each live reload dependency with context. For example, for if the context is # test/infra/docker and main.go is listed as a dep, the result is test/infra/docker/main.go. This adjustment is @@ -172,9 +181,40 @@ def enable_provider(name): # Set up a local_resource build of the provider's manager binary. The provider is expected to have a main.go in # manager_build_path or the main.go must be provided via go_main option. The binary is written to .tiltbuild/manager. + # TODO @randomvariable: Race detector mode only currently works on x86-64 Linux. + # Need to switch to building inside Docker when architecture is mismatched + race_detector_enabled = debug.get("race_detector", False) + if race_detector_enabled: + if os_name != "linux" or os_arch != "amd64": + fail("race_detector is only supported on Linux x86-64") + cgo_enabled = "1" + build_options = "-race" + ldflags = "-linkmode external -extldflags \"-static\"" + else: + cgo_enabled = "0" + build_options = "" + ldflags = "-extldflags \"-static\"" + + if debug_port != 0: + # disable optimisations and include line numbers when debugging + gcflags = "all=-N -l" + else: + gcflags = "" + + build_env = "CGO_ENABLED={cgo_enabled} GOOS=linux GOARCH=amd64".format( + cgo_enabled = cgo_enabled, + ) + build_cmd = "{build_env} go build {build_options} -gcflags '{gcflags}' -ldflags '{ldflags}' -o .tiltbuild/manager {go_main}".format( + build_env = build_env, + build_options = build_options, + gcflags = gcflags, + go_main = go_main, + ldflags = ldflags, + ) + local_resource( label.lower() + "_binary", - cmd = "cd " + context + ';mkdir -p .tiltbuild;CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags \'-extldflags "-static"\' -o .tiltbuild/manager ' + go_main, + cmd = "cd " + context + ";mkdir -p .tiltbuild;" + build_cmd, deps = live_reload_deps, labels = [label, "ALL.binaries"], ) @@ -189,9 +229,31 @@ def enable_provider(name): additional_docker_build_commands, ]) + port_forwards = [] + links = [] + + if debug_port != 0: + # Add delve when debugging. Delve will always listen on the pod side on port 30000. + entrypoint = ["sh", "/start.sh", "/dlv", "--listen=:" + str(30000), "--accept-multiclient", "--api-version=2", "--headless=true", "exec", "--", "/manager"] + port_forwards.append(port_forward(debug_port, 30000)) + if debug.get("continue", True): + entrypoint.insert(8, "--continue") + else: + entrypoint = ["sh", "/start.sh", "/manager"] + + metrics_port = int(debug.get("metrics_port", 0)) + profiler_port = int(debug.get("profiler_port", 0)) + if metrics_port != 0: + port_forwards.append(port_forward(metrics_port, 8080)) + links.append(link("http://localhost:" + str(metrics_port) + "/metrics", "metrics")) + + if profiler_port != 0: + port_forwards.append(port_forward(profiler_port, 6060)) + entrypoint.extend(["--profiler-address", ":6060"]) + links.append(link("http://localhost:" + str(profiler_port) + "/debug/pprof", "profiler")) + # Set up an image build for the provider. The live update configuration syncs the output from the local_resource # build into the container. - entrypoint = ["sh", "/start.sh", "/manager"] provider_args = extra_args.get(name) if provider_args: entrypoint.extend(provider_args) @@ -217,16 +279,19 @@ def enable_provider(name): os.environ.update(substitutions) # Apply the kustomized yaml for this provider - yaml = str(kustomize_with_envsubst(context + "/config/default")) + if debug_port == 0: + yaml = str(kustomize_with_envsubst(context + "/config/default", False)) + else: + yaml = str(kustomize_with_envsubst(context + "/config/default", True)) k8s_yaml(blob(yaml)) - manager_name = p.get("manager_name") - if manager_name: - k8s_resource( - workload = manager_name, - new_name = label.lower() + "_controller", - labels = [label, "ALL.controllers"], - ) + k8s_resource( + workload = manager_name, + new_name = label.lower() + "_controller", + labels = [label, "ALL.controllers"], + port_forwards = port_forwards, + links = links, + ) # Users may define their own Tilt customizations in tilt.d. This directory is excluded from git and these files will # not be checked in to version control. @@ -241,10 +306,24 @@ def enable_providers(): user_enable_providers = settings.get("enable_providers", []) union_enable_providers = {k: "" for k in user_enable_providers + always_enable_providers}.keys() for name in union_enable_providers: - enable_provider(name) + enable_provider(name, settings.get("debug").get(name, {})) + +def kustomize_with_envsubst(path, enable_debug = False): + # we need to ditch the readiness and liveness probes when debugging, otherwise K8s will restart the pod whenever execution + # has paused. + if enable_debug: + yq_cmd_line = "| {} eval 'del(.. | select(has\"livenessProbe\")).livenessProbe | del(.. | select(has\"readinessProbe\")).readinessProbe' -".format(yq_cmd) + else: + yq_cmd_line = "" + return str(local("{} build {} | {} {}".format(kustomize_cmd, path, envsubst_cmd, yq_cmd_line), quiet = True)) + +def ensure_yq(): + if not os.path.exists(yq_cmd): + local("make {}".format(yq_cmd)) -def kustomize_with_envsubst(path): - return str(local("{} build {} | {}".format(kustomize_cmd, path, envsubst_cmd), quiet = True)) +def ensure_kustomize(): + if not os.path.exists(kustomize_cmd): + local("make {}".format(kustomize_cmd)) ############################## # Actual work happens here @@ -258,4 +337,6 @@ load("ext://cert_manager", "deploy_cert_manager") if settings.get("deploy_cert_manager"): deploy_cert_manager(version = "v1.5.3") +ensure_yq() +ensure_kustomize() enable_providers() diff --git a/docs/book/src/developer/tilt.md b/docs/book/src/developer/tilt.md index a6a7b0c69e72..c7601ca27273 100644 --- a/docs/book/src/developer/tilt.md +++ b/docs/book/src/developer/tilt.md @@ -13,7 +13,7 @@ workflow that offers easy deployments and rapid iterative builds. 1. [kustomize](https://kubectl.docs.kubernetes.io/installation/kustomize/) standalone (`kubectl kustomize` does not work because it is missing some features of kustomize v3) -1. [Tilt](https://docs.tilt.dev/install.html) v0.16.0 or newer +1. [Tilt](https://docs.tilt.dev/install.html) v0.22.2 or newer 1. [envsubst](https://github.com/drone/envsubst) or similar to handle clusterctl var replacement. Note: drone/envsubst releases v1.0.2 and earlier do not have the binary packaged under cmd/envsubst. It is @@ -38,7 +38,7 @@ A script to create a KIND cluster along with a local docker registry and the cor To create a pre-configured cluster run: -```bash +```bash ./hack/kind-install-for-capd.sh ```` @@ -90,6 +90,70 @@ For example, if the yaml contains `${AWS_B64ENCODED_CREDENTIALS}`, you could do } ``` +**debug** (Map{string: Map} default{}): A map of named configurations for the provider. The key is the name of the provider. + +Supported settings: + + * **port** (int, default=0 (disabled)): If set to anything other than 0, then Tilt will run the provider with delve + and port forward the delve server to localhost on the specified debug port. This can then be used with IDEs such as + Visual Studio Code, Goland and IntelliJ. + + * **continue** (bool, default=true): By default, Tilt will run delve with `--continue`, such that any provider with + debugging turned on will run normally unless specifically having a breakpoint entered. Change to false if you + do not want the controller to start at all by default. + + * **profiler_port** (int, default=0 (disabled)): If set to anything other than 0, then Tilt will enable the profiler with + `--profiler-address` and set up a port forward. A "profiler" link will be visible in the Tilt Web UI for the controller. + + * **metrics_port** (int, default=0 (disabled)): If set to anything other than 0, then Tilt will port forward to the + default metrics port. A "metrics" link will be visible in the Tilt Web UI for the controller. + + * **race_detector** (bool, default=false) (Linux amd64 only): If enabled, Tilt will compile the specified controller with + cgo and statically compile in the system glibc and enable the race detector. Currently, this is only supported when + building on Linux amd64 systems. You must install glibc-static or have libc.a available for this to work. + + Example: Using the configuration below: + + ```json + "debug": { + "core": { + "continue": false, + "port": 30000, + "profiler_port": 40000, + "metrics_port": 40001 + } + }, + ``` + + ##### Wiring up debuggers + ###### Visual Studio + When using the example above, the core CAPI controller can be debugged in Visual Studio Code using the following launch configuration: + + ```json + { + "version": "0.2.0", + "configurations": [ + { + "name": "Core CAPI Controller", + "type": "go", + "request": "attach", + "mode": "remote", + "remotePath": "", + "port": 30000, + "host": "127.0.0.1", + "showLog": true, + "trace": "log", + "logOutput": "rpc" + } + ] + } + ``` + + ###### Goland + With the above example, you can configure [a Go Remote run/debug + configuration](https://www.jetbrains.com/help/go/attach-to-running-go-processes-with-debugger.html#step-3-create-the-remote-run-debug-configuration-on-the-client-computer) + pointing at port 30000. + {{#/tab }} {{#tab AZURE}} @@ -264,7 +328,7 @@ Set to `false` if your provider does not have a ./config folder or you do not wa **go_main** (String, default="main.go"): The go main file if not located at the root of the folder -**label** (String, default=provider name): The label to be used to group provider components in the tilt UI +**label** (String, default=provider name): The label to be used to group provider components in the tilt UI in tilt version >= v0.22.2 (see https://blog.tilt.dev/2021/08/09/resource-grouping.html); as a convention, provider abbreviation should be used (CAPD, KCP etc.). diff --git a/hack/tools/go.mod b/hack/tools/go.mod index f3c80d494d83..dd95d7650887 100644 --- a/hack/tools/go.mod +++ b/hack/tools/go.mod @@ -7,6 +7,7 @@ require ( github.com/drone/envsubst/v2 v2.0.0-20210615175204-7bf45dbf5372 github.com/hashicorp/go-multierror v1.0.0 github.com/joelanford/go-apidiff v0.1.0 + github.com/mikefarah/yq/v4 v4.13.5 github.com/onsi/ginkgo v1.16.4 github.com/pkg/errors v0.9.1 github.com/sergi/go-diff v1.2.0 // indirect diff --git a/hack/tools/go.sum b/hack/tools/go.sum index f8282375b80f..527e0fa59615 100644 --- a/hack/tools/go.sum +++ b/hack/tools/go.sum @@ -148,6 +148,8 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1 github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc= +github.com/elliotchance/orderedmap v1.4.0 h1:wZtfeEONCbx6in1CZyE6bELEt/vFayMvsxqI5SgsR+A= +github.com/elliotchance/orderedmap v1.4.0/go.mod h1:wsDwEaX5jEoyhbs7x93zk2H/qv0zwuhg4inXhDkYqys= github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs= github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= @@ -200,12 +202,18 @@ github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL9 github.com/go-openapi/jsonreference v0.19.5/go.mod h1:RdybgQwPxbL4UEjuAruzK1x3nE69AqPYEJeo/TWfEeg= github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gobuffalo/flect v0.2.3 h1:f/ZukRnSNA/DUpSNDadko7Qc0PhGvsew35p/2tu+CRY= github.com/gobuffalo/flect v0.2.3/go.mod h1:vmkQwuZYhN5Pc4ljYQZzP+1sq+NEkK+lh20jmEmX3jc= +github.com/goccy/go-yaml v1.8.9 h1:4AEXg2qx+/w29jXnXpMY6mTckmYu1TMoHteKuMf0HFg= +github.com/goccy/go-yaml v1.8.9/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -341,6 +349,8 @@ github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jinzhu/copier v0.2.8 h1:N8MbL5niMwE3P4dOwurJixz5rMkKfujmMRFmAanSzWE= +github.com/jinzhu/copier v0.2.8/go.mod h1:24xnZezI2Yqac9J61UC6/dG/k76ttpq0DdJI3QmUvro= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/joelanford/go-apidiff v0.1.0 h1:bt/247wfLDKFnCC5jYdapR3WY2laJMPB9apfc1U9Idw= github.com/joelanford/go-apidiff v0.1.0/go.mod h1:wgVWgVCwYYkjcYpJtBnWYkyUYZfVovO3Y5pX49mJsqs= @@ -377,11 +387,13 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= @@ -397,6 +409,8 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/mikefarah/yq/v4 v4.13.5 h1:uUlZIIALDjncF71iStcRgQfpVxqDowP0EWs7DExQykQ= +github.com/mikefarah/yq/v4 v4.13.5/go.mod h1:t4GWLXX68lG7216HHQLdZHGciStS1g8UdvaT1zkEfzg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= @@ -571,6 +585,10 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/timtadh/data-structures v0.5.3 h1:F2tEjoG9qWIyUjbvXVgJqEOGJPMIiYn7U5W5mE+i/vQ= +github.com/timtadh/data-structures v0.5.3/go.mod h1:9R4XODhJ8JdWFEI8P/HJKqxuJctfBQw6fDibMQny2oU= +github.com/timtadh/lexmachine v0.2.2 h1:g55RnjdYazm5wnKv59pwFcBJHOyvTPfDEoz21s4PHmY= +github.com/timtadh/lexmachine v0.2.2/go.mod h1:GBJvD5OAfRn/gnp92zb9KTgHLB7akKyxmVivoYCcjQI= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -835,8 +853,9 @@ golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 h1:RqytpXGR1iVNX7psjB3ff8y7sNFinVFvkx1c8SjBkio= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE= @@ -1057,6 +1076,8 @@ gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473 h1:6D+BvnJ/j6e222UW8s2qTSe3wGBtvo0MbVQG/c5k8RE= +gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473/go.mod h1:N1eN2tsCx0Ydtgjl4cqmbRCsY4/+z4cYDeqwZTk6zog= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/square/go-jose.v2 v2.2.2/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/src-d/go-billy.v4 v4.3.2 h1:0SQA1pRztfTFx2miS8sA97XvooFeNOmvUenF4o0EcVg= diff --git a/hack/tools/tools.go b/hack/tools/tools.go index 2ab9f7b33c15..792e899d13be 100644 --- a/hack/tools/tools.go +++ b/hack/tools/tools.go @@ -27,4 +27,5 @@ import ( _ "k8s.io/code-generator/cmd/conversion-gen" _ "sigs.k8s.io/controller-runtime/tools/setup-envtest" _ "sigs.k8s.io/controller-tools/cmd/controller-gen" + _ "github.com/mikefarah/yq/v4" ) diff --git a/tilt_modules/extensions.json b/tilt_modules/extensions.json index 6084a8ef232e..fc430e8e63d5 100644 --- a/tilt_modules/extensions.json +++ b/tilt_modules/extensions.json @@ -4,6 +4,11 @@ "Name": "cert_manager", "ExtensionRegistry": "https://github.com/tilt-dev/tilt-extensions", "TimeFetched": "2021-05-26T11:19:57.411132-06:00" + }, + { + "Name": "local_output", + "ExtensionRegistry": "https://github.com/tilt-dev/tilt-extensions", + "TimeFetched": "2021-10-25T15:14:03.060899611+01:00" } ] } diff --git a/tilt_modules/local_output/README.md b/tilt_modules/local_output/README.md new file mode 100644 index 000000000000..ca80df9cef64 --- /dev/null +++ b/tilt_modules/local_output/README.md @@ -0,0 +1,27 @@ +# local_output + +Author: [Çağatay Yücelen](https://github.com/cyucelen) + +Get the output of a shell command as string to use it in Tiltfile. + +`local_output` runs given command and strips trailing `\n`. + +## Usage + +Pass a command to execute with local. + +### Example: + +```python +desired_memory_capacity = "4096" +current_memory_capacity = local_output('minikube config get memory') # e.g. "2048" + +if int(current_memory_capacity) < int(desired_memory_capacity): + local('minikube config set memory {}'.format(desired_memory_capacity)) + local('minikube delete') + local('minikube start') +``` + +### Arguments: + +- `command`: shell command to execute with `local` function diff --git a/tilt_modules/local_output/Tiltfile b/tilt_modules/local_output/Tiltfile new file mode 100644 index 000000000000..ebd7f2466a81 --- /dev/null +++ b/tilt_modules/local_output/Tiltfile @@ -0,0 +1,2 @@ +def local_output(*args, **kwargs): + return str(local(*args, **kwargs)).rstrip('\n') diff --git a/tilt_modules/local_output/test/Tiltfile b/tilt_modules/local_output/test/Tiltfile new file mode 100644 index 000000000000..5d47abe20394 --- /dev/null +++ b/tilt_modules/local_output/test/Tiltfile @@ -0,0 +1,9 @@ +load('../Tiltfile', 'local_output') + +s = local_output('echo abc') +s += local_output('echo def') + +print(s) + +# since tilt ci requires a resource +local_resource('dummy', 'echo dummy') diff --git a/tilt_modules/local_output/test/test.sh b/tilt_modules/local_output/test/test.sh new file mode 100755 index 000000000000..4325b2a38c4e --- /dev/null +++ b/tilt_modules/local_output/test/test.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +cd "$(dirname "$0")" || exit + +set -euo pipefail + +OUTPUT="$(tilt ci)" + +if ! (echo "$OUTPUT" | grep -q abcdef); then + echo "did not find string 'abcdef' in output" + echo "output:" + echo + echo "$OUTPUT" + exit 1 +fi