Skip to content

Commit ca3b99c

Browse files
authored
feat: Configure devcontainer (#10)
1 parent 6ff839f commit ca3b99c

File tree

10 files changed

+191
-75
lines changed

10 files changed

+191
-75
lines changed

.devcontainer/Dockerfile

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
FROM golang:1.17.5
2+
3+
# Avoid warnings by switching to noninteractive
4+
ENV DEBIAN_FRONTEND=noninteractive
5+
6+
# This Dockerfile adds a non-root 'vscode' user with sudo access. However, for Linux,
7+
# this user's GID/UID must match your local user UID/GID to avoid permission issues
8+
# with bind mounts. Update USER_UID / USER_GID if yours is not 1000. See
9+
# https://aka.ms/vscode-remote/containers/non-root-user for details.
10+
ARG USERNAME=vscode
11+
ARG USER_UID=1000
12+
ARG USER_GID=$USER_UID
13+
14+
ENV GO111MODULE=auto
15+
16+
ENV GOLANGCI_LINT_V="v1.43.0"
17+
ENV DOCKER_COMPOSE_V="1.29.2"
18+
19+
# Configure apt, install packages and tools
20+
RUN apt-get update \
21+
&& apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
22+
#
23+
# Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
24+
&& apt-get -y install git iproute2 procps lsb-release \
25+
#
26+
# Install gocode-gomod
27+
&& go get -x -d github.com/stamblerre/gocode 2>&1 \
28+
&& go build -o gocode-gomod github.com/stamblerre/gocode \
29+
&& mv gocode-gomod $GOPATH/bin/ \
30+
#
31+
# Install Go tools
32+
&& go install golang.org/x/tools/[email protected] \
33+
&& go install golang.org/x/tools/cmd/[email protected] \
34+
&& go install golang.org/x/tools/cmd/[email protected] \
35+
&& go install golang.org/x/tools/cmd/[email protected] \
36+
&& go install github.com/uudashr/gopkgs/cmd/[email protected] \
37+
&& go install github.com/ramya-rao-a/[email protected] \
38+
&& go install github.com/godoctor/[email protected] \
39+
&& go install github.com/rogpeppe/[email protected] \
40+
&& go install github.com/zmb3/[email protected] \
41+
&& go install github.com/sqs/[email protected] \
42+
&& go install github.com/josharian/[email protected] \
43+
&& go install github.com/davidrjenni/reftools/cmd/[email protected] \
44+
&& go install github.com/fatih/[email protected] \
45+
&& go install github.com/cweill/gotests/[email protected] \
46+
&& go install github.com/golangci/golangci-lint/cmd/[email protected] \
47+
&& go install honnef.co/go/tools/[email protected] \
48+
&& go install github.com/mgechev/[email protected] \
49+
# Install golangci-lint
50+
&& curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_V} \
51+
#
52+
# Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
53+
&& groupadd --gid $USER_GID $USERNAME \
54+
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
55+
# [Optional] Add sudo support
56+
&& apt-get install -y sudo \
57+
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
58+
&& chmod 0440 /etc/sudoers.d/$USERNAME \
59+
# Docker install
60+
&& apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common lsb-release \
61+
&& curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | apt-key add - 2>/dev/null \
62+
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" \
63+
&& apt-get update \
64+
&& apt-get install -y docker-ce-cli \
65+
# Docker-compose install
66+
&& curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_V}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose \
67+
&& chmod +x /usr/local/bin/docker-compose && ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose \
68+
#
69+
# Install pip & pre-commit
70+
&& apt-get -y install python3-pip \
71+
&& python3 -m pip install --no-cache-dir pre-commit \
72+
#
73+
# Clean up
74+
&& apt-get autoremove -y \
75+
&& apt-get clean -y \
76+
&& rm -rf /var/lib/apt/lists/*
77+
78+
# Copy private key for test-git-server
79+
COPY test-git-server/private_keys/helm-repo-updater-test .
80+
81+
# Configure SSH adding helm-repo-updater-test key
82+
RUN mkdir -p ~/.ssh
83+
RUN chmod 600 ./helm-repo-updater-test
84+
85+
# Add git-server access with helm-repo-updater-test key to know_hosts
86+
RUN echo "git-server $(ssh-keygen -f ./helm-repo-updater-test -y | cut -d' ' -f-2)" >> ~/.ssh/known_hosts

.devcontainer/devcontainer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or the definition README at
2+
// https://github.com/microsoft/vscode-dev-containers/tree/master/containers/go
3+
{
4+
"name": "Go",
5+
"context": "..",
6+
"dockerFile": "Dockerfile",
7+
"initializeCommand": "docker network ls | tail -n +2 | awk '{print $2}' | grep vsc-helm-repo-updater-network || docker network create vsc-helm-repo-updater-network",
8+
"containerEnv": {
9+
"isDevContainer": "true"
10+
},
11+
"runArgs": [
12+
// Uncomment the next line to use a non-root user. On Linux, this will prevent
13+
// new files getting created as root, but you may need to update the USER_UID
14+
// and USER_GID in .devcontainer/Dockerfile to match your user if not 1000.
15+
// "-u", "vscode",
16+
17+
// Mount go mod cache
18+
"-v", "helm-repo-updater-gomodcache:/go/pkg",
19+
// Cache vscode exentsions installs and homedir
20+
"-v", "helm-repo-updater-vscodecache:/root/.vscode-server",
21+
22+
// Mount docker socket for docker builds
23+
"-v", "/var/run/docker.sock:/var/run/docker.sock",
24+
25+
"--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined",
26+
27+
// Use same network as created for docker-compose that create git-server container
28+
"--network=vsc-helm-repo-updater-network"
29+
],
30+
31+
// Use 'settings' to set *default* container specific settings.json values on container create.
32+
// You can edit these settings after create using File > Preferences > Settings > Remote.
33+
"settings": {
34+
"go.gopath": "/go",
35+
"terminal.integrated.profiles.linux": {
36+
"bash": {
37+
"path": "/usr/bin/flatpak-spawn",
38+
"args": ["--host", "--env=TERM=xterm-256color", "bash"]
39+
}
40+
}
41+
},
42+
43+
// Add the IDs of extensions you want installed when the container is created in the array below.
44+
"extensions": [
45+
"golang.go",
46+
"ms-azuretools.vscode-docker"
47+
]
48+
}

Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,25 @@ clean:
2424

2525
.PHONY: launch-test-deps
2626
launch-test-deps:
27+
ifndef isDevContainer
2728
ifndef isCI
2829
docker-compose -f test-git-server/docker-compose.yaml up -d
2930
endif
31+
endif
32+
ifdef isDevContainer
33+
docker-compose -f test-git-server/docker-compose-devcontainer.yaml up -d
34+
endif
3035

3136
.PHONY: clean-test-deps
3237
clean-test-deps:
38+
ifndef isDevContainer
39+
ifndef isCI
3340
docker-compose -f test-git-server/docker-compose.yaml down
41+
endif
42+
endif
43+
ifdef isDevContainer
44+
docker-compose -f test-git-server/docker-compose-devcontainer.yaml down
45+
endif
3446
docker volume prune -f && docker system prune -f
3547

3648
.PHONY: test
@@ -65,8 +77,8 @@ publish-build-tools: ## Publish build-tools image
6577
docker push $(IMAGE_BUILD_TOOLS)
6678

6779
.PHONY: publish-git-server-tool
68-
publish-git-server-tool: ## Publish build-tools image
69-
docker build -f test-git-server/git-server/Dockerfile -t $(IMAGE_GIT_REPO_SERVER_TOOL) .
80+
publish-git-server-tool: ## Publish git-server-tool image
81+
docker build -f test-git-server/Dockerfile -t $(IMAGE_GIT_REPO_SERVER_TOOL) .
7082
docker push $(IMAGE_GIT_REPO_SERVER_TOOL)
7183

7284
$(GOBIN_TOOL):

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ require (
6161
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
6262
gopkg.in/ini.v1 v1.66.2 // indirect
6363
gopkg.in/warnings.v0 v0.1.2 // indirect
64-
gopkg.in/yaml.v2 v2.4.0 // indirect
64+
gopkg.in/yaml.v2 v2.4.0
6565
gotest.tools/v3 v3.0.3
6666
)
6767

internal/app/updater/commit_test.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ import (
1212
"gotest.tools/v3/assert"
1313
)
1414

15-
const validGitCredentialsEmail = "[email protected]"
16-
const validGitCredentialsUsername = "test-user"
17-
18-
// TODO: Remove after CI tests docker layer name
19-
const SSHRepoPrefix = "ssh://git@"
20-
const SSHRepoLocalHostname = SSHRepoPrefix + "localhost:2222"
21-
const SSHRepoCIHostname = SSHRepoPrefix + "git-server"
22-
const validGitRepoRoute = "/git-server/repos/test-repo.git"
23-
const invalidGitRepoRoute = "/git-server/repos/test-r"
24-
const validSSHPrivKeyRelativeRoute = "/test-git-server/private_keys/helm-repo-updater-test"
25-
const validGitRepoBranch = "develop"
26-
const invalidGitRepoBranch = "developp"
27-
const validHelmAppName = "example-app"
28-
const validHelmAppFileToChange = validHelmAppName + "/values.yaml"
29-
const ciDiscoveryEnvironment = "isCI"
15+
const (
16+
validGitCredentialsEmail = "[email protected]"
17+
validGitCredentialsUsername = "test-user"
18+
SSHRepoPrefix = "ssh://git@"
19+
SSHRepoLocalHostname = SSHRepoPrefix + "localhost:2222"
20+
SSHRepoCIHostname = SSHRepoPrefix + "git-server"
21+
validGitRepoRoute = "/git-server/repos/test-repo.git"
22+
invalidGitRepoRoute = "/git-server/repos/test-r"
23+
SSHPrivKeyRelativePath = 2
24+
validSSHPrivKeyRelativeRoute = "/test-git-server/private_keys/helm-repo-updater-test"
25+
validGitRepoBranch = "develop"
26+
invalidGitRepoBranch = "developp"
27+
validHelmAppName = "example-app"
28+
validHelmAppFileToChange = validHelmAppName + "/values.yaml"
29+
ciDiscoveryEnvironmentName = "isCI"
30+
isDevContainerEnvironmentName = "isDevContainer"
31+
)
3032

3133
func TestUpdateApplicationDryRunNoChanges(t *testing.T) {
3234

@@ -48,6 +50,8 @@ func TestUpdateApplicationDryRunNoChanges(t *testing.T) {
4850

4951
validGitRepoURL := getSSHRepoHostnameAndPort() + validGitRepoRoute
5052

53+
fmt.Printf("validGitRepoURL: %s\n", validGitRepoURL)
54+
5155
gConf := git.Conf{
5256
RepoURL: validGitRepoURL,
5357
Branch: validGitRepoBranch,
@@ -532,8 +536,9 @@ func TestUpdateApplication(t *testing.T) {
532536
}
533537

534538
func getSSHRepoHostnameAndPort() string {
535-
_, isCI := os.LookupEnv(ciDiscoveryEnvironment)
536-
if !isCI {
539+
_, isCI := os.LookupEnv(ciDiscoveryEnvironmentName)
540+
_, isDevContainerEnvironment := os.LookupEnv(isDevContainerEnvironmentName)
541+
if !isCI && !isDevContainerEnvironment {
537542
return SSHRepoLocalHostname
538543
}
539544
return SSHRepoCIHostname
File renamed without changes.

test-git-server/create-repo/Dockerfile

Lines changed: 0 additions & 9 deletions
This file was deleted.

test-git-server/create-repo/start.sh

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: "3.6"
2+
services:
3+
git-server:
4+
extends:
5+
service: git-server
6+
file: docker-compose.yaml
7+
networks:
8+
- default
9+
networks:
10+
default:
11+
external: true
12+
name: vsc-helm-repo-updater-network

test-git-server/docker-compose.yaml

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
version: "3.6"
22
services:
3-
git-server:
4-
build:
5-
dockerfile: git-server/Dockerfile
6-
context: .
7-
container_name: git-server
8-
restart: always
9-
ports:
10-
- "2222:22"
11-
volumes:
12-
- "./repo_keys:/git-server/keys"
13-
- "repos:/git-server/repos"
14-
volumes:
15-
repos:
3+
git-server:
4+
build:
5+
dockerfile: Dockerfile
6+
context: ..
7+
container_name: git-server
8+
restart: always
9+
ports:
10+
- "2222:22"

0 commit comments

Comments
 (0)