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 27, 2024
1 parent e596843 commit 3843fb3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions pkg/schema/v1/deployment.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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"
"github.com/icinga/icinga-kubernetes/pkg/strcase"
kappsv1 "k8s.io/api/apps/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
"time"
)

type Deployment struct {
Expand All @@ -23,6 +25,8 @@ type Deployment struct {
ReadyReplicas int32
AvailableReplicas int32
UnavailableReplicas int32
IcingaState IcingaState
IcingaStateReason string
Conditions []DeploymentCondition `db:"-"`
Labels []Label `db:"-"`
DeploymentLabels []DeploymentLabel `db:"-"`
Expand Down Expand Up @@ -73,6 +77,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 = d.getIcingaState()

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

func (d *Deployment) getIcingaState() (IcingaState, string) {
// Check if the Deployment was created within the last 5 minutes
createdTime := d.GetCreationTimestamp()
gracePeriodDeadline := createdTime.Add(5 * time.Minute)
now := time.Now()

// Wait for the grace period to end
if now.Before(gracePeriodDeadline) {
timeLeft := gracePeriodDeadline.Sub(now)
reason := fmt.Sprintf("Deployment %s/%s is within the grace period. Time left: %s", d.Namespace, d.Name, timeLeft.Round(time.Second))
return Ok, reason
}

for _, condition := range d.Conditions {
if condition.Type == "Available" && condition.Status != "True" {
reason := fmt.Sprintf("Deployment %s/%s is not available: %s", d.Namespace, d.Name, condition.Message)
return Critical, reason
}
if condition.Type == "ReplicaFailure" && condition.Status != "False" {
reason := fmt.Sprintf("Deployment %s/%s has replica failure: %s", d.Namespace, d.Name, condition.Message)
return Critical, reason
}
}

switch {
case d.UnavailableReplicas > 0:
reason := fmt.Sprintf("Deployment %s/%s has %d unavailable replicas", d.Namespace, d.Name, d.UnavailableReplicas)
return Critical, reason
case d.AvailableReplicas < d.DesiredReplicas:
reason := fmt.Sprintf("The number of available replicas is less than the number of desired replicas: %d available/%d desired", d.AvailableReplicas, d.DesiredReplicas)
return Warning, reason
case d.ReadyReplicas < d.DesiredReplicas:
reason := fmt.Sprintf("The number of ready replicas is less than the number of desired replicas: %d ready/%d desired", d.ReadyReplicas, d.DesiredReplicas)
return Warning, reason
case d.UpdatedReplicas < d.DesiredReplicas:
reason := fmt.Sprintf("The number of updated replicas is less than the number of desired replicas: %d updated/%d desired", d.UpdatedReplicas, d.DesiredReplicas)
return Warning, reason
default:
reason := fmt.Sprintf("Deployment %s/%s is functioning as expected", d.Namespace, 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 3843fb3

Please sign in to comment.