เขียน Custom Health Check สำหรับ CRD และ Resource พิเศษใน Argo CD

Argo CD มี Built-in Health Check สำหรับ Kubernetes resource แบบมาตรฐาน เช่น Deployment, StatefulSet, Service เป็นต้น แต่เมื่อคุณใช้งาน Custom Resource Definition (CRD) หรือ Resource พิเศษจาก third-party operators ระบบต้องการ Custom Health Check เพื่อตรวจสอบสถานะที่แท้จริง บทความนี้จะแนะนำวิธีเขียน Custom Health Check ด้วย Lua script อย่างมืออาชีพ

ทำไมต้อง Custom Health Check

Built-in Health Check ของ Argo CD ไม่สามารถครอบคลุม Custom CRD ได้ทั้งหมด เพราะ:

  • CRD เฉพาะทาง: คุณอาจพัฒนา custom CRD สำหรับ business logic เฉพาะ และต้องการให้ Argo CD รู้ว่า CRD นั้น deploy สำเร็จหรือไม่
  • Third-party Operators: Operators เช่น Cert-Manager, Apache Kafka Operator, MongoDB Operator มี status field ที่ไม่ standard
  • Complex Conditions: Health status ไม่ได้ขึ้นกับ phase เพียงอย่างเดียว แต่ต้องตรวจสอบ multiple conditions หรือ sub-resources
  • Dependency Tracking: บาง CRD มี dependencies บน resources อื่นๆ ที่ต้องตรวจสอบด้วย

วิธีเขียน Custom Health Check ด้วย Lua Script

Argo CD ใช้ Lua scripting language สำหรับ Custom Health Check เพราะมันมีขนาดเล็กและมีประสิทธิภาพสำหรับการประเมิน resource status

โครงสร้างพื้นฐานของ Lua Script

-- Lua script สำหรับ Custom Health Check
hs = {}
if obj.status ~= nil then
    if obj.status.phase == "Ready" then
        hs.status = "Healthy"
        hs.message = "Resource is ready"
    elseif obj.status.phase == "Pending" then
        hs.status = "Progressing"
        hs.message = "Resource is being provisioned"
    else
        hs.status = "Degraded"
        hs.message = "Resource has issue"
    end
else
    hs.status = "Progressing"
    hs.message = "Waiting for status"
end
return hs

การตั้งค่า Custom Health Check ใน argocd-cm ConfigMap

Custom Health Check ถูกเก็บไว้ใน ConfigMap ชื่อ argocd-cm ใน argocd namespace โดยใช้ key format: resource.customizations.health.GROUP_KIND

ConfigMap Structure

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-cm
  namespace: argocd
data:
  resource.customizations.health.cert-manager.io_Certificate: |
    hs = {}
    if obj.status ~= nil then
      if obj.status.conditions ~= nil then
        for i, condition in ipairs(obj.status.conditions) do
          if condition.type == "Ready" and condition.status == "True" then
            hs.status = "Healthy"
            hs.message = "Certificate is valid"
            return hs
          end
        end
      end
    end
    hs.status = "Progressing"
    hs.message = "Waiting for certificate"
    return hs

ตัวอย่าง Lua Script สำหรับ CRD ต่างๆ

1. Certificate (Cert-Manager)

hs = {}
if obj.status ~= nil and obj.status.renewalTime ~= nil then
    hs.status = "Healthy"
    hs.message = "Certificate issued and valid"
    return hs
end
if obj.status ~= nil and obj.status.conditions ~= nil then
    for i, condition in ipairs(obj.status.conditions) do
        if condition.type == "Ready" then
            if condition.status == "True" then
                hs.status = "Healthy"
            else
                hs.status = "Degraded"
            end
            hs.message = condition.message
            return hs
        end
    end
end
hs.status = "Progressing"
hs.message = "Certificate is being processed"
return hs

2. Kafka (Strimzi Operator)

hs = {}
if obj.status ~= nil and obj.status.conditions ~= nil then
    for i, condition in ipairs(obj.status.conditions) do
        if condition.type == "Ready" then
            if condition.status == "True" then
                hs.status = "Healthy"
            else
                hs.status = "Degraded"
            end
            hs.message = condition.message
            return hs
        end
    end
