Skip to content

Commit 2da124b

Browse files
Merge pull request #6 from Nordix/bugfix-mael
🐛 Fix bug when cluster is not found
2 parents b49783d + 021eb9e commit 2da124b

File tree

5 files changed

+29
-34
lines changed

5 files changed

+29
-34
lines changed

controllers/ippool_controller.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ import (
3030
"sigs.k8s.io/cluster-api/util"
3131
"sigs.k8s.io/cluster-api/util/patch"
3232
ctrl "sigs.k8s.io/controller-runtime"
33-
"sigs.k8s.io/controller-runtime/pkg/builder"
3433
"sigs.k8s.io/controller-runtime/pkg/client"
35-
"sigs.k8s.io/controller-runtime/pkg/event"
3634
"sigs.k8s.io/controller-runtime/pkg/handler"
37-
"sigs.k8s.io/controller-runtime/pkg/predicate"
3835
"sigs.k8s.io/controller-runtime/pkg/source"
3936
)
4037

@@ -118,7 +115,7 @@ func (r *IPPoolReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, rerr erro
118115
return ctrl.Result{}, errors.Wrapf(err, "failed to create helper for managing the IP pool")
119116
}
120117

121-
if ipamv1IPPool.Spec.ClusterName != nil && cluster != nil {
118+
if ipamv1IPPool.Spec.ClusterName != nil && cluster != nil && cluster.Name != "" {
122119
metadataLog = metadataLog.WithValues("cluster", cluster.Name)
123120
if err := ipPoolMgr.SetClusterOwnerRef(cluster); err != nil {
124121
return ctrl.Result{}, err
@@ -181,14 +178,6 @@ func (r *IPPoolReconciler) SetupWithManager(mgr ctrl.Manager) error {
181178
&handler.EnqueueRequestsFromMapFunc{
182179
ToRequests: handler.ToRequestsFunc(r.IPClaimToIPPool),
183180
},
184-
// Do not trigger a reconciliation on updates of the claim, as the Spec
185-
// fields are immutable
186-
builder.WithPredicates(predicate.Funcs{
187-
UpdateFunc: func(e event.UpdateEvent) bool { return false },
188-
CreateFunc: func(e event.CreateEvent) bool { return true },
189-
DeleteFunc: func(e event.DeleteEvent) bool { return true },
190-
GenericFunc: func(e event.GenericEvent) bool { return false },
191-
}),
192181
).
193182
Complete(r)
194183
}

controllers/ippool_controller_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ var _ = Describe("IPPool controller", func() {
8787
if tc.setOwnerRefError {
8888
m.EXPECT().SetClusterOwnerRef(gomock.Any()).Return(errors.New(""))
8989
} else {
90-
m.EXPECT().SetClusterOwnerRef(gomock.Any()).Return(nil)
90+
if tc.cluster != nil {
91+
m.EXPECT().SetClusterOwnerRef(gomock.Any()).Return(nil)
92+
}
9193
}
9294
}
9395

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ require (
1010
github.com/google/go-cmp v0.4.1 // indirect
1111
github.com/hashicorp/golang-lru v0.5.4 // indirect
1212
github.com/imdario/mergo v0.3.9 // indirect
13+
github.com/onsi/ginkgo v1.12.0
14+
github.com/onsi/gomega v1.9.0
1315
github.com/opencontainers/go-digest v1.0.0 // indirect
1416
github.com/pkg/errors v0.9.1
1517
github.com/prometheus/client_golang v1.6.0 // indirect
@@ -21,7 +23,7 @@ require (
2123
gomodules.xyz/jsonpatch/v2 v2.1.0 // indirect
2224
gopkg.in/yaml.v2 v2.3.0 // indirect
2325
k8s.io/api v0.19.0-alpha.2
24-
k8s.io/apiextensions-apiserver v0.18.3 // indirect
26+
k8s.io/apiextensions-apiserver v0.18.3
2527
k8s.io/apimachinery v0.19.0-alpha.2
2628
k8s.io/client-go v11.0.0+incompatible
2729
k8s.io/klog v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
302302
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
303303
github.com/go-logr/logr v0.1.0 h1:M1Tv3VzNlEHg6uyACnRdtrploV2P7wZqH8BoQMtz0cg=
304304
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
305+
github.com/go-logr/zapr v0.1.0 h1:h+WVe9j6HAA01niTJPA/kKH0i7e0rLZBCwauQFcRE54=
305306
github.com/go-logr/zapr v0.1.0/go.mod h1:tabnROwaDl0UNxkVeFRbY8bwB37GwRv0P8lg6aAiEnk=
306307
github.com/go-logr/zapr v0.1.1 h1:qXBXPDdNncunGs7XeEpsJt8wCjYBygluzfdLO0G5baE=
307308
github.com/go-logr/zapr v0.1.1/go.mod h1:tabnROwaDl0UNxkVeFRbY8bwB37GwRv0P8lg6aAiEnk=
@@ -1540,6 +1541,7 @@ k8s.io/apiserver v0.17.3/go.mod h1:iJtsPpu1ZpEnHaNawpSV0nYTGBhhX2dUlnn7/QS7QiY=
15401541
k8s.io/apiserver v0.18.0 h1:ELAWpGWC6XdbRLi5lwAbEbvksD7hkXxPdxaJsdpist4=
15411542
k8s.io/apiserver v0.18.0/go.mod h1:3S2O6FeBBd6XTo0njUrLxiqk8GNy6wWOftjhJcXYnjw=
15421543
k8s.io/apiserver v0.18.2/go.mod h1:Xbh066NqrZO8cbsoenCwyDJ1OSi8Ag8I2lezeHxzwzw=
1544+
k8s.io/apiserver v0.18.3 h1:BVjccwKP/kEqY+ResOyWs0EKs7f4XL0d0E5GkU3uiqI=
15431545
k8s.io/apiserver v0.18.3/go.mod h1:tHQRmthRPLUtwqsOnJJMoI8SW3lnoReZeE861lH8vUw=
15441546
k8s.io/apiserver v0.19.0-alpha.2 h1:k1fpzJAPZvtRT9Z8Rc42kciGehIH0GiEmTgEmc46drw=
15451547
k8s.io/apiserver v0.19.0-alpha.2/go.mod h1:3sm/OyOD8RpEaJ4B2MzqeGXsfVZP+T/7y+Hr0+0qfao=

ipam/ippool_manager.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ func (m *IPPoolManager) updateAddress(ctx context.Context,
206206
}
207207
// Always patch addressClaim exiting this function so we can persist any changes.
208208
defer func() {
209-
fmt.Printf("\nPatching %v", addressClaim.Name)
210209
err := helper.Patch(ctx, addressClaim)
211210
if err != nil {
212211
m.Log.Info("failed to Patch IPClaim")
@@ -253,7 +252,6 @@ func (m *IPPoolManager) allocateAddress(addressClaim *ipamv1.IPClaim,
253252
index := 0
254253
for !ipAllocated {
255254
allocatedAddress, err = getIPAddress(pool, index)
256-
fmt.Println(allocatedAddress)
257255
if err != nil {
258256
break
259257
}
@@ -306,6 +304,22 @@ func (m *IPPoolManager) createAddress(ctx context.Context,
306304

307305
m.Log.Info("Address allocated", "Claim", addressClaim.Name, "address", allocatedAddress)
308306

307+
ownerRefs := addressClaim.OwnerReferences
308+
ownerRefs = append(ownerRefs,
309+
metav1.OwnerReference{
310+
APIVersion: m.IPPool.APIVersion,
311+
Kind: m.IPPool.Kind,
312+
Name: m.IPPool.Name,
313+
UID: m.IPPool.UID,
314+
},
315+
metav1.OwnerReference{
316+
APIVersion: addressClaim.APIVersion,
317+
Kind: addressClaim.Kind,
318+
Name: addressClaim.Name,
319+
UID: addressClaim.UID,
320+
},
321+
)
322+
309323
// Create the IPAddress object, with an Owner ref to the Metal3Machine
310324
// (curOwnerRef) and to the IPPool
311325
addressObject := &ipamv1.IPAddress{
@@ -314,24 +328,10 @@ func (m *IPPoolManager) createAddress(ctx context.Context,
314328
APIVersion: ipamv1.GroupVersion.String(),
315329
},
316330
ObjectMeta: metav1.ObjectMeta{
317-
Name: addressName,
318-
Namespace: m.IPPool.Namespace,
319-
OwnerReferences: []metav1.OwnerReference{
320-
metav1.OwnerReference{
321-
Controller: pointer.BoolPtr(true),
322-
APIVersion: m.IPPool.APIVersion,
323-
Kind: m.IPPool.Kind,
324-
Name: m.IPPool.Name,
325-
UID: m.IPPool.UID,
326-
},
327-
metav1.OwnerReference{
328-
APIVersion: addressClaim.APIVersion,
329-
Kind: addressClaim.Kind,
330-
Name: addressClaim.Name,
331-
UID: addressClaim.UID,
332-
},
333-
},
334-
Labels: addressClaim.Labels,
331+
Name: addressName,
332+
Namespace: m.IPPool.Namespace,
333+
OwnerReferences: ownerRefs,
334+
Labels: addressClaim.Labels,
335335
},
336336
Spec: ipamv1.IPAddressSpec{
337337
Address: allocatedAddress,

0 commit comments

Comments
 (0)