Argo CD เป็นเครื่องมือที่ทรงพลังสำหรับการจัดการ GitOps แต่บางครั้งระบบการเปรียบเทียบ (Diff) ของมันอาจแสดง false positive ซึ่งอาจทำให้เกิด Drift Detection ที่ไม่ถูกต้อง บทความนี้จะสอนวิธีการ Diffing Customization เพื่อปรับแต่งการเปรียบเทียบ Resource ให้แม่นยำขึ้น
ทำไมต้อง Diffing Customization
ในสภาพแวดล้อม Kubernetes นั้น Fields บางอย่างจะเปลี่ยนแปลงโดยอัตโนมัติเมื่อ Resource ทำงาน เช่น metadata.resourceVersion ที่อัปเดตเมื่อมีการเปลี่ยนแปลง status ที่เปลี่ยนจากการทำงานของ Controller หรือ replicas ที่ปรับโดย Horizontal Pod Autoscaler (HPA)
ปัญหา False Positive Diffs ส่งผลให้:
- Argo CD แสดงว่า Application มี Drift อยู่ตลอดเวลา
- ทีม DevOps ไม่สามารถแยกแยะ Drift ที่สำคัญจากการเปลี่ยนแปลงที่ไม่สำคัญ
- ประสิทธิภาพของระบบลดลงจากการเปรียบเทียบซ้ำโดยไม่จำเป็น
- การ Sync ที่ไม่จำเป็นสร้างภาระให้กับ Kubernetes Cluster
ignoreDifferences ใน Application Spec
วิธีที่ง่ายที่สุดในการแก้ปัญหา False Positive Diffs คือการใช้ ignoreDifferences ใน Argo CD Application Spec เพื่อระบุ Fields ที่ต้องการให้ Argo CD ไม่สนใจ:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/example/repo
targetRevision: HEAD
path: deploy
destination:
server: https://kubernetes.default.svc
namespace: default
ignoreDifferences:
- group: apps
kind: Deployment
jsonPointers:
- /spec/replicas
- /metadata/resourceVersion
- group: ""
kind: Pod
jqPathExpressions:
- .metadata.managedFields
jsonPointers vs jqPathExpressions
ignoreDifferences รองรับ 2 วิธีหลักในการระบุ Fields:
jsonPointers
ใช้ JSON Pointer Format (RFC 6901) เพื่อระบุตำแหน่ง Field ในโครงสร้าง JSON เหมาะสำหรับ Fields ที่อยู่ในระดับชั้นคงที่:
/spec/replicas– จำนวน Replicas/metadata/resourceVersion– Resource Version/status/conditions/0/lastUpdateTime– เวลา Update ของ Condition แรก
jqPathExpressions
ใช้ jq query syntax เพื่อค้นหา Fields ที่ซับซ้อน มีความยืดหยุ่นมากกว่า jsonPointers:
.metadata.managedFields– managedFields ในระดับ metadata.spec.template.spec.containers[].resources– resources ของทุก containers.status– status field ทั้งหมด
ตัวอย่างการ Ignore Fields ที่เปลี่ยนแปลงอัตโนมัติ
metadata.resourceVersion
ignoreDifferences:
- group: ""
kind: Pod
jsonPointers:
- /metadata/resourceVersion
- /metadata/generation
- /metadata/uid
status Fields
ignoreDifferences:
- group: apps
kind: Deployment
jqPathExpressions:
- .status
HPA ที่ปรับ Replicas อัตโนมัติ
ignoreDifferences:
- group: apps
kind: Deployment
jsonPointers:
- /spec/replicas
# ใช้เมื่อมี HPA จัดการ replicas อยู่
managedFields
ignoreDifferences:
- group: ""
kind: Pod
jqPathExpressions:
- .metadata.managedFields[]
การตั้งค่า System-Wide Diffing Customization
สำหรับ Kubernetes Cluster ขนาดใหญ่ อาจจำเป็นต้องตั้งค่า Diffing Customization แบบ system-wide ผ่าน argocd-cm ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-cm
namespace: argocd
data:
resource.customizations.ignoreDifferences.all: |
jsonPointers:
- /metadata/resourceVersion
- /metadata/generation
- /metadata/managedFields
resource.customizations.ignoreDifferences.apps_Deployment: |
jsonPointers:
- /spec/replicas
resource.customizations.ignoreDifferences.apps_StatefulSet: |
jsonPointers:
- /spec/replicas
managedFieldsManagers Configuration
managedFieldsManagers ช่วยให้ Argo CD ไม่สนใจการเปลี่ยนแปลงจาก Managers ที่ไม่ใช่ Argo CD:
ignoreDifferences:
- group: apps
kind: Deployment
managedFieldsManagers:
- kube-controller-manager
- kubectl-client-side-apply
jsonPointers:
- /spec/replicas
jqPathExpressions:
- .status
การใช้ Server-Side Diff
Argo CD v2.4+ รองรับ Server-Side Diff ซึ่งให้ความแม่นยำสูงขึ้นในการเปรียบเทียบ Resource เนื่องจากการเปรียบเทียบเกิดขึ้นบน Kubernetes API Server โดยตรง:
apiVersion: v1
kind: ConfigMap
metadata:
name: argocd-cm
namespace: argocd
data:
application.resourceTrackingMethod: annotation
diff.server.side: "true"
ข้อดีของ Server-Side Diff:
- ลดปัญหา False Positive Diffs อย่างมาก
- ความแม่นยำสูงขึ้นในการเปรียบเทียบ
- ลดการใช้ CPU บน Argo CD Server
- รองรับ Custom Resource Definitions (CRDs) ที่ซับซ้อน
ตัวอย่าง YAML สำหรับ Use Cases ต่างๆ
Helm Chart Application
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: helm-app
spec:
project: default
source:
repoURL: https://charts.example.com
chart: mychart
targetRevision: "1.0.0"
ignoreDifferences:
- group: ""
kind: ConfigMap
jqPathExpressions:
- .metadata.managedFields
- group: apps
kind: Deployment
jsonPointers:
- /spec/replicas
Kustomize Application
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: kustomize-app
spec:
project: default
source:
repoURL: https://github.com/example/repo
path: overlays/production
ignoreDifferences:
- group: batch
kind: CronJob
jsonPointers:
- /status
- group: ""
kind: ServiceAccount
jqPathExpressions:
- .metadata.managedFields
Stateful Application
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: stateful-app
spec:
project: default
ignoreDifferences:
- group: apps
kind: StatefulSet
jsonPointers:
- /status
- /spec/replicas
- group: ""
kind: Service
jsonPointers:
- /spec/clusterIP
- /metadata/resourceVersion
Best Practices สำหรับ Diffing Customization
- เพิ่มเฉพาะ Fields ที่จำเป็น: อย่า ignore Fields มากเกินไปเพราะจะทำให้ยากต่อการติดตามการเปลี่ยนแปลงที่สำคัญ
- เขียน Comments อธิบายเหตุผล: ระบุว่าทำไมต้อง ignore Field นี้เพื่อให้ทีมเข้าใจ
- ทดสอบก่อนนำไป Production: ตรวจสอบว่า Diffing Customization ไม่ทำให้ Drift Detection ล้มเหลว
- ใช้ jsonPointers สำหรับ Fields ธรรมดา: ใช้ jqPathExpressions เฉพาะเมื่อต้องการความยืดหยุ่น
- กำหนดค่า System-Wide สำหรับ Settings ทั่วไป: ใช้ argocd-cm ConfigMap สำหรับการตั้งค่าที่ใช้ร่วมกัน
- เปิดใช้ Server-Side Diff: สำหรับความแม่นยำที่สูงขึ้น
- ตรวจสอบ Logs: ใช้ Argo CD Logs เพื่อตรวจสอบว่า ignoreDifferences ทำงานถูกต้อง
- ปรับปรุงอย่างต่อเนื่อง: เมื่อพบ False Positive Diffs ใหม่ ให้เพิ่มการตั้งค่าเพิ่มเติม
การใช้งานร่วมกับ ผู้ให้บริการโฮสติ้ง Cloud VPS
สำหรับผู้ใช้งาน ผู้ให้บริการโฮสติ้ง Cloud VPS ที่รัน Argo CD ใน Kubernetes Cluster การตั้งค่า Diffing Customization จะช่วยให้ระบบ GitOps ทำงานได้อย่างมีประสิทธิภาพมากขึ้น
ด้วยการลดปัญหา False Positive Diffs คุณจะได้รับประโยชน์:
- ลดการใช้ CPU และ Memory ของ Kubernetes Cluster
- ทีม DevOps สามารถมุ่งเน้นไปยัง Drift ที่สำคัญจริงๆ
- Argo CD Sync Process ทำงานได้เร็วขึ้น
- ปรับปรุงความน่าเชื่อถือของระบบ GitOps บน Infrastructure ของ ผู้ให้บริการโฮสติ้ง
สรุป
Diffing Customization เป็นเครื่องมือสำคัญสำหรับการลดปัญหา False Positive Diffs ใน Argo CD โดยการใช้ ignoreDifferences สามารถระบุ Fields ที่ไม่จำเป็นต้องตรวจสอบได้อย่างแม่นยำ ด้วยการเข้าใจ jsonPointers และ jqPathExpressions คุณจะสามารถปรับแต่งพฤติกรรมการเปรียบเทียบของ Argo CD ได้ตามความต้องการ การตั้งค่าที่ถูกต้องจะช่วยให้ Argo CD ทำงานได้อย่างเชื่อถือได้และมีประสิทธิภาพบน ผู้ให้บริการโฮสติ้ง Cloud VPS

