github.com/cilium/cilium@v1.16.2/pkg/datapath/tables/ipset.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package tables 5 6 import ( 7 "net/netip" 8 9 "github.com/cilium/statedb" 10 "github.com/cilium/statedb/index" 11 "github.com/cilium/statedb/reconciler" 12 ) 13 14 const IPSetsTableName = "ipsets" 15 16 type IPSetEntryKey struct { 17 Name string 18 Addr netip.Addr 19 } 20 21 func (k IPSetEntryKey) Key() index.Key { 22 return append(index.NetIPAddr(k.Addr), []byte(k.Name)...) 23 } 24 25 var IPSetEntryIndex = statedb.Index[*IPSetEntry, IPSetEntryKey]{ 26 Name: IPSetsTableName, 27 FromObject: func(s *IPSetEntry) index.KeySet { 28 return index.NewKeySet(IPSetEntryKey{s.Name, s.Addr}.Key()) 29 }, 30 FromKey: IPSetEntryKey.Key, 31 Unique: true, 32 } 33 34 func NewIPSetTable(db *statedb.DB) (statedb.RWTable[*IPSetEntry], error) { 35 tbl, err := statedb.NewTable( 36 IPSetsTableName, 37 IPSetEntryIndex, 38 ) 39 if err != nil { 40 return nil, err 41 } 42 return tbl, db.RegisterTable(tbl) 43 } 44 45 func (s *IPSetEntry) TableHeader() []string { 46 return []string{"Name", "Family", "Addr", "Status"} 47 } 48 49 func (s *IPSetEntry) TableRow() []string { 50 return []string{s.Name, s.Family, s.Addr.String(), s.Status.String()} 51 } 52 53 type IPSetEntry struct { 54 Name string 55 Family string 56 Addr netip.Addr 57 58 Status reconciler.Status 59 } 60 61 func (s *IPSetEntry) GetStatus() reconciler.Status { 62 return s.Status 63 } 64 65 func (s *IPSetEntry) SetStatus(newStatus reconciler.Status) *IPSetEntry { 66 s.Status = newStatus 67 return s 68 } 69 70 func (s *IPSetEntry) Clone() *IPSetEntry { 71 s2 := *s 72 return &s2 73 }