-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscaffold_argocd.py
More file actions
317 lines (277 loc) · 12 KB
/
scaffold_argocd.py
File metadata and controls
317 lines (277 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
"""
DevOps-OS ArgoCD / GitOps Config Generator
Generates ArgoCD Application, AppProject, and supporting Kubernetes resources
for GitOps-based continuous delivery. Also supports generating a Flux
Kustomization when --method flux is selected.
Outputs:
argocd/ (default output dir)
├── application.yaml ArgoCD Application CR
├── appproject.yaml ArgoCD AppProject CR
└── rollout.yaml Argo Rollouts (when --rollouts flag used)
flux/
├── kustomization.yaml Flux Kustomization
└── image-update-automation.yaml Flux image update automation
"""
TEMPLATE_INFO = {
"name": "argocd",
"category": "GitOps",
"description": "ArgoCD/Flux manifests",
}
import os
import argparse
import yaml
from pathlib import Path
ENV_PREFIX = "DEVOPS_OS_ARGOCD_"
METHODS = ["argocd", "flux"]
# ---------------------------------------------------------------------------
# Argument parsing
# ---------------------------------------------------------------------------
def parse_arguments():
parser = argparse.ArgumentParser(description="Generate ArgoCD / Flux GitOps configs for DevOps-OS")
parser.add_argument("--name", default=os.environ.get(f"{ENV_PREFIX}NAME", "my-app"),
help="Application name")
parser.add_argument("--method", choices=METHODS,
default=os.environ.get(f"{ENV_PREFIX}METHOD", "argocd"),
help="GitOps tool: argocd or flux")
parser.add_argument("--repo", default=os.environ.get(f"{ENV_PREFIX}REPO", "https://github.com/myorg/my-app.git"),
help="Git repository URL for the application manifests")
parser.add_argument("--revision", default=os.environ.get(f"{ENV_PREFIX}REVISION", "HEAD"),
help="Git revision / branch / tag to sync")
parser.add_argument("--path", default=os.environ.get(f"{ENV_PREFIX}PATH", "k8s"),
help="Path inside the repository to the manifests")
parser.add_argument("--namespace", default=os.environ.get(f"{ENV_PREFIX}NAMESPACE", "default"),
help="Kubernetes namespace to deploy into")
parser.add_argument("--project", default=os.environ.get(f"{ENV_PREFIX}PROJECT", "default"),
help="ArgoCD project name")
parser.add_argument("--server", default=os.environ.get(f"{ENV_PREFIX}SERVER", "https://kubernetes.default.svc"),
help="Destination Kubernetes API server")
parser.add_argument("--auto-sync", action="store_true",
default=os.environ.get(f"{ENV_PREFIX}AUTO_SYNC", "false").lower() in ("true", "1", "yes"),
help="Enable ArgoCD auto-sync policy")
parser.add_argument("--rollouts", action="store_true",
default=os.environ.get(f"{ENV_PREFIX}ROLLOUTS", "false").lower() in ("true", "1", "yes"),
help="Generate an Argo Rollouts canary strategy")
parser.add_argument("--image", default=os.environ.get(f"{ENV_PREFIX}IMAGE", "ghcr.io/myorg/my-app"),
help="Container image (used in Rollouts / Flux image automation)")
parser.add_argument("--output-dir", default=os.environ.get(f"{ENV_PREFIX}OUTPUT_DIR", "."),
help="Root output directory")
parser.add_argument("--allow-any-source-repo", action="store_true",
default=os.environ.get(f"{ENV_PREFIX}ALLOW_ANY_SOURCE_REPO", "false").lower() in ("true", "1", "yes"),
help="Add '*' to AppProject sourceRepos (opt-in; grants access to any repo)")
return parser.parse_args()
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _write_yaml(path, data):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
with open(path, "w") as fh:
yaml.dump(data, fh, sort_keys=False, default_flow_style=False)
return path
# ---------------------------------------------------------------------------
# ArgoCD generators
# ---------------------------------------------------------------------------
def generate_argocd_application(args):
"""Generate an ArgoCD Application Custom Resource."""
sync_policy = {}
if args.auto_sync:
sync_policy = {
"automated": {"prune": True, "selfHeal": True},
"syncOptions": ["CreateNamespace=true"],
}
else:
sync_policy = {"syncOptions": ["CreateNamespace=true"]}
return {
"apiVersion": "argoproj.io/v1alpha1",
"kind": "Application",
"metadata": {
"name": args.name,
"namespace": "argocd",
"labels": {"app.kubernetes.io/name": args.name},
},
"spec": {
"project": args.project,
"source": {
"repoURL": args.repo,
"targetRevision": args.revision,
"path": args.path,
},
"destination": {
"server": args.server,
"namespace": args.namespace,
},
"syncPolicy": sync_policy,
},
}
def generate_argocd_appproject(args):
"""Generate an ArgoCD AppProject Custom Resource."""
return {
"apiVersion": "argoproj.io/v1alpha1",
"kind": "AppProject",
"metadata": {
"name": args.project,
"namespace": "argocd",
},
"spec": {
"description": f"Project for {args.name} deployments",
"sourceRepos": [args.repo, "*"] if getattr(args, "allow_any_source_repo", False) else [args.repo],
"destinations": [
{"namespace": args.namespace, "server": args.server},
{"namespace": "argocd", "server": args.server},
],
"clusterResourceWhitelist": [
{"group": "*", "kind": "Namespace"},
],
"namespaceResourceWhitelist": [
{"group": "apps", "kind": "Deployment"},
{"group": "apps", "kind": "StatefulSet"},
{"group": "", "kind": "Service"},
{"group": "", "kind": "ConfigMap"},
{"group": "", "kind": "Secret"},
{"group": "networking.k8s.io", "kind": "Ingress"},
],
},
}
def generate_argo_rollout(args):
"""Generate an Argo Rollouts canary Rollout resource."""
return {
"apiVersion": "argoproj.io/v1alpha1",
"kind": "Rollout",
"metadata": {"name": args.name, "namespace": args.namespace},
"spec": {
"replicas": 3,
"selector": {"matchLabels": {"app": args.name}},
"template": {
"metadata": {"labels": {"app": args.name}},
"spec": {
"containers": [
{
"name": args.name,
"image": f"{args.image}:stable",
"ports": [{"containerPort": 8080}],
}
]
},
},
"strategy": {
"canary": {
"steps": [
{"setWeight": 10},
{"pause": {"duration": "1m"}},
{"setWeight": 30},
{"pause": {"duration": "2m"}},
{"setWeight": 60},
{"pause": {"duration": "2m"}},
{"setWeight": 100},
]
}
},
},
}
# ---------------------------------------------------------------------------
# Flux generators
# ---------------------------------------------------------------------------
def generate_flux_kustomization(args):
"""Generate a Flux CD Kustomization resource."""
return {
"apiVersion": "kustomize.toolkit.fluxcd.io/v1",
"kind": "Kustomization",
"metadata": {"name": args.name, "namespace": "flux-system"},
"spec": {
"interval": "10m",
"retryInterval": "1m",
"timeout": "5m",
"prune": True,
"sourceRef": {"kind": "GitRepository", "name": args.name},
"path": f"./{args.path}",
"targetNamespace": args.namespace,
"healthChecks": [
{"apiVersion": "apps/v1", "kind": "Deployment", "name": args.name, "namespace": args.namespace}
],
},
}
def generate_flux_git_repository(args):
"""Generate a Flux CD GitRepository source."""
return {
"apiVersion": "source.toolkit.fluxcd.io/v1",
"kind": "GitRepository",
"metadata": {"name": args.name, "namespace": "flux-system"},
"spec": {
"interval": "1m",
"url": args.repo,
"ref": {"branch": args.revision if args.revision != "HEAD" else "main"},
},
}
def generate_flux_image_automation(args):
"""Generate Flux image update automation resources."""
image_repo = {
"apiVersion": "image.toolkit.fluxcd.io/v1beta2",
"kind": "ImageRepository",
"metadata": {"name": args.name, "namespace": "flux-system"},
"spec": {"image": args.image, "interval": "5m"},
}
image_policy = {
"apiVersion": "image.toolkit.fluxcd.io/v1beta2",
"kind": "ImagePolicy",
"metadata": {"name": args.name, "namespace": "flux-system"},
"spec": {
"imageRepositoryRef": {"name": args.name},
"policy": {"semver": {"range": ">=1.0.0"}},
},
}
image_update = {
"apiVersion": "image.toolkit.fluxcd.io/v1beta1",
"kind": "ImageUpdateAutomation",
"metadata": {"name": args.name, "namespace": "flux-system"},
"spec": {
"interval": "30m",
"sourceRef": {"kind": "GitRepository", "name": args.name},
"git": {
"checkout": {"ref": {"branch": "main"}},
"commit": {
"author": {"email": "fluxcdbot@users.noreply.github.com", "name": "FluxBot"},
"messageTemplate": f"chore: update {args.name} image to {{{{.AutomationObject}}}}",
},
"push": {"branch": "main"},
},
"update": {"path": f"./{args.path}", "strategy": "Setters"},
},
}
return image_repo, image_policy, image_update
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
args = parse_arguments()
output_root = Path(args.output_dir)
generated = []
if args.method == "argocd":
out_dir = output_root / "argocd"
app = generate_argocd_application(args)
path = _write_yaml(out_dir / "application.yaml", app)
generated.append(str(path))
project = generate_argocd_appproject(args)
path = _write_yaml(out_dir / "appproject.yaml", project)
generated.append(str(path))
if args.rollouts:
rollout = generate_argo_rollout(args)
path = _write_yaml(out_dir / "rollout.yaml", rollout)
generated.append(str(path))
else: # flux
out_dir = output_root / "flux"
git_repo = generate_flux_git_repository(args)
path = _write_yaml(out_dir / "git-repository.yaml", git_repo)
generated.append(str(path))
kustomization = generate_flux_kustomization(args)
path = _write_yaml(out_dir / "kustomization.yaml", kustomization)
generated.append(str(path))
image_repo, image_policy, image_update = generate_flux_image_automation(args)
path = _write_yaml(out_dir / "image-update-automation.yaml",
[image_repo, image_policy, image_update])
generated.append(str(path))
print(f"GitOps configs generated ({args.method}):")
for p in generated:
print(f" {p}")
if __name__ == "__main__":
main()