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 3, 2024
1 parent 51b7b34 commit f82ddba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
21 changes: 21 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-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/strcase"
Expand Down Expand Up @@ -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:"-"`
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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")

Expand Down
5 changes: 5 additions & 0 deletions schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f82ddba

Please sign in to comment.