Skip to content

Example Deployment Health Notification

Example: Deployment Health Notification

The below HealthCheck YAML definition considers all the cluster Deployments. It matches any Deployment:

  1. With the number of available replicas matching the number of requested replicas, it is marked as Healthy;
  2. With the number of available replicas different than the number of requested replicas, it is marked as Progressing;
  3. With the number of unavailable replicas set and different than zero, it is marked as Degraded.
apiVersion: lib.projectsveltos.io/v1alpha1
kind: HealthCheck
metadata:
  name: deployment-replicas
spec:
  resourceSelectors:
  - group: "apps"
    version: v1
    kind: Deployment
  evaluateHealth: |
    function evaluate()
      local statuses = {}
      status = "Progressing"
      message = ""

      for _,resource in ipairs(resources) do
        if resource.status ~= nil then
          if resource.status.availableReplicas ~= nil then
            if resource.status.availableReplicas == resource.spec.replicas then
              status = "Healthy"
            else
              status = "Progressing"
              message = "expected replicas: " .. resource.spec.replicas .. " available: " .. resource.status.availableReplicas
            end
          end
          if resource.status.unavailableReplicas ~= nil then
            status = "Degraded"
            message = "deployments have unavailable replicas"
          end
        end
        table.insert(statuses, {resource=resource, status = status, message = message})
      end

      local hs = {}
      if #statuses > 0 then
        hs.resources = statuses 
      end
      return hs
    end