|
| 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 | +} |
0 commit comments