Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for devcontainers #288

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
}
}
}
}
18 changes: 18 additions & 0 deletions .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

mkdir -p $HOME/.kube
Kidswiss marked this conversation as resolved.
Show resolved Hide resolved
kubectl completion bash > /home/vscode/.kube/completion.bash.inc
printf "
source /usr/share/bash-completion/bash_completion
source $HOME/.kube/completion.bash.inc
complete -F __start_kubectl k
" >> $HOME/.bashrc

printf "
source <(kubectl completion zsh)
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 https://github.com/vshn/kindev .kind && \
cd .kind && \
rm -rf .git && \
make clean && \
make vshnall
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,49 @@ 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.

Kindev will be installed in the `.kind` folder.
Vscode handles all port-forwarding automagically, so kindev and all endpoints will be available all the same from the host.
Simply point `KUBECONFIG` to `.kind/.kind/kind-config` and use `kubectl` as usual.

### 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
Loading