|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "log" |
| 7 | + "syscall" |
| 8 | + "fmt" |
| 9 | + "time" |
| 10 | + "net/http" |
| 11 | + "os/signal" |
| 12 | + le "k8s.io/client-go/tools/leaderelection" |
| 13 | + rl "k8s.io/client-go/tools/leaderelection/resourcelock" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + "k8s.io/client-go/kubernetes" |
| 16 | + discoveryv1client "k8s.io/client-go/kubernetes/typed/discovery/v1" |
| 17 | + discoveryv1app "k8s.io/client-go/applyconfigurations/discovery/v1" |
| 18 | + discoveryv1 "k8s.io/api/discovery/v1" |
| 19 | + corev1app "k8s.io/client-go/applyconfigurations/core/v1" |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | + "k8s.io/client-go/rest" |
| 23 | +) |
| 24 | + |
| 25 | +type Callbacks struct { |
| 26 | + PodName string |
| 27 | + PodUID string |
| 28 | + PodIP string |
| 29 | + NodeName string |
| 30 | + ServiceName string |
| 31 | + Namespace string |
| 32 | + EndpointSlices discoveryv1client.EndpointSliceInterface |
| 33 | +} |
| 34 | + |
| 35 | +func (c *Callbacks) OnStartedLeading(ctx context.Context) { |
| 36 | + log.Println(fmt.Sprintf("Started leading")) |
| 37 | + apply := discoveryv1app.EndpointSlice(c.ServiceName, c.Namespace). |
| 38 | + WithLabels(map[string]string{"kubernetes.io/service-name": c.ServiceName}). |
| 39 | + WithAddressType(discoveryv1.AddressTypeIPv4). |
| 40 | + WithPorts(discoveryv1app.EndpointPort(). |
| 41 | + WithName("ldaps"). |
| 42 | + WithProtocol(corev1.ProtocolTCP). |
| 43 | + WithPort(636)). |
| 44 | + WithEndpoints(discoveryv1app.Endpoint(). |
| 45 | + WithAddresses(c.PodIP). |
| 46 | + WithNodeName(c.NodeName). |
| 47 | + WithConditions(discoveryv1app.EndpointConditions(). |
| 48 | + WithReady(true). |
| 49 | + WithServing(true). |
| 50 | + WithTerminating(false)). |
| 51 | + WithTargetRef(corev1app.ObjectReference(). |
| 52 | + WithKind("Pod"). |
| 53 | + WithName(c.PodName). |
| 54 | + WithNamespace(c.Namespace). |
| 55 | + WithUID(types.UID(c.PodUID)))) |
| 56 | + ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
| 57 | + defer cancel() |
| 58 | + c.EndpointSlices.Apply(ctx, apply, metav1.ApplyOptions{FieldManager: "openldap-leader"}) |
| 59 | + log.Println(fmt.Sprintf("Updated EndpointSlice")) |
| 60 | +} |
| 61 | + |
| 62 | +func (c *Callbacks) OnStoppedLeading() { |
| 63 | + log.Println(fmt.Sprintf("Stopped leading")) |
| 64 | +} |
| 65 | + |
| 66 | +func (c *Callbacks) OnNewLeader(identity string) { |
| 67 | + log.Println(fmt.Sprintf("New leader: %s", identity)) |
| 68 | +} |
| 69 | + |
| 70 | +type Handler func(http.ResponseWriter, *http.Request) |
| 71 | + |
| 72 | +func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 73 | + h(w, req) |
| 74 | +} |
| 75 | + |
| 76 | +func main() { |
| 77 | + podName := os.Getenv("POD_NAME") |
| 78 | + podUID := os.Getenv("POD_UID") |
| 79 | + podIP := os.Getenv("POD_IP") |
| 80 | + namespace := os.Getenv("NAMESPACE") |
| 81 | + nodeName := os.Getenv("NODE_NAME") |
| 82 | + serviceName := os.Getenv("SERVICE_NAME") |
| 83 | + |
| 84 | + lockType := rl.LeasesResourceLock |
| 85 | + config, err := rest.InClusterConfig() |
| 86 | + if err != nil { |
| 87 | + panic(err) |
| 88 | + } |
| 89 | + client := kubernetes.NewForConfigOrDie(config) |
| 90 | + lock, err := rl.New(lockType, namespace, serviceName, client.CoreV1(), client.CoordinationV1(), rl.ResourceLockConfig{ |
| 91 | + Identity: podName, |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + panic(err) |
| 95 | + } |
| 96 | + |
| 97 | + callbacks := Callbacks{ |
| 98 | + PodName: podName, |
| 99 | + PodUID: podUID, |
| 100 | + PodIP: podIP, |
| 101 | + NodeName: nodeName, |
| 102 | + ServiceName: serviceName, |
| 103 | + Namespace: namespace, |
| 104 | + EndpointSlices: client.DiscoveryV1().EndpointSlices(namespace), |
| 105 | + } |
| 106 | + |
| 107 | + elector, err := le.NewLeaderElector(le.LeaderElectionConfig{ |
| 108 | + Lock: lock, |
| 109 | + LeaseDuration: 15 * time.Second, |
| 110 | + RenewDeadline: 10 * time.Second, |
| 111 | + RetryPeriod: 2 * time.Second, |
| 112 | + ReleaseOnCancel: true, |
| 113 | + Callbacks: le.LeaderCallbacks{ |
| 114 | + OnStartedLeading: callbacks.OnStartedLeading, |
| 115 | + OnStoppedLeading: callbacks.OnStoppedLeading, |
| 116 | + OnNewLeader: callbacks.OnNewLeader, |
| 117 | + }, |
| 118 | + }) |
| 119 | + if err != nil { |
| 120 | + panic(err) |
| 121 | + } |
| 122 | + |
| 123 | + ctx, cancel := context.WithCancel(context.Background()) |
| 124 | + defer cancel() |
| 125 | + |
| 126 | + canceled := false |
| 127 | + |
| 128 | + go func() { |
| 129 | + for !canceled { |
| 130 | + elector.Run(ctx) |
| 131 | + } |
| 132 | + }() |
| 133 | + |
| 134 | + sigint := make(chan os.Signal, 1) |
| 135 | + signal.Notify(sigint, os.Interrupt) |
| 136 | + sigkill := make(chan os.Signal, 1) |
| 137 | + signal.Notify(sigkill, os.Kill) |
| 138 | + sigterm := make(chan os.Signal, 1) |
| 139 | + signal.Notify(sigterm, syscall.SIGTERM) |
| 140 | + |
| 141 | + done := make(chan os.Signal, 1) |
| 142 | + |
| 143 | + go func() { |
| 144 | + http.ListenAndServe("0.0.0.0:80", Handler(func(w http.ResponseWriter, req *http.Request) { |
| 145 | + log.Println("Stopping leader election", req) |
| 146 | + close(done) |
| 147 | + w.WriteHeader(http.StatusNoContent) |
| 148 | + _, _ = w.Write([]byte{}) |
| 149 | + })) |
| 150 | + }() |
| 151 | + |
| 152 | + defer func() { log.Println("Shutting down") }() |
| 153 | + |
| 154 | + select { |
| 155 | + case <-sigint: |
| 156 | + return |
| 157 | + case <-sigkill: |
| 158 | + return |
| 159 | + case <-sigterm: |
| 160 | + return |
| 161 | + case <-done: |
| 162 | + canceled = true |
| 163 | + cancel() |
| 164 | + } |
| 165 | + |
| 166 | + for { |
| 167 | + select { |
| 168 | + case <-sigint: |
| 169 | + return |
| 170 | + case <-sigkill: |
| 171 | + return |
| 172 | + case <-sigterm: |
| 173 | + return |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments