Skip to content

Commit 6b2ab8a

Browse files
authored
Merge pull request kubernetes-sigs#2044 from k8s-infra-cherrypick-robot/cherry-pick-2043-to-release-0.10
[release-0.10] 🐛 Fix webhook panic when adding managed security groups
2 parents b1771d5 + 34f9c73 commit 6b2ab8a

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

pkg/webhooks/fuzz_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 webhooks
18+
19+
import (
20+
"context"
21+
"runtime/debug"
22+
"testing"
23+
24+
"github.com/onsi/gomega/format"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/client-go/kubernetes/scheme"
27+
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
28+
"sigs.k8s.io/controller-runtime/pkg/webhook"
29+
30+
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
31+
)
32+
33+
type pointerToObject[T any] interface {
34+
*T
35+
runtime.Object
36+
}
37+
38+
// fuzzCustomValidator fuzzes a CustomValidator with objects of the validator's expected type.
39+
func fuzzCustomValidator[O any, PO pointerToObject[O]](t *testing.T, name string, validator webhook.CustomValidator) {
40+
t.Helper()
41+
fuzz := utilconversion.GetFuzzer(scheme.Scheme)
42+
ctx := context.TODO()
43+
44+
t.Run(name, func(t *testing.T) {
45+
for i := 0; i < 1000; i++ {
46+
var previous PO = new(O)
47+
var dst PO = new(O)
48+
fuzz.Fuzz(previous)
49+
fuzz.Fuzz(dst)
50+
51+
checkPanic := func(f func(), name string, args ...runtime.Object) {
52+
defer func() {
53+
if r := recover(); r != nil {
54+
t.Errorf("PANIC in %s", name)
55+
for i, arg := range args {
56+
t.Errorf("arg %d:\n%s", i, format.Object(arg, 1))
57+
}
58+
t.Errorf("Stack trace:\n%s", debug.Stack())
59+
t.FailNow()
60+
}
61+
}()
62+
f()
63+
}
64+
65+
checkPanic(func() {
66+
_, _ = validator.ValidateCreate(ctx, dst)
67+
}, "ValidateCreate()", dst)
68+
checkPanic(func() {
69+
_, _ = validator.ValidateUpdate(ctx, previous, dst)
70+
}, "ValidateUpdate()", previous, dst)
71+
checkPanic(func() {
72+
_, _ = validator.ValidateDelete(ctx, previous)
73+
}, "ValidateDelete()", previous)
74+
}
75+
})
76+
}
77+
78+
func Test_FuzzClusterWebhook(t *testing.T) {
79+
fuzzCustomValidator[infrav1.OpenStackCluster](t, "OpenStackCluster", &openStackClusterWebhook{})
80+
}
81+
82+
func Test_FuzzClusterTemplateWebhook(t *testing.T) {
83+
fuzzCustomValidator[infrav1.OpenStackClusterTemplate](t, "OpenStackClusterTemplate", &openStackClusterTemplateWebhook{})
84+
}
85+
86+
func Test_FuzzMachineWebhook(t *testing.T) {
87+
fuzzCustomValidator[infrav1.OpenStackMachine](t, "OpenStackMachine", &openStackMachineWebhook{})
88+
}
89+
90+
func Test_FuzzMachineTemplateWebhook(t *testing.T) {
91+
fuzzCustomValidator[infrav1.OpenStackMachineTemplate](t, "OpenStackMachineTemplate", &openStackMachineTemplateWebhook{})
92+
}

pkg/webhooks/openstackcluster_webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ func (*openStackClusterWebhook) ValidateUpdate(_ context.Context, oldObjRaw, new
121121

122122
// Allow changes to the managed allNodesSecurityGroupRules.
123123
if newObj.Spec.ManagedSecurityGroups != nil {
124+
if oldObj.Spec.ManagedSecurityGroups == nil {
125+
oldObj.Spec.ManagedSecurityGroups = &infrav1.ManagedSecurityGroups{}
126+
}
127+
124128
oldObj.Spec.ManagedSecurityGroups.AllNodesSecurityGroupRules = []infrav1.SecurityGroupRuleSpec{}
125129
newObj.Spec.ManagedSecurityGroups.AllNodesSecurityGroupRules = []infrav1.SecurityGroupRuleSpec{}
126130

0 commit comments

Comments
 (0)