Skip to content

Commit dcf89b3

Browse files
committed
updated scaffolding
1 parent eebe65f commit dcf89b3

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

pkg/plugins/golang/v4/scaffolds/webhook.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package scaffolds
1919
import (
2020
"errors"
2121
"fmt"
22+
"path/filepath"
2223
"strings"
2324

2425
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/api"
@@ -35,6 +36,7 @@ import (
3536
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/hack"
3637
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/test/e2e"
3738
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/v4/scaffolds/internal/templates/webhooks"
39+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
3840
)
3941

4042
var _ plugins.Scaffolder = &webhookScaffolder{}
@@ -173,5 +175,33 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f
173175
}
174176
}
175177
}
178+
179+
// After scaffolding, uncomment relevant sections in kustomization.yaml
180+
kustomizationPath := filepath.Join("config", "default", "kustomization.yaml")
181+
182+
var blocksToUncomment []string
183+
184+
// Add patterns based on webhook type
185+
if s.isDefaultingWebhook() {
186+
blocksToUncomment = append(blocksToUncomment,
187+
"- ../webhook",
188+
"- ../certmanager",
189+
"- ../manager")
190+
}
191+
192+
if s.isConversionWebhook() {
193+
blocksToUncomment = append(blocksToUncomment,
194+
"- ../webhook",
195+
"- ../certmanager",
196+
"- ../manager",
197+
"webhookConversion:")
198+
}
199+
200+
if err := util.UncommentKustomizationBlocks(kustomizationPath, blocksToUncomment); err != nil {
201+
// Log warning instead of returning error to not block the workflow
202+
log.Printf("Warning: Could not automatically uncomment kustomization blocks: %v", err)
203+
log.Println("You may need to manually uncomment the relevant sections in config/default/kustomization.yaml")
204+
}
205+
176206
return nil
177207
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package scaffolds
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestWebhookScaffolderUncommentsKustomization(t *testing.T) {
8+
// Test setup code...
9+
10+
tests := []struct {
11+
name string
12+
webhookType string
13+
expectedBlocks []string
14+
}{
15+
{
16+
name: "defaulting webhook",
17+
webhookType: "defaulting",
18+
expectedBlocks: []string{
19+
"- ../webhook",
20+
"- ../certmanager",
21+
"- ../manager",
22+
},
23+
},
24+
{
25+
name: "conversion webhook",
26+
webhookType: "conversion",
27+
expectedBlocks: []string{
28+
"- ../webhook",
29+
"- ../certmanager",
30+
"- ../manager",
31+
"webhookConversion:",
32+
},
33+
},
34+
}
35+
36+
for _, tt := range tests {
37+
t.Run(tt.name, func(t *testing.T) {
38+
// Test implementation...
39+
})
40+
}
41+
}

plugin/util/util.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// UncommentKustomizationBlocks uncomments specified blocks in kustomization.yaml
2+
func UncommentKustomizationBlocks(path string, patterns []string) error {
3+
content, err := os.ReadFile(path)
4+
if err != nil {
5+
return fmt.Errorf("failed to read kustomization file: %w", err)
6+
}
7+
8+
lines := strings.Split(string(content), "\n")
9+
modified := false
10+
11+
for i, line := range lines {
12+
for _, pattern := range patterns {
13+
if strings.Contains(line, pattern) && strings.HasPrefix(strings.TrimSpace(line), "#") {
14+
lines[i] = strings.TrimPrefix(strings.TrimSpace(line), "#")
15+
modified = true
16+
}
17+
}
18+
}
19+
20+
if modified {
21+
err = os.WriteFile(path, []byte(strings.Join(lines, "\n")), 0644)
22+
if err != nil {
23+
return fmt.Errorf("failed to write kustomization file: %w", err)
24+
}
25+
}
26+
27+
return nil
28+
}

0 commit comments

Comments
 (0)