|
| 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 github |
| 18 | + |
| 19 | +import ( |
| 20 | + "path/filepath" |
| 21 | + |
| 22 | + "sigs.k8s.io/kubebuilder/v4/pkg/machinery" |
| 23 | +) |
| 24 | + |
| 25 | +var _ machinery.Template = &HelmChartCI{} |
| 26 | + |
| 27 | +// HelmChartCI scaffolds the GitHub Action for testing Helm charts |
| 28 | +type HelmChartCI struct { |
| 29 | + machinery.TemplateMixin |
| 30 | + machinery.ProjectNameMixin |
| 31 | +} |
| 32 | + |
| 33 | +// SetTemplateDefaults implements machinery.Template |
| 34 | +func (f *HelmChartCI) SetTemplateDefaults() error { |
| 35 | + if f.Path == "" { |
| 36 | + f.Path = filepath.Join(".github", "workflows", "test-chart.yml") |
| 37 | + } |
| 38 | + |
| 39 | + f.TemplateBody = testChartTemplate |
| 40 | + |
| 41 | + f.IfExistsAction = machinery.SkipFile |
| 42 | + |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// nolint:lll |
| 47 | +const testChartTemplate = `name: Test Chart |
| 48 | +
|
| 49 | +on: |
| 50 | + push: |
| 51 | + pull_request: |
| 52 | +
|
| 53 | +jobs: |
| 54 | + test-e2e: |
| 55 | + name: Run on Ubuntu |
| 56 | + runs-on: ubuntu-latest |
| 57 | + steps: |
| 58 | + - name: Clone the code |
| 59 | + uses: actions/checkout@v4 |
| 60 | +
|
| 61 | + - name: Setup Go |
| 62 | + uses: actions/setup-go@v5 |
| 63 | + with: |
| 64 | + go-version: '~1.22' |
| 65 | +
|
| 66 | + - name: Install the latest version of kind |
| 67 | + run: | |
| 68 | + curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64 |
| 69 | + chmod +x ./kind |
| 70 | + sudo mv ./kind /usr/local/bin/kind |
| 71 | +
|
| 72 | + - name: Verify kind installation |
| 73 | + run: kind version |
| 74 | +
|
| 75 | + - name: Create kind cluster |
| 76 | + run: kind create cluster |
| 77 | +
|
| 78 | + - name: Prepare {{ .ProjectName }} |
| 79 | + run: | |
| 80 | + go mod tidy |
| 81 | + make docker-build IMG={{ .ProjectName }}:v0.1.0 |
| 82 | + kind load docker-image {{ .ProjectName }}:v0.1.0 |
| 83 | +
|
| 84 | + - name: Install Helm |
| 85 | + run: | |
| 86 | + curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash |
| 87 | +
|
| 88 | + - name: Verify Helm installation |
| 89 | + run: helm version |
| 90 | +
|
| 91 | + - name: Lint Helm Chart |
| 92 | + run: | |
| 93 | + helm lint ./dist/chart |
| 94 | +
|
| 95 | +# TODO: Uncomment if cert-manager is enabled |
| 96 | +# - name: Install cert-manager via Helm |
| 97 | +# run: | |
| 98 | +# helm repo add jetstack https://charts.jetstack.io |
| 99 | +# helm repo update |
| 100 | +# helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set installCRDs=true |
| 101 | +# |
| 102 | +# - name: Wait for cert-manager to be ready |
| 103 | +# run: | |
| 104 | +# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager |
| 105 | +# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector |
| 106 | +# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook |
| 107 | +
|
| 108 | +# TODO: Uncomment if Prometheus is enabled |
| 109 | +# - name: Install Prometheus via Helm |
| 110 | +# run: | |
| 111 | +# helm repo add prometheus-community https://prometheus-community.github.io/helm-charts |
| 112 | +# helm repo update |
| 113 | +# helm install prometheus prometheus-community/prometheus --namespace monitoring --create-namespace |
| 114 | +# |
| 115 | +# - name: Wait for Prometheus to be ready |
| 116 | +# run: | |
| 117 | +# kubectl wait --namespace monitoring --for=condition=available --timeout=300s deployment/prometheus-server |
| 118 | +
|
| 119 | + - name: Install Helm chart for project |
| 120 | + run: | |
| 121 | + helm install my-release ./dist/chart --create-namespace --namespace {{ .ProjectName }}-system |
| 122 | +
|
| 123 | + - name: Check Helm release status |
| 124 | + run: | |
| 125 | + helm status my-release --namespace {{ .ProjectName }}-system |
| 126 | +` |
0 commit comments