Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 2936045

Browse files
committed
Fix security group rules comparison
1 parent 96bd35f commit 2936045

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

api/v1beta1/scalewaycluster_webhook.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net"
55
"reflect"
66

7+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
78
"github.com/scaleway/scaleway-sdk-go/scw"
89
apierrors "k8s.io/apimachinery/pkg/api/errors"
910
"k8s.io/apimachinery/pkg/runtime"
@@ -238,14 +239,20 @@ func (r *ScalewayCluster) validateSecurityGroupPolicy(sgp *SecurityGroupPolicy,
238239
return field.Invalid(innerPath.Child("action"), rule.Action, err.Error())
239240
}
240241

241-
if _, err := rule.Protocol.ToInstance(); err != nil {
242+
proto, err := rule.Protocol.ToInstance()
243+
if err != nil {
242244
return field.Invalid(innerPath.Child("protocol"), rule.Protocol, err.Error())
243245
}
244246

245247
if _, _, err := rule.Ports.ToRange(); err != nil {
246248
return field.Invalid(innerPath.Child("ports"), rule.Ports, err.Error())
247249
}
248250

251+
// When using ANY or ICMP, ports must be nil.
252+
if (proto == instance.SecurityGroupRuleProtocolANY || proto == instance.SecurityGroupRuleProtocolICMP) && rule.Ports != nil {
253+
return field.Invalid(innerPath.Child("ports"), rule.Ports, "ports must not be set when using ANY or ICMP protocols")
254+
}
255+
249256
if rule.IPRange != nil {
250257
if _, _, err := net.ParseCIDR(*rule.IPRange); err != nil {
251258
return field.Invalid(innerPath.Child("ipRange"), rule.IPRange, err.Error())

internal/service/scaleway/securitygroup/securitygroup.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net"
7+
"reflect"
78

89
"github.com/Tomy2e/cluster-api-provider-scaleway/api/v1beta1"
910
"github.com/Tomy2e/cluster-api-provider-scaleway/internal/scope"
@@ -72,11 +73,11 @@ func compareRules(a []v1beta1.SecurityGroupRule, b []*instance.SecurityGroupRule
7273
return false, err
7374
}
7475

75-
if from != b[i].DestPortFrom {
76+
if !reflect.DeepEqual(from, b[i].DestPortFrom) {
7677
return false, nil
7778
}
7879

79-
if to != b[i].DestPortTo {
80+
if !reflect.DeepEqual(to, b[i].DestPortTo) {
8081
return false, nil
8182
}
8283
}
@@ -278,7 +279,12 @@ func (s *Service) ensureSecurityGroups(ctx context.Context, securityGroups []v1b
278279
return fmt.Errorf("failed to set security group rules: %w", err)
279280
}
280281

281-
l.Info("security group rules were updated", "securityGroupName", s.SecurityGroupName(sg.Name))
282+
l.Info(
283+
"security group rules were updated",
284+
"securityGroupName", s.SecurityGroupName(sg.Name),
285+
"compareInbound", compareInbound,
286+
"compareOutbound", compareOutbound,
287+
)
282288
}
283289
}
284290
}

0 commit comments

Comments
 (0)