Skip to content

Commit

Permalink
Merge pull request #90 from R-HNF/add-docker-dev-env
Browse files Browse the repository at this point in the history
Add development environment using dev container
  • Loading branch information
R-HNF authored Sep 5, 2023
2 parents 6950b4c + 9342f69 commit 17bfcfd
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 4 deletions.
35 changes: 35 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
{
"name": "Gatling Operator Dev Container",
"build": {
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerfile": "../Dockerfile.dev"
},
"mounts": [
// Mount the host's Docker socket.
// Official document: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/docker.md
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind"
],
"runArgs": [
"--name=gatling-operator-dev-container",
"--hostname=gatling-operator-dev-container",
// Set network mode to host to communicate with other containers.
"--network=host"
],
"containerEnv": {
"IN_DEV_CONTAINER": "true"
},
// Restore the local kubectl config to a dev container.
"postStartCommand": "if [ -d ${containerWorkspaceFolder}/.kube ]; then cp -r ${containerWorkspaceFolder}/.kube $HOME/.kube; fi",
"customizations": {
"vscode": {
"extensions": [
"streetsidesoftware.code-spell-checker",
"mhutchie.git-graph"
]
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ testbin/*
# Kubernetes Generated files - skip generated files, except for vendored files

!vendor/**/zz_generated.*
.kube

# editor and IDE paraphernalia
.idea
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"devcontainer"
]
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ COPY controllers/ controllers/
COPY pkg/ pkg/

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

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
53 changes: 53 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
FROM ubuntu:22.04

ENV GO_VERSION 1.17.13
ENV KUBECTL_VERSION v1.21.10

# KEEP the value as arm64.
# This environment variable is for arm64, but should be left as is for any architecture
# References:
# https://github.com/etcd-io/etcd/issues/10677
# https://github.com/k0sproject/k0s/issues/424
ENV ETCD_UNSUPPORTED_ARCH arm64

# Development tools
RUN apt-get update && apt-get upgrade -y && apt-get install -y \
wget \
git \
make \
gcc

# Docker CLI
# Referring to https://docs.docker.com/engine/install/ubuntu/#installation-methods
RUN apt-get install -y \
ca-certificates \
curl \
gnupg \
&& install -m 0755 -d /etc/apt/keyrings \
&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
| gpg --dearmor -o /etc/apt/keyrings/docker.gpg \
&& chmod a+r /etc/apt/keyrings/docker.gpg \
&& echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "${VERSION_CODENAME}")" stable" \
| tee /etc/apt/sources.list.d/docker.list > /dev/null \
&& apt-get update && apt-get install -y \
docker-ce-cli

# kubectl
# Referring to https://kubernetes.io/ja/docs/tasks/tools/install-kubectl/
RUN curl -LO "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/"$(dpkg --print-architecture)"/kubectl" \
&& chmod +x ./kubectl \
&& mv ./kubectl /usr/local/bin/kubectl

# golang
# Referring to https://go.dev/doc/install
RUN wget "https://go.dev/dl/go${GO_VERSION}.linux-"$(dpkg --print-architecture)".tar.gz" \
&& tar -C /usr/local -xzf "go${GO_VERSION}.linux-"$(dpkg --print-architecture)".tar.gz"
ENV PATH "${PATH}:/usr/local/go/bin"
ENV PATH "${PATH}:/root/go/bin"

# kind
# References:
# https://github.com/kind-ci/examples/blob/master/.github/workflows/kind.yml
# https://kind.sigs.k8s.io/docs/user/resources/
RUN GO111MODULE=on go install sigs.k8s.io/kind@latest
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
KIND_CONFIG_DIR=$(shell pwd)/config/kind
KIND_CLUSTER_CONFIG_DIR=$(shell pwd)/config/kind
KUBECONFIG_BACKUP_DIR=$(shell pwd)/.kube

all: build

Expand All @@ -46,12 +47,16 @@ all: build
help: ## Display this help.
@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)

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

##@ Development
Expand Down

0 comments on commit 17bfcfd

Please sign in to comment.