Skip to content

Commit b18acb2

Browse files
JoelSpeedopenshift-cherrypick-robot
authored and
openshift-cherrypick-robot
committed
UPSTREAM: 130450: Fix implementation of ContainsCIDR to allow non-equal addresses
1 parent 97471c6 commit b18acb2

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

staging/src/k8s.io/apiserver/pkg/cel/library/cidr.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ func cidrContainsCIDR(arg ref.Val, other ref.Val) ref.Val {
223223
return types.MaybeNoSuchOverloadErr(other)
224224
}
225225

226-
equalMasked := cidr.Prefix.Masked() == netip.PrefixFrom(containsCIDR.Prefix.Addr(), cidr.Prefix.Bits())
227-
return types.Bool(equalMasked && cidr.Prefix.Bits() <= containsCIDR.Prefix.Bits())
226+
return types.Bool(cidr.Overlaps(containsCIDR.Prefix) && cidr.Prefix.Bits() <= containsCIDR.Prefix.Bits())
228227
}
229228

230229
func prefixLength(arg ref.Val) ref.Val {

staging/src/k8s.io/apiserver/pkg/cel/library/cidr_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,21 @@ func TestCIDR(t *testing.T) {
151151
expr: `cidr("192.168.0.0/24").containsCIDR(cidr("192.168.0.0/25"))`,
152152
expectResult: trueVal,
153153
},
154+
{
155+
name: "contains CIDR ipv4 (CIDR) (/32)",
156+
expr: `cidr("192.168.0.0/24").containsCIDR(cidr("192.168.0.1/32"))`,
157+
expectResult: trueVal,
158+
},
154159
{
155160
name: "does not contain IP ipv4 (CIDR)",
156161
expr: `cidr("192.168.0.0/24").containsCIDR(cidr("192.168.0.0/23"))`,
157162
expectResult: falseVal,
158163
},
164+
{
165+
name: "does not contain IP ipv4 (CIDR) (/32)",
166+
expr: `cidr("192.168.0.0/24").containsCIDR(cidr("192.169.0.1/32"))`,
167+
expectResult: falseVal,
168+
},
159169
{
160170
name: "contains CIDR ipv4 (string)",
161171
expr: `cidr("192.168.0.0/24").containsCIDR("192.168.0.0/25")`,

0 commit comments

Comments
 (0)