Skip to content

Commit 17bfcfd

Browse files
authored
Merge pull request #90 from R-HNF/add-docker-dev-env
Add development environment using dev container
2 parents 6950b4c + 9342f69 commit 17bfcfd

File tree

6 files changed

+103
-4
lines changed

6 files changed

+103
-4
lines changed

.devcontainer/devcontainer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
3+
{
4+
"name": "Gatling Operator Dev Container",
5+
"build": {
6+
// Sets the run context to one level up instead of the .devcontainer folder.
7+
"context": "..",
8+
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
9+
"dockerfile": "../Dockerfile.dev"
10+
},
11+
"mounts": [
12+
// Mount the host's Docker socket.
13+
// Official document: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/docker.md
14+
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"
15+
],
16+
"runArgs": [
17+
"--name=gatling-operator-dev-container",
18+
"--hostname=gatling-operator-dev-container",
19+
// Set network mode to host to communicate with other containers.
20+
"--network=host"
21+
],
22+
"containerEnv": {
23+
"IN_DEV_CONTAINER": "true"
24+
},
25+
// Restore the local kubectl config to a dev container.
26+
"postStartCommand": "if [ -d ${containerWorkspaceFolder}/.kube ]; then cp -r ${containerWorkspaceFolder}/.kube $HOME/.kube; fi",
27+
"customizations": {
28+
"vscode": {
29+
"extensions": [
30+
"streetsidesoftware.code-spell-checker",
31+
"mhutchie.git-graph"
32+
]
33+
}
34+
}
35+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ testbin/*
1717
# Kubernetes Generated files - skip generated files, except for vendored files
1818

1919
!vendor/**/zz_generated.*
20+
.kube
2021

2122
# editor and IDE paraphernalia
2223
.idea

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cSpell.words": [
3+
"devcontainer"
4+
]
5+
}

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ COPY controllers/ controllers/
1616
COPY pkg/ pkg/
1717

1818
# Build
19-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
19+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=$(dpkg --print-architecture) go build -a -o manager main.go
2020

2121
# Use distroless as minimal base image to package the manager binary
2222
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Dockerfile.dev

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
FROM ubuntu:22.04
2+
3+
ENV GO_VERSION 1.17.13
4+
ENV KUBECTL_VERSION v1.21.10
5+
6+
# KEEP the value as arm64.
7+
# This environment variable is for arm64, but should be left as is for any architecture
8+
# References:
9+
# https://github.com/etcd-io/etcd/issues/10677
10+
# https://github.com/k0sproject/k0s/issues/424
11+
ENV ETCD_UNSUPPORTED_ARCH arm64
12+
13+
# Development tools
14+
RUN apt-get update && apt-get upgrade -y && apt-get install -y \
15+
wget \
16+
git \
17+
make \
18+
gcc
19+
20+
# Docker CLI
21+
# Referring to https://docs.docker.com/engine/install/ubuntu/#installation-methods
22+
RUN apt-get install -y \
23+
ca-certificates \
24+
curl \
25+
gnupg \
26+
&& install -m 0755 -d /etc/apt/keyrings \
27+
&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
28+
| gpg --dearmor -o /etc/apt/keyrings/docker.gpg \
29+
&& chmod a+r /etc/apt/keyrings/docker.gpg \
30+
&& echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
31+
"$(. /etc/os-release && echo "${VERSION_CODENAME}")" stable" \
32+
| tee /etc/apt/sources.list.d/docker.list > /dev/null \
33+
&& apt-get update && apt-get install -y \
34+
docker-ce-cli
35+
36+
# kubectl
37+
# Referring to https://kubernetes.io/ja/docs/tasks/tools/install-kubectl/
38+
RUN curl -LO "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/"$(dpkg --print-architecture)"/kubectl" \
39+
&& chmod +x ./kubectl \
40+
&& mv ./kubectl /usr/local/bin/kubectl
41+
42+
# golang
43+
# Referring to https://go.dev/doc/install
44+
RUN wget "https://go.dev/dl/go${GO_VERSION}.linux-"$(dpkg --print-architecture)".tar.gz" \
45+
&& tar -C /usr/local -xzf "go${GO_VERSION}.linux-"$(dpkg --print-architecture)".tar.gz"
46+
ENV PATH "${PATH}:/usr/local/go/bin"
47+
ENV PATH "${PATH}:/root/go/bin"
48+
49+
# kind
50+
# References:
51+
# https://github.com/kind-ci/examples/blob/master/.github/workflows/kind.yml
52+
# https://kind.sigs.k8s.io/docs/user/resources/
53+
RUN GO111MODULE=on go install sigs.k8s.io/kind@latest

Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ SHELL = /usr/bin/env bash -o pipefail
2626
.SHELLFLAGS = -ec
2727

2828
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
29-
KIND_CONFIG_DIR=$(shell pwd)/config/kind
29+
KIND_CLUSTER_CONFIG_DIR=$(shell pwd)/config/kind
30+
KUBECONFIG_BACKUP_DIR=$(shell pwd)/.kube
3031

3132
all: build
3233

@@ -46,12 +47,16 @@ all: build
4647
help: ## Display this help.
4748
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
4849

49-
kind-create: ## Create a kind cluster named ${KIND_CLUSTER_NAME} locally if necessary
50+
kind-create: ## Create a kind cluster named ${KIND_CLUSTER_NAME} locally if necessary and save the kubectl config.
5051
ifeq (1, $(shell kind get clusters | grep ${KIND_CLUSTER_NAME} | wc -l | tr -d ' '))
5152
@echo "Cluster already exists"
5253
else
5354
@echo "Creating Cluster"
54-
kind create cluster --name ${KIND_CLUSTER_NAME} --image=kindest/node:${K8S_NODE_IMAGE} --config ${KIND_CONFIG_DIR}/cluster.yaml
55+
kind create cluster --name ${KIND_CLUSTER_NAME} --image=kindest/node:${K8S_NODE_IMAGE} --config ${KIND_CLUSTER_CONFIG_DIR}/cluster.yaml
56+
ifeq ($(IN_DEV_CONTAINER), true)
57+
@echo "kubeconfig backup =>"
58+
mkdir -p ${KUBECONFIG_BACKUP_DIR} && kind get kubeconfig --name ${KIND_CLUSTER_NAME} > ${KUBECONFIG_BACKUP_DIR}/kind-conifg.yaml
59+
endif
5560
endif
5661

5762
##@ Development

0 commit comments

Comments
 (0)