Skip to content

Commit c6578e3

Browse files
committed
Calculate total number of endpoints for service
In this commit the Reconcile function of the endpointslice controller is implemented and calculates the total number of endpoints for a service. Signed-off-by: Elis Lulja <[email protected]>
1 parent 69279ef commit c6578e3

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

controllers/endpointslice_controller.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package controllers
1818

1919
import (
20-
"context"
20+
"fmt"
2121
"sync"
2222

2323
"k8s.io/api/discovery/v1beta1"
@@ -52,10 +52,32 @@ type EndpointSliceReconciler struct {
5252

5353
// Reconcile keeps track counts in the endpointslice length
5454
func (r *EndpointSliceReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
55-
_ = context.Background()
56-
_ = r.Log.WithName("EndpointSliceReconciler").WithValues("endpointslice", req.NamespacedName)
55+
l := r.Log.WithName("EndpointSliceReconciler").WithValues("endpointslice", req.NamespacedName)
56+
namespacedName := req.NamespacedName.String()
57+
data := func(nname string) *epsData {
58+
r.lock.Lock()
59+
defer r.lock.Unlock()
60+
defer delete(r.epsDataActions, nname)
61+
62+
return r.epsDataActions[nname]
63+
}(namespacedName)
64+
if data == nil {
65+
l.Error(fmt.Errorf("no data exists"), "could not get endpointslice data for this endpointslice")
66+
return ctrl.Result{}, nil
67+
}
68+
69+
srvName := ktypes.NamespacedName{Namespace: req.Namespace, Name: data.srv}.String()
70+
if _, exists := r.srvCounts[srvName]; !exists {
71+
r.srvCounts[srvName] = map[string]int{}
72+
}
73+
r.srvCounts[srvName][req.Name] = data.count
74+
75+
totalCount := 0
76+
for _, c := range r.srvCounts[srvName] {
77+
totalCount += c
78+
}
5779

58-
// TODO: implement me
80+
// TODO: do something with the total count
5981

6082
return ctrl.Result{}, nil
6183
}

controllers/endpointslice_controller_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/api/discovery/v1beta1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
29+
ktypes "k8s.io/apimachinery/pkg/types"
2930
ctrl "sigs.k8s.io/controller-runtime"
3031
"sigs.k8s.io/controller-runtime/pkg/client"
3132
fakecli "sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -213,3 +214,66 @@ func TestEpSliceDeletePredicate(t *testing.T) {
213214
}
214215
}
215216
}
217+
218+
func TestReconcile(t *testing.T) {
219+
a := assert.New(t)
220+
cases := []struct {
221+
dataActions map[string]*epsData
222+
srvCounts map[string]map[string]int
223+
expRes ctrl.Result
224+
expErr error
225+
expSrvCounts map[string]map[string]int
226+
}{
227+
{},
228+
{
229+
dataActions: map[string]*epsData{
230+
"ns-name/name": {
231+
count: 3,
232+
srv: "srv",
233+
},
234+
},
235+
srvCounts: map[string]map[string]int{
236+
"ns-name/srv": {
237+
"another-name": 5,
238+
},
239+
},
240+
expSrvCounts: map[string]map[string]int{
241+
"ns-name/srv": {
242+
"another-name": 5,
243+
"name": 3,
244+
},
245+
},
246+
},
247+
{
248+
dataActions: map[string]*epsData{
249+
"ns-name/name": {
250+
count: 3,
251+
srv: "srv",
252+
},
253+
},
254+
srvCounts: map[string]map[string]int{},
255+
expSrvCounts: map[string]map[string]int{
256+
"ns-name/srv": {
257+
"name": 3,
258+
},
259+
},
260+
},
261+
}
262+
failed := func(i int) {
263+
a.FailNow(fmt.Sprintf("case %d failed", i))
264+
}
265+
for i, currCase := range cases {
266+
eps := &EndpointSliceReconciler{
267+
BaseReconciler: &BaseReconciler{
268+
Log: ctrl.Log.WithName("controllers"),
269+
},
270+
epsDataActions: currCase.dataActions,
271+
srvCounts: currCase.srvCounts,
272+
}
273+
274+
res, err := eps.Reconcile(ctrl.Request{NamespacedName: ktypes.NamespacedName{Namespace: "ns-name", Name: "name"}})
275+
if !a.Equal(currCase.expRes, res) || !a.Equal(currCase.expErr, err) || !a.Equal(currCase.expSrvCounts, eps.srvCounts) {
276+
failed(i)
277+
}
278+
}
279+
}

0 commit comments

Comments
 (0)