diff --git a/pkg/schema/v1/deployment.go b/pkg/schema/v1/deployment.go index b6f1b63d..14cb89da 100644 --- a/pkg/schema/v1/deployment.go +++ b/pkg/schema/v1/deployment.go @@ -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" @@ -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 @@ -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:"-"` @@ -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{ @@ -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") diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index 9f8c0229..19546052 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -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;