Skip to content

Commit

Permalink
Support for arm64, k8s, helm (CloudburstMC#1552)
Browse files Browse the repository at this point in the history
* Initial helm chart, docker for arm64.

* Add missing slash to local helm values file in gitignore.

* Add in support to provide loadBalancerIP by helm value.

* Ports should not be set on the service if using default ClusterIP svc type.

* Revert svc condition.

* Remove condition on service ports.

* Add in nodePort key as a value configurable.

* Provide a default nodePort value.

* Make nodePort conditional.
  • Loading branch information
flaccid authored Jan 15, 2021
1 parent 8343435 commit 63b273f
Show file tree
Hide file tree
Showing 16 changed files with 512 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,6 @@ creativeitems.json
recipes.json
data/
data/*

# a file that can be used for your helm chart values
/helm-values.local.yaml
33 changes: 33 additions & 0 deletions Dockerfile.arm64
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM arm64v8/openjdk:8-jdk-slim AS build
WORKDIR /usr/local/src/nukkit
COPY src /usr/local/src/nukkit/src
COPY mvn* pom.xml /usr/local/src/nukkit/
COPY .git /usr/local/src/nukkit/.git
COPY .mvn /usr/local/src/nukkit/.mvn
COPY .gitmodules /usr/local/src/nukkit/.gitmodules
RUN apt-get -y update && \
apt-get install -y build-essential git maven && \
git submodule update --init && \
mvn clean package

FROM arm64v8/openjdk:8-jre-slim AS run
LABEL maintainer="Chris Fordham <[email protected]>"
COPY --from=build /usr/local/src/nukkit/target/nukkit-1.0-SNAPSHOT.jar /opt/nukkit/nukkit.jar
COPY nukkit.yml.default /etc/opt/nukkit/nukkit.yml
RUN useradd --user-group \
--no-create-home \
--home-dir /var/opt/nukkit \
--shell /usr/sbin/nologin \
minecraft && \
mkdir -p /var/opt/nukkit && \
chown -R minecraft /opt/nukkit /var/opt/nukkit /etc/opt/nukkit/nukkit.yml && \
ln -sfv /etc/opt/nukkit/nukkit.yml /var/opt/nukkit/nukkit.yml && \
apt-get -y update && \
apt-get -y install lsof && \
rm -rf /var/lib/apt/lists/*
USER minecraft
VOLUME /etc/opt/nukkit /var/opt/nukkit /opt/nukkit
EXPOSE 19132
WORKDIR /var/opt/nukkit
ENTRYPOINT ["java"]
CMD [ "-jar", "/opt/nukkit/nukkit.jar" ]
85 changes: 85 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
DOCKER_REGISTRY = index.docker.io
IMAGE_NAME = nukkit
IMAGE_VERSION = latest
IMAGE_ORG = flaccid
IMAGE_TAG = $(DOCKER_REGISTRY)/$(IMAGE_ORG)/$(IMAGE_NAME):$(IMAGE_VERSION)
export DOCKER_BUILDKIT = 1

WORKING_DIR := $(shell pwd)

.DEFAULT_GOAL := docker-build

.PHONY: build run

# TODO: finish
#build:: ## builds nukkit

# TODO: finish
#run:: ## runs the binary

docker-release:: docker-build docker-push ## builds and pushes the docker image to the registry
docker-release-arm64:: docker-build-arm64 docker-push-arm64 ## builds and pushes the arm64 docker image to the registry

docker-push:: ## pushes the docker image to the registry
@docker push $(IMAGE_TAG)

docker-push-arm64:: ## pushes the arm64 docker image to the registry
@docker push $(DOCKER_REGISTRY)/$(IMAGE_ORG)/$(IMAGE_NAME):arm64

docker-build:: ## builds the docker image locally
@echo http_proxy=$(HTTP_PROXY) http_proxy=$(HTTPS_PROXY)
@echo building $(IMAGE_TAG)
@docker build --pull \
--build-arg=http_proxy=$(HTTP_PROXY) \
--build-arg=https_proxy=$(HTTPS_PROXY) \
-t $(IMAGE_TAG) $(WORKING_DIR)

docker-build-arm64:: ## builds the arm64 docker image locally
@echo http_proxy=$(HTTP_PROXY) http_proxy=$(HTTPS_PROXY)
@echo building $(IMAGE_TAG)
@docker build --pull \
--build-arg=http_proxy=$(HTTP_PROXY) \
--build-arg=https_proxy=$(HTTPS_PROXY) \
-f Dockerfile.arm64 \
-t $(DOCKER_REGISTRY)/$(IMAGE_ORG)/$(IMAGE_NAME):arm64 $(WORKING_DIR)

docker-run:: ## runs the docker image locally
@docker run \
-it \
-p 19132:19132 \
$(DOCKER_REGISTRY)/$(IMAGE_ORG)/$(IMAGE_NAME):$(IMAGE_VERSION)

helm-install:: ## installs using helm from chart in repo
@helm install --name nukkit ./charts/nukkit

helm-upgrade:: ## upgrades deployed helm release
@helm upgrade nukkit ./charts/nukkit

helm-purge:: ## deletes and purges deployed helm release
@helm delete --purge nukkit

helm-render:: ## prints out the rendered chart
@helm install --dry-run --debug charts/nukkit

helm-validate:: ## runs a lint on the helm chart
@helm lint charts/nukkit

install-ghr:: ## installs ghr
@cd /tmp
@wget https://github.com/tcnksm/ghr/releases/download/v0.12.2/ghr_v0.12.2_linux_amd64.tar.gz
@tar zxvf ghr_v0.12.2_linux_amd64.tar.gz
@sudo mv ghr_v0.12.2_linux_amd64/ghr /usr/local/bin/

# a help target including self-documenting targets (see the awk statement)
define HELP_TEXT
Usage: make [TARGET]... [MAKEVAR1=SOMETHING]...

Available targets:
endef
export HELP_TEXT
help: ## this help target
@cat .banner
@echo
@echo "$$HELP_TEXT"
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / \
{printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ It has a few key advantages over other server software:
* Written in Java, Nukkit is faster and more stable.
* Having a friendly structure, it's easy to contribute to Nukkit's development and rewrite plugins from other platforms into Nukkit plugins.

Nukkit is **under improvement** yet, we welcome contributions.
Nukkit is **under improvement** yet, we welcome contributions.

Links
--------------------
Expand Down Expand Up @@ -67,6 +67,51 @@ Use [docker-compose](https://docs.docker.com/compose/overview/) to start server
docker-compose up -d
```

Kubernetes & Helm
-------------

Validate the chart:

`helm lint charts/nukkit`

Dry run and print out rendered YAML:

`helm install --dry-run --debug nukkit charts/nukkit`

Install the chart:

`helm install nukkit charts/nukkit`

Or, with some different values:

```
helm install nukkit \
--set image.tag="arm64" \
--set service.type="LoadBalancer" \
charts/nukkit
```

Or, the same but with a custom values from a file:

```
helm install nukkit \
-f helm-values.local.yaml \
charts/nukkit
```

Upgrade the chart:

`helm upgrade nukkit charts/nukkit`

Testing after deployment:

`helm test nukkit`

Completely remove the chart:

`helm uninstall nukkit`


Contributing
------------
Please read the [CONTRIBUTING](.github/CONTRIBUTING.md) guide before submitting any issue. Issues with insufficient information or in the wrong format will be closed and will not be reviewed.
23 changes: 23 additions & 0 deletions charts/nukkit/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
6 changes: 6 additions & 0 deletions charts/nukkit/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: nukkit
description: A Helm chart for Nukkit
type: application
version: 0.1.0
appVersion: 0.1.0
15 changes: 15 additions & 0 deletions charts/nukkit/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1. Get the application URL by running these commands:
{{- if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "nukkit.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "nukkit.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "nukkit.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "nukkit.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:80
{{- end }}
63 changes: 63 additions & 0 deletions charts/nukkit/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "nukkit.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "nukkit.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "nukkit.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "nukkit.labels" -}}
helm.sh/chart: {{ include "nukkit.chart" . }}
{{ include "nukkit.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "nukkit.selectorLabels" -}}
app.kubernetes.io/name: {{ include "nukkit.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "nukkit.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "nukkit.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
14 changes: 14 additions & 0 deletions charts/nukkit/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "nukkit.name" . }}-conf
labels:
app: {{ template "nukkit.name" . }}
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
release: "{{ .Release.Name }}"
heritage: "{{ .Release.Service }}"
data:
nukkit.yml: |-
{{- if .Values.config }}
{{ .Values.config | indent 4 }}
{{- end -}}
70 changes: 70 additions & 0 deletions charts/nukkit/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "nukkit.fullname" . }}
labels:
{{- include "nukkit.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "nukkit.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "nukkit.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "nukkit.serviceAccountName" . }}
securityContext:
{{- toYaml .Values.podSecurityContext | nindent 8 }}
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
volumeMounts:
- name: {{ template "nukkit.name" . }}-conf
subPath: nukkit.yml
mountPath: /var/opt/nukkit/nukkit.yml
ports:
- name: udp
containerPort: 19132
protocol: UDP
# TODO: fix/re-test
# readinessProbe:
# exec:
# command:
# - lsof
# - -i
# - :19132
# initialDelaySeconds: 3
# periodSeconds: 5
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumes:
- name: {{ template "nukkit.name" . }}-conf
configMap:
name: {{ template "nukkit.name" . }}-conf
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
Loading

0 comments on commit 63b273f

Please sign in to comment.