Skip to content

Commit 63aa70f

Browse files
committed
Check that endpoint has subset before accessing first subset
1 parent a89265b commit 63aa70f

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

pkg/master/controller_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,32 @@ func TestReconcileEndpoints(t *testing.T) {
558558

559559
}
560560

561+
func TestEmptySubsets(t *testing.T) {
562+
ns := metav1.NamespaceDefault
563+
om := func(name string) metav1.ObjectMeta {
564+
return metav1.ObjectMeta{Namespace: ns, Name: name}
565+
}
566+
endpoints := &corev1.EndpointsList{
567+
Items: []corev1.Endpoints{{
568+
ObjectMeta: om("foo"),
569+
Subsets: nil,
570+
}},
571+
}
572+
fakeClient := fake.NewSimpleClientset()
573+
if endpoints != nil {
574+
fakeClient = fake.NewSimpleClientset(endpoints)
575+
}
576+
epAdapter := reconcilers.NewEndpointsAdapter(fakeClient.CoreV1(), nil)
577+
reconciler := reconcilers.NewMasterCountEndpointReconciler(1, epAdapter)
578+
endpointPorts := []corev1.EndpointPort{
579+
{Name: "foo", Port: 8080, Protocol: "TCP"},
580+
}
581+
err := reconciler.RemoveEndpoints("foo", net.ParseIP("1.2.3.4"), endpointPorts)
582+
if err != nil {
583+
t.Errorf("unexpected error: %v", err)
584+
}
585+
}
586+
561587
func TestCreateOrUpdateMasterService(t *testing.T) {
562588
ns := metav1.NamespaceDefault
563589
om := func(name string) metav1.ObjectMeta {

pkg/master/reconcilers/lease.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ func (s *storageLeases) ListLeases() ([]string, error) {
6767
return nil, err
6868
}
6969

70-
ipList := make([]string, len(ipInfoList.Items))
71-
for i, ip := range ipInfoList.Items {
72-
ipList[i] = ip.Subsets[0].Addresses[0].IP
70+
ipList := make([]string, 0, len(ipInfoList.Items))
71+
for _, ip := range ipInfoList.Items {
72+
if len(ip.Subsets) > 0 && len(ip.Subsets[0].Addresses) > 0 && len(ip.Subsets[0].Addresses[0].IP) > 0 {
73+
ipList = append(ipList, ip.Subsets[0].Addresses[0].IP)
74+
}
7375
}
7476

7577
klog.V(6).Infof("Current master IPs listed in storage are %v", ipList)

pkg/master/reconcilers/lease_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,19 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
617617
}},
618618
},
619619
},
620+
{
621+
testName: "endpoint with no subset",
622+
serviceName: "foo",
623+
ip: "5.6.7.8",
624+
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
625+
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
626+
endpoints: &corev1.EndpointsList{
627+
Items: []corev1.Endpoints{{
628+
ObjectMeta: om("foo"),
629+
Subsets: nil,
630+
}},
631+
},
632+
},
620633
}
621634
for _, test := range stopTests {
622635
t.Run(test.testName, func(t *testing.T) {

pkg/master/reconcilers/mastercount.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func (r *masterCountEndpointReconciler) RemoveEndpoints(serviceName string, ip n
149149
return err
150150
}
151151

152+
if len(e.Subsets) == 0 {
153+
// no action is needed to remove the endpoint
154+
return nil
155+
}
152156
// Remove our IP from the list of addresses
153157
new := []corev1.EndpointAddress{}
154158
for _, addr := range e.Subsets[0].Addresses {

0 commit comments

Comments
 (0)