getting error while deploying sample Kubernetes deployment using bazel

8/31/2019

Bazel failed to deploy sample k8s deployment (deployment.yaml) file in k8s tenant.

I followed the link https://github.com/bazelbuild/rules_k8s#aliasing-eg-k8s_deploy and tried one sample deployment.yaml file to deploy the application in k8s tenant. I have one k8s tenant already configured in the build machine. To deploy the application I executed:

bazel run //main:dev.create

But the bazel command is failing with below error:

[root@localhost t2]# bazel  run //main:dev.create <br/>
Starting local Bazel server and connecting to it... <br/>
INFO: Analyzed target //main:dev.create (68 packages loaded, 6876 targets configured).<br/>
INFO: Found 1 target...<br/>
INFO: Deleting stale sandbox base <br/>/root/.cache/bazel/_bazel_root/5ad59170e5ff426844f68e5dd9f66fb3/sandbox
Target //main:dev.create up-to-date:<br/>
  bazel-bin/main/dev.create<br/>
INFO: Elapsed time: 33.497s, Critical Path: 2.04s<br/>
INFO: 0 processes.<br/>
INFO: Build completed successfully, 1 total action<br/>
INFO: Build completed successfully, 1 total action<br/>
$ /usr/local/bin/kubectl --cluster=kubernetes --context= --user= create -f -<br/>
error: error parsing STDIN: error converting YAML to JSON: yaml: line 4: <br/>mapping values are not allowed in this context<br/>

this is my WORKSPACE file

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "io_bazel_rules_go",
    remote = "https://github.com/bazelbuild/rules_go.git",
    tag = "0.18.5"
)

git_repository(
    name = "bazel_gazelle",
    remote = "https://github.com/bazelbuild/bazel-gazelle.git",
    tag = "0.17.0",
)


load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk","go_register_toolchains","go_rules_dependencies")

go_download_sdk(
    name = "gosdk",
    sdks = {
       ......
    },
    urls = [....],
)

go_register_toolchains(
     "@//:gosdk",
)

go_rules_dependencies()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

gazelle_dependencies()


git_repository(
    name = "io_bazel_rules_docker",
    commit = "e12e276a9a6ded09363a6c1f0de46c573bd6096c",
    remote = "https://github.com/xxxxx/rules_docker.git",
)
load(
    "@io_bazel_rules_docker//repositories:repositories.bzl",
    container_repositories = "repositories",
)

container_repositories()

load("@io_bazel_rules_docker//container:container.bzl", "container_pull")
load(
    "@io_bazel_rules_docker//go:image.bzl",
    go_image_repos = "repositories",
)

go_image_repos()

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_k8s",
    sha256 = "91fef3e6054096a8947289ba0b6da3cba559ecb11c851d7bdfc9ca395b46d8d8",
    strip_prefix = "rules_k8s-0.1",
    urls = ["https://github.com/bazelbuild/rules_k8s/releases/download/v0.1/rules_k8s-v0.1.tar.gz"],
)

load("@io_bazel_rules_k8s//k8s:k8s.bzl", "k8s_repositories")

k8s_repositories()

load("@io_bazel_rules_k8s//k8s:k8s.bzl", "k8s_defaults")

k8s_defaults(
  name = "k8s_deploy",
  kind = "deployment",
  cluster = "kubernetes",
)

build.bazel file :

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_binary(
    name = "hello_go",
    embed = [":go_default_library"],
    visibility = ["//visibility:public"],
)

go_library(
    name = "go_default_library",
    srcs = ["main.go"],
)

load("@io_bazel_rules_docker//go:image.bzl", "go_image")

go_image(
    name = "go-image",
    base = ":test",
    embed = [":go_default_library"],
)

load("@io_bazel_rules_docker//container:image.bzl", "container_image")

container_image(
    name = "test",
    base = "@go_image_base//image",
    user = "101",
)

load("@io_bazel_rules_k8s//k8s:object.bzl", "k8s_object")

k8s_object(
    name = "dev",
    kind = "deployment",
    template = ":deployment.yaml",
    cluster = "kubernetes", 
    images = {
        "xxxxx.net/test/new:v1": ":go-image",
    },
)

deployment.yaml file :

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: staging
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test
        image: xxxxx.net/test/new:v1
        imagePullPolicy: Always
        ports:
        - containerPort: 50051

On the same server, i have kubeconfig file kept at /root/.kube/config.

-- Manish Thakur
bazel
kubernetes

0 Answers