Merge pull request #84862 from yutedz/master-subset-check

Check that endpoint has subset before accessing first subset
This commit is contained in:
Kubernetes Prow Robot 2019-11-12 10:23:44 -08:00 committed by GitHub
commit fb87f72b88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 3 deletions

View File

@ -558,6 +558,32 @@ func TestReconcileEndpoints(t *testing.T) {
}
func TestEmptySubsets(t *testing.T) {
ns := metav1.NamespaceDefault
om := func(name string) metav1.ObjectMeta {
return metav1.ObjectMeta{Namespace: ns, Name: name}
}
endpoints := &corev1.EndpointsList{
Items: []corev1.Endpoints{{
ObjectMeta: om("foo"),
Subsets: nil,
}},
}
fakeClient := fake.NewSimpleClientset()
if endpoints != nil {
fakeClient = fake.NewSimpleClientset(endpoints)
}
epAdapter := reconcilers.NewEndpointsAdapter(fakeClient.CoreV1(), nil)
reconciler := reconcilers.NewMasterCountEndpointReconciler(1, epAdapter)
endpointPorts := []corev1.EndpointPort{
{Name: "foo", Port: 8080, Protocol: "TCP"},
}
err := reconciler.RemoveEndpoints("foo", net.ParseIP("1.2.3.4"), endpointPorts)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestCreateOrUpdateMasterService(t *testing.T) {
ns := metav1.NamespaceDefault
om := func(name string) metav1.ObjectMeta {

View File

@ -67,9 +67,11 @@ func (s *storageLeases) ListLeases() ([]string, error) {
return nil, err
}
ipList := make([]string, len(ipInfoList.Items))
for i, ip := range ipInfoList.Items {
ipList[i] = ip.Subsets[0].Addresses[0].IP
ipList := make([]string, 0, len(ipInfoList.Items))
for _, ip := range ipInfoList.Items {
if len(ip.Subsets) > 0 && len(ip.Subsets[0].Addresses) > 0 && len(ip.Subsets[0].Addresses[0].IP) > 0 {
ipList = append(ipList, ip.Subsets[0].Addresses[0].IP)
}
}
klog.V(6).Infof("Current master IPs listed in storage are %v", ipList)

View File

@ -617,6 +617,19 @@ func TestLeaseRemoveEndpoints(t *testing.T) {
}},
},
},
{
testName: "endpoint with no subset",
serviceName: "foo",
ip: "5.6.7.8",
endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
endpointKeys: []string{"1.2.3.4", "4.3.2.2", "4.3.2.3", "4.3.2.4"},
endpoints: &corev1.EndpointsList{
Items: []corev1.Endpoints{{
ObjectMeta: om("foo"),
Subsets: nil,
}},
},
},
}
for _, test := range stopTests {
t.Run(test.testName, func(t *testing.T) {

View File

@ -149,6 +149,10 @@ func (r *masterCountEndpointReconciler) RemoveEndpoints(serviceName string, ip n
return err
}
if len(e.Subsets) == 0 {
// no action is needed to remove the endpoint
return nil
}
// Remove our IP from the list of addresses
new := []corev1.EndpointAddress{}
for _, addr := range e.Subsets[0].Addresses {