func serviceAvailable(service *corev1.Service) bool {
	if service == nil {
		return false
	}
	if len(service.Spec.ClusterIP) != 0 {
		log.Infoln("Cluster IP found:", service.Spec.ClusterIP)
		return true
	}
	return false
}
serviceAvailable checks the amount of ingress endpoints associated to the service.
This will return a true if there is at least 1 hostname endpoint.
package main
import (
	"context"
	"errors"
	"fmt"
	"strconv"
	"time"
	log "github.com/sirupsen/logrus"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/intstr"
)
func createServiceConfig(labels map[string]string) *corev1.Service {
	service := &corev1.Service{}
	log.Infoln("Creating service resource for", checkNamespace, "namespace.")
	ports := make([]corev1.ServicePort, 0)
	basicPort := corev1.ServicePort{
		Port: checkLoadBalancerPort,  
		TargetPort: intstr.IntOrString{  
			IntVal: checkContainerPort,
			StrVal: strconv.Itoa(int(checkContainerPort)),
		},
		Protocol: corev1.ProtocolTCP,
	}
	ports = append(ports, basicPort)
	serviceSpec := corev1.ServiceSpec{
		Type:     corev1.ServiceTypeClusterIP,
		Ports:    ports,
		Selector: labels,
	}
	service.Spec = serviceSpec
	service.Name = checkServiceName  
	service.Namespace = checkNamespace
	return service
}
type ServiceResult struct {
	Service *corev1.Service
	Err     error
}
func createService(ctx context.Context, serviceConfig *corev1.Service) chan ServiceResult {
	createChan := make(chan ServiceResult)
	go func() {
		log.Infoln("Creating service in cluster with name:", serviceConfig.Name)
		defer close(createChan)
		result := ServiceResult{}
		service, err := client.CoreV1().Services(checkNamespace).Create(ctx, serviceConfig, metav1.CreateOptions{})
		if err != nil {
			log.Infoln("Failed to create a service in the cluster:", err)
			result.Err = err
			createChan <- result
			return
		}
		if service == nil {
			err = errors.New("got a nil service result: ")
			log.Errorln("Failed to create a service in the cluster: %w", err)
			result.Err = err
			createChan <- result
		}
		for {
			log.Infoln("Watching for service to exist.")
			watch, err := client.CoreV1().Services(checkNamespace).Watch(ctx, metav1.ListOptions{
				Watch:         true,
				FieldSelector: "metadata.name=" + service.Name,
			})
			if err != nil {
				result.Err = err
				createChan <- result
				return
			}
			if watch == nil {
				continue
			}
			select {
			case event := <-watch.ResultChan():
				log.Debugln("Received an event watching for service changes:", event.Type)
				s, ok := event.Object.(*corev1.Service)
				if !ok {  
					log.Debugln("Got a watch event for a non-service object -- ignoring.")
					continue
				}
				if serviceAvailable(s) {
					result.Service = s
					createChan <- result
					return
				}
			case s := <-serviceHasClusterIP(ctx):
				log.Debugln("A cluster IP belonging to the created service has been found:")
				result.Service = s
				createChan <- result
				return
			case <-ctx.Done():
				log.Errorln("context expired while waiting for service to create.")
				err = cleanUp(ctx)
				if err != nil {
					result.Err = err
				}
				createChan <- result
				return
			}
			watch.Stop()
		}
	}()
	return createChan
}
func deleteServiceAndWait(ctx context.Context) error {
	deleteChan := make(chan error)
	go func() {
		defer close(deleteChan)
		log.Debugln("Checking if service has been deleted.")
		for {
			select {
			case <-ctx.Done():
				deleteChan <- fmt.Errorf("timed out while waiting for service to delete")
				return
			default:
				log.Debugln("Delete service and wait has not yet timed out.")
			}
			log.Debugln("Waiting 5 seconds before trying again.")
			time.Sleep(time.Second * 5)
			serviceList, err := client.CoreV1().Services(checkNamespace).List(ctx, metav1.ListOptions{
				FieldSelector: "metadata.name=" + checkServiceName,
			})
			if err != nil {
				log.Errorln("Error creating service listing client:", err.Error())
				continue
			}
			var serviceExists bool
			for _, svc := range serviceList.Items {
				if svc.GetName() == checkServiceName {
					serviceExists = true
					err = deleteService(ctx)
					if err != nil {
						log.Errorln("Error when running a delete on service", checkServiceName+":", err.Error())
					}
					break
				}
			}
			if !serviceExists {
				deleteChan <- nil
				break
			}
		}
	}()
	err := deleteService(ctx)
	if err != nil {
		log.Infoln("Could not delete service:", checkServiceName)
	}
	return <-deleteChan
}
func deleteService(ctx context.Context) error {
	log.Infoln("Attempting to delete service", checkServiceName, "in", checkNamespace, "namespace.")
	deletePolicy := metav1.DeletePropagationForeground
	graceSeconds := int64(1)
	deleteOpts := metav1.DeleteOptions{
		GracePeriodSeconds: &graceSeconds,
		PropagationPolicy:  &deletePolicy,
	}
	return client.CoreV1().Services(checkNamespace).Delete(ctx, checkServiceName, deleteOpts)
}
func findPreviousService(ctx context.Context) (bool, error) {
	log.Infoln("Attempting to find previously created service(s) belonging to this check.")
	serviceList, err := client.CoreV1().Services(checkNamespace).List(ctx, metav1.ListOptions{})
	if err != nil {
		log.Infoln("Error listing services:", err)
		return false, err
	}
	if serviceList == nil {
		log.Infoln("Received an empty list of services:", serviceList)
		return false, errors.New("received empty list of services")
	}
	log.Debugln("Found", len(serviceList.Items), "service(s).")
	if debug {  
		for _, svc := range serviceList.Items {
			log.Debugln("Service:", svc.Name)
		}
	}
	for _, svc := range serviceList.Items {
		if svc.Name == checkServiceName {
			log.Infoln("Found an old service belonging to this check:", svc.Name)
			return true, nil
		}
	}
	log.Infoln("Did not find any old service(s) belonging to this check.")
	return false, nil
}
func getServiceClusterIP(ctx context.Context) string {
	svc, err := client.CoreV1().Services(checkNamespace).Get(ctx, checkServiceName, metav1.GetOptions{})
	if err != nil {
		log.Errorln("Error occurred attempting to list service while retrieving service cluster IP:", err)
		return ""
	}
	if svc == nil {
		log.Errorln("Failed to get service, received a nil object:", svc)
		return ""
	}
	log.Debugln("Retrieving a cluster IP belonging to:", svc.Name)
	if len(svc.Spec.ClusterIP) != 0 {
		log.Infoln("Found service cluster IP address:", svc.Spec.ClusterIP)
		return svc.Spec.ClusterIP
	}
	return ""
}