Skip to content

Commit 79aa3d1

Browse files
committed
feat(devcontainer): ✨ Devcontainer configuration file for
kubebuilder
1 parent c7cde51 commit 79aa3d1

File tree

18 files changed

+486
-1
lines changed

18 files changed

+486
-1
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Test DevContainer Image
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test-devcontainer:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout repository
12+
uses: actions/checkout@v3
13+
14+
- name: Setup Go 1.22.x
15+
uses: actions/setup-go@v5
16+
with:
17+
go-version: "1.22.x"
18+
19+
- name: Setup NodeJS 20.x
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: "20.x"
23+
24+
- name: Setup Devcontainer CLI
25+
run: |
26+
npm install -g @devcontainers/cli
27+
28+
- name: Build and Validate DevContainer
29+
run: |
30+
cd testdata/project-v4
31+
32+
OUTPUT=$(devcontainer up --workspace-folder=./)
33+
STATUS=$(echo "$OUTPUT" | jq -r '.outcome')
34+
35+
if [[ "$STATUS" == "success" ]]; then
36+
echo "Devcontainer setup was successful."
37+
exit 0
38+
else
39+
echo "Devcontainer setup failed."
40+
exit 1
41+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "golang:1.22",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/git:1": {}
7+
},
8+
9+
"runArgs": ["--network=host"],
10+
11+
"customizations": {
12+
"vscode": {
13+
"settings": {
14+
"terminal.integrated.shell.linux": "/bin/bash"
15+
},
16+
"extensions": [
17+
"ms-kubernetes-tools.vscode-kubernetes-tools",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
}
21+
},
22+
23+
"onCreateCommand": "bash .devcontainer/post-install.sh"
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -x
3+
4+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
5+
chmod +x ./kind
6+
mv ./kind /usr/local/bin/kind
7+
8+
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/linux/amd64
9+
chmod +x kubebuilder
10+
mv kubebuilder /usr/local/bin/
11+
12+
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
13+
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
14+
chmod +x kubectl
15+
mv kubectl /usr/local/bin/kubectl
16+
17+
docker network create -d=bridge --subnet=172.19.0.0/24 kind
18+
19+
kind version
20+
kubebuilder version
21+
docker --version
22+
go version
23+
kubectl version --client
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "golang:1.22",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/git:1": {}
7+
},
8+
9+
"runArgs": ["--network=host"],
10+
11+
"customizations": {
12+
"vscode": {
13+
"settings": {
14+
"terminal.integrated.shell.linux": "/bin/bash"
15+
},
16+
"extensions": [
17+
"ms-kubernetes-tools.vscode-kubernetes-tools",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
}
21+
},
22+
23+
"onCreateCommand": "bash .devcontainer/post-install.sh"
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -x
3+
4+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
5+
chmod +x ./kind
6+
mv ./kind /usr/local/bin/kind
7+
8+
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/linux/amd64
9+
chmod +x kubebuilder
10+
mv kubebuilder /usr/local/bin/
11+
12+
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
13+
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
14+
chmod +x kubectl
15+
mv kubectl /usr/local/bin/kubectl
16+
17+
docker network create -d=bridge --subnet=172.19.0.0/24 kind
18+
19+
kind version
20+
kubebuilder version
21+
docker --version
22+
go version
23+
kubectl version --client

pkg/plugins/golang/v4/scaffolds/init.go

+2
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,7 @@ func (s *initScaffolder) Scaffold() error {
162162
&e2e.Test{},
163163
&e2e.SuiteTest{},
164164
&utils.Utils{},
165+
&templates.DevContainer{},
166+
&templates.DevContainerPostInstallScript{},
165167
)
166168
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package templates
18+
19+
import (
20+
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
21+
)
22+
23+
const devContainerTemplate = `{
24+
"name": "Kubebuilder DevContainer",
25+
"image": "golang:1.22",
26+
"features": {
27+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
28+
"ghcr.io/devcontainers/features/git:1": {}
29+
},
30+
31+
"runArgs": ["--network=host"],
32+
33+
"customizations": {
34+
"vscode": {
35+
"settings": {
36+
"terminal.integrated.shell.linux": "/bin/bash"
37+
},
38+
"extensions": [
39+
"ms-kubernetes-tools.vscode-kubernetes-tools",
40+
"ms-azuretools.vscode-docker"
41+
]
42+
}
43+
},
44+
45+
"onCreateCommand": "bash .devcontainer/post-install.sh"
46+
}
47+
48+
`
49+
50+
const postInstallScript = `#!/bin/bash
51+
set -x
52+
53+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
54+
chmod +x ./kind
55+
mv ./kind /usr/local/bin/kind
56+
57+
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/linux/amd64
58+
chmod +x kubebuilder
59+
mv kubebuilder /usr/local/bin/
60+
61+
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
62+
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
63+
chmod +x kubectl
64+
mv kubectl /usr/local/bin/kubectl
65+
66+
docker network create -d=bridge --subnet=172.19.0.0/24 kind
67+
68+
kind version
69+
kubebuilder version
70+
docker --version
71+
go version
72+
kubectl version --client
73+
`
74+
75+
var _ machinery.Template = &DevContainer{}
76+
var _ machinery.Template = &DevContainerPostInstallScript{}
77+
78+
// DevCotaniner scaffoldds a `devcontainer.json` configurations file for
79+
// creating Kubebuilder & Kind based DevContainer.
80+
type DevContainer struct {
81+
machinery.TemplateMixin
82+
}
83+
84+
type DevContainerPostInstallScript struct {
85+
machinery.TemplateMixin
86+
}
87+
88+
func (f *DevContainer) SetTemplateDefaults() error {
89+
if f.Path == "" {
90+
f.Path = ".devcontainer/devcontainer.json"
91+
}
92+
93+
f.TemplateBody = devContainerTemplate
94+
95+
return nil
96+
}
97+
98+
func (f *DevContainerPostInstallScript) SetTemplateDefaults() error {
99+
if f.Path == "" {
100+
f.Path = ".devcontainer/post-install.sh"
101+
}
102+
103+
f.TemplateBody = postInstallScript
104+
105+
return nil
106+
}

test/check-license.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ set -o pipefail
2121
source $(dirname "$0")/common.sh
2222

2323
echo "Checking for license header..."
24-
allfiles=$(listFiles|grep -v ./internal/bindata/...)
24+
allfiles=$(listFiles | grep -v -e './internal/bindata/...' -e '.devcontainer/post-install.sh')
2525
licRes=""
2626
for file in $allfiles; do
2727
if ! head -n4 "${file}" | grep -Eq "(Copyright|generated|GENERATED|Licensed)" ; then
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "golang:1.22",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/git:1": {}
7+
},
8+
9+
"runArgs": ["--network=host"],
10+
11+
"customizations": {
12+
"vscode": {
13+
"settings": {
14+
"terminal.integrated.shell.linux": "/bin/bash"
15+
},
16+
"extensions": [
17+
"ms-kubernetes-tools.vscode-kubernetes-tools",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
}
21+
},
22+
23+
"onCreateCommand": "bash .devcontainer/post-install.sh"
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -x
3+
4+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
5+
chmod +x ./kind
6+
mv ./kind /usr/local/bin/kind
7+
8+
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/linux/amd64
9+
chmod +x kubebuilder
10+
mv kubebuilder /usr/local/bin/
11+
12+
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
13+
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
14+
chmod +x kubectl
15+
mv kubectl /usr/local/bin/kubectl
16+
17+
docker network create -d=bridge --subnet=172.19.0.0/24 kind
18+
19+
kind version
20+
kubebuilder version
21+
docker --version
22+
go version
23+
kubectl version --client
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "golang:1.22",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/git:1": {}
7+
},
8+
9+
"runArgs": ["--network=host"],
10+
11+
"customizations": {
12+
"vscode": {
13+
"settings": {
14+
"terminal.integrated.shell.linux": "/bin/bash"
15+
},
16+
"extensions": [
17+
"ms-kubernetes-tools.vscode-kubernetes-tools",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
}
21+
},
22+
23+
"onCreateCommand": "bash .devcontainer/post-install.sh"
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -x
3+
4+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
5+
chmod +x ./kind
6+
mv ./kind /usr/local/bin/kind
7+
8+
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/linux/amd64
9+
chmod +x kubebuilder
10+
mv kubebuilder /usr/local/bin/
11+
12+
KUBECTL_VERSION=$(curl -L -s https://dl.k8s.io/release/stable.txt)
13+
curl -LO "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl"
14+
chmod +x kubectl
15+
mv kubectl /usr/local/bin/kubectl
16+
17+
docker network create -d=bridge --subnet=172.19.0.0/24 kind
18+
19+
kind version
20+
kubebuilder version
21+
docker --version
22+
go version
23+
kubectl version --client
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Kubebuilder DevContainer",
3+
"image": "golang:1.22",
4+
"features": {
5+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
6+
"ghcr.io/devcontainers/features/git:1": {}
7+
},
8+
9+
"runArgs": ["--network=host"],
10+
11+
"customizations": {
12+
"vscode": {
13+
"settings": {
14+
"terminal.integrated.shell.linux": "/bin/bash"
15+
},
16+
"extensions": [
17+
"ms-kubernetes-tools.vscode-kubernetes-tools",
18+
"ms-azuretools.vscode-docker"
19+
]
20+
}
21+
},
22+
23+
"onCreateCommand": "bash .devcontainer/post-install.sh"
24+
}
25+

0 commit comments

Comments
 (0)