Skip to content

Commit

Permalink
CR improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
programmer04 committed May 10, 2023
1 parent 88b9530 commit 37a0021
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions pkg/utils/kubernetes/networking/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func WaitForServiceLoadBalancerAddress(ctx context.Context, c kubernetes.Interfa
// type Service to allow connections to the Service and port from outside the cluster while
// the connection attempts are made using the LoadBalancer public address.
func WaitForConnectionOnServicePort(ctx context.Context, c kubernetes.Interface, namespace, name string, port int, dialTimeout time.Duration) error {
service, err := c.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{})
svcClient := c.CoreV1().Services(namespace)
service, err := svcClient.Get(ctx, name, metav1.GetOptions{})
if err != nil {
return err
}
Expand All @@ -83,7 +84,8 @@ func WaitForConnectionOnServicePort(ctx context.Context, c kubernetes.Interface,
// the endpoints of the target server is manually created, but not chosen from pods by labels in selector.
// so we need to manually create the same endpoints as the target service has here.
if len(service.Spec.Selector) == 0 {
endpointSlices, err := c.DiscoveryV1().EndpointSlices(namespace).List(
epsClient := c.DiscoveryV1().EndpointSlices(namespace)
endpointSlices, err := epsClient.List(
ctx, metav1.ListOptions{LabelSelector: discoveryv1.LabelServiceName + "=" + name},
)
if err != nil {
Expand All @@ -93,39 +95,35 @@ func WaitForConnectionOnServicePort(ctx context.Context, c kubernetes.Interface,
// Recreate EndpointSlices for the lb service with proper metadata.
tempEndpointSlices := endpointSlices.DeepCopy().Items
for i := range tempEndpointSlices {
epsName := fmt.Sprintf("%s-%d", lbServiceName, i)
tempEndpointSlices[i].ObjectMeta = metav1.ObjectMeta{
Namespace: namespace,
Name: fmt.Sprintf("%s-%d", lbServiceName, i),
Name: epsName,
Labels: map[string]string{
discoveryv1.LabelServiceName: lbServiceName, // Maps EndpointSlice to Service.
correspondingSvcNameLabel: name,
},
}
_, err = c.DiscoveryV1().EndpointSlices(namespace).Create(ctx, &tempEndpointSlices[i], metav1.CreateOptions{})
if err != nil {
if _, err = epsClient.Create(ctx, &tempEndpointSlices[i], metav1.CreateOptions{}); err != nil {
return err
}
}

defer func() {
for _, eps := range tempEndpointSlices {
err := c.DiscoveryV1().EndpointSlices(namespace).Delete(ctx, eps.Name, metav1.DeleteOptions{})
// For each successfully created temporary EndpointSlice ensure deletion on return from the function.
defer func(epsName string) {
err := epsClient.Delete(ctx, epsName, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
fmt.Printf("failed to delete endpoints %s/%s after testing, error %v\n",
namespace, eps.Name, err,
fmt.Printf("failed to delete EndpointSlice %s/%s after testing, error %v\n",
namespace, epsName, err,
)
}
}
}()
}(epsName)
}
}

_, err = c.CoreV1().Services(namespace).Create(ctx, tempLoadBalancer, metav1.CreateOptions{})
if err != nil {
if _, err = svcClient.Create(ctx, tempLoadBalancer, metav1.CreateOptions{}); err != nil {
return err
}

defer func() {
err := c.CoreV1().Services(namespace).Delete(ctx, lbServiceName, metav1.DeleteOptions{})
err := svcClient.Delete(ctx, lbServiceName, metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
fmt.Printf("failed to delete service %s/%s after testing, error %v\n",
namespace, lbServiceName, err)
Expand All @@ -145,8 +143,7 @@ func WaitForConnectionOnServicePort(ctx context.Context, c kubernetes.Interface,
return fmt.Errorf("context completed while waiting for %s:%d to be connected", ip, port)
case <-ticker.C:
dialer := &net.Dialer{Timeout: dialTimeout}
_, err := dialer.Dial("tcp", address)
if err == nil {
if _, err := dialer.Dial("tcp", address); err == nil {
return nil
}
}
Expand Down

0 comments on commit 37a0021

Please sign in to comment.