Skip to content

Commit

Permalink
Add support for devcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Dec 31, 2024
1 parent 93a71c4 commit c3925d1
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM mcr.microsoft.com/devcontainers/go:dev-1.23-bookworm

ARG TARGETOS
ARG TARGETARCH

ENV HELMVERS="v3.16.4"
ENV YQVERSION="v4.44.6"
ENV KUBECOLORVERSION="0.4.0"

# Install kubectl
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/${TARGETARCH}/kubectl && chmod +x ./kubectl && mv ./kubectl /usr/local/bin

# Install helm
RUN bash -c "curl -s https://get.helm.sh/helm-${HELMVERS}-linux-${TARGETARCH}.tar.gz > helm3.tar.gz" && tar -zxvf helm3.tar.gz linux-${TARGETARCH}/helm && chmod +x linux-${TARGETARCH}/helm && mv linux-${TARGETARCH}/helm /usr/local/bin && rm helm3.tar.gz && rm -R linux-${TARGETARCH}

# Install yq
RUN curl -L -o yq "https://github.com/mikefarah/yq/releases/download/${YQVERSION}/yq_linux_${TARGETARCH}" && install -c -m 0755 yq /usr/local/bin

# Install kubecolor
RUN curl -L -o kubecolor.tar.gz https://github.com/kubecolor/kubecolor/releases/download/v${KUBECOLORVERSION}/kubecolor_${KUBECOLORVERSION}_linux_${TARGETARCH}.tar.gz && tar -xvzf kubecolor.tar.gz && install -c -m 0755 kubecolor /usr/local/bin

# Install bash-complete to make kubectl autocomplete work
RUN apt-get update && apt-get install bash-completion make vim -y && rm -rf /var/lib/apt/lists/*
54 changes: 54 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "KubeBuilder Example",
"build": {
"dockerfile": "./Dockerfile"
},
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"version": "latest",
"moby": true
},
"ghcr.io/mikaello/devcontainer-features/modern-shell-utils:1": {}
},
"postCreateCommand": "./.devcontainer/postCreateCommand.sh",
"customizations": {
// "devpod": {
// "prebuildRepository": "ghcr.io/loft-sh/devpod-kubebuilder-template"
// },
"vscode": {
"extensions": [
"golang.go",
"ms-vscode.makefile-tools",
"redhat.vscode-yaml",
"timonwong.shellcheck",
"eamodio.gitlens",
"mhutchie.git-graph"
],
"settings": {
// This has to be set individually to each developers preference
// in their local vscode configs.
// "terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.profiles.linux": {
"bash": {
"path": "bash",
"icon": "terminal-bash"
},
"zsh": {
"path": "zsh"
},
"fish": {
"path": "fish"
},
"tmux": {
"path": "tmux",
"icon": "terminal-tmux"
},
"pwsh": {
"path": "pwsh",
"icon": "terminal-powershell"
}
}
}
}
}
}
19 changes: 19 additions & 0 deletions .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mkdir -p $HOME/.kube
kubectl completion bash > /home/vscode/.kube/completion.bash.inc
printf "
source /usr/share/bash-completion/bash_completion
source $HOME/.kube/completion.bash.inc
alias k=kubectl
complete -F __start_kubectl k
" >> $HOME/.bashrc

printf "
source <(kubectl completion zsh)
alias k=kubectl
alias ll='ls -al'
complete -F __start_kubectl k
" >> $HOME/.zshrc

make setup-kindev

cp .kind/.kind/kind-config ~/.kube/config
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ __debug_bin*
hack/res
hack/tmp
hack/diff/function.yaml

# MacOS stuff
.DS_store
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,13 @@ render-diff: ## Render diff between the cluster in KUBECONF and the local branch
# this will speed up the compare in CI/CD environments.
if ! docker pull $(IMG); then $(MAKE) docker-build-branchtag; fi
hack/diff/compare.sh $(DEBUG)

.PHONY: setup-kindev

setup-kindev: ## Setup kindev in the .kind folder, will always create a new instance
rm -rf .kind && \
git clone --depth=1 -b add/devcontainers https://github.com/vshn/kindev .kind && \
cd .kind && \
rm -rf .git && \
make clean && \
make vshnall
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ Documentation: https://vshn.github.io/appcat
└── tools.go
```

## Getting started with devcontainers

This project contains a `.devcontainer` folder, which enables devcontainer support in vscode and other IDEs.
Make sure you've installed the dev containers extension for vscode.

Then open the command palette and do `Dev Containers: Reopen in container`.
The first time doing this will take quite some as it builds the whole container.
After building the container it will spin up kindev, which will also take some time.

Once it's finished there should be a ready to go dev environment to use.

The container will contain:
* go
* helm
* kubectl
* yq
* kubecolor

Additionally it will install some useful extensions for vscode to make development easier.

### Devcontainer customizations

It's possible to customize the devcontainer.
By setting `"terminal.integrated.defaultProfile.linux": "zsh"` in the vscode config, it's possible to switch the default shell to zsh.

It's also possible to provide your own dotfiles. They will be installed after the kindev setup has finished.
For that, simply write a small script that contains all your desired configurations and put it in a publicly available repository.
Here's an example repo: https://github.com/lugoues/vscode-dev-containers-dotfiles

After that set this configuration in vscode:
```
{
"dotfiles.repository": "your-github-id/your-dotfiles-repo",
"dotfiles.targetPath": "~/dotfiles",
"dotfiles.installCommand": "install.sh"
}
```


## Generate Kubernetes code, XRDs with Go / KubeBuilder

In `/apis` there is code in Go to generate the XRDs (composites) as this is in OpenAPI.
Expand Down

0 comments on commit c3925d1

Please sign in to comment.