From a907366a50346ef33e0d9daf13a044940dbad3d6 Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Thu, 16 May 2024 10:04:18 +0200 Subject: [PATCH 1/2] Add Icinga states to daemon set --- pkg/schema/v1/daemon_set.go | 27 +++++++++++++++++++++++++++ schema/mysql/schema.sql | 2 ++ 2 files changed, 29 insertions(+) diff --git a/pkg/schema/v1/daemon_set.go b/pkg/schema/v1/daemon_set.go index 3a3621e2..ac3ea931 100644 --- a/pkg/schema/v1/daemon_set.go +++ b/pkg/schema/v1/daemon_set.go @@ -1,6 +1,7 @@ package v1 import ( + "fmt" "github.com/icinga/icinga-go-library/types" "github.com/icinga/icinga-kubernetes/pkg/database" "github.com/icinga/icinga-kubernetes/pkg/strcase" @@ -24,6 +25,8 @@ type DaemonSet struct { NumberAvailable int32 NumberUnavailable int32 Yaml string + IcingaState IcingaState + IcingaStateReason string Conditions []DaemonSetCondition `db:"-"` Labels []Label `db:"-"` DaemonSetLabels []DaemonSetLabel `db:"-"` @@ -61,6 +64,7 @@ func (d *DaemonSet) Obtain(k8s kmetav1.Object) { d.UpdateNumberScheduled = daemonSet.Status.UpdatedNumberScheduled d.NumberAvailable = daemonSet.Status.NumberAvailable d.NumberUnavailable = daemonSet.Status.NumberUnavailable + d.IcingaState, d.IcingaStateReason = d.getIcingaState() for _, condition := range daemonSet.Status.Conditions { d.Conditions = append(d.Conditions, DaemonSetCondition{ @@ -92,6 +96,29 @@ func (d *DaemonSet) Obtain(k8s kmetav1.Object) { d.Yaml = string(output) } +func (d *DaemonSet) getIcingaState() (IcingaState, string) { + if d.DesiredNumberScheduled < 1 { + reason := fmt.Sprintf("DaemonSet %s/%s has an invalid desired node count: %d", d.Namespace, d.Name, d.DesiredNumberScheduled) + + return Unknown, reason + } + + switch { + case d.NumberAvailable == 0: + reason := fmt.Sprintf("DaemonSet %s/%s does not have a single pod available which should run on %d desired nodes", d.Namespace, d.Name, d.DesiredNumberScheduled) + + return Critical, reason + case d.NumberAvailable < d.DesiredNumberScheduled: + reason := fmt.Sprintf("DaemonSet %s/%s pods are only available on %d out of %d desired nodes", d.Namespace, d.Name, d.NumberAvailable, d.DesiredNumberScheduled) + + return Warning, reason + default: + reason := fmt.Sprintf("DaemonSet %s/%s has pods available on all %d desired nodes", d.Namespace, d.Name, d.DesiredNumberScheduled) + + return Ok, reason + } +} + func (d *DaemonSet) Relations() []database.Relation { fk := database.WithForeignKey("daemon_set_uuid") diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index 85a18094..4c0bc7ea 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -402,6 +402,8 @@ CREATE TABLE daemon_set ( number_available int unsigned NOT NULL, number_unavailable int unsigned NOT NULL, yaml mediumblob DEFAULT NULL, + icinga_state enum('ok', 'warning', 'critical', 'unknown') COLLATE utf8mb4_unicode_ci NOT NULL, + icinga_state_reason text NOT NULL, created bigint unsigned NOT NULL, PRIMARY KEY (uuid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; From b485ff46cb60fc7fd50ba789a9e54c36662e57a2 Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Mon, 27 May 2024 14:09:28 +0200 Subject: [PATCH 2/2] Add grace period check for DaemonSet availability --- pkg/schema/v1/daemon_set.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/schema/v1/daemon_set.go b/pkg/schema/v1/daemon_set.go index ac3ea931..d94568b3 100644 --- a/pkg/schema/v1/daemon_set.go +++ b/pkg/schema/v1/daemon_set.go @@ -103,6 +103,10 @@ func (d *DaemonSet) getIcingaState() (IcingaState, string) { return Unknown, reason } + if gracePeriodReason := IsWithinGracePeriod(d); gracePeriodReason != nil { + return Ok, *gracePeriodReason + } + switch { case d.NumberAvailable == 0: reason := fmt.Sprintf("DaemonSet %s/%s does not have a single pod available which should run on %d desired nodes", d.Namespace, d.Name, d.DesiredNumberScheduled)