Skip to content

Commit

Permalink
Add Icinga states to pod
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed May 14, 2024
1 parent 230491b commit 773bbe5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
67 changes: 67 additions & 0 deletions pkg/schema/v1/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"strings"
)

const Ok = "ok"
const Warning = "warning"
const Critical = "critical"
const Unknown = "unknown"

type PodFactory struct {
clientset *kubernetes.Clientset
}
Expand All @@ -23,6 +28,7 @@ type Pod struct {
NominatedNodeName string
Ip string
Phase string
IcingaState string
CpuLimits int64
CpuRequests int64
MemoryLimits int64
Expand Down Expand Up @@ -99,6 +105,7 @@ func (p *Pod) Obtain(k8s kmetav1.Object) {
p.NominatedNodeName = pod.Status.NominatedNodeName
p.Ip = pod.Status.PodIP
p.Phase = strcase.Snake(string(pod.Status.Phase))
p.IcingaState = getPodMonitoringState(pod)
p.Reason = pod.Status.Reason
p.Message = pod.Status.Message
p.Qos = strcase.Snake(string(pod.Status.QOSClass))
Expand Down Expand Up @@ -269,6 +276,66 @@ func (p *Pod) Obtain(k8s kmetav1.Object) {
}
}

func getPodMonitoringState(pod *kcorev1.Pod) string {
readyContainers := 0
state := Unknown

if pod.DeletionTimestamp != nil {
return Ok
}

initializing := false
for _, container := range pod.Status.InitContainerStatuses {
switch {
case container.State.Terminated != nil && container.State.Terminated.ExitCode == 0:
continue
case container.State.Terminated != nil:
state = Critical
initializing = true
case container.State.Waiting != nil && len(container.State.Waiting.Reason) > 0 && container.State.Waiting.Reason != "PodInitializing":
state = Critical
initializing = true
default:
initializing = true
}
break
}
if !initializing {
for _, container := range pod.Status.ContainerStatuses {
if !container.Ready {
state = Critical
}
if container.State.Waiting != nil && container.State.Waiting.Reason != "" && container.RestartCount >= 3 {
state = Critical
} else if container.State.Terminated != nil && container.State.Terminated.Reason != "" && container.State.Terminated.ExitCode == 0 {
state = Ok
} else if container.State.Terminated != nil && container.State.Terminated.Reason == "" {
state = Critical
} else if container.Ready && container.State.Running != nil {
readyContainers++
state = Ok
}
}
}

for _, condition := range pod.Status.Conditions {
if pod.Status.Phase == kcorev1.PodRunning {
if condition.Type == kcorev1.PodReady && condition.Status == kcorev1.ConditionTrue {
state = Ok
}
if condition.Type == kcorev1.PodReady && condition.Status == kcorev1.ConditionFalse {
state = Critical
}
}
}

if readyContainers == len(pod.Spec.Containers) {
state = Ok
}

return state
}

func (p *Pod) Relations() []database.Relation {
fk := database.WithForeignKey("pod_id")

Expand Down
1 change: 1 addition & 0 deletions schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ CREATE TABLE pod (
memory_limits bigint unsigned NOT NULL,
memory_requests bigint unsigned NOT NULL,
phase enum('pending', 'running', 'succeeded', 'failed') COLLATE utf8mb4_unicode_ci NOT NULL,
icinga_state enum('ok', 'warning', 'critical', 'unknown') COLLATE utf8mb4_unicode_ci NOT NULL,
reason varchar(255) NULL DEFAULT NULL,
message varchar(255) NULL DEFAULT NULL,
qos enum('guaranteed', 'burstable', 'best_effort') COLLATE utf8mb4_unicode_ci NOT NULL,
Expand Down

0 comments on commit 773bbe5

Please sign in to comment.