如果你看 Argo CD Resource Hooks 官方文件,會告訴你 PreSync 可以拿來做 database schema migration,然而這是有問題的。
這有個問題,譬如說外部的資料庫我們會這樣宣告
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
type: ExternalName
externalName: foo.bar.us-east-2.rds.amazonaws.com
大部分像這樣的 Resource 都是在 PreSync 之後的 Sync phase 去同步的,這就造成 Application 第一次安裝的時候,尚未跑到 Sync phase -> 資料庫不存在 -> PreSync 階段的 databae schema migration kind: Job
就會失敗。
如果把 kind: Service
標記成 Presync phase 會有另一個問題,Presync hook 成功跑完以後就會消失。(另一個模式是每次跑之前砍掉重建,問題差不多)
由於會消失,像是 kind: Service
或是 kind: PersistentVOlume
就不能放在 Presync phase。
解決方法是改用 Sync Waves
apiVersion: v1
kind: Service
metadata:
name: postgres
annotations:
argocd.argoproj.io/sync-wave: "-1"
spec:
type: ExternalName
externalName: foo.bar.us-east-2.rds.amazonaws.com
透過自由標記正負整數來控制執行順序,都 2021 了還有這種非 DAG 的設計也頗為奇妙。Flux CD 則是設計成比較好用的 spec.dependsOn
參考資料: