Skip to content

Commit

Permalink
feat: handle empty healths on change creation and config creation
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Sep 11, 2024
1 parent 52e3baa commit 1903e63
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
6 changes: 3 additions & 3 deletions chart/crds/configs.flanksource.com_scrapeconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1405,9 +1405,9 @@ spec:
type: array
crdSync:
description: |-
CRDSync when set to true, will create (or update) the corresponding database records for all mission-control resources
i.e. configs with type having prefix Flanksource::
eg: MissionControl::Playbook, MissionControl::Connection, MissionControl::Notification
CRDSync when set to true, will create (or update) the corresponding database record
for a config item of the following types
- MissionControl::Playbook, MissionControl::ScrapeConfig, MissionControl::Canary
type: boolean
file:
items:
Expand Down
2 changes: 2 additions & 0 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/ohler55/ojg/oj"
"github.com/samber/lo"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -142,6 +143,7 @@ func NewConfigItemFromResult(ctx api.ScrapeContext, result v1.ScrapeResult) (*mo
Ready: result.Ready,
LastScrapedTime: result.LastScrapedTime,
Parents: result.Parents,
Health: lo.ToPtr(dutyModels.HealthUnknown),
Children: result.Children,
}

Expand Down
18 changes: 12 additions & 6 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,15 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult, ci, existing *model
updates["updated_at"] = gorm.Expr("NOW()")
}

if lo.FromPtr(existing.Health) != lo.FromPtr(ci.Health) {
previousHealth := lo.CoalesceOrEmpty(lo.FromPtr(existing.Health), dutyModels.HealthUnknown)
newHealth := lo.CoalesceOrEmpty(lo.FromPtr(ci.Health), dutyModels.HealthUnknown)
if previousHealth != newHealth {
// For every health change, we add a config change.
healthChange := &models.ConfigChange{
ConfigID: ci.ID,
ChangeType: lo.PascalCase(string(lo.FromPtr(ci.Health))),
ChangeType: lo.PascalCase(string(newHealth)),
Source: "config-db",
Summary: fmt.Sprintf("Health changed from %s to %s", lo.FromPtr(existing.Health), lo.FromPtr(ci.Health)),
Summary: fmt.Sprintf("Health changed from %s to %s", previousHealth, newHealth),
Count: 1,
Details: map[string]any{
"previous": map[string]any{
Expand All @@ -169,11 +171,11 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult, ci, existing *model
},
}

if lo.FromPtr(ci.Health) == dutyModels.HealthUnknown {
if newHealth == dutyModels.HealthUnknown {
healthChange.ChangeType = "HealthUnknown"
}

switch lo.FromPtr(ci.Health) {
switch newHealth {
case dutyModels.HealthHealthy:
healthChange.Severity = string(dutyModels.SeverityInfo)
case dutyModels.HealthUnhealthy:
Expand Down Expand Up @@ -206,7 +208,11 @@ func updateCI(ctx api.ScrapeContext, result v1.ScrapeResult, ci, existing *model
updates["ready"] = ci.Ready
}
if lo.FromPtr(ci.Health) != lo.FromPtr(existing.Health) {
updates["health"] = ci.Health
if lo.FromPtr(ci.Health) == "" {
updates["health"] = dutyModels.HealthUnknown
} else {
updates["health"] = ci.Health
}
}
if !stringEqual(ci.Name, existing.Name) {
updates["name"] = ci.Name
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ require (
github.com/evanphx/json-patch v5.7.0+incompatible
github.com/flanksource/artifacts v1.0.14
github.com/flanksource/commons v1.29.10
github.com/flanksource/duty v1.0.628
github.com/flanksource/duty v1.0.637
github.com/flanksource/is-healthy v1.0.31
github.com/flanksource/ketall v1.1.7
github.com/flanksource/mapstructure v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,8 @@ github.com/flanksource/artifacts v1.0.14 h1:Vv70bccsae0MwGaf/uSPp34J5V1/PyKfct9z
github.com/flanksource/artifacts v1.0.14/go.mod h1:qHVCnQu5k50aWNJ5UhpcAKEl7pAzqUrFFKGSm147G70=
github.com/flanksource/commons v1.29.10 h1:T/S95Pl8kASEFvQjQ7fJjTUqeVdhxQXg1vfkULTYFJQ=
github.com/flanksource/commons v1.29.10/go.mod h1:iTbrXOSp3Spv570Nly97D/U9cQjLZoVlmWCXqWzsvRU=
github.com/flanksource/duty v1.0.628 h1:FSOIDHcetEZMQgAuuTKn2Ba3E8xmFkbNUlTWbT1gIa4=
github.com/flanksource/duty v1.0.628/go.mod h1:vW+6GGKVGqoQeB0jkj1OiCKLFJsbRTueYWXKwBvmuvM=
github.com/flanksource/duty v1.0.637 h1:VmJJxSNyyyJ84KJOi1BtHN7zIldad7mZk/PD6Pf6J+o=
github.com/flanksource/duty v1.0.637/go.mod h1:Oj9zIX94CR2U+nmnt97gNLMrsBWILyIhIBeJynIIgqE=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
github.com/flanksource/gomplate/v3 v3.24.30 h1:6Y25KnAMX4iCuEXe1C8d1kB2PdV+zD1ZulZrv6DV14Q=
github.com/flanksource/gomplate/v3 v3.24.30/go.mod h1:/lAM7+fkcCCfghCAjzdCqwgWxN5Ow8Sk6nkdFPjejCE=
Expand Down

0 comments on commit 1903e63

Please sign in to comment.