Skip to content

Commit

Permalink
Add Icinga states to deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed May 23, 2024
1 parent 230491b commit 5d23e8d
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/deployment.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 Deployment struct {
Meta
Id types.Binary
Expand All @@ -22,6 +28,8 @@ type Deployment struct {
ReadyReplicas int32
AvailableReplicas int32
UnavailableReplicas int32
IcingaState string
IcingaStateReason string
Conditions []DeploymentCondition `db:"-"`
Labels []Label `db:"-"`
DeploymentLabels []DeploymentLabel `db:"-"`
Expand Down Expand Up @@ -72,6 +80,7 @@ func (d *Deployment) Obtain(k8s kmetav1.Object) {
d.AvailableReplicas = deployment.Status.AvailableReplicas
d.ReadyReplicas = deployment.Status.ReadyReplicas
d.UnavailableReplicas = deployment.Status.UnavailableReplicas
d.IcingaState, d.IcingaStateReason = getIcingaState(d)

for _, condition := range deployment.Status.Conditions {
d.Conditions = append(d.Conditions, DeploymentCondition{
Expand Down Expand Up @@ -99,6 +108,20 @@ func (d *Deployment) Obtain(k8s kmetav1.Object) {
}
}

func getIcingaState(d *Deployment) (string, string) {
switch {
case d.UnavailableReplicas > 0:
reason := fmt.Sprintf("Deployment %s has %d unavailable replicas", d.Name, d.UnavailableReplicas)
return Critical, reason
case d.AvailableReplicas < d.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", d.Name)
return Ok, reason
}
}

func (d *Deployment) Relations() []database.Relation {
fk := database.WithForeignKey("deployment_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 @@ -176,6 +176,8 @@ CREATE TABLE deployment (
ready_replicas int unsigned NOT NULL,
available_replicas int unsigned NOT NULL,
unavailable_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 5d23e8d

Please sign in to comment.