func (c *Cluster) ensureService() error {
	e := c.etcd

	internalSvc, err := service.NewEtcdHeadlessService(e)
	if err != nil {
		return err
	}

	// if internal service not existed, create it
	if _, err := c.svcLister.Services(internalSvc.Namespace).Get(internalSvc.Name); err != nil && k8serr.IsNotFound(err) {
		if _, err := c.kubeClient.CoreV1().Services(internalSvc.Namespace).Create(context.TODO(), internalSvc, metav1.CreateOptions{}); err != nil {
			return err
		}
		c.recorder.Eventf(e, corev1.EventTypeNormal, "HeadlessServiceCreated", "headless service for etcdcluster has been created")
	} else if err != nil {
		return err
	}

	externalSvc, err := service.NewEtcdAccessService(e)
	if err != nil {
		return err
	}
	if svc, err := c.svcLister.Services(externalSvc.Namespace).Get(externalSvc.Name); err != nil && k8serr.IsNotFound(err) {
		if _, err := c.kubeClient.CoreV1().Services(externalSvc.Namespace).Create(context.TODO(), externalSvc, metav1.CreateOptions{}); err != nil {
			return err
		}
		c.recorder.Eventf(e, corev1.EventTypeNormal, "AccessServiceCreated", "access service for etcdcluster has been created")
	} else if err != nil {
		return err
	} else {
		updated, err := service.GetUpdateAccessService(e, svc)
		if err != nil {
			return err
		}
		if !reflect.DeepEqual(updated, svc) {
			if _, err := c.kubeClient.CoreV1().Services(updated.Namespace).Update(context.TODO(), updated, metav1.UpdateOptions{}); err != nil {
				return err
			}
			c.recorder.Eventf(e, corev1.EventTypeNormal, "AccessServiceUpdated", "access service for etcdcluster has been updated")
		}
	}

	return c.ensureLoadBalancerStatus()
}
// ensureService create a headless service for internal communication if not exist,
// create a ClusterIP or LoadBalancer type service for external access if not exist.
// using annotation for LoadBalancer provider
ensureService create a headless service for internal communication if not exist,
create a ClusterIP or LoadBalancer type service for external access if not exist.
using annotation for LoadBalancer provider
// ensureService create a headless service for internal communication if not exist // create a ClusterIP or LoadBalancer type service for external access if not exist . // using annotation for LoadBalancer provider
func (c *Cluster) ensureService() error {
	e := c.etcd
	internalSvc, err := service.NewEtcdHeadlessService(e)
	if err != nil {
		return err
	}
	if _, err := c.svcLister.Services(internalSvc.Namespace).Get(internalSvc.Name); err != nil && k8serr.IsNotFound(err) {
		if _, err := c.kubeClient.CoreV1().Services(internalSvc.Namespace).Create(context.TODO(), internalSvc, metav1.CreateOptions{}); err != nil {
			return err
		}
		c.recorder.Eventf(e, corev1.EventTypeNormal, "HeadlessServiceCreated", "headless service for etcdcluster has been created")
	} else if err != nil {
		return err
	}
	externalSvc, err := service.NewEtcdAccessService(e)
	if err != nil {
		return err
	}
	if svc, err := c.svcLister.Services(externalSvc.Namespace).Get(externalSvc.Name); err != nil && k8serr.IsNotFound(err) {
		if _, err := c.kubeClient.CoreV1().Services(externalSvc.Namespace).Create(context.TODO(), externalSvc, metav1.CreateOptions{}); err != nil {
			return err
		}
		c.recorder.Eventf(e, corev1.EventTypeNormal, "AccessServiceCreated", "access service for etcdcluster has been created")
	} else if err != nil {
		return err
	} else {
		updated, err := service.GetUpdateAccessService(e, svc)
		if err != nil {
			return err
		}
		if !reflect.DeepEqual(updated, svc) {
			if _, err := c.kubeClient.CoreV1().Services(updated.Namespace).Update(context.TODO(), updated, metav1.UpdateOptions{}); err != nil {
				return err
			}
			c.recorder.Eventf(e, corev1.EventTypeNormal, "AccessServiceUpdated", "access service for etcdcluster has been updated")
		}
	}
	return c.ensureLoadBalancerStatus()
}
func ( c * Cluster ) ensureService ( ) error { e := c . etcd internalSvc , err := service . NewEtcdHeadlessService ( e ) if err != nil { return err } if _ , err := c . svcLister . Services ( internalSvc . Namespace ) . Get ( internalSvc . Name ) ; err != nil && k8serr . IsNotFound ( err ) { if _ , err := c . kubeClient . CoreV1 ( ) . Services ( internalSvc . Namespace ) . Create ( context . TODO ( ) , internalSvc , metav1 . CreateOptions { } ) ; err != nil { return err } c . recorder . Eventf ( e , corev1 . EventTypeNormal , " " , " " ) } else if err != nil { return err } externalSvc , err := service . NewEtcdAccessService ( e ) if err != nil { return err } if svc , err := c . svcLister . Services ( externalSvc . Namespace ) . Get ( externalSvc . Name ) ; err != nil && k8serr . IsNotFound ( err ) { if _ , err := c . kubeClient . CoreV1 ( ) . Services ( externalSvc . Namespace ) . Create ( context . TODO ( ) , externalSvc , metav1 . CreateOptions { } ) ; err != nil { return err } c . recorder . Eventf ( e , corev1 . EventTypeNormal , " " , " " ) } else if err != nil { return err } else { updated , err := service . GetUpdateAccessService ( e , svc ) if err != nil { return err } if ! reflect . DeepEqual ( updated , svc ) { if _ , err := c . kubeClient . CoreV1 ( ) . Services ( updated . Namespace ) . Update ( context . TODO ( ) , updated , metav1 . UpdateOptions { } ) ; err != nil { return err } c . recorder . Eventf ( e , corev1 . EventTypeNormal , " " , " " ) } } return c . ensureLoadBalancerStatus ( ) }
ensureService create a headless service for internal communication if not exist,
create a ClusterIP or LoadBalancer type service for external access if not exist.
ensureService create a headless service for internal communication if not exist create a ClusterIP or LoadBalancer type service for external access if not exist .
// if internal service not existed, create it