<p>I am trying to use <a href="https://github.com/bazelbuild/rules_k8s" rel="nofollow noreferrer">rules_k8s</a> for Bazel to deploy to my Kubernetes cluster.</p>

<p>Thus I have this <code>cloudbuild.yaml</code> file, which is executed by Google Cloud Build:</p>

<pre><code>steps:
  - name: gcr.io/cloud-builders/bazel
    args: ['run', '//:kubernetes.apply']
</code></pre>

<p>(<code>//:kubernetes</code> is just a <a href="https://github.com/bazelbuild/rules_k8s#multi-object-actions" rel="nofollow noreferrer"><code>k8s_objects</code></a>)</p>

<hr>

<p>On my local machine running <code>bazel run //:kubernetes.apply</code> works fine, but
although the Google Cloud Build succeeds, it logs those errors. So the configuration is not applied to my Kubernetes cluster:</p>

<pre><code>Target //:kubernetes.apply up-to-date:
  bazel-bin/kubernetes.apply
INFO: Elapsed time: 29.863s, Critical Path: 0.14s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/kubernetes.apply
INFO: Build Event Protocol files produced successfully.
INFO: Build completed successfully, 1 total action
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so projection_database_k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so projection_database_k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so k8s_service.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_deployment.apply cannot be executed.
kubectl toolchain was not properly configured so event_store_k8s_service.apply cannot be executed.
</code></pre>

<p>I also get a warning from the bazel cache:</p>

<pre><code>DEBUG: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_k8s/toolchains/kubectl/kubectl_toolchain.bzl:28:9: No kubectl tool was found or built, executing run for rules_k8s targets might not work.
</code></pre>

<p>P.S.: I get the same errors when using <code>//:kubernetes.create</code></p>

<h1>My setup</h1>

<p><strong>Deployments</strong></p>

<pre><code>load("@io_bazel_rules_k8s//k8s:object.bzl", "k8s_object")
k8s_object(
  name = "k8s_deployment",
  kind = "deployment",
  cluster = "gke_cents-ideas_europe-west3-a_cents-ideas",
  template = ":ideas.deployment.yaml",
  images = {
    "gcr.io/cents-ideas/ideas:latest": ":image"
  },
)
</code></pre>

<p><strong>Services</strong></p>

<pre><code>k8s_object(
  name = "k8s_service",
  kind = "service",
  cluster = "gke_cents-ideas_europe-west3-a_cents-ideas",
  template = ":ideas.service.yaml",
)
</code></pre>

<p><strong>Aggregations</strong></p>

<pre><code>load("@io_bazel_rules_k8s//k8s:objects.bzl", "k8s_objects")
k8s_objects(
    name = "k8s",
    objects = [
      ":k8s_deployment",
      ":k8s_service",
    ]
)
</code></pre>

<p><strong>Final composition</strong></p>

<pre><code>k8s_objects(
    name = "kubernetes",
    objects = [
        "//services/ideas:k8s",
        # ...
    ]
)
</code></pre>

<h1>Update</h1>

<p>I've now tried to make my own docker image with Bazel and kubectl:</p>

<pre><code>FROM gcr.io/cloud-builders/bazel

RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/kubectl
</code></pre>

<p>I pushed it to GCR and changed my <code>cloudbuild.yaml</code> to:</p>

<pre><code>steps:
  - name: eu.gcr.io/cents-ideas/bazel-kubectl
    args: ["run", "//:kubernetes.apply"]
</code></pre>

<p>I firstly noticed, that the step took way longer than before. However at the end it throws an error:</p>

<pre><code>$ /usr/local/bin/kubectl --kubeconfig= --cluster=gke_cents-ideas_europe-west3-a_cents-ideas --context= --user= apply -f -
error: cluster "gke_cents-ideas_europe-west3-a_cents-ideas" does not exist
</code></pre>

<p><a href="https://00e9e64bac389a379917c629a22bd56b248c258ede42cf265c-apidata.googleusercontent.com/download/storage/v1/b/64811142611.cloudbuild-logs.googleusercontent.com/o/log-bb2f4b6f-84c2-4f56-af89-15a305e536e0.txt?qk=AD5uMEu_YRlHAfSU5xwyiKmKh8lsG_Gb6O2Q114junmxLrr_VyNhbo_udKtW0yniyjEeWqRQDe-o2NohuW6dXWXmXvyvInr7nimY35qmp-KsvBdWmbCPs-eAcqz6TTk65lcEz-3CwYr7LgKFx0QCOHvZQEzMC6cDquYJNOV6oTGurHmt67gsKkf5DHLiZ7cCdGPsoucsZoByVL6Bf95bfCOch0TByIeQdnkNSVJCn-uoa7JAe3n9ge0TBh01frBLXraYxW_3T9EYsAkNA5vDkdA-UIckqXy86rloR4bruDv_bBwYdqgUkkJ1sl83BDpfT5rbTim11lfeF9a1lbvNPWXB3dO7lfjiiqPIcsDaPpM4V5dRy0InSHO7AYhoJom6chfj-MXLZfcDeuUZdqW2845BGOijkFp0d5vB_YVHp47dMAxI8TYJztiY5ktgQ7n3DzG4aynzLKTXgn6BLGpZqkz70JFW0ZMTE0Gr2afsoIXN1rZeI-BkDyZqs4MXd3m6HfSnO0o9z08IlWgNwzNUf68zTO2gk2BFA1QVvbOFFdfTl9z0wDIMdBRT7vHqgTGXF1bhjK1fWhROAxsJdOQp3qrFftLPFE2POwiFXG-wAqTYULN6usLlAcPLyJcU0VzGmNaDovZdXXzYtcdSzmCgIC6fk-_cOJiq6RsicK_co3sevGqqjL3Hehba-VxyLw930GyuwtzC6zVst3p9-RbKg9xw3tzznikg8q7hIOu9leLfAG7LzmejLkB7dKSrDKUBVDLDtxNzRzK2pNU9D-ErBZcvb2o8qRNWoMrbiZaU0l3cRDRLlrRJYSFt90PbsfHBmyZXcYHSnXyg6rQB08sWq_MM0awUySouQ3IYJZQhNfXFmFUUoAFtafwRC4po1gbzxBzjhtpw6WKj" rel="nofollow noreferrer">Here</a> is the full log.</p>

<p>As for the updated question, now you need to authenticate somehow to GKE inside the container.</p>

<p>First thing, I recommend installing gcloud tool to your container.
Btw, as for the huge container size 1.2 GB, that's because <code>cloud-builders/bazel</code> is huge :)</p>

<p>Have a look at our example on slim bazel container version:
<a href="https://github.com/aspect-development/bazel-k8s-example/blob/master/tools/Dockerfile.dazel" rel="nofollow noreferrer">https://github.com/aspect-development/bazel-k8s-example/blob/master/tools/Dockerfile.dazel</a></p>

<p>And here is Dockerfile for installing gcloud and kubectl, so you can grab needed parts from both files:
<a href="https://github.com/GoogleCloudPlatform/cloud-builders/blob/master/gcloud/Dockerfile" rel="nofollow noreferrer">https://github.com/GoogleCloudPlatform/cloud-builders/blob/master/gcloud/Dockerfile</a></p>

<p>The second thing is authenticating, after gcloud is installed it should be easy.
Overall cloudbuild step should look similar to this:</p>

<pre><code>- name: &lt;link to your container&gt;
  entrypoint: /bin/sh
  args:
  - -c
  - |
    gcloud container clusters get-credentials cents-ideas --zone europe-west3-a --project cents-ideas
    bazel run //:kubernetes.apply
</code></pre>
