Skip to content

Commit 8ec8969

Browse files
feat(firewall): add support for int type in sets
1 parent 27c531d commit 8ec8969

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

apis/networking/v1beta1/firewall/set_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
package firewall
1616

1717
// SetDataType is the type of a set element
18-
// +kubebuilder:validation:Enum="ipv4_addr"
18+
// +kubebuilder:validation:Enum=integer;ipv4_addr
1919
type SetDataType string
2020

2121
// Possible SetDataType values.
2222
const (
23-
SetDataTypeIPAddr SetDataType = "ipv4_addr"
23+
SetDataTypeInteger SetDataType = "integer"
24+
SetDataTypeIPAddr SetDataType = "ipv4_addr"
2425
)
2526

2627
// Set represents a nftables set

pkg/firewall/set.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ func getSetDataType(dataType *firewallapi.SetDataType) (nftables.SetDatatype, er
232232
switch *dataType {
233233
case firewallapi.SetDataTypeIPAddr:
234234
return nftables.TypeIPAddr, nil
235+
case firewallapi.SetDataTypeInteger:
236+
return nftables.TypeInteger, nil
235237
default:
236238
return nftables.SetDatatype{}, fmt.Errorf("unsupported set data type: %s", *dataType)
237239
}

pkg/firewall/utils/set.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package utils
1717
import (
1818
"fmt"
1919
"net"
20+
"strconv"
2021

2122
firewallapi "github.com/liqotech/liqo/apis/networking/v1beta1/firewall"
2223
)
@@ -41,6 +42,18 @@ func ConvertSetData(data *string, dataType *firewallapi.SetDataType) ([]byte, er
4142
}
4243
return ip.To4(), nil
4344

45+
case firewallapi.SetDataTypeInteger:
46+
intValue, err := strconv.Atoi(*data)
47+
if err != nil {
48+
return nil, fmt.Errorf("set element has invalid integer value %s", *data)
49+
}
50+
return []byte{
51+
byte((intValue >> 24) & 0xFF),
52+
byte((intValue >> 16) & 0xFF),
53+
byte((intValue >> 8) & 0xFF),
54+
byte(intValue & 0xFF),
55+
}, nil
56+
4457
default:
4558
return nil, fmt.Errorf("invalid set value type %s", *dataType)
4659
}

0 commit comments

Comments
 (0)