Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
*: enable tolerate unready in service
Browse files Browse the repository at this point in the history
also fix the port naming issue
  • Loading branch information
hongchaodeng committed Jul 13, 2017
1 parent a89a9e8 commit 5635478
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions pkg/util/k8sutil/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const (
operatorEtcdTLSVolume = "etcd-client-tls"
)

const TolerateUnreadyEndpointsAnnotation = "service.alpha.kubernetes.io/tolerate-unready-endpoints"

func GetEtcdVersion(pod *v1.Pod) string {
return pod.Annotations[etcdVersionAnnotationKey]
}
Expand Down Expand Up @@ -119,19 +121,37 @@ func PodWithNodeSelector(p *v1.Pod, ns map[string]string) *v1.Pod {
}

func CreateClientService(kubecli kubernetes.Interface, clusterName, ns string, owner metav1.OwnerReference) error {
return createService(kubecli, ClientServiceName(clusterName), clusterName, ns, "", 2379, owner)
ports := []v1.ServicePort{{
Name: "client",
Port: 2379,
TargetPort: intstr.FromInt(2379),
Protocol: v1.ProtocolTCP,
}}
return createService(kubecli, ClientServiceName(clusterName), clusterName, ns, "", ports, owner)
}

func ClientServiceName(clusterName string) string {
return clusterName + "-client"
}

func CreatePeerService(kubecli kubernetes.Interface, clusterName, ns string, owner metav1.OwnerReference) error {
return createService(kubecli, clusterName, clusterName, ns, v1.ClusterIPNone, 2380, owner)
ports := []v1.ServicePort{{
Name: "client",
Port: 2379,
TargetPort: intstr.FromInt(2379),
Protocol: v1.ProtocolTCP,
}, {
Name: "peer",
Port: 2380,
TargetPort: intstr.FromInt(2380),
Protocol: v1.ProtocolTCP,
}}

return createService(kubecli, clusterName, clusterName, ns, v1.ClusterIPNone, ports, owner)
}

func createService(kubecli kubernetes.Interface, svcName, clusterName, ns, clusterIP string, port int32, owner metav1.OwnerReference) error {
svc := newEtcdServiceManifest(svcName, clusterName, clusterIP, port)
func createService(kubecli kubernetes.Interface, svcName, clusterName, ns, clusterIP string, ports []v1.ServicePort, owner metav1.OwnerReference) error {
svc := newEtcdServiceManifest(svcName, clusterName, clusterIP, ports)
addOwnerRefToObject(svc.GetObjectMeta(), owner)
_, err := kubecli.CoreV1().Services(ns).Create(svc)
return err
Expand Down Expand Up @@ -172,7 +192,7 @@ func CreateAndWaitPod(kubecli kubernetes.Interface, ns string, pod *v1.Pod, time
return retPod, nil
}

func newEtcdServiceManifest(svcName, clusterName string, clusterIP string, port int32) *v1.Service {
func newEtcdServiceManifest(svcName, clusterName, clusterIP string, ports []v1.ServicePort) *v1.Service {
labels := map[string]string{
"app": "etcd",
"etcd_cluster": clusterName,
Expand All @@ -181,16 +201,12 @@ func newEtcdServiceManifest(svcName, clusterName string, clusterIP string, port
ObjectMeta: metav1.ObjectMeta{
Name: svcName,
Labels: labels,
Annotations: map[string]string{
TolerateUnreadyEndpointsAnnotation: "true",
},
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Name: "client",
Port: port,
TargetPort: intstr.FromInt(int(port)),
Protocol: v1.ProtocolTCP,
},
},
Ports: ports,
Selector: labels,
ClusterIP: clusterIP,
},
Expand Down

0 comments on commit 5635478

Please sign in to comment.