From 2ceab8490e198efa2b3e109130cf34164d8be99e Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Fri, 18 Oct 2024 10:31:01 +0545 Subject: [PATCH] chore: remove setupError --- checks/database_backup.go | 10 +------- checks/namespace.go | 4 --- checks/utils.go | 52 --------------------------------------- pkg/results.go | 41 ++++++------------------------ 4 files changed, 9 insertions(+), 98 deletions(-) diff --git a/checks/database_backup.go b/checks/database_backup.go index 6ac541474..b0a5d1298 100644 --- a/checks/database_backup.go +++ b/checks/database_backup.go @@ -1,7 +1,6 @@ package checks import ( - "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/flanksource/canary-checker/api/context" @@ -52,13 +51,6 @@ func (c *DatabaseBackupChecker) Check(ctx *context.Context, extConfig external.C case check.GCP != nil: return GCPDatabaseBackupCheck(ctx, check) default: - return FailDatabaseBackupParse(ctx, check) + return pkg.Invalid(check, ctx.Canary, "GCP details not provided in check") } } - -func FailDatabaseBackupParse(ctx *context.Context, check v1.DatabaseBackupCheck) pkg.Results { - result := pkg.Fail(check, ctx.Canary) - var results pkg.Results - results = append(results, result) - return results.ErrorMessage(errors.New("Could not parse databaseBackup input")) -} diff --git a/checks/namespace.go b/checks/namespace.go index ae3f4c182..32ada77d4 100644 --- a/checks/namespace.go +++ b/checks/namespace.go @@ -49,15 +49,11 @@ func NewNamespaceChecker() *NamespaceChecker { // Returns check result and metrics func (c *NamespaceChecker) Run(ctx *context.Context) pkg.Results { logger.Warnf("namespace check is deprecated. Please use the kubernetes resource check") - var err error var results pkg.Results for _, conf := range ctx.Canary.Spec.Namespace { if c.k8s == nil { c.k8s = ctx.Kubernetes() c.ctx = ctx - if err != nil { - return pkg.SetupError(ctx.Canary, err) - } } results = append(results, c.Check(c.ctx, conf)...) } diff --git a/checks/utils.go b/checks/utils.go index cc3211652..c07002120 100644 --- a/checks/utils.go +++ b/checks/utils.go @@ -3,21 +3,11 @@ package checks import ( "fmt" "sync" - "time" "github.com/flanksource/canary-checker/api/external" "github.com/flanksource/canary-checker/pkg" ) -func Error(check external.Check, err error) *pkg.CheckResult { - return &pkg.CheckResult{ - Check: check, - Pass: false, - Invalid: true, - Error: err.Error(), - } -} - func Failf(check external.Check, msg string, args ...interface{}) *pkg.CheckResult { return &pkg.CheckResult{ Check: check, @@ -27,48 +17,6 @@ func Failf(check external.Check, msg string, args ...interface{}) *pkg.CheckResu } } -// TextFailf used for failure in case of text based results -func TextFailf(check external.Check, textResults bool, msg string, args ...interface{}) *pkg.CheckResult { - if textResults { - return &pkg.CheckResult{ - Check: check, - Pass: false, - Invalid: false, - DisplayType: "Text", - Message: fmt.Sprintf(msg, args...), - } - } - return Failf(check, msg, args...) -} -func Success(check external.Check, start time.Time) *pkg.CheckResult { - return &pkg.CheckResult{ - Check: check, - Pass: true, - Invalid: false, - Duration: time.Since(start).Milliseconds(), - } -} - -func Successf(check external.Check, start time.Time, textResults bool, msg string, args ...interface{}) *pkg.CheckResult { - if textResults { - return &pkg.CheckResult{ - Check: check, - Pass: true, - DisplayType: "Text", - Invalid: false, - Message: fmt.Sprintf(msg, args...), - Duration: time.Since(start).Milliseconds(), - } - } - return &pkg.CheckResult{ - Check: check, - Pass: true, - Invalid: false, - Message: fmt.Sprintf(msg, args...), - Duration: time.Since(start).Milliseconds(), - } -} - func Passf(check external.Check, msg string, args ...interface{}) *pkg.CheckResult { return &pkg.CheckResult{ Check: check, diff --git a/pkg/results.go b/pkg/results.go index b943682a2..7e81607e7 100644 --- a/pkg/results.go +++ b/pkg/results.go @@ -10,47 +10,20 @@ import ( type Results []*CheckResult -func Fail(check external.Check, canary v1.Canary) *CheckResult { - return &CheckResult{ - Check: check, - Data: map[string]interface{}{ - "results": make(map[string]interface{}), - }, - Start: time.Now(), - Pass: false, - Canary: canary, - } -} - -func SetupError(canary v1.Canary, err error) Results { - var results Results - for _, check := range canary.Spec.GetAllChecks() { - results = append(results, &CheckResult{ - Start: time.Now(), - Pass: false, - Invalid: true, - Error: err.Error(), - Check: check, - Data: map[string]interface{}{ - "results": make(map[string]interface{}), - }, - }) - } - return results -} - func Invalid(check external.Check, canary v1.Canary, reason string) Results { return Results{&CheckResult{ - Start: time.Now(), - Pass: false, - Error: reason, - Check: check, + Start: time.Now(), + Pass: false, + Invalid: true, + Error: reason, + Check: check, Data: map[string]interface{}{ "results": make(map[string]interface{}), }, Canary: canary, }} } + func Success(check external.Check, canary v1.Canary) *CheckResult { result := New(check, canary) result.Pass = true @@ -162,10 +135,12 @@ func (r Results) Failf(msg string, args ...interface{}) Results { r[0].Failf(msg, args...) return r } + func (r Results) Invalidf(msg string, args ...interface{}) Results { r[0].Invalidf(msg, args...) return r } + func (r Results) WithError(err error) Results { r[0].ErrorObject = err return r