Skip to content

Commit 258b9c6

Browse files
authored
Merge pull request #4781 from dongjiang1989/fix-aliasing-import
🐛Fix: Incorrect Webhook Import Aliasing in cmd/main.go Scaffold
2 parents cd48b2b + add42eb commit 258b9c6

File tree

12 files changed

+26
-44
lines changed

12 files changed

+26
-44
lines changed

docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ linters:
3636
- dupl
3737
- lll
3838
path: internal/*
39-
- linters:
40-
- staticcheck
41-
text: "ST1019"
4239
paths:
4340
- third_party$
4441
- builtin$

docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040

4141
batchv1 "tutorial.kubebuilder.io/project/api/v1"
4242
"tutorial.kubebuilder.io/project/internal/controller"
43-
webhookbatchv1 "tutorial.kubebuilder.io/project/internal/webhook/v1"
43+
webhookv1 "tutorial.kubebuilder.io/project/internal/webhook/v1"
4444
// +kubebuilder:scaffold:imports
4545
)
4646

@@ -242,7 +242,7 @@ func main() {
242242
*/
243243
// nolint:goconst
244244
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
245-
if err := webhookbatchv1.SetupCronJobWebhookWithManager(mgr); err != nil {
245+
if err := webhookv1.SetupCronJobWebhookWithManager(mgr); err != nil {
246246
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
247247
os.Exit(1)
248248
}

docs/book/src/getting-started/testdata/project/.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ linters:
3636
- dupl
3737
- lll
3838
path: internal/*
39-
- linters:
40-
- staticcheck
41-
text: "ST1019"
4239
paths:
4340
- third_party$
4441
- builtin$

docs/book/src/multiversion-tutorial/testdata/project/.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ linters:
3636
- dupl
3737
- lll
3838
path: internal/*
39-
- linters:
40-
- staticcheck
41-
text: "ST1019"
4239
paths:
4340
- third_party$
4441
- builtin$

docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ import (
4242
batchv1 "tutorial.kubebuilder.io/project/api/v1"
4343
batchv2 "tutorial.kubebuilder.io/project/api/v2"
4444
"tutorial.kubebuilder.io/project/internal/controller"
45-
webhookbatchv1 "tutorial.kubebuilder.io/project/internal/webhook/v1"
46-
webhookbatchv2 "tutorial.kubebuilder.io/project/internal/webhook/v2"
45+
webhookv1 "tutorial.kubebuilder.io/project/internal/webhook/v1"
46+
webhookv2 "tutorial.kubebuilder.io/project/internal/webhook/v2"
4747
// +kubebuilder:scaffold:imports
4848
)
4949

@@ -235,14 +235,14 @@ func main() {
235235
*/
236236
// nolint:goconst
237237
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
238-
if err := webhookbatchv1.SetupCronJobWebhookWithManager(mgr); err != nil {
238+
if err := webhookv1.SetupCronJobWebhookWithManager(mgr); err != nil {
239239
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
240240
os.Exit(1)
241241
}
242242
}
243243
// nolint:goconst
244244
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
245-
if err := webhookbatchv2.SetupCronJobWebhookWithManager(mgr); err != nil {
245+
if err := webhookv2.SetupCronJobWebhookWithManager(mgr); err != nil {
246246
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
247247
os.Exit(1)
248248
}

pkg/plugins/golang/v4/scaffolds/internal/templates/cmd/main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,11 @@ func (f *MainUpdater) GetCodeFragments() machinery.CodeFragmentsMap {
158158
imports = append(imports, fmt.Sprintf(apiImportCodeFragment, f.Resource.ImportAlias(), f.Resource.Path))
159159
}
160160
if f.WireWebhook && !f.IsLegacyPath {
161-
importPath := fmt.Sprintf("webhook%s", f.Resource.ImportAlias())
162161
if !f.MultiGroup || f.Resource.Group == "" {
162+
importPath := fmt.Sprintf("webhook%s", f.Resource.Version)
163163
imports = append(imports, fmt.Sprintf(webhookImportCodeFragment, importPath, f.Repo, f.Resource.Version))
164164
} else {
165+
importPath := fmt.Sprintf("webhook%s", f.Resource.ImportAlias())
165166
imports = append(imports, fmt.Sprintf(multiGroupWebhookImportCodeFragment, importPath,
166167
f.Repo, f.Resource.Group, f.Resource.Version))
167168
}
@@ -198,8 +199,13 @@ func (f *MainUpdater) GetCodeFragments() machinery.CodeFragmentsMap {
198199
setup = append(setup, fmt.Sprintf(webhookSetupCodeFragmentLegacy,
199200
f.Resource.ImportAlias(), f.Resource.Kind, f.Resource.Kind))
200201
} else {
201-
setup = append(setup, fmt.Sprintf(webhookSetupCodeFragment,
202-
"webhook"+f.Resource.ImportAlias(), f.Resource.Kind, f.Resource.Kind))
202+
if !f.MultiGroup || f.Resource.Group == "" {
203+
setup = append(setup, fmt.Sprintf(webhookSetupCodeFragment,
204+
"webhook"+f.Resource.Version, f.Resource.Kind, f.Resource.Kind))
205+
} else {
206+
setup = append(setup, fmt.Sprintf(webhookSetupCodeFragment,
207+
"webhook"+f.Resource.ImportAlias(), f.Resource.Kind, f.Resource.Kind))
208+
}
203209
}
204210
}
205211

pkg/plugins/golang/v4/scaffolds/internal/templates/golangci.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ linters:
7979
- dupl
8080
- lll
8181
path: internal/*
82-
- linters:
83-
- staticcheck
84-
text: "ST1019"
8582
paths:
8683
- third_party$
8784
- builtin$

testdata/project-v4-multigroup/.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ linters:
3636
- dupl
3737
- lll
3838
path: internal/*
39-
- linters:
40-
- staticcheck
41-
text: "ST1019"
4239
paths:
4340
- third_party$
4441
- builtin$

testdata/project-v4-with-plugins/.golangci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ linters:
3636
- dupl
3737
- lll
3838
path: internal/*
39-
- linters:
40-
- staticcheck
41-
text: "ST1019"
4239
paths:
4340
- third_party$
4441
- builtin$

testdata/project-v4-with-plugins/cmd/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import (
4141
examplecomv1alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v4-with-plugins/api/v1alpha1"
4242
examplecomv2 "sigs.k8s.io/kubebuilder/testdata/project-v4-with-plugins/api/v2"
4343
"sigs.k8s.io/kubebuilder/testdata/project-v4-with-plugins/internal/controller"
44-
webhookexamplecomv1 "sigs.k8s.io/kubebuilder/testdata/project-v4-with-plugins/internal/webhook/v1"
45-
webhookexamplecomv1alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v4-with-plugins/internal/webhook/v1alpha1"
44+
webhookv1 "sigs.k8s.io/kubebuilder/testdata/project-v4-with-plugins/internal/webhook/v1"
45+
webhookv1alpha1 "sigs.k8s.io/kubebuilder/testdata/project-v4-with-plugins/internal/webhook/v1alpha1"
4646
// +kubebuilder:scaffold:imports
4747
)
4848

@@ -226,7 +226,7 @@ func main() {
226226
}
227227
// nolint:goconst
228228
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
229-
if err := webhookexamplecomv1alpha1.SetupMemcachedWebhookWithManager(mgr); err != nil {
229+
if err := webhookv1alpha1.SetupMemcachedWebhookWithManager(mgr); err != nil {
230230
setupLog.Error(err, "unable to create webhook", "webhook", "Memcached")
231231
os.Exit(1)
232232
}
@@ -240,7 +240,7 @@ func main() {
240240
}
241241
// nolint:goconst
242242
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
243-
if err := webhookexamplecomv1.SetupWordpressWebhookWithManager(mgr); err != nil {
243+
if err := webhookv1.SetupWordpressWebhookWithManager(mgr); err != nil {
244244
setupLog.Error(err, "unable to create webhook", "webhook", "Wordpress")
245245
os.Exit(1)
246246
}

0 commit comments

Comments
 (0)