Skip to content

Commit

Permalink
Add Icinga states to replica set
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed May 23, 2024
1 parent 230491b commit 4e22941
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/schema/v1/replica_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 @@ -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
Expand All @@ -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:"-"`
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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")

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

0 comments on commit 4e22941

Please sign in to comment.