From 4e22941cf158f920a9325b922fc177d17cbf1c5d Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Tue, 14 May 2024 09:26:38 +0200 Subject: [PATCH] Add Icinga states to replica set --- pkg/schema/v1/replica_set.go | 28 ++++++++++++++++++++++++++++ schema/mysql/schema.sql | 2 ++ 2 files changed, 30 insertions(+) diff --git a/pkg/schema/v1/replica_set.go b/pkg/schema/v1/replica_set.go index 670201aa..b271add0 100644 --- a/pkg/schema/v1/replica_set.go +++ b/pkg/schema/v1/replica_set.go @@ -1,6 +1,7 @@ package v1 import ( + "fmt" "github.com/icinga/icinga-kubernetes/pkg/database" "github.com/icinga/icinga-kubernetes/pkg/strcase" "github.com/icinga/icinga-kubernetes/pkg/types" @@ -10,6 +11,11 @@ import ( "strings" ) +const OK = "ok" +const Warning = "warning" +const Critical = "critical" +const Unknown = "unknown" + type ReplicaSet struct { Meta Id types.Binary @@ -19,6 +25,8 @@ type ReplicaSet struct { FullyLabeledReplicas int32 ReadyReplicas int32 AvailableReplicas int32 + IcingaState string + IcingaStateReason string Conditions []ReplicaSetCondition `db:"-"` Owners []ReplicaSetOwner `db:"-"` Labels []Label `db:"-"` @@ -68,6 +76,7 @@ func (r *ReplicaSet) Obtain(k8s kmetav1.Object) { r.FullyLabeledReplicas = replicaSet.Status.FullyLabeledReplicas r.ReadyReplicas = replicaSet.Status.ReadyReplicas r.AvailableReplicas = replicaSet.Status.AvailableReplicas + r.IcingaState, r.IcingaStateReason = getReplicaSetMonitoringState(r) for _, condition := range replicaSet.Status.Conditions { r.Conditions = append(r.Conditions, ReplicaSetCondition{ @@ -118,6 +127,25 @@ func (r *ReplicaSet) Obtain(k8s kmetav1.Object) { } } +func getReplicaSetMonitoringState(r *ReplicaSet) (string, string) { + if r.DesiredReplicas < 1 { + reason := fmt.Sprintf("Replica Set %s has an invalid desired replica count: %d", r.Name, r.DesiredReplicas) + return Unknown, reason + } + + switch { + case r.AvailableReplicas < 1: + reason := fmt.Sprintf("Replica Set %s has no available replicas", r.Name) + return Critical, reason + case r.AvailableReplicas < r.DesiredReplicas: + reason := "The number of desired replicas exceeds the number of available replicas" + return Warning, reason + default: + reason := fmt.Sprintf("Replica Set %s is functioning as expected", r.Name) + return OK, reason + } +} + func (r *ReplicaSet) Relations() []database.Relation { fk := database.WithForeignKey("replica_set_id") diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index 9f8c0229..d4fc221d 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -353,6 +353,8 @@ CREATE TABLE replica_set ( fully_labeled_replicas int unsigned NOT NULL, ready_replicas int unsigned NOT NULL, available_replicas int unsigned NOT 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 (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;