jsonnet-libs/k8s

bazel rules

Geethree opened this issue · 7 comments

Would this group be open for me adding a bazel build file, some rules and macros?

Currently, I am pulling in k8s-alpha via a bazel http_archive and creating a jsonnet_library out of them. But it would be lovely to just be able to grab this repository and build them directly.

xvzf commented

Since there is no build system yet - just do a PR and we can go from that?

I was planning to automate it through Github actions for now. I'm not familiar with Bazel.

The github actions are now in place.

The most common way to pull in jsonnet libraries is currently through jsonnet-bundler, this also does leverages the http-archive afaik. Maybe a jsonnet_bundler bazel rule could be a thing? Or maybe the jsonnet_library could build-in the http_archive rule?

My main question is: why put the burden of integrating into someone else's CI system on the source files?

0xd3e commented

There is no need to put Bazel rules into the repository.

@Geethree you can put the following into your WORKSPACE file:

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

new_git_repository(
    name = "com_github_jsonnet-libs_k8s-libsonnet",
    remote = "https://github.com/jsonnet-libs/k8s-libsonnet",
    commit = "f47562c4e3bfaf46a5a5bc6a9e1e76a4fc76c18e",
    build_file_content = """
load("@io_bazel_rules_jsonnet//jsonnet:jsonnet.bzl", "jsonnet_library")

jsonnet_library(
    name = "1.20",
    srcs = glob(["1.20/**/*.libsonnet"]),
    visibility = ["//visibility:public"],
    imports = ["."],
)
""",

You can now use it in your BUILD files like this:

jsonnet_to_json(
    name = "statefulset",
    src = "statefulset.jsonnet",
    outs = ["statefulset.json"],
    deps = [
        "@com_github_jsonnet-libs_k8s-libsonnet//:1.20",
    ],
)

My example Jsonnet file looks like this:

local k = import "1.20/main.libsonnet";

{
  cockroachdb: {
    statefulset: k.apps.v1.statefulSet.new(
      name="cockroachdb",
      replicas=1,
      containers=[ 
        k.core.v1.container.new("cockroachdb", "cockroachdb/cockroach")
      ]
    )
  }
}

Personally I do not like that I have to use the path 1.20 when importing, but I couldn't find a better way yet.

0xd3e commented

Update: to get rid of the 1.20 in the import path, you can write a small wrapper and import that instead.

Wrapper content:

(import '1.20/main.libsonnet')

This also offers the benefit that you can add additional functionality like Grafana did (https://github.com/grafana/jsonnet-libs/tree/master/ksonnet-util).

The import line in other packages will then look like this:

local k = import "path/to/wrapper.libsonnet";

It is a common pattern to have this wrapper in lib/k.libsonnet, for example this works well with ksonnet-util on which quite a bit of libraries depend upon.

Judging on above discussion, I believe the original question was answered with a suggested solution by @0xIDANT. I think we can close it based on that.