Skip to content

Commit

Permalink
Add Icinga states to stateful set
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed May 23, 2024
1 parent 230491b commit 96cf0fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/schema/v1/stateful_set.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -9,6 +10,11 @@ import (
"strings"
)

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

type StatefulSet struct {
Meta
Id types.Binary
Expand All @@ -25,6 +31,8 @@ type StatefulSet struct {
CurrentReplicas int32
UpdatedReplicas int32
AvailableReplicas int32
IcingaState string
IcingaStateReason string
Conditions []StatefulSetCondition `db:"-"`
Labels []Label `db:"-"`
StatefulSetLabels []StatefulSetLabel `db:"-"`
Expand Down Expand Up @@ -82,6 +90,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 = getIcingaState(s)

for _, condition := range statefulSet.Status.Conditions {
s.Conditions = append(s.Conditions, StatefulSetCondition{
Expand All @@ -108,6 +117,20 @@ func (s *StatefulSet) Obtain(k8s kmetav1.Object) {
}
}

func getIcingaState(s *StatefulSet) (string, string) {
switch {
case s.AvailableReplicas == 0:
reason := fmt.Sprintf("Stateful Set %s has 0 available replicas", s.Name)
return Critical, reason
case s.AvailableReplicas < s.DesiredReplicas:
reason := "The number of desired replicas exceeds the number of available replicas"
return Warning, reason
default:
reason := fmt.Sprintf("Stateful Set %s is functioning as expected", s.Name)
return Ok, reason
}
}

func (s *StatefulSet) Relations() []database.Relation {
fk := database.WithForeignKey("stateful_set_id")

Expand Down
2 changes: 2 additions & 0 deletions schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ CREATE TABLE stateful_set (
current_replicas int unsigned NOT NULL,
updated_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;
Expand Down

0 comments on commit 96cf0fb

Please sign in to comment.