From f82ddba263299a651949069040e14bcfffaccb3f Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Thu, 16 May 2024 12:16:40 +0200 Subject: [PATCH] Add Icinga states to stateful set --- pkg/schema/v1/stateful_set.go | 21 +++++++++++++++++++++ schema/mysql/schema.sql | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/pkg/schema/v1/stateful_set.go b/pkg/schema/v1/stateful_set.go index a560354d..d18cde40 100644 --- a/pkg/schema/v1/stateful_set.go +++ b/pkg/schema/v1/stateful_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" @@ -28,6 +29,8 @@ type StatefulSet struct { UpdatedReplicas int32 AvailableReplicas int32 Yaml string + IcingaState IcingaState + IcingaStateReason string Conditions []StatefulSetCondition `db:"-"` Labels []Label `db:"-"` StatefulSetLabels []StatefulSetLabel `db:"-"` @@ -85,6 +88,7 @@ func (s *StatefulSet) Obtain(k8s kmetav1.Object) { s.CurrentReplicas = statefulSet.Status.CurrentReplicas s.UpdatedReplicas = statefulSet.Status.UpdatedReplicas s.AvailableReplicas = statefulSet.Status.AvailableReplicas + s.IcingaState, s.IcingaStateReason = s.getIcingaState() for _, condition := range statefulSet.Status.Conditions { s.Conditions = append(s.Conditions, StatefulSetCondition{ @@ -117,6 +121,23 @@ func (s *StatefulSet) Obtain(k8s kmetav1.Object) { s.Yaml = string(output) } +func (s *StatefulSet) getIcingaState() (IcingaState, string) { + switch { + case s.AvailableReplicas == 0: + reason := fmt.Sprintf("StatefulSet %s/%s has no replica available from %d desired", s.Namespace, s.Name, s.DesiredReplicas) + + return Critical, reason + case s.AvailableReplicas < s.DesiredReplicas: + reason := fmt.Sprintf("StatefulSet %s/%s only has %d out of %d desired replicas available", s.Namespace, s.Name, s.AvailableReplicas, s.DesiredReplicas) + + return Warning, reason + default: + reason := fmt.Sprintf("StatefulSet %s/%s has all %d desired replicas available", s.Namespace, s.Name, s.DesiredReplicas) + + return Ok, reason + } +} + func (s *StatefulSet) Relations() []database.Relation { fk := database.WithForeignKey("stateful_set_uuid") diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index 85a18094..d3084b98 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -435,7 +435,12 @@ CREATE TABLE stateful_set ( current_replicas int unsigned NOT NULL, updated_replicas int unsigned NOT NULL, available_replicas int unsigned NOT NULL, +<<<<<<< HEAD yaml mediumblob DEFAULT NULL, +======= + icinga_state enum('ok', 'warning', 'critical', 'unknown') COLLATE utf8mb4_unicode_ci NOT NULL, + icinga_state_reason text NOT NULL, +>>>>>>> baae465 (Add Icinga states to stateful set) created bigint unsigned NOT NULL, PRIMARY KEY (uuid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;