Skip to content

Commit

Permalink
Merge pull request #73 from Icinga/add-icinga-states-to-replica-set
Browse files Browse the repository at this point in the history
Add Icinga states to replica set
  • Loading branch information
lippserd authored Jun 3, 2024
2 parents de1cc58 + 51b7b34 commit 641f02b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/schema/v1/contracts.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package v1

import (
"fmt"
"github.com/google/uuid"
"github.com/icinga/icinga-go-library/types"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ktypes "k8s.io/apimachinery/pkg/types"
kcache "k8s.io/client-go/tools/cache"
"time"
)

var NameSpaceKubernetes = uuid.MustParse("3f249403-2bb0-428f-8e91-504d1fd7ddb6")
Expand Down Expand Up @@ -75,6 +78,25 @@ func NewUUID(space types.UUID, data string) types.UUID {
return types.UUID{UUID: uuid.NewSHA1(space.UUID, []byte(data))}
}

func IsWithinGracePeriod(k kmetav1.Object) *string {
const gracePeriod = 5 * time.Minute

deadline := k.GetCreationTimestamp().Add(gracePeriod)
now := time.Now()
if now.Before(deadline) {
key, _ := kcache.MetaNamespaceKeyFunc(k)
reason := fmt.Sprintf(
"%s %s is within grace period until %s, so its state is not yet evaluated.",
types.Name(k),
key,
deadline)

return &reason
}

return nil
}

// Assert interface compliance.
var (
_ kmetav1.Object = (*Meta)(nil)
Expand Down
40 changes: 40 additions & 0 deletions pkg/schema/v1/replica_set.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package v1

import (
"fmt"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/strcase"
kappsv1 "k8s.io/api/apps/v1"
kcorev1 "k8s.io/api/core/v1"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
kserializer "k8s.io/apimachinery/pkg/runtime/serializer"
Expand All @@ -22,6 +24,8 @@ type ReplicaSet struct {
ReadyReplicas int32
AvailableReplicas int32
Yaml string
IcingaState IcingaState
IcingaStateReason string
Conditions []ReplicaSetCondition `db:"-"`
Owners []ReplicaSetOwner `db:"-"`
Labels []Label `db:"-"`
Expand Down Expand Up @@ -70,6 +74,7 @@ func (r *ReplicaSet) Obtain(k8s kmetav1.Object) {
r.FullyLabeledReplicas = replicaSet.Status.FullyLabeledReplicas
r.ReadyReplicas = replicaSet.Status.ReadyReplicas
r.AvailableReplicas = replicaSet.Status.AvailableReplicas
r.IcingaState, r.IcingaStateReason = r.getIcingaState()

for _, condition := range replicaSet.Status.Conditions {
r.Conditions = append(r.Conditions, ReplicaSetCondition{
Expand Down Expand Up @@ -126,6 +131,41 @@ func (r *ReplicaSet) Obtain(k8s kmetav1.Object) {
r.Yaml = string(output)
}

func (r *ReplicaSet) getIcingaState() (IcingaState, string) {
if r.DesiredReplicas < 1 {
reason := fmt.Sprintf("ReplicaSet %s/%s has an invalid desired replica count: %d", r.Namespace, r.Name, r.DesiredReplicas)

return Unknown, reason
}

if gracePeriodReason := IsWithinGracePeriod(r); gracePeriodReason != nil {
return Ok, *gracePeriodReason
}

for _, condition := range r.Conditions {
if condition.Type == string(kappsv1.ReplicaSetReplicaFailure) && condition.Status == string(kcorev1.ConditionTrue) {
reason := fmt.Sprintf("ReplicaSet %s/%s has a failure condition: %s", r.Namespace, r.Name, condition.Message)

return Critical, reason
}
}

switch {
case r.AvailableReplicas < 1:
reason := fmt.Sprintf("ReplicaSet %s/%s has no replica available from %d desired", r.Namespace, r.Name, r.DesiredReplicas)

return Critical, reason
case r.AvailableReplicas < r.DesiredReplicas:
reason := fmt.Sprintf("ReplicaSet %s/%s only has %d out of %d desired replicas available", r.Namespace, r.Name, r.AvailableReplicas, r.DesiredReplicas)

return Warning, reason
default:
reason := fmt.Sprintf("ReplicaSet %s/%s has all %d desired replicas available", r.Namespace, r.Name, r.DesiredReplicas)

return Ok, reason
}
}

func (r *ReplicaSet) Relations() []database.Relation {
fk := database.WithForeignKey("replica_set_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 @@ -360,6 +360,8 @@ CREATE TABLE replica_set (
ready_replicas int unsigned NOT NULL,
available_replicas int unsigned NOT NULL,
yaml mediumblob DEFAULT 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 641f02b

Please sign in to comment.