From da33fedafb20a17f2afbe68f85436ef07e4d86b7 Mon Sep 17 00:00:00 2001 From: Nick Novitski Date: Fri, 13 Dec 2024 15:09:06 -0800 Subject: [PATCH 1/2] feat(skaffold): buildx builder handles minikube --- skaffold-buildx-build.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/skaffold-buildx-build.sh b/skaffold-buildx-build.sh index 274c156..5680cc5 100755 --- a/skaffold-buildx-build.sh +++ b/skaffold-buildx-build.sh @@ -6,19 +6,32 @@ # docker images, and works on Apple Silicon via rosetta. PLATFORMS=${PLATFORMS:-linux/amd64} -# `docker buildx build` requires a builder instance, and builder instances also -# have a platform. For each value of PLATFORMS, we create or re-use -# a builder instance. +# `docker buildx build` requires a builder instance. Builder instances have an +# associated set of platforms, and a context. Therefore, we create or re-use a +# builder for each combination of those things # REF: https://docs.docker.com/reference/cli/docker/buildx/create/ +if [[ -n "${MINIKUBE_ACTIVE_DOCKERD:-}" ]]; then + context=minikube + echo "Deploying to a minikube cluster, using '$context' docker context" + # Since minikube instances can change without this context changing, it must + # be kept up-to-date, and the simplest way to do that is by always + # re-creating it. + docker context rm $context &> /dev/null || echo "'$context' docker context not found, creating it" + docker context create $context &> /dev/null +else + context=$(docker context show) +fi + # PLATFORMS can have `/` and `,` characters in it, but builder names can't. # This shell expansion replaces those with `-` characters -builder="skaffold-${PLATFORMS//[,\/]/-}" +builder="skaffold-${PLATFORMS//[,\/]/-}-$context" + if docker buildx inspect "$builder" >/dev/null 2>&1; then echo "Using existing builder $builder" else echo "Creating builder $builder" - docker buildx create --name "$builder" --platform "$PLATFORMS" + docker buildx create --name "$builder" --platform "$PLATFORMS" --use "$context" fi if [ "$PUSH_IMAGE" = "true" ]; then From a935b2e48a6b2dcf24b0eefcb0c4c5d1e9c3c2f3 Mon Sep 17 00:00:00 2001 From: Nick Novitski Date: Fri, 13 Dec 2024 15:27:07 -0800 Subject: [PATCH 2/2] fix(k8s): cluster option preserves kubectl context --- shell-modules/k8s.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/shell-modules/k8s.nix b/shell-modules/k8s.nix index dcbfe6e..d864ef0 100644 --- a/shell-modules/k8s.nix +++ b/shell-modules/k8s.nix @@ -26,8 +26,10 @@ in ]; gcloud.enable = true; gcloud.extra-components = [ "gke-gcloud-auth-plugin" ]; - interactiveShellHook = lib.optionalString ( - cfg.cluster != null - ) "gcloud container clusters get-credentials ${cfg.cluster} --region ${cfg.region}"; + interactiveShellHook = lib.optionalString (cfg.cluster != null) '' + kubectlContext="$(kubectl config current-context)" + gcloud container clusters get-credentials ${cfg.cluster} --region ${cfg.region} + kubectl config use "$kubectlContext" + ''; }; }