@@ -25,75 +25,86 @@ import (
2525)
2626
2727type networkInfo struct {
28- cidr string
28+ network
2929 creationTimestamp time.Time
3030}
3131
32+ type network struct {
33+ cidr string
34+ preAllocated uint
35+ }
36+
37+ func (n network ) String () string {
38+ return n .cidr
39+ }
40+
3241// reserveNetwork reserves a network, saving it in the cache.
33- func (lipam * LiqoIPAM ) reserveNetwork (cidr string , preAllocated uint ) error {
42+ func (lipam * LiqoIPAM ) reserveNetwork (nw * network ) error {
3443 lipam .mutex .Lock ()
3544 defer lipam .mutex .Unlock ()
3645
3746 // TODO: implement real network reserve logic
38- _ = preAllocated
3947 if lipam .cacheNetworks == nil {
4048 lipam .cacheNetworks = make (map [string ]networkInfo )
4149 }
42- lipam .cacheNetworks [cidr ] = networkInfo {
43- cidr : cidr ,
50+ lipam .cacheNetworks [nw . String () ] = networkInfo {
51+ network : * nw ,
4452 creationTimestamp : time .Now (),
4553 }
4654
47- klog .Infof ("Reserved network %q" , cidr )
55+ klog .Infof ("Reserved network %q" , nw )
4856 return nil
4957}
5058
5159// acquireNetwork acquires a network, eventually remapped if conflicts are found.
52- func (lipam * LiqoIPAM ) acquireNetwork (cidr string , immutable bool , preAllocated uint ) (string , error ) {
60+ func (lipam * LiqoIPAM ) acquireNetwork (cidr string , preAllocated uint , immutable bool ) (string , error ) {
5361 lipam .mutex .Lock ()
5462 defer lipam .mutex .Unlock ()
5563
5664 // TODO: implement real network acquire logic
5765 _ = immutable
58- _ = preAllocated
5966 if lipam .cacheNetworks == nil {
6067 lipam .cacheNetworks = make (map [string ]networkInfo )
6168 }
62- lipam .cacheNetworks [cidr ] = networkInfo {
63- cidr : cidr ,
69+ nw := network {
70+ cidr : cidr ,
71+ preAllocated : preAllocated ,
72+ }
73+ lipam .cacheNetworks [nw .String ()] = networkInfo {
74+ network : nw ,
6475 creationTimestamp : time .Now (),
6576 }
6677
67- klog .Infof ("Acquired network %q" , cidr )
68- return cidr , nil
78+ klog .Infof ("Acquired network %q" , nw )
79+ return nw . cidr , nil
6980}
7081
7182// freeNetwork frees a network, removing it from the cache.
72- func (lipam * LiqoIPAM ) freeNetwork (cidr string ) {
83+ func (lipam * LiqoIPAM ) freeNetwork (nw * network ) {
7384 lipam .mutex .Lock ()
7485 defer lipam .mutex .Unlock ()
7586
7687 // TODO: implement real network free logic
77- delete (lipam .cacheNetworks , cidr )
78- klog .Infof ("Freed network %q" , cidr )
88+ delete (lipam .cacheNetworks , nw . String () )
89+ klog .Infof ("Freed network %q" , nw . cidr )
7990}
8091
8192// isNetworkAvailable checks if a network is available.
82- func (lipam * LiqoIPAM ) isNetworkAvailable (cidr string ) bool {
93+ func (lipam * LiqoIPAM ) isNetworkAvailable (nw * network ) bool {
8394 lipam .mutex .Lock ()
8495 defer lipam .mutex .Unlock ()
8596
8697 // TODO: implement real network availability check logic
8798 if lipam .cacheNetworks == nil {
8899 return true
89100 }
90- _ , ok := lipam .cacheNetworks [cidr ]
101+ _ , ok := lipam .cacheNetworks [nw . String () ]
91102
92103 return ok
93104}
94105
95- func listNetworksOnCluster (ctx context.Context , cl client.Client ) ([]string , error ) {
96- var nets []string
106+ func listNetworksOnCluster (ctx context.Context , cl client.Client ) ([]network , error ) {
107+ var nets []network
97108 var networks ipamv1alpha1.NetworkList
98109 if err := cl .List (ctx , & networks ); err != nil {
99110 return nil , err
@@ -108,7 +119,10 @@ func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]string, err
108119 continue
109120 }
110121
111- nets = append (nets , cidr )
122+ nets = append (nets , network {
123+ cidr : cidr ,
124+ preAllocated : net .Spec .PreAllocated ,
125+ })
112126 }
113127
114128 return nets , nil
0 commit comments