├── .dockerignore ├── .github └── workflows │ └── release.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── Makefile ├── README.md ├── examples ├── httpbin-example.yaml └── singleton-service.yml ├── go.mod ├── go.sum ├── main.go └── pkg └── consts └── consts.go /.dockerignore: -------------------------------------------------------------------------------- 1 | out/ 2 | Dockerfile 3 | README.md -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: create release binary 2 | on: 3 | release: 4 | types: [created] 5 | jobs: 6 | artifacts: 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | goarch: 11 | - amd64 12 | - s390x 13 | - arm 14 | - arm64 15 | - ppc64le 16 | steps: 17 | - uses: actions/checkout@v4 18 | - name: compile and release 19 | uses: wangyoucao577/go-release-action@v1 20 | with: 21 | github_token: ${{ secrets.GITHUB_TOKEN }} 22 | goarch: ${{ matrix.goarch }} 23 | goos: linux 24 | ldflags: > 25 | -extldflags -static 26 | -X "main.Version=${{ github.ref }}" 27 | -w 28 | -s 29 | md5sum: "FALSE" 30 | sha256sum: "TRUE" 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/go 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=go 4 | 5 | ### Go ### 6 | # Binaries for programs and plugins 7 | *.exe 8 | *.exe~ 9 | *.dll 10 | *.so 11 | *.dylib 12 | 13 | # Test binary, built with `go test -c` 14 | *.test 15 | 16 | # Output of the go coverage tool, specifically when used with LiteIDE 17 | *.out 18 | 19 | # Dependency directories (remove the comment below to include it) 20 | # vendor/ 21 | 22 | ### Go Patch ### 23 | /vendor/ 24 | /Godeps/ 25 | 26 | # End of https://www.toptal.com/developers/gitignore/api/go 27 | 28 | /out/ 29 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [Unreleased] 8 | 9 | ## [v0.4.1] 2023-02-14 10 | 11 | ### Fixed 12 | * Fixed release action version 13 | 14 | ## [v0.4.0] 2023-02-14 15 | 16 | ### Added 17 | * Directly `execve()` the child process if not running leader election. 18 | 19 | ### Changed 20 | * Update deps to kubernetes 1.29. 21 | * Use golang 1.21 to build. 22 | 23 | ## [v0.3.1] 2021-08-20 24 | 25 | ### Changed 26 | * Changed download url for golang in github actions 27 | 28 | ## [v0.3.0] 2021-08-20 29 | 30 | ### Added 31 | * Set leader election timeouts via environment variables (see [the readme](./README.md) for their use). 32 | - `K8S_AWAIT_ELECTION_LEASE_DURATION` 33 | - `K8S_AWAIT_ELECTION_RENEW_DEADLINE` 34 | - `K8S_AWAIT_ELECTION_RETRY_PERIOD` 35 | 36 | ### Changed 37 | * Use golang 1.17 to build release. 38 | * Update dependencies to kubernetes 1.22. 39 | * Use sha256sum instead of md5 hashes 40 | 41 | ## [v0.2.4] 2021-04-30 42 | 43 | ### Added 44 | 45 | * Builds for `arm`, `arm64`, `ppc64le` 46 | 47 | ## [v0.2.3] 2021-01-20 48 | 49 | ### Added 50 | * Set `nodeName` on endpoints if `K8S_AWAIT_ELECTION_NODE_NAME` is set. This enables Kubernetes to route traffic 51 | from the leader Pod to itself via the service. 52 | 53 | ## [v0.2.2] 2021-01-12 54 | 55 | ### Changed 56 | * Fixed golang version download for releases 57 | 58 | ## [v0.2.1] 2021-01-12 59 | 60 | ### Changed 61 | * Used golang 1.15 to build releases 62 | 63 | ## [v0.2.0] 2020-08-20 64 | 65 | ### Added 66 | * Ability to update service endpoints based on current leader 67 | 68 | ### Changed 69 | * health endpoint now reports error on expired lease 70 | 71 | ## [v0.1.0] 2020-07-29 72 | 73 | ### Added 74 | * Running in elected mode in k8s cluster 75 | * Running without election outside of k8s cluster 76 | * Basic error handling 77 | 78 | [Unreleased]: https://github.com/LINBIT/k8s-await-election/compare/v0.4.1...HEAD 79 | [v0.4.1]: https://github.com/LINBIT/k8s-await-election/compare/v0.4.0...v0.4.1 80 | [v0.4.0]: https://github.com/LINBIT/k8s-await-election/compare/v0.3.1...v0.4.0 81 | [v0.3.1]: https://github.com/LINBIT/k8s-await-election/compare/v0.3.0...v0.3.1 82 | [v0.3.0]: https://github.com/LINBIT/k8s-await-election/compare/v0.2.4...v0.3.0 83 | [v0.2.4]: https://github.com/LINBIT/k8s-await-election/compare/v0.2.3...v0.2.4 84 | [v0.2.3]: https://github.com/LINBIT/k8s-await-election/compare/v0.2.2...v0.2.3 85 | [v0.2.2]: https://github.com/LINBIT/k8s-await-election/compare/v0.2.1...v0.2.2 86 | [v0.2.1]: https://github.com/LINBIT/k8s-await-election/compare/v0.2.0...v0.2.1 87 | [v0.2.0]: https://github.com/LINBIT/k8s-await-election/compare/v0.1.0...v0.2.0 88 | [v0.1.0]: https://github.com/LINBIT/k8s-await-election/commits/v0.1.0 89 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | GOOS ?= $(shell go env GOOS) 2 | GOARCH ?= $(shell go env GOARCH) 3 | GO_FILES := $(shell find . -type f -name '*.go' -or -name "go.*") 4 | VERSION ?= $(shell git describe --dirty --always --tags --match "v*.*") 5 | GO_LDFLAGS := '-extldflags "-static" -w -s -X "main.Version=$(VERSION)"' 6 | 7 | out/k8s-await-election-$(GOARCH): $(GO_FILES) 8 | GOARCH=$(GOARCH) GOOS=linux CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -o $@ 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # k8s-await-election 2 | 3 | ![Latest release](https://img.shields.io/github/v/release/linbit/k8s-await-election) 4 | 5 | Ensure that only a single instance of a process is running in your Kubernetes cluster. 6 | 7 | `k8s-await-election` leverages Kubernetes built-in [leader election](https://pkg.go.dev/k8s.io/client-go/tools/leaderelection?tab=doc) 8 | capabilities to coordinate commands running in different pods. It acts as a gatekeeper, only 9 | starting the command when the pod becomes a leader. 10 | 11 | ### Why leader election? 12 | Some applications aren't natively able to run in a replicated way. 13 | For example, they maintain some internal state which is only synchronized at the start. 14 | 15 | Running such an application as a single replica is not ideal. Should the node the pod 16 | is running on go offline unexpectedly, Kubernetes will take its time to reschedule 17 | (5min+ by default). 18 | 19 | Leader election is able to elect a new leader in about 10-15 seconds in such an event. 20 | 21 | ## Usage 22 | Just set `k8s-await-election` as entry point into your image. Configuration of the leader election 23 | happens via environment variables. If no environment variables were passed, `k8s-await-election` 24 | will just start the command without waiting to become elected. 25 | 26 | The relevant environment variables are 27 | 28 | | Variable | Description | 29 | |-----------------------------------------|-------------------------------------------------------------------| 30 | | `K8S_AWAIT_ELECTION_ENABLED` | Set to any non-empty value to enable leader election | 31 | | `K8S_AWAIT_ELECTION_NAME` | Name of the election processes. Useful for debugging | 32 | | `K8S_AWAIT_ELECTION_LOCK_NAME` | Name of the `leases.coordination.k8s.io` resource | 33 | | `K8S_AWAIT_ELECTION_LOCK_NAMESPACE` | Namespace of the `leases.coordination.k8s.io` resource | 34 | | `K8S_AWAIT_ELECTION_IDENTITY` | Unique identity for each member of the election process | 35 | | `K8S_AWAIT_ELECTION_STATUS_ENDPOINT` | Optional: endpoint to report if the election process is running | 36 | | `K8S_AWAIT_ELECTION_SERVICE_NAME` | Optional: set the service to update. [On Service Updates] | 37 | | `K8S_AWAIT_ELECTION_SERVICE_NAMESPACE` | Optional: set the service namespace. | 38 | | `K8S_AWAIT_ELECTION_SERVICE_PORTS_JSON` | Optional: set to json array of endpoint ports. | 39 | | `K8S_AWAIT_ELECTION_POD_IP` | Optional: IP of the pod, which will be used to update the service | 40 | | `K8S_AWAIT_ELECTION_NODE_NAME` | Optional: Node name, will be used to update the service | 41 | | `K8S_AWAIT_ELECTION_LEASE_DURATION` | Optional: Time in seconds after which followers will start elections, if a leader did not renew it's claim. Default: 15| 42 | | `K8S_AWAIT_ELECTION_RENEW_DEADLINE` | Optional: Time in seconds after which leaders will renew claims. Default: 10 | 43 | | `K8S_AWAIT_ELECTION_RETRY_PERIOD` | Optional: Time in seconds after which failed claims are retried. Default: 2 | 44 | 45 | [On Service Updates]: #service-updates 46 | 47 | Most of the time you will want to use this process in a Deployment spec or similar context. Here is 48 | an example: 49 | 50 | ```yaml 51 | apiVersion: apps/v1 52 | kind: Deployment 53 | metadata: 54 | name: my-singleton-with-replicas 55 | spec: 56 | replicas: 5 57 | selector: 58 | matchLabels: 59 | app: my-singleton-server 60 | template: 61 | metadata: 62 | labels: 63 | app: my-singleton-server 64 | spec: 65 | containers: 66 | - name: my-singleton-server 67 | args: 68 | - my-singleton-server 69 | env: 70 | - name: K8S_AWAIT_ELECTION_ENABLED 71 | value: "1" 72 | - name: K8S_AWAIT_ELECTION_NAME 73 | value: linstor-controller 74 | - name: K8S_AWAIT_ELECTION_LOCK_NAME 75 | value: piraeus-op-cs 76 | - name: K8S_AWAIT_ELECTION_LOCK_NAMESPACE 77 | value: default 78 | - name: K8S_AWAIT_ELECTION_IDENTITY 79 | valueFrom: 80 | fieldRef: 81 | apiVersion: v1 82 | fieldPath: metadata.name 83 | - name: K8S_AWAIT_ELECTION_STATUS_ENDPOINT 84 | value: :9999 85 | ``` 86 | 87 | ### Service Updates 88 | 89 | `k8s-await-election` can also be used to select which pod should receive traffic for a service. 90 | This is done by updating the endpoint resource associated with a service whenever a new leader is elected. 91 | This leader will be the only pod receiving traffic via the service. 92 | To enable this feature, set the `K8S_AWAIT_ELECTION_SERVICE_*` variables. 93 | See [the full example](./examples/singleton-service.yml) 94 | 95 | #### Why update service endpoints? 96 | 97 | For deployments that provide some kind of external API (for example a REST API), we would 98 | also like to automatically re-route traffic to the current leader. 99 | 100 | This is normally done via the readiness state of the pod: only ready pods associated with 101 | a service receive traffic. Because we only start the application if we are elected, only 102 | one pod is ever "ready" in the sense that it should receive traffic. 103 | 104 | This means that if we wanted to use the automatic service configuration via selectors, we 105 | run into some issues. The "ready" state of a pod has a kind of dual use. Consider a rolling upgrade of a deployment: 106 | 107 | 1. A new pod starts. 108 | 2. It won't become leader as the old one is still running 109 | 3. Since the app starts, it is never "ready" to receive traffic 110 | 4. the deployment controller sees the pod is not ready, and does not continue with upgrading 111 | 112 | `k8s-await-election` has all the information it needs to tell Kubernetes which pod should receive 113 | traffic. This works around the above issue, at the cost of a non-usable readiness probe. 114 | -------------------------------------------------------------------------------- /examples/httpbin-example.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: ServiceAccount 4 | metadata: 5 | name: k8s-await 6 | --- 7 | apiVersion: rbac.authorization.k8s.io/v1 8 | kind: Role 9 | metadata: 10 | name: get-leases 11 | rules: 12 | - apiGroups: [ "" ] 13 | resources: [ "endpoints" ] 14 | verbs: [ "get", "watch", "list", "create", "update" ] 15 | - apiGroups: [ "coordination.k8s.io" ] 16 | resources: [ "leases" ] 17 | verbs: [ "get", "watch", "list", "create", "update" ] 18 | --- 19 | apiVersion: rbac.authorization.k8s.io/v1 20 | kind: RoleBinding 21 | metadata: 22 | name: get-leases 23 | subjects: 24 | - kind: ServiceAccount 25 | name: k8s-await 26 | roleRef: 27 | apiGroup: rbac.authorization.k8s.io 28 | kind: Role 29 | name: get-leases 30 | --- 31 | apiVersion: v1 32 | kind: Service 33 | metadata: 34 | name: my-service 35 | spec: 36 | clusterIP: "" 37 | ports: 38 | - name: http 39 | port: 80 40 | protocol: TCP 41 | # NOTE: No selector here! A selector would automatically add all matching and ready pods to the endpoint 42 | --- 43 | apiVersion: apps/v1 44 | kind: Deployment 45 | metadata: 46 | name: my-server-with-replicas 47 | spec: 48 | replicas: 2 49 | selector: 50 | matchLabels: 51 | app: httpbin 52 | template: 53 | metadata: 54 | labels: 55 | app: httpbin 56 | spec: 57 | serviceAccountName: k8s-await 58 | volumes: 59 | - name: shared-binary 60 | emptyDir: {} 61 | initContainers: 62 | - name: get-binary 63 | image: alpine 64 | command: 65 | - '/bin/sh' 66 | - '-c' 67 | - 'wget https://github.com/LINBIT/k8s-await-election/releases/download/v0.2.3/k8s-await-election-v0.2.3-linux-amd64.tar.gz -O - | tar -xz' 68 | workingDir: /tmp/utils 69 | volumeMounts: 70 | - name: shared-binary 71 | mountPath: /tmp/utils 72 | containers: 73 | - name: httpbin 74 | image: kennethreitz/httpbin 75 | ports: 76 | - containerPort: 80 77 | name: http 78 | command: 79 | - /tmp/utils/k8s-await-election 80 | args: [ "gunicorn", "-b", "0.0.0.0:80", "httpbin:app", "-k", "gevent" ] 81 | env: 82 | - name: K8S_AWAIT_ELECTION_ENABLED 83 | value: "1" 84 | - name: K8S_AWAIT_ELECTION_NAME 85 | value: my-server 86 | - name: K8S_AWAIT_ELECTION_LOCK_NAME 87 | value: my-server 88 | - name: K8S_AWAIT_ELECTION_LOCK_NAMESPACE 89 | value: default 90 | - name: K8S_AWAIT_ELECTION_IDENTITY 91 | valueFrom: 92 | fieldRef: 93 | fieldPath: metadata.name 94 | - name: K8S_AWAIT_ELECTION_STATUS_ENDPOINT 95 | value: :9999 96 | - name: K8S_AWAIT_ELECTION_SERVICE_NAME 97 | value: my-service 98 | - name: K8S_AWAIT_ELECTION_SERVICE_NAMESPACE 99 | value: default 100 | - name: K8S_AWAIT_ELECTION_SERVICE_PORTS_JSON 101 | value: '[{"name":"http","port":80}]' 102 | - name: K8S_AWAIT_ELECTION_POD_IP 103 | valueFrom: 104 | fieldRef: 105 | fieldPath: status.podIP 106 | - name: K8S_AWAIT_ELECTION_NODE_NAME 107 | valueFrom: 108 | fieldRef: 109 | fieldPath: spec.nodeName 110 | volumeMounts: 111 | - name: shared-binary 112 | mountPath: /tmp/utils 113 | --- 114 | -------------------------------------------------------------------------------- /examples/singleton-service.yml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: Service 4 | metadata: 5 | name: my-service 6 | spec: 7 | clusterIP: "" 8 | ports: 9 | - name: my-port 10 | port: 1024 11 | protocol: TCP 12 | # NOTE: No selector here! A selector would automatically add all matching and ready pods to the endpoint 13 | --- 14 | apiVersion: apps/v1 15 | kind: Deployment 16 | metadata: 17 | name: my-server-with-replicas 18 | spec: 19 | replicas: 5 20 | selector: 21 | matchLabels: 22 | app: my-server 23 | template: 24 | metadata: 25 | labels: 26 | app: my-server 27 | spec: 28 | containers: 29 | - name: my-server 30 | image: my-server 31 | args: 32 | - my-singleton-server 33 | ports: 34 | - containerPort: 1024 35 | name: my-port 36 | env: 37 | - name: K8S_AWAIT_ELECTION_ENABLED 38 | value: "1" 39 | - name: K8S_AWAIT_ELECTION_NAME 40 | value: my-server 41 | - name: K8S_AWAIT_ELECTION_LOCK_NAME 42 | value: my-server 43 | - name: K8S_AWAIT_ELECTION_LOCK_NAMESPACE 44 | value: default 45 | - name: K8S_AWAIT_ELECTION_IDENTITY 46 | valueFrom: 47 | fieldRef: 48 | fieldPath: metadata.name 49 | - name: K8S_AWAIT_ELECTION_STATUS_ENDPOINT 50 | value: :9999 51 | - name: K8S_AWAIT_ELECTION_SERVICE_NAME 52 | value: my-service 53 | - name: K8S_AWAIT_ELECTION_SERVICE_NAMESPACE 54 | value: default 55 | - name: K8S_AWAIT_ELECTION_SERVICE_PORTS_JSON 56 | value: '[{"name":"my-port","port":1024}]' 57 | - name: K8S_AWAIT_ELECTION_POD_IP 58 | valueFrom: 59 | fieldRef: 60 | fieldPath: status.podIP 61 | - name: K8S_AWAIT_ELECTION_NODE_NAME 62 | valueFrom: 63 | fieldRef: 64 | fieldPath: spec.nodeName 65 | --- 66 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/linbit/k8s-await-election 2 | 3 | go 1.21 4 | 5 | toolchain go1.21.7 6 | 7 | require ( 8 | github.com/sirupsen/logrus v1.9.3 9 | golang.org/x/sys v0.17.0 10 | k8s.io/api v0.29.1 11 | k8s.io/apimachinery v0.29.1 12 | k8s.io/client-go v0.29.1 13 | ) 14 | 15 | require ( 16 | github.com/davecgh/go-spew v1.1.1 // indirect 17 | github.com/emicklei/go-restful/v3 v3.11.2 // indirect 18 | github.com/go-logr/logr v1.4.1 // indirect 19 | github.com/go-openapi/jsonpointer v0.20.2 // indirect 20 | github.com/go-openapi/jsonreference v0.20.4 // indirect 21 | github.com/go-openapi/swag v0.22.9 // indirect 22 | github.com/gogo/protobuf v1.3.2 // indirect 23 | github.com/golang/protobuf v1.5.3 // indirect 24 | github.com/google/gnostic-models v0.6.8 // indirect 25 | github.com/google/gofuzz v1.2.0 // indirect 26 | github.com/google/uuid v1.6.0 // indirect 27 | github.com/josharian/intern v1.0.0 // indirect 28 | github.com/json-iterator/go v1.1.12 // indirect 29 | github.com/mailru/easyjson v0.7.7 // indirect 30 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 31 | github.com/modern-go/reflect2 v1.0.2 // indirect 32 | github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect 33 | golang.org/x/net v0.21.0 // indirect 34 | golang.org/x/oauth2 v0.17.0 // indirect 35 | golang.org/x/term v0.17.0 // indirect 36 | golang.org/x/text v0.14.0 // indirect 37 | golang.org/x/time v0.5.0 // indirect 38 | google.golang.org/appengine v1.6.8 // indirect 39 | google.golang.org/protobuf v1.32.0 // indirect 40 | gopkg.in/inf.v0 v0.9.1 // indirect 41 | gopkg.in/yaml.v2 v2.4.0 // indirect 42 | gopkg.in/yaml.v3 v3.0.1 // indirect 43 | k8s.io/klog/v2 v2.120.1 // indirect 44 | k8s.io/kube-openapi v0.0.0-20240209001042-7a0d5b415232 // indirect 45 | k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect 46 | sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect 47 | sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect 48 | sigs.k8s.io/yaml v1.4.0 // indirect 49 | ) 50 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/emicklei/go-restful/v3 v3.11.2 h1:1onLa9DcsMYO9P+CXaL0dStDqQ2EHHXLiz+BtnqkLAU= 5 | github.com/emicklei/go-restful/v3 v3.11.2/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= 6 | github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84= 7 | github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= 8 | github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= 9 | github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= 10 | github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= 11 | github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= 12 | github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdXSSgNeAhojU= 13 | github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4= 14 | github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE= 15 | github.com/go-openapi/swag v0.22.9/go.mod h1:3/OXnFfnMAwBD099SwYRk7GD3xOrr1iL7d/XNLXVVwE= 16 | github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= 17 | github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= 18 | github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= 19 | github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= 20 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= 21 | github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= 22 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= 23 | github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= 24 | github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= 25 | github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= 26 | github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I= 27 | github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U= 28 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 29 | github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 30 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 31 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 32 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 33 | github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= 34 | github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 35 | github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= 36 | github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= 37 | github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= 38 | github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 39 | github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= 40 | github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= 41 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 42 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 43 | github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= 44 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 45 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 46 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 47 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 48 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 49 | github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= 50 | github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= 51 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 52 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 53 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 54 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 55 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 56 | github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= 57 | github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= 58 | github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4= 59 | github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o= 60 | github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg= 61 | github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= 62 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 63 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 64 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 65 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 66 | github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= 67 | github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= 68 | github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= 69 | github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= 70 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 71 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 72 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 73 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 74 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 75 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 76 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 77 | github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= 78 | github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= 79 | github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= 80 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 81 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 82 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 83 | golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 84 | golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= 85 | golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= 86 | golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 87 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 88 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 89 | golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 90 | golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= 91 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 92 | golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= 93 | golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= 94 | golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= 95 | golang.org/x/oauth2 v0.17.0 h1:6m3ZPmLEFdVxKKWnKq4VqZ60gutO35zm+zrAHVmHyDQ= 96 | golang.org/x/oauth2 v0.17.0/go.mod h1:OzPDGQiuQMguemayvdylqddI7qcD9lnSDb+1FiwQ5HA= 97 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 98 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 99 | golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 100 | golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 101 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 102 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 103 | golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 104 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 105 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 106 | golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 107 | golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 108 | golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 109 | golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= 110 | golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 111 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 112 | golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= 113 | golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= 114 | golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= 115 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 116 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 117 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 118 | golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= 119 | golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= 120 | golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 121 | golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= 122 | golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= 123 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 124 | golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 125 | golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= 126 | golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= 127 | golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= 128 | golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA= 129 | golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= 130 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 131 | golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 132 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 133 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 134 | google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM= 135 | google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= 136 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 137 | google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= 138 | google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= 139 | google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= 140 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 141 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 142 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 143 | gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= 144 | gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= 145 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 146 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 147 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 148 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 149 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 150 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 151 | k8s.io/api v0.29.1 h1:DAjwWX/9YT7NQD4INu49ROJuZAAAP/Ijki48GUPzxqw= 152 | k8s.io/api v0.29.1/go.mod h1:7Kl10vBRUXhnQQI8YR/R327zXC8eJ7887/+Ybta+RoQ= 153 | k8s.io/apimachinery v0.29.1 h1:KY4/E6km/wLBguvCZv8cKTeOwwOBqFNjwJIdMkMbbRc= 154 | k8s.io/apimachinery v0.29.1/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU= 155 | k8s.io/client-go v0.29.1 h1:19B/+2NGEwnFLzt0uB5kNJnfTsbV8w6TgQRz9l7ti7A= 156 | k8s.io/client-go v0.29.1/go.mod h1:TDG/psL9hdet0TI9mGyHJSgRkW3H9JZk2dNEUS7bRks= 157 | k8s.io/klog/v2 v2.120.1 h1:QXU6cPEOIslTGvZaXvFWiP9VKyeet3sawzTOvdXb4Vw= 158 | k8s.io/klog/v2 v2.120.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= 159 | k8s.io/kube-openapi v0.0.0-20240209001042-7a0d5b415232 h1:MMq4iF9pHuAz/9dLnHwBQKEoeigXClzs3MFh/seyqtA= 160 | k8s.io/kube-openapi v0.0.0-20240209001042-7a0d5b415232/go.mod h1:Pa1PvrP7ACSkuX6I7KYomY6cmMA0Tx86waBhDUgoKPw= 161 | k8s.io/utils v0.0.0-20240102154912-e7106e64919e h1:eQ/4ljkx21sObifjzXwlPKpdGLrCfRziVtos3ofG/sQ= 162 | k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= 163 | sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo= 164 | sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= 165 | sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4= 166 | sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08= 167 | sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= 168 | sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= 169 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "fmt" 7 | "net" 8 | "net/http" 9 | "os" 10 | "os/exec" 11 | "strconv" 12 | "time" 13 | 14 | "github.com/sirupsen/logrus" 15 | "golang.org/x/sys/unix" 16 | corev1 "k8s.io/api/core/v1" 17 | "k8s.io/apimachinery/pkg/api/errors" 18 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 19 | "k8s.io/client-go/kubernetes" 20 | "k8s.io/client-go/rest" 21 | "k8s.io/client-go/tools/leaderelection" 22 | "k8s.io/client-go/tools/leaderelection/resourcelock" 23 | 24 | "github.com/linbit/k8s-await-election/pkg/consts" 25 | ) 26 | 27 | var Version = "development" 28 | 29 | var log = logrus.New() 30 | 31 | type AwaitElection struct { 32 | Name string 33 | LockName string 34 | LockNamespace string 35 | LeaderIdentity string 36 | StatusEndpoint string 37 | ServiceName string 38 | ServiceNamespace string 39 | PodIP string 40 | NodeName *string 41 | ServicePorts []corev1.EndpointPort 42 | LeaderExec func(ctx context.Context) error 43 | } 44 | 45 | type ConfigError struct { 46 | missingEnv string 47 | } 48 | 49 | func (e *ConfigError) Error() string { 50 | return fmt.Sprintf("config: missing required environment variable: '%s'", e.missingEnv) 51 | } 52 | 53 | func NewAwaitElectionConfig(exec func(ctx context.Context) error) (*AwaitElection, error) { 54 | name := os.Getenv(consts.AwaitElectionNameKey) 55 | if name == "" { 56 | return nil, &ConfigError{missingEnv: consts.AwaitElectionNameKey} 57 | } 58 | 59 | lockName := os.Getenv(consts.AwaitElectionLockNameKey) 60 | if lockName == "" { 61 | return nil, &ConfigError{missingEnv: consts.AwaitElectionLockNameKey} 62 | } 63 | 64 | lockNamespace := os.Getenv(consts.AwaitElectionLockNamespaceKey) 65 | if lockNamespace == "" { 66 | return nil, &ConfigError{missingEnv: consts.AwaitElectionLockNamespaceKey} 67 | } 68 | 69 | leaderIdentity := os.Getenv(consts.AwaitElectionIdentityKey) 70 | if leaderIdentity == "" { 71 | return nil, &ConfigError{missingEnv: consts.AwaitElectionIdentityKey} 72 | } 73 | 74 | // Optional 75 | statusEndpoint := os.Getenv(consts.AwaitElectionStatusEndpointKey) 76 | podIP := os.Getenv(consts.AwaitElectionPodIP) 77 | var nodeName *string 78 | if val, ok := os.LookupEnv(consts.AwaitElectionNodeName); ok { 79 | nodeName = &val 80 | } 81 | 82 | serviceName := os.Getenv(consts.AwaitElectionServiceName) 83 | serviceNamespace := os.Getenv(consts.AwaitElectionServiceNamespace) 84 | 85 | servicePortsJson := os.Getenv(consts.AwaitElectionServicePortsJson) 86 | var servicePorts []corev1.EndpointPort 87 | err := json.Unmarshal([]byte(servicePortsJson), &servicePorts) 88 | if serviceName != "" && err != nil { 89 | return nil, fmt.Errorf("failed to parse ports from env: %w", err) 90 | } 91 | 92 | return &AwaitElection{ 93 | Name: name, 94 | LockName: lockName, 95 | LockNamespace: lockNamespace, 96 | LeaderIdentity: leaderIdentity, 97 | StatusEndpoint: statusEndpoint, 98 | PodIP: podIP, 99 | NodeName: nodeName, 100 | ServiceName: serviceName, 101 | ServiceNamespace: serviceNamespace, 102 | ServicePorts: servicePorts, 103 | LeaderExec: exec, 104 | }, nil 105 | } 106 | 107 | func (el *AwaitElection) Run() error { 108 | ctx, cancel := context.WithCancel(context.Background()) 109 | defer cancel() 110 | 111 | // Create kubernetes client 112 | kubeCfg, err := rest.InClusterConfig() 113 | if err != nil { 114 | return fmt.Errorf("failed to read cluster config: %w", err) 115 | } 116 | 117 | kubeClient, err := kubernetes.NewForConfig(kubeCfg) 118 | if err != nil { 119 | return fmt.Errorf("failed to create KubeClient for config: %w", err) 120 | } 121 | 122 | // result of the LeaderExec(ctx) command will be send over this channel 123 | execResult := make(chan error) 124 | 125 | // Create lock for leader election using provided settings 126 | lock := resourcelock.LeaseLock{ 127 | LeaseMeta: metav1.ObjectMeta{ 128 | Name: el.LockName, 129 | Namespace: el.LockNamespace, 130 | }, 131 | Client: kubeClient.CoordinationV1(), 132 | LockConfig: resourcelock.ResourceLockConfig{ 133 | Identity: el.LeaderIdentity, 134 | }, 135 | } 136 | 137 | leaderCfg := leaderelection.LeaderElectionConfig{ 138 | Lock: &lock, 139 | Name: el.Name, 140 | ReleaseOnCancel: true, 141 | // Suggested default values 142 | LeaseDuration: 15 * time.Second, 143 | RenewDeadline: 10 * time.Second, 144 | RetryPeriod: 2 * time.Second, 145 | Callbacks: leaderelection.LeaderCallbacks{ 146 | OnStartedLeading: func(ctx context.Context) { 147 | // First we need to register our pod as the service endpoint 148 | err := el.setServiceEndpoint(ctx, kubeClient) 149 | if err != nil { 150 | execResult <- err 151 | return 152 | } 153 | // actual start the command here. 154 | // Note: this callback is started in a goroutine, so we can block this 155 | // execution path for as long as we want. 156 | execResult <- el.LeaderExec(ctx) 157 | }, 158 | OnNewLeader: func(identity string) { 159 | log.Infof("long live our new leader: '%s'!", identity) 160 | }, 161 | OnStoppedLeading: func() { 162 | log.Info("lost leader status") 163 | }, 164 | }, 165 | } 166 | 167 | leaseDuration, err := strconv.Atoi(os.Getenv(consts.AwaitElectionLeaseDuration)) 168 | if err == nil { 169 | leaderCfg.LeaseDuration = time.Duration(leaseDuration) * time.Second 170 | } 171 | 172 | renewDeadline, err := strconv.Atoi(os.Getenv(consts.AwaitElectionRenewDeadline)) 173 | if err == nil { 174 | leaderCfg.RenewDeadline = time.Duration(renewDeadline) * time.Second 175 | } 176 | 177 | retryPeriod, err := strconv.Atoi(os.Getenv(consts.AwaitElectionRetryPeriod)) 178 | if err == nil { 179 | leaderCfg.RetryPeriod = time.Duration(retryPeriod) * time.Second 180 | } 181 | 182 | elector, err := leaderelection.NewLeaderElector(leaderCfg) 183 | if err != nil { 184 | return fmt.Errorf("failed to create leader elector: %w", err) 185 | } 186 | 187 | statusServerResult := el.startStatusEndpoint(ctx, elector) 188 | 189 | go elector.Run(ctx) 190 | 191 | // the different end conditions: 192 | // 1. context was cancelled -> error 193 | // 2. command executed -> either error or nil, depending on return value 194 | // 3. status endpoint failed -> could not create status endpoint 195 | select { 196 | case <-ctx.Done(): 197 | return ctx.Err() 198 | case r := <-execResult: 199 | return r 200 | case r := <-statusServerResult: 201 | return r 202 | } 203 | } 204 | 205 | func (el *AwaitElection) setServiceEndpoint(ctx context.Context, client *kubernetes.Clientset) error { 206 | if el.ServiceName == "" { 207 | return nil 208 | } 209 | 210 | endpoints := &corev1.Endpoints{ 211 | ObjectMeta: metav1.ObjectMeta{ 212 | Name: el.ServiceName, 213 | Namespace: el.ServiceNamespace, 214 | }, 215 | Subsets: []corev1.EndpointSubset{ 216 | { 217 | Addresses: []corev1.EndpointAddress{{IP: el.PodIP, NodeName: el.NodeName}}, 218 | Ports: el.ServicePorts, 219 | }, 220 | }, 221 | } 222 | _, err := client.CoreV1().Endpoints(el.ServiceNamespace).Create(ctx, endpoints, metav1.CreateOptions{}) 223 | if err != nil { 224 | if !errors.IsAlreadyExists(err) { 225 | return err 226 | } 227 | 228 | _, err := client.CoreV1().Endpoints(el.ServiceNamespace).Update(ctx, endpoints, metav1.UpdateOptions{}) 229 | return err 230 | } 231 | 232 | return nil 233 | } 234 | 235 | func (el *AwaitElection) startStatusEndpoint(ctx context.Context, elector *leaderelection.LeaderElector) <-chan error { 236 | statusServerResult := make(chan error) 237 | 238 | if el.StatusEndpoint == "" { 239 | log.Info("no status endpoint specified, will not be created") 240 | return statusServerResult 241 | } 242 | 243 | serveMux := http.NewServeMux() 244 | serveMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { 245 | err := elector.Check(2 * time.Second) 246 | if err != nil { 247 | log.WithField("err", err).Error("failed to step down gracefully, reporting unhealthy status") 248 | writer.WriteHeader(500) 249 | _, err := writer.Write([]byte("{\"status\": \"expired\"}")) 250 | if err != nil { 251 | log.WithField("err", err).Error("failed to serve request") 252 | } 253 | return 254 | } 255 | 256 | _, err = writer.Write([]byte("{\"status\": \"ok\"}")) 257 | if err != nil { 258 | log.WithField("err", err).Error("failed to serve request") 259 | } 260 | }) 261 | 262 | statusServer := http.Server{ 263 | Addr: el.StatusEndpoint, 264 | Handler: serveMux, 265 | BaseContext: func(listener net.Listener) context.Context { 266 | return ctx 267 | }, 268 | } 269 | go func() { 270 | statusServerResult <- statusServer.ListenAndServe() 271 | }() 272 | return statusServerResult 273 | } 274 | 275 | // Run the command specified the this program's arguments to completion. 276 | // Stdout and Stderr are inherited from this process. If the provided context is cancelled, 277 | // the started process is killed. 278 | func Execute(ctx context.Context) error { 279 | log.Infof("starting command '%s' with arguments: '%v'", os.Args[1], os.Args[2:]) 280 | cmd := exec.CommandContext(ctx, os.Args[1], os.Args[2:]...) 281 | cmd.Stderr = os.Stderr 282 | cmd.Stdout = os.Stdout 283 | return cmd.Run() 284 | } 285 | 286 | func main() { 287 | log.WithField("version", Version).Info("running k8s-await-election") 288 | 289 | if len(os.Args) <= 1 { 290 | log.WithField("args", os.Args).Fatal("Need at least one argument to run") 291 | } 292 | 293 | if os.Getenv(consts.AwaitElectionEnabledKey) == "" { 294 | path, err := exec.LookPath(os.Args[1]) 295 | if err != nil { 296 | log.WithError(err).Fatal("Error looking up path") 297 | } 298 | 299 | err = unix.Exec(path, os.Args[1:], os.Environ()) 300 | log.WithError(err).Fatal("Failed to execve()") 301 | } 302 | 303 | awaitElectionConfig, err := NewAwaitElectionConfig(Execute) 304 | if err != nil { 305 | log.WithField("err", err).Fatal("failed to create runner") 306 | } 307 | 308 | err = awaitElectionConfig.Run() 309 | if err != nil { 310 | log.WithField("err", err).Fatalf("failed to run") 311 | } 312 | } 313 | -------------------------------------------------------------------------------- /pkg/consts/consts.go: -------------------------------------------------------------------------------- 1 | package consts 2 | 3 | const ( 4 | AwaitElectionEnabledKey = "K8S_AWAIT_ELECTION_ENABLED" 5 | AwaitElectionNameKey = "K8S_AWAIT_ELECTION_NAME" 6 | AwaitElectionLockNameKey = "K8S_AWAIT_ELECTION_LOCK_NAME" 7 | AwaitElectionLockNamespaceKey = "K8S_AWAIT_ELECTION_LOCK_NAMESPACE" 8 | AwaitElectionIdentityKey = "K8S_AWAIT_ELECTION_IDENTITY" 9 | AwaitElectionStatusEndpointKey = "K8S_AWAIT_ELECTION_STATUS_ENDPOINT" 10 | AwaitElectionPodIP = "K8S_AWAIT_ELECTION_POD_IP" 11 | AwaitElectionNodeName = "K8S_AWAIT_ELECTION_NODE_NAME" 12 | AwaitElectionServiceName = "K8S_AWAIT_ELECTION_SERVICE_NAME" 13 | AwaitElectionServiceNamespace = "K8S_AWAIT_ELECTION_SERVICE_NAMESPACE" 14 | AwaitElectionServicePortsJson = "K8S_AWAIT_ELECTION_SERVICE_PORTS_JSON" 15 | AwaitElectionLeaseDuration = "K8S_AWAIT_ELECTION_LEASE_DURATION" 16 | AwaitElectionRenewDeadline = "K8S_AWAIT_ELECTION_RENEW_DEADLINE" 17 | AwaitElectionRetryPeriod = "K8S_AWAIT_ELECTION_RETRY_PERIOD" 18 | ) 19 | --------------------------------------------------------------------------------