Skip to content

Commit

Permalink
feat: Canary health check
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Dec 16, 2024
1 parent fe7d410 commit 2a2023d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,25 @@ func GetHealthCheckFunc(gvk schema.GroupVersionKind) func(obj *unstructured.Unst
case "Application":
return getArgoApplicationHealth
}
case "canaries.flanksource.com":
switch gvk.Kind {
case "Canary":
return getCanaryHealth
// case "Component":
// case "Topology":
}
// case "configs.flanksource.com":
// switch gvk.Kind {
// case "ScrapeConfig":
// case "ScrapePlugin":
// }
// case "mission-control.flanksource.com":
// switch gvk.Kind {
// case "Notification":
// case "Playbook":
// case "NotificationSilence":
// case "Connection":
// }
case "kustomize.toolkit.fluxcd.io", "helm.toolkit.fluxcd.io", "source.toolkit.fluxcd.io":
return GetDefaultHealth
case "cert-manager.io":
Expand Down
68 changes: 68 additions & 0 deletions pkg/health/health_flanksource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package health

import (
"regexp"
"strconv"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

var re = regexp.MustCompile(`(?:\((\d+\.?\d*)%\))|(\d+\.?\d*)%`)

func getCanaryHealth(obj *unstructured.Unstructured) (*HealthStatus, error) {
errorMsg, _, err := unstructured.NestedString(obj.Object, "status", "errorMessage")
if err != nil {
return nil, err
}

if errorMsg != "" {
return &HealthStatus{
Message: errorMsg,
Health: HealthUnhealthy,
}, nil
}

message, _, _ := unstructured.NestedString(obj.Object, "status", "message")
canaryStatus, _, _ := unstructured.NestedString(obj.Object, "status", "status")
uptime1h, _, _ := unstructured.NestedString(obj.Object, "status", "uptime1h")

output := HealthStatus{
Message: message,
Status: HealthStatusCode(canaryStatus),
Ready: true,
}

switch canaryStatus {
case "Passed":
output.Health = HealthHealthy
if uptime := parseCanaryUptime(uptime1h); uptime != nil && *uptime < float64(80) {
output.Health = HealthWarning
}
case "Failed":
output.Health = HealthUnhealthy
case "Invalid":
output.Health = HealthUnhealthy
output.Ready = false // needs manual intervention
}

return &output, nil
}

func parseCanaryUptime(uptime string) *float64 {
matches := re.FindStringSubmatch(uptime)
var matched string
if len(matches) > 0 {
if matches[1] != "" {
matched = matches[1]
} else if matches[2] != "" {
matched = matches[2]
}
}

v, err := strconv.ParseFloat(matched, 64)
if err != nil {
return nil
}

return &v
}

0 comments on commit 2a2023d

Please sign in to comment.