Skip to content

Commit

Permalink
[#52] Add rules for Minikube
Browse files Browse the repository at this point in the history
  • Loading branch information
ljupcovangelski committed Jul 6, 2022
1 parent e0dacc2 commit c421d1c
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 2 deletions.
66 changes: 64 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ bazel test //.../helm-chart:template

**Parameters:**

- `name` Unique name of the test rule.
- `name` Unique name of the rule.
- `_helm_binary` Location of the Helm binary to be used (Defaults to the binary downloaded with the repository rule).
- `chart` A tgz archive containing the Helm chart.

Expand Down Expand Up @@ -395,7 +395,7 @@ bazel run //.../helm-chart:push

**Parameters:**

- `name` Unique name of the test rule.
- `name` Unique name of the rule.
- `_helm_binary` Location of the Helm binary to be used (Defaults to the binary downloaded with the repository rule).
- `chart` A tgz archive containing the Helm chart.
- `repository_url` The URL of the repository where the Helm chart is pushed.
Expand All @@ -407,6 +407,68 @@ bazel run //.../helm-chart:push

Note that only `basic` auth is supported at the moment. If you are using it, you must export `HELM_REPO_USERNAME` and `HELM_REPO_PASSWORD` with the username and the password of your Chartmuseum helm repository.

## Minikube

Currently the Minikube rule set supports starting (creating) and stopping (destroying) a Minikube Kubernetes cluster.

For downloading the Minikube binary, add this to your WORKSPACE file:

```python
load("@com_github_airyhq_bazel_tools//minikube:minikube.bzl", "minikube_tool")

minikube_tool(
name = "minikube_binary",
)
```

For creating a Kubernetes cluster add this to your BUILD file:

```python
load("@com_github_airyhq_bazel_tools//minikube:minikube.bzl", "minikube_start")

minikube_start(
name = "minikube-start",
)
```

Then run:

```shell
bazel run //.../infrastructure:minikube-start
```

**Parameters:**

- `name` Unique name of the rule.
- `_minikube_binary` (optional) Location of the Minikube binary to be used (Defaults to the binary downloaded with the repository rule).
- `profile` (optional) The Minikube profile [default: airy-core].
- `driver` (optional) The Minikube driver [default: docker].
- `cpus` (optional) The number of CPU cores for the Kubernetes node [default: 4].
- `memory` (optional) The amount of memory of the Kubernetes node [default: 7168].
- `ingress_port` (optional) The NodePort to be opened for the Ingress Controller [default: 80].

For destroying a Kubernetes cluster add this to your BUILD file:

```python
load("@com_github_airyhq_bazel_tools//minikube:minikube.bzl", "minikube_stop")

minikube_stop(
name = "minikube-stop",
)
```

Then run:

```shell
bazel run //.../infrastructure:minikube-stop
```

**Parameters:**

- `name` Unique name of the rule.
- `_minikube_binary` (optional) Location of the Minikube binary to be used (Defaults to the binary downloaded with the repository rule).
- `profile` (optional) The Minikube profile [default: airy-core].

## Aspects

We provide a simple [aspect](https://docs.bazel.build/versions/main/skylark/aspects.html) that helps discover the output groups of a target.
Expand Down
Empty file added minikube/BUILD
Empty file.
90 changes: 90 additions & 0 deletions minikube/minikube.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
def _minikube_tool_impl(ctx):
os_build_name = "linux"
if ctx.os.name.startswith("mac"):
os_build_name = "darwin"
ctx.download(
"https://storage.googleapis.com/minikube/releases/latest/minikube-%s-amd64" % (os_build_name),
output='minikube',
executable=True,
)
ctx.file("BUILD", 'exports_files(["minikube"])')

minikube_tool = repository_rule(
implementation = _minikube_tool_impl,
)

def _minikube_start_impl(ctx):
script = "{} -p {} start --driver={} --cpus={} --memory={} --container-runtime=containerd --ports={}:{} --extra-config=apiserver.service-nodeport-range=1-65535".format(ctx.executable._minikube_binary.path, ctx.attr.profile, ctx.attr.driver, ctx.attr.cpus, ctx.attr.memory, ctx.attr.ingress_port, ctx.attr.ingress_port)

ctx.actions.write(
output = ctx.outputs.executable,
content = script,
is_executable = True,
)

runfiles = ctx.runfiles(files = [ctx.executable._minikube_binary])
return [DefaultInfo(runfiles = runfiles)]

minikube_start = rule(
implementation = _minikube_start_impl,
executable = True,
attrs = {
"_minikube_binary": attr.label(
executable = True,
cfg = "exec",
allow_files = True,
default = Label("@minikube_binary//:minikube"),
doc = "The Minikube binary downloaded with a repository rule.",
),
"profile": attr.string(
doc = "The Minikube profile.",
default = "airy-core",
),
"driver": attr.string(
doc = "The Minikube driver.",
default = "docker",
),
"cpus": attr.string(
doc = "CPU cores for the Kubernetes node.",
default = "4",
),
"memory": attr.string(
doc = "Memory for the Kubernetes node.",
default = "7168",
),
"ingress_port": attr.string(
doc = "Port for the ingress controller NodePort.",
default = "80",
),
},
)

def _minikube_stop_impl(ctx):
script = "{} -p {} delete".format(ctx.executable._minikube_binary.path, ctx.attr.profile)

ctx.actions.write(
output = ctx.outputs.executable,
content = script,
is_executable = True,
)

runfiles = ctx.runfiles(files = [ctx.executable._minikube_binary])
return [DefaultInfo(runfiles = runfiles)]

minikube_stop = rule(
implementation = _minikube_stop_impl,
executable = True,
attrs = {
"_minikube_binary": attr.label(
executable = True,
cfg = "exec",
allow_files = True,
default = Label("@minikube_binary//:minikube"),
doc = "The Minikube binary downloaded with a repository rule.",
),
"profile": attr.string(
doc = "The Minikube profile.",
default = "airy-core",
),
},
)

0 comments on commit c421d1c

Please sign in to comment.