Skip to content

Commit

Permalink
fix: pod health fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Nov 20, 2024
1 parent ee8838c commit ca04daa
Show file tree
Hide file tree
Showing 17 changed files with 842 additions and 383 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/flanksource/is-healthy

go 1.20
go 1.22.0

toolchain go1.22.9

require (
github.com/cert-manager/cert-manager v1.9.0
Expand All @@ -25,6 +27,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func main() {
os.Exit(1)
}

fmt.Printf("%s: %s\n", _health.Status, _health.Message)
fmt.Printf("%s\n", *_health)

if health.IsWorse(health.HealthStatusHealthy, _health.Status) {
if _health.Health.IsWorseThan(health.HealthWarning) {
os.Exit(1)
}
}
92 changes: 54 additions & 38 deletions pkg/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/samber/lo"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/duration"
Expand All @@ -27,6 +28,47 @@ func IsValidHealth(s string) bool {
s == string(HealthWarning)
}

var healthOrder = []Health{
HealthUnknown,
HealthHealthy,
HealthWarning,
HealthUnhealthy,
}

func (h Health) Worst(others ...Health) Health {
all := append(others, h)
slices.SortFunc(all, CompareHealth)
return all[len(all)-1]
}

func (h Health) IsWorseThan(other Health) bool {
return h.CompareTo(other) >= 0
}

func CompareHealth(a, b Health) int {
return a.CompareTo(b)
}

func (h Health) CompareTo(other Health) int {
currentIndex := 0
newIndex := 0
for i, code := range healthOrder {
if h == code {
currentIndex = i
}
if other == code {
newIndex = i
}
}
if newIndex == currentIndex {
return 0
}
if currentIndex > newIndex {
return 1
}
return -1
}

// Represents resource health status
type HealthStatusCode string

Expand Down Expand Up @@ -64,53 +106,27 @@ const (
HealthStatusScaling HealthStatusCode = "Scaling"
HealthStatusRestart HealthStatusCode = "Restarting"
HealthStatusStarting HealthStatusCode = "Starting"
HealthStatusFailed HealthStatusCode = "Failed"
HealthStatusUnschedulable HealthStatusCode = "Unschedulable"
HealthStatusUpgradeFailed HealthStatusCode = "UpgradeFailed"

HealthStatusScalingUp HealthStatusCode = "Scaling Up"
HealthStatusScaledToZero HealthStatusCode = "Scaled to Zero"
HealthStatusScalingDown HealthStatusCode = "Scaling Down"
HealthStatusRunning HealthStatusCode = "Running"

HealthStatusRollingOut HealthStatusCode = "Rolling Out"

HealthStatusUnhealthy HealthStatusCode = "Unhealthy"
HealthStatusUpdating HealthStatusCode = "Updating"
HealthStatusWarning HealthStatusCode = "Warning"
HealthStatusStopped HealthStatusCode = "Stopped"
HealthStatusStopping HealthStatusCode = "Stopping"
HealthStatusOOMKilled HealthStatusCode = "OOMKilled"
HealthStatusScalingUp HealthStatusCode = "Scaling Up"
HealthStatusScaledToZero HealthStatusCode = "Scaled to Zero"
HealthStatusScalingDown HealthStatusCode = "Scaling Down"
HealthStatusRunning HealthStatusCode = "Running"
HealthStatusRollingOut HealthStatusCode = "Rolling Out"
HealthStatusUnhealthy HealthStatusCode = "Unhealthy"
HealthStatusUpdating HealthStatusCode = "Updating"
HealthStatusWarning HealthStatusCode = "Warning"
HealthStatusStopped HealthStatusCode = "Stopped"
HealthStatusStopping HealthStatusCode = "Stopping"
)

// Implements custom health assessment that overrides built-in assessment
type HealthOverride interface {
GetResourceHealth(obj *unstructured.Unstructured) (*HealthStatus, error)
}

// healthOrder is a list of health codes in order of most healthy to least healthy
var healthOrder = []HealthStatusCode{
HealthStatusHealthy,
HealthStatusSuspended,
HealthStatusProgressing,
HealthStatusMissing,
HealthStatusDegraded,
HealthStatusUnknown,
}

// IsWorse returns whether or not the new health status code is a worse condition than the current
func IsWorse(current, new HealthStatusCode) bool {
currentIndex := 0
newIndex := 0
for i, code := range healthOrder {
if current == code {
currentIndex = i
}
if new == code {
newIndex = i
}
}
return newIndex > currentIndex
}

func get(obj map[string]any, keys ...string) string {
v, _, _ := unstructured.NestedString(obj, keys...)
return strings.TrimSpace(v)
Expand Down
Loading

0 comments on commit ca04daa

Please sign in to comment.