Skip to content

Commit

Permalink
Merge pull request #92 from Icinga/add-icinga-states-to-node
Browse files Browse the repository at this point in the history
Add Icinga states to node
  • Loading branch information
lippserd authored Jun 3, 2024
2 parents df6bddd + f7f833e commit 2a70704
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/schema/v1/node.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/pkg/errors"
Expand Down Expand Up @@ -37,6 +38,8 @@ type Node struct {
ContainerRuntimeVersion string
KubeletVersion string
KubeProxyVersion string
IcingaState IcingaState
IcingaStateReason string
Conditions []NodeCondition `db:"-"`
Volumes []NodeVolume `db:"-"`
Labels []Label `db:"-"`
Expand Down Expand Up @@ -122,6 +125,8 @@ func (n *Node) Obtain(k8s kmetav1.Object) {
}
n.Roles = strings.Join(roles, ", ")

n.IcingaState, n.IcingaStateReason = n.getIcingaState(node)

for _, condition := range node.Status.Conditions {
n.Conditions = append(n.Conditions, NodeCondition{
NodeUuid: n.Uuid,
Expand Down Expand Up @@ -185,6 +190,47 @@ func (n *Node) Obtain(k8s kmetav1.Object) {
}
}

func (n *Node) getIcingaState(node *kcorev1.Node) (IcingaState, string) {
if node.Status.Phase == kcorev1.NodePending {
return Pending, fmt.Sprintf("Node %s is pending", node.Name)
}

if node.Status.Phase == kcorev1.NodeTerminated {
return Ok, fmt.Sprintf("Node %s is terminated", node.Name)
}

var state IcingaState

if node.Status.Phase == kcorev1.NodeRunning {
var reason []string

for _, condition := range n.Conditions {
if condition.Status == string(kcorev1.ConditionTrue) {
switch condition.Type {
case string(kcorev1.NodeDiskPressure):
state = Critical
reason = append(reason, fmt.Sprintf("Node %s is running out of disk space", n.Name))
case string(kcorev1.NodeMemoryPressure):
state = Critical
reason = append(reason, fmt.Sprintf("Node %s is running out of available memory", n.Name))
case string(kcorev1.NodePIDPressure):
state = Critical
reason = append(reason, fmt.Sprintf("Node %s is running out of process IDs", n.Name))
case string(kcorev1.NodeNetworkUnavailable):
state = Critical
reason = append(reason, fmt.Sprintf("Node %s network is not correctly configured", n.Name))
}
}
}

if state != Ok {
return state, strings.Join(reason, ". ")
}
}

return Ok, fmt.Sprintf("Node %s is healthy", n.Name)
}

func (n *Node) Relations() []database.Relation {
fk := database.WithForeignKey("node_uuid")

Expand Down
2 changes: 2 additions & 0 deletions schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ CREATE TABLE node (
container_runtime_version varchar(255) NOT NULL,
kubelet_version varchar(255) NOT NULL,
kube_proxy_version varchar(255) 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 (uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
Expand Down

0 comments on commit 2a70704

Please sign in to comment.