Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Icinga states to node #92

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading