clean up code

This commit is contained in:
lucming 2022-07-15 19:38:08 +08:00
parent a0fe0d6b37
commit e536d565ea
4 changed files with 36 additions and 35 deletions

View File

@ -463,16 +463,15 @@ func (e *Controller) syncService(ctx context.Context, key string) error {
// See if there's actually an update here. // See if there's actually an update here.
currentEndpoints, err := e.endpointsLister.Endpoints(service.Namespace).Get(service.Name) currentEndpoints, err := e.endpointsLister.Endpoints(service.Namespace).Get(service.Name)
if err != nil { if err != nil {
if errors.IsNotFound(err) { if !errors.IsNotFound(err) {
return err
}
currentEndpoints = &v1.Endpoints{ currentEndpoints = &v1.Endpoints{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: service.Name, Name: service.Name,
Labels: service.Labels, Labels: service.Labels,
}, },
} }
} else {
return err
}
} }
createEndpoints := len(currentEndpoints.ResourceVersion) == 0 createEndpoints := len(currentEndpoints.ResourceVersion) == 0
@ -618,13 +617,12 @@ func addEndpointSubset(subsets []v1.EndpointSubset, pod *v1.Pod, epa v1.Endpoint
} }
func endpointPortFromServicePort(servicePort *v1.ServicePort, portNum int) *v1.EndpointPort { func endpointPortFromServicePort(servicePort *v1.ServicePort, portNum int) *v1.EndpointPort {
epp := &v1.EndpointPort{ return &v1.EndpointPort{
Name: servicePort.Name, Name: servicePort.Name,
Port: int32(portNum), Port: int32(portNum),
Protocol: servicePort.Protocol, Protocol: servicePort.Protocol,
AppProtocol: servicePort.AppProtocol, AppProtocol: servicePort.AppProtocol,
} }
return epp
} }
// capacityAnnotationSetCorrectly returns false if number of endpoints is greater than maxCapacity or // capacityAnnotationSetCorrectly returns false if number of endpoints is greater than maxCapacity or

View File

@ -326,15 +326,16 @@ func (c *Controller) syncService(key string) error {
service, err := c.serviceLister.Services(namespace).Get(name) service, err := c.serviceLister.Services(namespace).Get(name)
if err != nil { if err != nil {
if apierrors.IsNotFound(err) { if !apierrors.IsNotFound(err) {
return err
}
c.triggerTimeTracker.DeleteService(namespace, name) c.triggerTimeTracker.DeleteService(namespace, name)
c.reconciler.deleteService(namespace, name) c.reconciler.deleteService(namespace, name)
c.endpointSliceTracker.DeleteService(namespace, name) c.endpointSliceTracker.DeleteService(namespace, name)
// The service has been deleted, return nil so that it won't be retried. // The service has been deleted, return nil so that it won't be retried.
return nil return nil
} }
return err
}
if service.Spec.Selector == nil { if service.Spec.Selector == nil {
// services without a selector receive no endpoint slices from this controller; // services without a selector receive no endpoint slices from this controller;

View File

@ -781,7 +781,10 @@ func TestPodOrphaningAndAdoptionWhenLabelsChange(t *testing.T) {
if err != nil { if err != nil {
// If the pod is not found, it means the RS picks the pod for deletion (it is extra) // If the pod is not found, it means the RS picks the pod for deletion (it is extra)
// Verify there is only one pod in namespace and it has ControllerRef to the RS // Verify there is only one pod in namespace and it has ControllerRef to the RS
if apierrors.IsNotFound(err) { if !apierrors.IsNotFound(err) {
return false, err
}
pods := getPods(t, podClient, labelMap()) pods := getPods(t, podClient, labelMap())
if len(pods.Items) != 1 { if len(pods.Items) != 1 {
return false, fmt.Errorf("Expected 1 pod in current namespace, got %d", len(pods.Items)) return false, fmt.Errorf("Expected 1 pod in current namespace, got %d", len(pods.Items))
@ -790,8 +793,6 @@ func TestPodOrphaningAndAdoptionWhenLabelsChange(t *testing.T) {
pod = &pods.Items[0] pod = &pods.Items[0]
return true, nil return true, nil
} }
return false, err
}
// Always update the pod so that we can save a GET call to API server later // Always update the pod so that we can save a GET call to API server later
pod = newPod pod = newPod
// If the pod is found, verify the pod has a ControllerRef // If the pod is found, verify the pod has a ControllerRef

View File

@ -682,7 +682,10 @@ func TestPodOrphaningAndAdoptionWhenLabelsChange(t *testing.T) {
if err != nil { if err != nil {
// If the pod is not found, it means the RC picks the pod for deletion (it is extra) // If the pod is not found, it means the RC picks the pod for deletion (it is extra)
// Verify there is only one pod in namespace and it has ControllerRef to the RC // Verify there is only one pod in namespace and it has ControllerRef to the RC
if apierrors.IsNotFound(err) { if !apierrors.IsNotFound(err) {
return false, err
}
pods := getPods(t, podClient, labelMap()) pods := getPods(t, podClient, labelMap())
if len(pods.Items) != 1 { if len(pods.Items) != 1 {
return false, fmt.Errorf("Expected 1 pod in current namespace, got %d", len(pods.Items)) return false, fmt.Errorf("Expected 1 pod in current namespace, got %d", len(pods.Items))
@ -691,8 +694,6 @@ func TestPodOrphaningAndAdoptionWhenLabelsChange(t *testing.T) {
pod = &pods.Items[0] pod = &pods.Items[0]
return true, nil return true, nil
} }
return false, err
}
// Always update the pod so that we can save a GET call to API server later // Always update the pod so that we can save a GET call to API server later
pod = newPod pod = newPod
// If the pod is found, verify the pod has a ControllerRef // If the pod is found, verify the pod has a ControllerRef