Skip to content

Commit d90d602

Browse files
authored
Merge pull request #5043 from n2h9/5020-chore-uncomment-cert-manager
🐛 (helm/v1-alpha): When scaffolding a Helm project with webhooks, the generated GitHub Actions workflow now installs and waits for cert-manager. Without webhooks, the cert-manager step remains commented.
2 parents 7c93070 + 445321d commit d90d602

File tree

5 files changed

+146
-22
lines changed

5 files changed

+146
-22
lines changed

.github/workflows/test-helm-samples.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,70 @@ jobs:
8888
- name: Check Presence of ServiceMonitor
8989
run: |
9090
kubectl wait --namespace project-v4-with-plugins-system --for=jsonpath='{.kind}'=ServiceMonitor servicemonitor/project-v4-with-plugins-controller-manager-metrics-monitor
91+
92+
# Test scenario:
93+
# - scaffold project without creating webhooks,
94+
# - deploy helm chart without installing cert manager;
95+
# - check that deployment has been deployed;
96+
#
97+
# Command to use to scaffold project without creating webhooks and so no need to install cert manager:
98+
# - kubebuilder init
99+
# - kubebuilder create api --group example.com --version v1 --kind App --controller=true --resource=true
100+
# - kubebuilder edit --plugins=helm.kubebuilder.io/v1-alpha
101+
test-helm-no-webhooks:
102+
runs-on: ubuntu-latest
103+
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
104+
steps:
105+
- name: Checkout repository
106+
uses: actions/checkout@v5
107+
108+
- name: Setup Go
109+
uses: actions/setup-go@v5
110+
with:
111+
go-version-file: go.mod
112+
113+
- name: Install the latest version of kind
114+
run: |
115+
curl -Lo ./kind https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64
116+
chmod +x ./kind
117+
sudo mv ./kind /usr/local/bin/kind
118+
119+
- name: Create kind cluster
120+
run: kind create cluster
121+
122+
- name: Install Helm
123+
run: |
124+
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
125+
126+
- name: Install kubebuilder binary
127+
run: make install
128+
129+
- name: Create test directory
130+
run: mkdir -p test-helm-no-webhooks
131+
132+
- name: Scaffold project with kubebuilder commands
133+
working-directory: test-helm-no-webhooks
134+
run: |
135+
go mod init test-helm-no-webhooks
136+
kubebuilder init
137+
kubebuilder create api --group example.com --version v1 --kind App --controller=true --resource=true
138+
kubebuilder edit --plugins=helm.kubebuilder.io/v1-alpha
139+
140+
- name: Build and load Docker image
141+
working-directory: test-helm-no-webhooks
142+
run: |
143+
make docker-build IMG=test-helm-no-webhooks:v0.1.0
144+
kind load docker-image test-helm-no-webhooks:v0.1.0
145+
146+
- name: Lint Helm chart
147+
working-directory: test-helm-no-webhooks
148+
run: helm lint ./dist/chart
149+
150+
- name: Deploy Helm chart without cert-manager
151+
working-directory: test-helm-no-webhooks
152+
run: helm install my-release ./dist/chart --create-namespace --namespace test-helm-no-webhooks-system
153+
154+
- name: Verify deployment is working
155+
working-directory: test-helm-no-webhooks
156+
run: |
157+
helm status my-release --namespace test-helm-no-webhooks-system

docs/book/src/cronjob-tutorial/testdata/project/.github/workflows/test-chart.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ jobs:
4747
helm lint ./dist/chart
4848
4949
# TODO: Uncomment if cert-manager is enabled
50-
# - name: Install cert-manager via Helm
51-
# run: |
52-
# helm repo add jetstack https://charts.jetstack.io
53-
# helm repo update
54-
# helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set installCRDs=true
55-
#
56-
# - name: Wait for cert-manager to be ready
57-
# run: |
58-
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
59-
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
60-
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook
50+
- name: Install cert-manager via Helm
51+
run: |
52+
helm repo add jetstack https://charts.jetstack.io
53+
helm repo update
54+
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set installCRDs=true
55+
56+
- name: Wait for cert-manager to be ready
57+
run: |
58+
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
59+
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
60+
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook
6161
6262
# TODO: Uncomment if Prometheus is enabled
6363
# - name: Install Prometheus Operator CRDs