end
hs.status = "Progressing"
hs.message = "Kafka cluster is initializing"
return hs

3. Custom Database Resource

hs = {}
if obj.status ~= nil and obj.status.phase == "Running" then
    if obj.status.readyReplicas == obj.spec.size then
        hs.status = "Healthy"
        hs.message = string.format("Database running with %d/%d replicas",
                                   obj.status.readyReplicas, obj.spec.size)
    else
        hs.status = "Progressing"
        hs.message = string.format("Database: %d/%d replicas ready",
                                   obj.status.readyReplicas or 0, obj.spec.size)
    end
    return hs
elseif obj.status ~= nil and obj.status.phase == "Failed" then
    hs.status = "Degraded"
    hs.message = obj.status.lastError or "Database failed"
    return hs
end
hs.status = "Progressing"
hs.message = "Database initializing"
return hs

การ Override Built-in Health Check

บาง resource type มี Built-in Health Check ที่ไม่เหมาะสมกับระบบของคุณ สามารถ override ได้โดยใช้ key เดียวกัน:

apiVersion: v1
kind: ConfigMap
metadata:
  name: argocd-cm
  namespace: argocd
data:
  resource.customizations.health.apps_Deployment: |
    hs = {}
    if obj.status ~= nil then
      if obj.status.replicas ~= nil and obj.status.updatedReplicas ~= nil then
        if obj.status.updatedReplicas == obj.spec.replicas then
          hs.status = "Healthy"
          hs.message = "All replicas updated"
        else
          hs.status = "Progressing"
          hs.message = string.format("%d/%d replicas updated",
                                    obj.status.updatedReplicas, obj.spec.replicas)
        end
        return hs
      end
    end
    hs.status = "Unknown"
    hs.message = "Unable to determine status"
    return hs

การ Debug Custom Health Check

ตรวจสอบ ConfigMap

# ดู Custom Health Check ทั้งหมดใน argocd-cm
kubectl get configmap argocd-cm -n argocd -o yaml | grep -A 20 resource.customizations.health

ดู Application Resource Health Status

# ดู health status ของแต่ละ resource
kubectl get application myapp -n argocd -o jsonpath='{.status.resources[*].health}'

# ดู Argo CD Application Controller Logs
kubectl logs -n argocd -l app.kubernetes.io/name=argocd-application-controller -f | grep health

Best Practices สำหรับ Custom Health Check

  • Nil Checking เสมอ: ตรวจสอบว่า fields มีค่าก่อนที่จะเข้าถึง เพื่อหลีกเลี่ยง nil pointer errors
  • Message ที่ชัดเจน: ให้ message บอกปัญหาที่ชัดเจน เพื่อง่ายต่อการ debug
  • ทดสอบทุก Conditions: ทดสอบทุก status conditions ที่เป็นไปได้ เช่น pending, ready, failed, updating
  • Performance: เขียน script ให้มีประสิทธิภาพ เพราะ Argo CD จะ evaluate บ่อยๆ
  • Documentation: เพิ่ม comment ในแต่ละ script เพื่อให้ทีมเข้าใจ
  • Version Control: เก็บ Custom Health Check ใน Git repository พร้อมกับ application manifest

การใช้งานร่วมกับ ผู้ให้บริการโฮสติ้ง Cloud VPS

เมื่อ deploy Argo CD บน ผู้ให้บริการโฮสติ้ง Cloud VPS คุณจะได้ประโยชน์จาก infrastructure ที่เสถียรสำหรับการรัน Custom Health Check อย่างต่อเนื่อง Cloud VPS ของ ผู้ให้บริการโฮสติ้ง มี SLA 99.9% uptime พร้อม firewall และ DDoS protection ที่ช่วยปกป้อง Argo CD infrastructure ของคุณ

สรุป

Custom Health Check ใน Argo CD เป็นพื้นฐานสำคัญสำหรับการ monitor CRD และ resource พิเศษ การเขียน Lua script ที่ดีจะทำให้ Argo CD สามารถประเมิน health status ที่แท้จริงและ react ได้อย่างถูกต้อง เมื่อใช้งานบน ผู้ให้บริการโฮสติ้ง Cloud VPS คุณจะมี infrastructure ที่มั่นคงรองรับการทำงาน Argo CD อย่าง seamless