Skip to content

Commit

Permalink
Merge pull request #128 from metacoma/publish-pkg-argo-cd-order
Browse files Browse the repository at this point in the history
publish argo-cd-order
  • Loading branch information
Peefy authored Apr 19, 2024
2 parents 502869c + 5a9d332 commit b19375e
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
83 changes: 83 additions & 0 deletions argo-cd-order/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
## Introduction

argo-cd-order is a module for ordering argocd sync operations
For more details: https://argo-cd.readthedocs.io/en/stable/user-guide/sync-waves/


How to use

```
kcl mod add argo-cd-order
```

Write the code

```
import argo_cd_order as ArgoCdOrder
import yaml
import manifests
import k8s.api.core.v1 as k8core
import file
import argoproj.v1alpha1 as argoproj
testApp = ArgoCdOrder.make({
preSync = [
k8core.Namespace {
metadata.name = "test-namespace"
}
k8core.ConfigMap {
metadata = {
name = "test-configmap"
namespace = "test-namespace"
annotations = {
hello = "world"
}
}
}
] + yaml.decode_all(file.read("./knative-operator.yaml"))
sync = [
argoproj.Application {
metadata = {
name = "testApp"
namespace = "argocd"
}
spec = {
destination = {
namespace = "test-namespace"
server = "https://kubernetes.default.svc"
}
project = "default"
source = {
chart = "hello"
repoURL = "https://cloudecho.github.io/charts/"
targetRevision = "0.1.2"
helm = {
values = yaml.encode({})
releaseName = "my-hello"
}
}
syncPolicy = {
automated = {}
syncOptions = [
"CreateNamespace=true"
]
}
}
}
]
postSync = []
})
manifests.yaml_stream([
testApp
])
```

7 changes: 7 additions & 0 deletions argo-cd-order/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "argo-cd-order"
edition = "v0.1.0"
version = "0.1.0"

[dependencies]
json_merge_patch = { oci = "oci://ghcr.io/kcl-lang/json_merge_patch", tag = "0.1.0" }
60 changes: 60 additions & 0 deletions argo-cd-order/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json_merge_patch as p
WAVE_MIN = 0

validatedAnnotations = lambda a: {str:str} {
assert a["argocd.argoproj.io/hook"] in ["preSync", "sync", "postSync"]
assert int(a["argocd.argoproj.io/sync-wave"]) >= WAVE_MIN
a
}

schema ArgoCdManifest:
[str]: any
metadata: ArgoCdManifestMetadata

schema ArgoCdManifestMetadata:
[str]: any
annotations: ArgoCdManifestMetadataAnnotations

schema ArgoCdManifestMetadataAnnotations:
"""
When Argo CD starts a sync, it orders the resources in the following precedence:

1. The phase
2. The wave they are in (lower values first)
3. By kind (e.g. namespaces first and then other Kubernetes resources, followed by custom resources)
4. By name
"""
[str]: any
"argocd.argoproj.io/sync-wave": str
"argocd.argoproj.io/hook": str

preparePhase = lambda phase: str, phaseResources: [any] -> [ArgoCdManifest] {
[
p.merge(resource, { metadata.annotations = validatedAnnotations({
"argocd.argoproj.io/sync-wave" = str(sync_wave)
"argocd.argoproj.io/hook" = phase
})})
for sync_wave, resource in phaseResources if resource != None

]
}

schema ArgoCdOrder:
"""

Argo CD executes a sync operation in a number of steps.
At a high-level, there are three phases pre-sync, sync and post-sync.
For more details: https://argo-cd.readthedocs.io/en/stable/user-guide/sync-waves/

"""
preSync: [any]
sync: [any]
postSync: [any]

make = lambda argoCdApp: ArgoCdOrder -> [[ArgoCdManifest]] {
[
preparePhase(phase, argoCdApp[phase])
for phase in argoCdApp
]
}

0 comments on commit b19375e

Please sign in to comment.