Skip to content

Commit 0164fb6

Browse files
⚠️ (cleanup/fix): ImplementWebhooks method should only be used by the e2e tests and should be under its package (#4065)
(cleanup/fix): ImplementWebhooks method should only be used by the e2e tests and should be under its package
1 parent b5235ef commit 0164fb6

File tree

3 files changed

+80
-57
lines changed

3 files changed

+80
-57
lines changed

pkg/plugin/util/util.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -218,60 +218,6 @@ func CommentCode(filename, target, prefix string) error {
218218
return os.WriteFile(filename, out.Bytes(), 0644)
219219
}
220220

221-
// ImplementWebhooks will mock an webhook data
222-
func ImplementWebhooks(filename string) error {
223-
// false positive
224-
// nolint:gosec
225-
bs, err := os.ReadFile(filename)
226-
if err != nil {
227-
return err
228-
}
229-
str := string(bs)
230-
231-
str, err = EnsureExistAndReplace(
232-
str,
233-
"import (",
234-
`import (
235-
"errors"`)
236-
if err != nil {
237-
return err
238-
}
239-
240-
// implement defaulting webhook logic
241-
str, err = EnsureExistAndReplace(
242-
str,
243-
"// TODO(user): fill in your defaulting logic.",
244-
`if r.Spec.Count == 0 {
245-
r.Spec.Count = 5
246-
}`)
247-
if err != nil {
248-
return err
249-
}
250-
251-
// implement validation webhook logic
252-
str, err = EnsureExistAndReplace(
253-
str,
254-
"// TODO(user): fill in your validation logic upon object creation.",
255-
`if r.Spec.Count < 0 {
256-
return nil, errors.New(".spec.count must >= 0")
257-
}`)
258-
if err != nil {
259-
return err
260-
}
261-
str, err = EnsureExistAndReplace(
262-
str,
263-
"// TODO(user): fill in your validation logic upon object update.",
264-
`if r.Spec.Count < 0 {
265-
return nil, errors.New(".spec.count must >= 0")
266-
}`)
267-
if err != nil {
268-
return err
269-
}
270-
// false positive
271-
// nolint:gosec
272-
return os.WriteFile(filename, []byte(str), 0644)
273-
}
274-
275221
// EnsureExistAndReplace check if the content exists and then do the replace
276222
func EnsureExistAndReplace(input, match, replace string) (string, error) {
277223
if !strings.Contains(input, match) {

test/e2e/utils/webhooks.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 utils
18+
19+
import (
20+
"os"
21+
22+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
23+
)
24+
25+
// ImplementWebhooks will mock an webhook data
26+
func ImplementWebhooks(filename string) error {
27+
// false positive
28+
// nolint:gosec
29+
bs, err := os.ReadFile(filename)
30+
if err != nil {
31+
return err
32+
}
33+
str := string(bs)
34+
35+
str, err = util.EnsureExistAndReplace(
36+
str,
37+
"import (",
38+
`import (
39+
"errors"`)
40+
if err != nil {
41+
return err
42+
}
43+
44+
// implement defaulting webhook logic
45+
str, err = util.EnsureExistAndReplace(
46+
str,
47+
"// TODO(user): fill in your defaulting logic.",
48+
`if r.Spec.Count == 0 {
49+
r.Spec.Count = 5
50+
}`)
51+
if err != nil {
52+
return err
53+
}
54+
55+
// implement validation webhook logic
56+
str, err = util.EnsureExistAndReplace(
57+
str,
58+
"// TODO(user): fill in your validation logic upon object creation.",
59+
`if r.Spec.Count < 0 {
60+
return nil, errors.New(".spec.count must >= 0")
61+
}`)
62+
if err != nil {
63+
return err
64+
}
65+
str, err = util.EnsureExistAndReplace(
66+
str,
67+
"// TODO(user): fill in your validation logic upon object update.",
68+
`if r.Spec.Count < 0 {
69+
return nil, errors.New(".spec.count must >= 0")
70+
}`)
71+
if err != nil {
72+
return err
73+
}
74+
// false positive
75+
// nolint:gosec
76+
return os.WriteFile(filename, []byte(str), 0644)
77+
}

test/e2e/v4/generate_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GenerateV4(kbc *utils.TestContext) {
4949
ExpectWithOffset(1, err).NotTo(HaveOccurred())
5050

5151
By("implementing the mutating and validating webhooks")
52-
err = pluginutil.ImplementWebhooks(filepath.Join(
52+
err = utils.ImplementWebhooks(filepath.Join(
5353
kbc.Dir, "api", kbc.Version,
5454
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
5555
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -89,7 +89,7 @@ func GenerateV4WithoutMetrics(kbc *utils.TestContext) {
8989
ExpectWithOffset(1, err).NotTo(HaveOccurred())
9090

9191
By("implementing the mutating and validating webhooks")
92-
err = pluginutil.ImplementWebhooks(filepath.Join(
92+
err = utils.ImplementWebhooks(filepath.Join(
9393
kbc.Dir, "api", kbc.Version,
9494
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
9595
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -152,7 +152,7 @@ func GenerateV4WithNetworkPolicies(kbc *utils.TestContext) {
152152
ExpectWithOffset(1, err).NotTo(HaveOccurred())
153153

154154
By("implementing the mutating and validating webhooks")
155-
err = pluginutil.ImplementWebhooks(filepath.Join(
155+
err = utils.ImplementWebhooks(filepath.Join(
156156
kbc.Dir, "api", kbc.Version,
157157
fmt.Sprintf("%s_webhook.go", strings.ToLower(kbc.Kind))))
158158
ExpectWithOffset(1, err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)