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 Jun 2, 2024
1 parent 5c4df9b commit baae465
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
33 changes: 33 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-go-library/types"
"github.com/icinga/icinga-go-library/utils"
"github.com/icinga/icinga-kubernetes/pkg/database"
Expand All @@ -26,6 +27,8 @@ type StatefulSet struct {
CurrentReplicas int32
UpdatedReplicas int32
AvailableReplicas int32
IcingaState IcingaState
IcingaStateReason string
Conditions []StatefulSetCondition `db:"-"`
Labels []Label `db:"-"`
StatefulSetLabels []StatefulSetLabel `db:"-"`
Expand Down Expand Up @@ -83,6 +86,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{
Expand All @@ -109,6 +113,35 @@ func (s *StatefulSet) Obtain(k8s kmetav1.Object) {
}
}

func (s *StatefulSet) getIcingaState() (IcingaState, string) {
switch {
case s.AvailableReplicas == 0:
reason := fmt.Sprintf("StatefulSet %s/%s has 0 available replicas", s.Namespace, s.Name)

return Critical, reason
case s.AvailableReplicas < s.DesiredReplicas:
reason := fmt.Sprintf("The number of available replicas is less than the number of desired replicas: %d current/%d desired", s.AvailableReplicas, s.DesiredReplicas)

return Warning, reason
case s.ReadyReplicas < s.DesiredReplicas:
reason := fmt.Sprintf("The number of ready replicas is less than the number of desired replicas: %d ready/%d desired", s.ReadyReplicas, s.DesiredReplicas)

return Warning, reason
case s.CurrentReplicas != s.DesiredReplicas:
reason := fmt.Sprintf("StatefulSet %s/%s has a mismatch between current number of replicas and desired number of replicas: %d current/%d desired", s.Namespace, s.Name, s.CurrentReplicas, s.DesiredReplicas)

return Warning, reason
case s.UpdatedReplicas < s.DesiredReplicas:
reason := fmt.Sprintf("The number of updated replicas is less than the number of desired replicas: %d updated/%d desired", s.UpdatedReplicas, s.DesiredReplicas)

return Warning, reason
default:
reason := fmt.Sprintf("StatefulSet %s/%s is functioning as expected", s.Namespace, 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 @@ -427,6 +427,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 baae465

Please sign in to comment.