Skip to content

Commit f9c4c14

Browse files
authored
feature/multi cluster (#2)
* feat: adding possibility to define clusters * feat: adding multi-cluster support and partial secret loads * docs: describing multi-cluster support
1 parent db290d5 commit f9c4c14

17 files changed

Lines changed: 758 additions & 183 deletions

README.md

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ __ _ __
1414
CLI tool for performing GitOps operations
1515

1616
## Usage
17+
1718
```
1819
NAME:
1920
gitpos - GitOps CLI
@@ -36,16 +37,21 @@ GLOBAL OPTIONS:
3637
```
3738

3839
### Planning secret application to a cluster
40+
3941
**NOTE:** It is expected, that the cluster's KUBECONFIG is already set up. Alternatively, the `--kubeconfig` flag can be used.
4042

4143
```bash
4244
gitops secrets plan kubernetes
4345
```
46+
4447
Or in short
48+
4549
```bash
4650
gitops s p k8s
4751
```
52+
4853
Example output:
54+
4955
```
5056
__ _ __
5157
\ \ ____ _(_) /_____ ____ _____
@@ -74,17 +80,22 @@ use gitops secrets apply kubernetes to apply these changes to your cluster
7480
```
7581

7682
### Applying secrets to a cluster
83+
7784
**NOTE:** It is expected, that the cluster's KUBECONFIG is already set up. Alternatively, the `--kubeconfig` flag can be used.
7885

7986
```bash
8087
gitops secrets apply kubernetes
8188
```
89+
8290
Or in short
91+
8392
```bash
8493
gitops s a k8s
8594
```
95+
8696
The user will be prompted to confirm the changes before they are applied to the cluster. The prompt can be bypassed by using the `--auto-approve` flag.
8797
Example output:
98+
8899
```
89100
__ _ __
90101
\ \ ____ _(_) /_____ ____ _____
@@ -168,7 +179,8 @@ The secrets files must follow the following format:
168179

169180
```yaml
170181
# target of the secret
171-
target: < k8s | vault >
182+
targetType: < k8s | vault >
183+
172184
# name of the secret
173185
name: <my-secret-name>
174186
# optional namespace of the secret (default: default)
@@ -185,8 +197,8 @@ data:
185197
##### Case 1: Secret for K8s
186198
187199
```yaml
188-
# target of the secret
189-
target: k8s
200+
# targetType of the secret
201+
targetType: k8s
190202
# name of the secret
191203
name: my-secret-name
192204
# optional namespace of the secret (default: default)
@@ -208,27 +220,155 @@ name: my-secret-name
208220

209221
This implies, that the filename must be a valid K8s secret name.
210222

211-
212223
##### Case 2: Secret for Vault
224+
213225
**NOTE:** Vault secrets are still WIP
214226

215227
```yaml
216228
# target of the secret
217-
target: vault
229+
targetType: vault
218230
# name of the secret - will be used as path in vault
219231
name: /my/secret/name
220232
# data of the secret as kv pairs
221233
data:
222234
key: value
223235
```
224236

225-
226237
#### Secrets Templating
227238

228239
It is possible to use Go templates in the secret files. The values will originate from sops-encrypted `values.gitops.secret.enc.y[a]ml` files.
229240
Values files can be located anywhere in the repository. The GitOps CLI will pick up all files that are located on the direct path towards the respective secret file.
230241
Values files closer to the secret file will have higher precedence. Any object structure is allowed to be used in a values file.
231242

243+
Example:
244+
245+
```yaml
246+
# /foo/bar/dev/values.gitops.secret.enc.yaml
247+
environment: dev
248+
database:
249+
host: localhost
250+
port: 5432
251+
user: postgres
252+
password: postgres
253+
```
254+
255+
```yaml
256+
# /foo/bar/dev/values.gitops.secret.enc.yaml
257+
targetType: k8s
258+
# name of the secret - will be used as path in vault
259+
name: my-service
260+
namespace: "{{ .Values.environment }}"
261+
data:
262+
application.properties: |-
263+
service.environment={{ .Values.environment }}
264+
spring.datasource.url=jdbc:postgresql://{{ .Values.database.host }}:{{ .Values.database.port }}/mydb
265+
spring.datasource.username={{ .Values.database.user }}
266+
spring.datasource.password={{ .Values.database.password }}
267+
```
268+
**NOTE** that the template string (`{{ .Values.someValue }}`) must be enclosed in quotes for sops to work properly. In the above example, the entire `application.properties` data value is considered as a string and thus does not need further quoting.
269+
270+
#### Multi-cluster support
271+
It is possible to address multiple clusters with a single GitOps repository.
272+
To add a new cluster to the GitOps state use
273+
```
274+
gitops clusters add <cluster-name> <kubeconfig-path>
275+
```
276+
The kubeconfig file can either be a plain text file or a sops-encrypted file. If the file is encrypted, it must adhere to the following naming convention to be decrypted properly:
277+
```
278+
*.kubeconfig.secret.enc.ya?ml
279+
```
280+
281+
To inspect the currently configured clusters use
282+
```
283+
gitops clusters list
284+
```
285+
```
286+
__ _ __
287+
\ \ ____ _(_) /_____ ____ _____
288+
\ \ / __ `/ / __/ __ \/ __ \/ ___/
289+
/ / / /_/ / / /_/ /_/ / /_/ (__ )
290+
/_/ \__, /_/\__/\____/ .___/____/
291+
/____/ /_/
292+
293+
dev => kubeconfigs/dev.kubeconfig.secret.enc.yml
294+
int => kubeconfigs/int.kubeconfig.secret.enc.yml
295+
prod => kubeconfigs/prod.kubeconfig.secret.enc.yml
296+
```
297+
298+
To remove a cluster from the GitOps state use
299+
```
300+
gitops clusters remove <cluster-name>
301+
```
302+
303+
To check connectivity with the configured clusters use
304+
```
305+
gitops clusters test
306+
```
307+
```
308+
__ _ __
309+
\ \ ____ _(_) /_____ ____ _____
310+
\ \ / __ `/ / __/ __ \/ __ \/ ___/
311+
/ / / /_/ / / /_/ /_/ / /_/ (__ )
312+
/_/ \__, /_/\__/\____/ .___/____/
313+
/____/ /_/
314+
315+
Cluster: __default (v1.25.3) Connected: true
316+
Cluster: dev (v1.24.8) Connected: true
317+
Cluster: int (v1.24.8) Connected: true
318+
Cluster: prod (v1.24.8) Connected: true
319+
```
320+
321+
A secret can be configured to be applied to a specific cluster using the `target` attribute in the secret file. The default value is the `__default` cluster which is inferred from the `KUBECONFIG` environment variable or the default kubeconfig file. The `target` attribute can also be set using a templating variable so that all secrets under a certain directory will be applied to a specific cluster.
322+
```yaml
323+
# /foo/bar/dev/values.gitops.secret.enc.yaml
324+
target: dev
325+
```
326+
```yaml
327+
# /foo/bar/dev/myservice/service.gitops.secret.enc.yaml
328+
targetType: k8s
329+
target: "{{ .Values.target }}" # will be replaced with "dev"
330+
name: my-service
331+
data:
332+
key: value
333+
```
334+
335+
By default, secrets will be applied to all configured clusters. This can be limited by giving the cluster as an argument:
336+
```
337+
gitops secrets plan kubernetes dev
338+
__ _ __
339+
\ \ ____ _(_) /_____ ____ _____
340+
\ \ / __ `/ / __/ __ \/ __ \/ ___/
341+
/ / / /_/ / / /_/ /_/ / /_/ (__ )
342+
/_/ \__, /_/\__/\____/ .___/____/
343+
/____/ /_/
344+
345+
Limiting to cluster dev
346+
347+
[Loading local secrets] 100% |██████████████████████████████████████████████████| (63/63)
348+
349+
350+
No changes to apply.
351+
```
352+
353+
#### Directory limiter
354+
It is possible to restrict the secrets input to a specific directory to speed up loading and decryption of secrets. This can be done by providing the `--dir` flag:
355+
```
356+
gitops secrets --dir application/dev plan kubernetes
357+
358+
__ _ __
359+
\ \ ____ _(_) /_____ ____ _____
360+
\ \ / __ `/ / __/ __ \/ __ \/ ___/
361+
/ / / /_/ / / /_/ /_/ / /_/ (__ )
362+
/_/ \__, /_/\__/\____/ .___/____/
363+
/____/ /_/
364+
365+
Limiting to directory applications/dev
366+
367+
[Loading local secrets] 100% |██████████████████████████████████████████████████| (1/1)
368+
369+
No changes to apply.
370+
```
371+
**NOTE** that the directory path must be relative to the repository root and that only forward slashes (`/`) are supported.
232372
## Repository
233373

234374
### After the first clone

0 commit comments

Comments
 (0)