docs/book/src/multiversion-tutorial/testdata/project/.github/workflows/test-chart.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
5959
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
6060
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook
61+
6162
# TODO: Uncomment if Prometheus is enabled
6263
# - name: Install Prometheus Operator CRDs
6364
# run: |

pkg/plugins/optional/helm/v1alpha/edit.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ package v1alpha
1818

1919
import (
2020
"fmt"
21+
log "log/slog"
22+
"os"
23+
"path/filepath"
2124

2225
"github.com/spf13/pflag"
2326

2427
"sigs.k8s.io/kubebuilder/v4/pkg/config"
2528
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
2629
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
30+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
2731
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds"
2832
)
2933

@@ -85,3 +89,55 @@ func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
8589
// Track the resources following a declarative approach
8690
return insertPluginMetaToConfig(p.config, pluginConfig{})
8791
}
92+
93+
// PostScaffold automatically uncomments cert-manager installation when webhooks are present
94+
func (p *editSubcommand) PostScaffold() error {
95+
hasWebhooks := hasWebhooksWith(p.config)
96+
97+
if hasWebhooks {
98+
workflowFile := filepath.Join(".github", "workflows", "test-chart.yml")
99+
if _, err := os.Stat(workflowFile); err != nil {
100+
log.Info(
101+
"Workflow file not found, unable to uncomment cert-manager installation",
102+
"error", err,
103+
"file", workflowFile,
104+
)
105+
return nil
106+
}
107+
//nolint:lll
108+
target := `
109+
# - name: Install cert-manager via Helm
110+
# run: |
111+
# helm repo add jetstack https://charts.jetstack.io
112+
# helm repo update
113+
# helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set installCRDs=true
114+
#
115+
# - name: Wait for cert-manager to be ready
116+
# run: |
117+
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
118+
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
119+
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook`
120+
if err := util.UncommentCode(workflowFile, target, "#"); err != nil {
121+
hasUncommented, errCheck := util.HasFileContentWith(workflowFile, "- name: Install cert-manager via Helm")
122+
if !hasUncommented || errCheck != nil {
123+
log.Warn("Failed to uncomment cert-manager installation in workflow file", "error", err, "file", workflowFile)
124+
}
125+
}
126+
}
127+
return nil
128+
}
129+
130+
func hasWebhooksWith(c config.Config) bool {
131+
resources, err := c.GetResources()
132+
if err != nil {
133+
return false
134+
}
135+
136+
for _, res := range resources {
137+
if res.HasDefaultingWebhook() || res.HasValidationWebhook() || res.HasConversionWebhook() {
138+
return true
139+
}
140+
}
141+
142+
return false
143+
}

testdata/project-v4-with-plugins/.github/workflows/test-chart.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ jobs:
4747
helm lint ./dist/chart
4848
4949
# TODO: Uncomment if cert-manager is enabled
50-
# - name: Install cert-manager via Helm
51-
# run: |
52-
# helm repo add jetstack https://charts.jetstack.io
53-
# helm repo update
54-
# helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set installCRDs=true
55-
#
56-
# - name: Wait for cert-manager to be ready
57-
# run: |
58-
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
59-
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
60-
# kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook
50+
- name: Install cert-manager via Helm
51+
run: |
52+
helm repo add jetstack https://charts.jetstack.io
53+
helm repo update
54+
helm install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --set installCRDs=true
55+
56+
- name: Wait for cert-manager to be ready
57+
run: |
58+
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager
59+
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-cainjector
60+
kubectl wait --namespace cert-manager --for=condition=available --timeout=300s deployment/cert-manager-webhook
6161
6262
# TODO: Uncomment if Prometheus is enabled
6363
# - name: Install Prometheus Operator CRDs

0 commit comments

Comments
 (0)