Skip to content

Commit d986eff

Browse files
committed
IPs > AllIPs
1 parent c02ec44 commit d986eff

File tree

7 files changed

+75
-57
lines changed

7 files changed

+75
-57
lines changed

api/v1alpha7/openstackfloatingippool_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type OpenStackFloatingIPPoolStatus struct {
7474

7575
// +kubebuilder:default={}
7676
// +optional
77-
IPs []string `json:"ips"`
77+
AllIPs []string `json:"allIPs"`
7878

7979
// FailedIPs contains a list of floating ips that failed to be allocated
8080
// +optional

api/v1alpha7/zz_generated.deepcopy.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackfloatingippools.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ spec:
9797
description: OpenStackFloatingIPPoolStatus defines the observed state
9898
of OpenStackFloatingIPPool.
9999
properties:
100+
allIPs:
101+
items:
102+
type: string
103+
type: array
100104
availableIPs:
101105
items:
102106
type: string
@@ -127,10 +131,6 @@ spec:
127131
- id
128132
- name
129133
type: object
130-
ips:
131-
items:
132-
type: string
133-
type: array
134134
type: object
135135
type: object
136136
served: true

config/rbac/role.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ rules:
7373
- get
7474
- patch
7575
- update
76+
- apiGroups:
77+
- infrastructure.cluster.x-k8s.io
78+
resources:
79+
- openstackfloatingippools
80+
verbs:
81+
- create
82+
- delete
83+
- get
84+
- list
85+
- patch
86+
- update
87+
- watch
88+
- apiGroups:
89+
- infrastructure.cluster.x-k8s.io
90+
resources:
91+
- openstackfloatingippools/status
92+
verbs:
93+
- get
94+
- patch
95+
- update
7696
- apiGroups:
7797
- infrastructure.cluster.x-k8s.io
7898
resources:
@@ -93,6 +113,30 @@ rules:
93113
- get
94114
- patch
95115
- update
116+
- apiGroups:
117+
- ipam.cluster.x-k8s.io
118+
resources:
119+
- ipaddressclaims
120+
- ipaddressclaims/status
121+
verbs:
122+
- create
123+
- delete
124+
- get
125+
- list
126+
- update
127+
- watch
128+
- apiGroups:
129+
- ipam.cluster.x-k8s.io
130+
resources:
131+
- ipaddresses
132+
- ipaddresses/status
133+
verbs:
134+
- create
135+
- delete
136+
- get
137+
- list
138+
- update
139+
- watch
96140
- apiGroups:
97141
- ipam.cluster.x-k8x.io.cluster.x-k8s.io
98142
resources:

controllers/openstackfloatingippool_controller.go

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import (
2222
"fmt"
2323

2424
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external"
25+
"golang.org/x/exp/slices"
2526
corev1 "k8s.io/api/core/v1"
2627
apierrors "k8s.io/apimachinery/pkg/api/errors"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"k8s.io/apimachinery/pkg/runtime"
2930
"k8s.io/client-go/tools/record"
3031
"k8s.io/utils/pointer"
32+
"k8s.io/utils/set"
3133
ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1beta1"
3234
"sigs.k8s.io/cluster-api/util/patch"
3335
ctrl "sigs.k8s.io/controller-runtime"
@@ -60,6 +62,7 @@ type OpenStackFloatingIPPoolReconciler struct {
6062
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=openstackfloatingippools/status,verbs=get;update;patch
6163
// +kubebuilder:rbac:groups=ipam.cluster.x-k8s.io,resources=ipaddressclaims;ipaddressclaims/status,verbs=get;list;watch;update;create;delete
6264
// +kubebuilder:rbac:groups=ipam.cluster.x-k8s.io,resources=ipaddresses;ipaddresses/status,verbs=get;list;watch;create;update;delete
65+
6366
func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
6467
log := ctrl.LoggerFrom(ctx)
6568
pool := &infrav1.OpenStackFloatingIPPool{}
@@ -95,6 +98,9 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
9598
}
9699
}()
97100

101+
set := set.New(pool.Spec.PreAllocatedFloatingIPs...)
102+
fmt.Println(set)
103+
98104
if err := r.reconcileFloatingIPNetwork(scope, pool); err != nil {
99105
return ctrl.Result{}, err
100106
}
@@ -194,7 +200,7 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileDelete(ctx context.Context,
194200
}
195201

196202
// Clean up ips created by the pool
197-
for _, ip := range diff(pool.Status.IPs, pool.Spec.PreAllocatedFloatingIPs) {
203+
for _, ip := range set.New(pool.Status.AllIPs...).Difference(set.New(pool.Spec.PreAllocatedFloatingIPs...)).SortedList() {
198204
if err := networkingService.DeleteFloatingIP(pool, ip); err != nil {
199205
return fmt.Errorf("delete floating IP: %w", err)
200206
}
@@ -207,49 +213,21 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileDelete(ctx context.Context,
207213
return nil
208214
}
209215

210-
func union(a []string, b []string) []string {
211-
m := make(map[string]struct{})
212-
for _, item := range a {
213-
m[item] = struct{}{}
214-
}
215-
for _, item := range b {
216-
m[item] = struct{}{}
217-
}
218-
result := make([]string, 0, len(m))
219-
for item := range m {
220-
result = append(result, item)
221-
}
222-
return result
223-
}
224-
225-
func diff(a []string, b []string) []string {
226-
m := make(map[string]struct{})
227-
for _, item := range a {
228-
m[item] = struct{}{}
229-
}
230-
for _, item := range b {
231-
delete(m, item)
232-
}
233-
result := make([]string, 0, len(m))
234-
for item := range m {
235-
result = append(result, item)
236-
}
237-
return result
238-
}
239-
240216
func (r *OpenStackFloatingIPPoolReconciler) setIPStatuses(ctx context.Context, pool *infrav1.OpenStackFloatingIPPool) error {
241217
ipAddresses := &ipamv1.IPAddressList{}
242218
if err := r.Client.List(ctx, ipAddresses, client.InNamespace(pool.Namespace), client.MatchingFields{infrav1.OpenStackFloatingIPPoolNameIndex: pool.Name}); err != nil {
243219
return err
244220
}
245-
pool.Status.ClaimedIPs = []string{}
221+
pool.Status.ClaimedIPs = make([]string, 0, len(ipAddresses.Items))
246222
for _, ip := range ipAddresses.Items {
247223
pool.Status.ClaimedIPs = append(pool.Status.ClaimedIPs, ip.Spec.Address)
248224
}
249-
pool.Status.IPs = append([]string{}, pool.Spec.PreAllocatedFloatingIPs...)
225+
pool.Status.AllIPs = slices.Clone(pool.Spec.PreAllocatedFloatingIPs)
250226

251-
pool.Status.IPs = union(pool.Status.IPs, pool.Status.ClaimedIPs)
252-
pool.Status.AvailableIPs = diff(diff(pool.Status.IPs, pool.Status.ClaimedIPs), pool.Status.FailedIPs)
227+
pool.Status.AllIPs = set.New(pool.Status.AllIPs...).Union(set.New(pool.Status.ClaimedIPs...)).SortedList()
228+
pool.Status.FailedIPs = set.New(pool.Status.FailedIPs...).Difference(set.New(pool.Status.AllIPs...)).SortedList()
229+
unclaimedIps := set.New(pool.Status.AllIPs...).Difference(set.New(pool.Status.ClaimedIPs...))
230+
pool.Status.AvailableIPs = unclaimedIps.Difference(set.New(pool.Status.FailedIPs...)).SortedList()
253231
return nil
254232
}
255233

@@ -273,20 +251,14 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(scope scope.Scope, pool *infra
273251
if err != nil {
274252
return "", fmt.Errorf("get floating IP: %w", err)
275253
}
276-
// If the IP exist return it, else we continue and try to allocate it if we fail to allocate it we will mark it as failed
277254
if fp != nil {
278255
return fp.FloatingIP, nil
279256
}
280257
}
281258

282-
// ip could be empty meaning we want to get a new one or the IP
283-
// if ip is not empty it got and ip from availableIPs that does not exist in openstack
284-
// we try to allocate it, if we fail we mark it as failed and skip it next time
285-
fp, err := networkingService.CreateFloatingIPForPool(pool, ip)
259+
fp, err := networkingService.CreateFloatingIPForPool(pool, "")
286260
if err != nil {
287-
scope.Logger().Error(err, "Failed to create floating IP", "pool", pool.Name, "ip", ip)
288-
// If we tried to allocate a specific IP, we should mark it as failed so we don't try again
289-
// this should only happen if the pool thinks this IP is available and we do not have permission to allocate a specific IP
261+
scope.Logger().Error(err, "Failed to create floating IP", "pool", pool.Name)
290262
if ip != "" {
291263
pool.Status.FailedIPs = append(pool.Status.FailedIPs, ip)
292264
}
@@ -295,7 +267,7 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(scope scope.Scope, pool *infra
295267

296268
ip = fp.FloatingIP
297269
pool.Status.ClaimedIPs = append(pool.Status.ClaimedIPs, ip)
298-
pool.Status.IPs = append(pool.Status.IPs, ip)
270+
pool.Status.AllIPs = append(pool.Status.AllIPs, ip)
299271
return ip, nil
300272
}
301273

@@ -323,12 +295,11 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileFloatingIPNetwork(scope sco
323295
return fmt.Errorf("found multiple networks, expects filter to match one (result: %v)", networkList)
324296
}
325297

326-
if pool.Status.FloatingIPNetwork == nil {
327-
pool.Status.FloatingIPNetwork = &infrav1.NetworkStatus{}
298+
pool.Status.FloatingIPNetwork = &infrav1.NetworkStatus{
299+
ID: networkList[0].ID,
300+
Name: networkList[0].Name,
301+
Tags: networkList[0].Tags,
328302
}
329-
pool.Status.FloatingIPNetwork.ID = networkList[0].ID
330-
pool.Status.FloatingIPNetwork.Name = networkList[0].Name
331-
pool.Status.FloatingIPNetwork.Tags = networkList[0].Tags
332303
return nil
333304
}
334305

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
k8s.io/component-base v0.28.4
2626
k8s.io/klog/v2 v2.100.1
2727
k8s.io/kubernetes v1.28.3
28-
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
28+
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
2929
sigs.k8s.io/cluster-api v1.6.0
3030
sigs.k8s.io/cluster-api/test v1.6.0
3131
sigs.k8s.io/controller-runtime v0.16.3

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,10 +841,13 @@ k8s.io/kms v0.28.4 h1:PMgY/3CQTWP9eIKmNQiTgjLIZ0ns6O+voagzD2/4mSg=
841841
k8s.io/kms v0.28.4/go.mod h1:HL4/lR/bhjAJPbqycKtfhWiKh1Sp21cpHOL8P4oo87w=
842842
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
843843
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
844+
k8s.io/kubernetes v1.15.0-alpha.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
844845
k8s.io/kubernetes v1.28.3 h1:XTci6gzk+JR51UZuZQCFJ4CsyUkfivSjLI4O1P9z6LY=
845846
k8s.io/kubernetes v1.28.3/go.mod h1:NhAysZWvHtNcJFFHic87ofxQN7loylCQwg3ZvXVDbag=
846847
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
847848
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
849+
k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ=
850+
k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
848851
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
849852
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
850853
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

0 commit comments

Comments
 (0)