From dbc93bcfbd5398c8b00e66bda3d01f153c3db047 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Mon, 29 Apr 2024 14:40:57 +0545 Subject: [PATCH] fix: usage of Failf and Errorf --- checks/alertmanager.go | 7 +++---- checks/aws_config.go | 2 +- checks/aws_config_rule.go | 10 +++------- checks/azure_devops.go | 14 +++++++------- checks/catalog.go | 2 +- checks/cloudwatch.go | 2 +- checks/database_backup_gcp.go | 2 +- checks/dns.go | 4 ++-- checks/dynatrace.go | 4 ++-- checks/elasticsearch.go | 2 +- checks/exec.go | 2 +- checks/folder_gcs.go | 6 +++--- checks/folder_s3.go | 4 ++-- checks/folder_sftp.go | 2 +- checks/folder_smb.go | 2 +- checks/git_protocol.go | 6 +++--- checks/github.go | 8 ++++---- checks/http.go | 6 +++++- checks/jmeter.go | 12 ++++++------ checks/junit.go | 4 ++-- checks/kubernetes.go | 18 +++++++++--------- checks/kubernetes_resource.go | 20 ++++++++++---------- checks/ldap.go | 6 +++--- checks/mongodb.go | 9 +++------ checks/namespace.go | 14 +++++++------- checks/opensearch.go | 10 +++++----- checks/pod.go | 10 +++++----- checks/prometheus.go | 6 +++--- checks/redis.go | 2 +- checks/restic.go | 10 +++++----- checks/runchecks.go | 2 +- checks/s3.go | 10 +++++----- checks/sql.go | 5 ++--- pkg/utils/utils.go | 2 +- 34 files changed, 110 insertions(+), 115 deletions(-) diff --git a/checks/alertmanager.go b/checks/alertmanager.go index ea9b3a7eb..1a6794631 100644 --- a/checks/alertmanager.go +++ b/checks/alertmanager.go @@ -36,12 +36,12 @@ func (c *AlertManagerChecker) Check(ctx *context.Context, extConfig external.Che connection, err := ctx.GetConnection(check.Connection) if err != nil { - return results.Failf("error getting connection: %v", err) + return results.Errorf("error getting connection: %v", err) } parsedURL, err := url.Parse(connection.URL) if err != nil { - return results.Failf("error parsing url: %v", err) + return results.Errorf("error parsing url: %v", err) } client := alertmanagerClient.NewHTTPClientWithConfig(nil, &alertmanagerClient.TransportConfig{ Host: parsedURL.Host, @@ -67,8 +67,7 @@ func (c *AlertManagerChecker) Check(ctx *context.Context, extConfig external.Che Filter: filters, }) if err != nil { - results.Errorf("error fetching from alertmanager: %v", err) - return results + return results.Errorf("error fetching from alertmanager: %v", err) } type Alerts struct { diff --git a/checks/aws_config.go b/checks/aws_config.go index 5796eeeca..a75186bc8 100644 --- a/checks/aws_config.go +++ b/checks/aws_config.go @@ -40,7 +40,7 @@ func (c *AwsConfigChecker) Check(ctx *context.Context, extConfig external.Check) check.AWSConnection = &connection.AWSConnection{} } else { if err := check.AWSConnection.Populate(ctx); err != nil { - return results.Failf("failed to populate aws connection: %v", err) + return results.Errorf("failed to populate aws connection: %v", err) } } diff --git a/checks/aws_config_rule.go b/checks/aws_config_rule.go index ee6489f35..6dfb60b7d 100644 --- a/checks/aws_config_rule.go +++ b/checks/aws_config_rule.go @@ -42,19 +42,15 @@ func (c *AwsConfigRuleChecker) Check(ctx *context.Context, extConfig external.Ch if check.AWSConnection == nil { check.AWSConnection = &connection.AWSConnection{} } else if err := check.AWSConnection.Populate(ctx); err != nil { - return results.Failf("failed to populate aws connection: %v", err) + return results.Errorf("failed to populate aws connection: %v", err) } cfg, err := awsUtil.NewSession(ctx.Context, *check.AWSConnection) if err != nil { - return results.Failf("failed to create a session: %v", err) + return results.Errorf("failed to create a session: %v", err) } client := configservice.NewFromConfig(*cfg) - if err != nil { - return results.Failf("failed to describe compliance rules: %v", err) - } - var complianceTypes = []types.ComplianceType{} for _, i := range check.ComplianceTypes { complianceTypes = append(complianceTypes, types.ComplianceType(i)) @@ -64,7 +60,7 @@ func (c *AwsConfigRuleChecker) Check(ctx *context.Context, extConfig external.Ch ConfigRuleNames: check.Rules, }) if err != nil { - return results.Failf("failed to describe compliance rules: %v", err) + return results.Errorf("failed to describe compliance rules: %v", err) } type ConfigRuleResource struct { diff --git a/checks/azure_devops.go b/checks/azure_devops.go index 5ada2a9cc..fe772c903 100644 --- a/checks/azure_devops.go +++ b/checks/azure_devops.go @@ -41,22 +41,22 @@ func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsChe if check.PersonalAccessToken.ValueStatic != "" { c = &models.Connection{Password: check.PersonalAccessToken.ValueStatic} } else if c, err = ctx.HydrateConnectionByURL(check.ConnectionName); err != nil { - return results.Failf("failed to hydrate connection: %v", err) + return results.Errorf("failed to hydrate connection: %v", err) } else if c != nil { if c, err = c.Merge(ctx, check); err != nil { - return results.Failf("failed to merge connection: %v", err) + return results.Errorf("failed to merge connection: %v", err) } } connection := azuredevops.NewPatConnection(fmt.Sprintf("https://dev.azure.com/%s", check.Organization), c.Password) coreClient, err := core.NewClient(ctx, connection) if err != nil { - return results.Errorf("failed to create core client: %w", err) + return results.Errorf("failed to create core client: %v", err) } project, err := coreClient.GetProject(ctx, core.GetProjectArgs{ProjectId: &check.Project}) if err != nil { - return results.Errorf("failed to get project (name=%s): %w", check.Project, err) + return results.Errorf("failed to get project (name=%s): %v", check.Project, err) } projectID := project.Id.String() @@ -68,7 +68,7 @@ func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsChe pipelineClient := pipelines.NewClient(ctx, connection) allPipelines, err := pipelineClient.ListPipelines(ctx, pipelines.ListPipelinesArgs{Project: &projectID}) if err != nil { - return results.Errorf("failed to get pipeline (project=%s): %w", check.Project, err) + return results.Errorf("failed to get pipeline (project=%s): %v", check.Project, err) } for _, pipeline := range *allPipelines { @@ -89,7 +89,7 @@ func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsChe // https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/list?view=azure-devops-rest-7.1 runs, err := pipelineClient.ListRuns(ctx, pipelines.ListRunsArgs{PipelineId: pipeline.Id, Project: &projectID}) if err != nil { - return results.Errorf("failed to get runs (pipeline=%s): %w", check.Pipeline, err) + return results.Errorf("failed to get runs (pipeline=%s): %v", check.Pipeline, err) } latestRun := getLatestCompletedRun(*runs) @@ -105,7 +105,7 @@ func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsChe // because the ListRuns API doesn't return Resources. latestRun, err = pipelineClient.GetRun(ctx, pipelines.GetRunArgs{Project: &projectID, PipelineId: pipeline.Id, RunId: (*runs)[0].Id}) if err != nil { - return results.Errorf("failed to get run (pipeline=%s): %w", check.Pipeline, err) + return results.Errorf("failed to get run (pipeline=%s): %v", check.Pipeline, err) } if !matchBranchNames(check.Branches, latestRun.Resources) { diff --git a/checks/catalog.go b/checks/catalog.go index 46fa5e2e2..0aa428de7 100644 --- a/checks/catalog.go +++ b/checks/catalog.go @@ -30,7 +30,7 @@ func (c *CatalogChecker) Check(ctx *canaryContext.Context, check v1.CatalogCheck items, err := query.FindConfigsByResourceSelector(ctx.Context, check.Selector...) if err != nil { - return results.Failf("failed to fetch catalogs: %v", err) + return results.Errorf("failed to fetch catalogs: %v", err) } var configItems []map[string]any diff --git a/checks/cloudwatch.go b/checks/cloudwatch.go index e5455a325..2f44c0d2f 100644 --- a/checks/cloudwatch.go +++ b/checks/cloudwatch.go @@ -39,7 +39,7 @@ func (c *CloudWatchChecker) Check(ctx *context.Context, extConfig external.Check results = append(results, result) if err := check.AWSConnection.Populate(ctx); err != nil { - return results.Failf("failed to populate aws connection: %v", err) + return results.Errorf("failed to populate aws connection: %v", err) } cfg, err := awsUtil.NewSession(ctx.Context, check.AWSConnection) diff --git a/checks/database_backup_gcp.go b/checks/database_backup_gcp.go index bbc165ba0..ced70e7b8 100644 --- a/checks/database_backup_gcp.go +++ b/checks/database_backup_gcp.go @@ -29,7 +29,7 @@ func GCPDatabaseBackupCheck(ctx *context.Context, check v1.DatabaseBackupCheck) results = append(results, result) if err := check.GCP.HydrateConnection(ctx); err != nil { - return results.Failf("failed to populate GCP connection: %v", err) + return results.Errorf("failed to populate GCP connection: %v", err) } svc, err := gcp.NewSQLAdmin(ctx.Context, check.GCP.GCPConnection) diff --git a/checks/dns.go b/checks/dns.go index 8dd9c6d88..c65bbebd7 100644 --- a/checks/dns.go +++ b/checks/dns.go @@ -54,7 +54,7 @@ func (c *DNSChecker) Check(ctx *canaryContext.Context, extConfig external.Check) if check.Server != "" { dialer, err := getDialer(check, timeout) if err != nil { - return results.Failf("Failed to get dialer, %v", err) + return results.Errorf("Failed to get dialer, %v", err) } r = net.Resolver{ PreferGo: true, @@ -71,7 +71,7 @@ func (c *DNSChecker) Check(ctx *canaryContext.Context, extConfig external.Check) resultCh := make(chan *pkg.CheckResult, 1) if fn, ok := resolvers[strings.ToUpper(queryType)]; !ok { - return results.Failf("unknown query type: %s", queryType) + return results.Errorf("unknown query type: %s", queryType) } else { go func() { pass, message, err := fn(ctx, &r, check) diff --git a/checks/dynatrace.go b/checks/dynatrace.go index 23d7399d6..7e54d8edc 100644 --- a/checks/dynatrace.go +++ b/checks/dynatrace.go @@ -33,7 +33,7 @@ func (t *DynatraceChecker) Check(ctx *context.Context, extConfig external.Check) apiKey, err := ctx.GetEnvValueFromCache(check.APIKey) if err != nil { - return results.Failf("error getting Dynatrace API key: %v", err) + return results.Errorf("error getting Dynatrace API key: %v", err) } config := dynatrace.NewConfiguration() @@ -46,7 +46,7 @@ func (t *DynatraceChecker) Check(ctx *context.Context, extConfig external.Check) apiClient := dynatrace.NewAPIClient(config) problems, apiResponse, err := apiClient.ProblemsApi.GetProblems(ctx).Execute() if err != nil { - return results.Failf("error getting Dynatrace problems: %s", err.Error()) + return results.Errorf("error getting Dynatrace problems: %s", err.Error()) } defer apiResponse.Body.Close() diff --git a/checks/elasticsearch.go b/checks/elasticsearch.go index a7e9f5548..3657a277d 100644 --- a/checks/elasticsearch.go +++ b/checks/elasticsearch.go @@ -35,7 +35,7 @@ func (c *ElasticsearchChecker) Check(ctx *context.Context, extConfig external.Ch connection, err := ctx.GetConnection(check.Connection) if err != nil { - return results.Failf("error getting connection: %v", err) + return results.Errorf("error getting connection: %v", err) } cfg := elasticsearch.Config{ diff --git a/checks/exec.go b/checks/exec.go index cd405119d..03842d3f9 100644 --- a/checks/exec.go +++ b/checks/exec.go @@ -141,7 +141,7 @@ func execPowershell(ctx *context.Context, check v1.ExecCheck, envParams *execEnv ps, err := exec.LookPath("powershell.exe") if err != nil { - return results.Failf("powershell not found") + return results.Errorf("powershell not found") } args := []string{check.Script} diff --git a/checks/folder_gcs.go b/checks/folder_gcs.go index 9e1edbc07..e34fad54b 100644 --- a/checks/folder_gcs.go +++ b/checks/folder_gcs.go @@ -22,7 +22,7 @@ func CheckGCSBucket(ctx *context.Context, check v1.FolderCheck) pkg.Results { results = append(results, result) if check.GCSConnection == nil { - return results.Errorf("missing GCS connection") + return results.Invalidf("missing GCS connection") } var bucket string @@ -30,7 +30,7 @@ func CheckGCSBucket(ctx *context.Context, check v1.FolderCheck) pkg.Results { connection, err := ctx.HydrateConnectionByURL(check.GCPConnection.ConnectionName) if err != nil { - return results.Failf("failed to populate GCS connection: %v", err) + return results.Errorf("failed to populate GCS connection: %v", err) } else if connection == nil { connection = &models.Connection{Type: models.ConnectionTypeGCS} if check.GCSConnection.Bucket == "" { @@ -39,7 +39,7 @@ func CheckGCSBucket(ctx *context.Context, check v1.FolderCheck) pkg.Results { connection, err = connection.Merge(ctx, check.GCSConnection) if err != nil { - return results.Failf("failed to populate GCS connection: %v", err) + return results.Errorf("failed to populate GCS connection: %v", err) } } diff --git a/checks/folder_s3.go b/checks/folder_s3.go index 79740b98e..563e1919e 100644 --- a/checks/folder_s3.go +++ b/checks/folder_s3.go @@ -32,7 +32,7 @@ func CheckS3Bucket(ctx *context.Context, check v1.FolderCheck) pkg.Results { connection, err := ctx.HydrateConnectionByURL(check.AWSConnection.ConnectionName) if err != nil { - return results.Failf("failed to populate AWS connection: %v", err) + return results.Errorf("failed to populate AWS connection: %v", err) } else if connection == nil { connection = &models.Connection{Type: models.ConnectionTypeS3} if check.S3Connection.Bucket == "" { @@ -41,7 +41,7 @@ func CheckS3Bucket(ctx *context.Context, check v1.FolderCheck) pkg.Results { connection, err = connection.Merge(ctx, check.S3Connection) if err != nil { - return results.Failf("failed to populate AWS connection: %v", err) + return results.Errorf("failed to populate AWS connection: %v", err) } } diff --git a/checks/folder_sftp.go b/checks/folder_sftp.go index 281a77f9f..2a2855ff9 100644 --- a/checks/folder_sftp.go +++ b/checks/folder_sftp.go @@ -18,7 +18,7 @@ func CheckSFTP(ctx *context.Context, check v1.FolderCheck) pkg.Results { foundConn, err := check.SFTPConnection.HydrateConnection(ctx) if err != nil { - return results.Failf("failed to populate SFTP connection: %v", err) + return results.Errorf("failed to populate SFTP connection: %v", err) } auth := check.SFTPConnection.Authentication diff --git a/checks/folder_smb.go b/checks/folder_smb.go index 960566a53..6adcd5777 100644 --- a/checks/folder_smb.go +++ b/checks/folder_smb.go @@ -23,7 +23,7 @@ func CheckSmb(ctx *context.Context, check v1.FolderCheck) pkg.Results { foundConn, err := check.SMBConnection.HydrateConnection(ctx) if err != nil { - return results.Failf("failed to populate SMB connection: %v", err) + return results.Errorf("failed to populate SMB connection: %v", err) } auth := check.SMBConnection.Authentication diff --git a/checks/git_protocol.go b/checks/git_protocol.go index 047b06e02..ad1ddbe2e 100644 --- a/checks/git_protocol.go +++ b/checks/git_protocol.go @@ -105,12 +105,12 @@ func (c *GitProtocolChecker) Check(ctx *context.Context, extConfig external.Chec // Fetching Git Username username, err := ctx.GetEnvValueFromCache(check.Username) if err != nil { - return results.Failf("error fetching git user from env cache: %v", err) + return results.Errorf("error fetching git user from env cache: %v", err) } // Fetching Git Password password, err := ctx.GetEnvValueFromCache(check.Password) if err != nil { - return results.Failf("error fetching git password from env cache: %v", err) + return results.Errorf("error fetching git password from env cache: %v", err) } if len(filename) == 0 { @@ -119,7 +119,7 @@ func (c *GitProtocolChecker) Check(ctx *context.Context, extConfig external.Chec // Push Changes if err := pushChanges(check.Repository, username, password, filename); err != nil { - return results.Failf("error pushing changes: %v", err) + return results.Errorf("error pushing changes: %v", err) } details := map[string]string{ diff --git a/checks/github.go b/checks/github.go index 9518d7a05..e91c489eb 100644 --- a/checks/github.go +++ b/checks/github.go @@ -38,13 +38,13 @@ func (c *GitHubChecker) Check(ctx *context.Context, extConfig external.Check) pk var githubToken string if connection, err := ctx.HydrateConnectionByURL(check.ConnectionName); err != nil { - return results.Failf("failed to find connection for github token %q: %v", check.ConnectionName, err) + return results.Errorf("failed to find connection for github token %q: %v", check.ConnectionName, err) } else if connection != nil { githubToken = connection.Password } else { githubToken, err = ctx.GetEnvValueFromCache(check.GithubToken) if err != nil { - return results.Failf("error fetching github token from env cache: %v", err) + return results.Errorf("error fetching github token from env cache: %v", err) } } @@ -56,13 +56,13 @@ func (c *GitHubChecker) Check(ctx *context.Context, extConfig external.Check) pk cmd.Env = append(cmd.Env, "GITHUB_TOKEN="+githubToken) output, err := cmd.CombinedOutput() if err != nil { - return results.Failf("error executing askgit command. output=%q: %v", output, err) + return results.Errorf("error executing askgit command. output=%q: %v", output, err) } var rowResults = make([]map[string]any, 0) err = json.Unmarshal(output, &rowResults) if err != nil { - return results.Failf("error parsing mergestat result: %v", err) + return results.Errorf("error parsing mergestat result: %v", err) } result.AddDetails(rowResults) diff --git a/checks/http.go b/checks/http.go index 0d802a2c2..800454e8b 100644 --- a/checks/http.go +++ b/checks/http.go @@ -241,7 +241,11 @@ func (c *HTTPChecker) Check(ctx *context.Context, check v1.HTTPCheck) pkg.Result } if ok := response.IsOK(check.ResponseCodes...); !ok { - return results.Failf("response code invalid %d != %v", status, check.ResponseCodes) + if len(check.ResponseCodes) > 0 { + return results.Failf("response code invalid. %d != %v", status, check.ResponseCodes) + } else { + return results.Failf("response code invalid. %d is not 2xx", status) + } } if check.ThresholdMillis > 0 && check.ThresholdMillis < int(elapsed.Milliseconds()) { diff --git a/checks/jmeter.go b/checks/jmeter.go index 44b4c8531..b7542fa97 100644 --- a/checks/jmeter.go +++ b/checks/jmeter.go @@ -44,7 +44,7 @@ func (c *JmeterChecker) Check(ctx *context.Context, extConfig external.Check) pk //FIXME: the jmx file should not be cached value, err := ctx.GetEnvValueFromCache(check.Jmx) if err != nil { - return results.Failf("Failed to parse the jmx plan: %v", err) + return results.Errorf("Failed to parse the jmx plan: %v", err) } testPlanFilename := fmt.Sprintf("/tmp/jmx-%s-%s-%d.jmx", namespace, check.Jmx.Name, rand.Int()) @@ -52,7 +52,7 @@ func (c *JmeterChecker) Check(ctx *context.Context, extConfig external.Check) pk err = os.WriteFile(testPlanFilename, []byte(value), 0755) defer os.Remove(testPlanFilename) // nolint: errcheck if err != nil { - return results.Failf("unable to write test plan file") + return results.Errorf("unable to write test plan file") } var host string @@ -67,21 +67,21 @@ func (c *JmeterChecker) Check(ctx *context.Context, extConfig external.Check) pk _, ok := exec.SafeExec(jmeterCmd) defer os.Remove(logFilename) // nolint: errcheck if !ok { - return results.Failf("error running the jmeter command: %v", jmeterCmd) + return results.Errorf("error running the jmeter command: %v", jmeterCmd) } raw, err := os.ReadFile(logFilename) if err != nil { - return results.Failf("error opening the log file: %v", err) + return results.Errorf("error opening the log file: %v", err) } elapsedTime, err := checkLogs(raw) if err != nil { - return results.Failf("check failed: %v", err) + return results.Errorf("check failed: %v", err) } totalDuration := time.Duration(elapsedTime) * time.Millisecond if check.ResponseDuration != "" { resDuration, err := time.ParseDuration(check.ResponseDuration) if err != nil { - return results.Failf("error parsing response duration: %v", err) + return results.Errorf("error parsing response duration: %v", err) } if totalDuration > resDuration { return results.Failf("the response took %v longer than specified", (totalDuration - resDuration).String()) diff --git a/checks/junit.go b/checks/junit.go index 7b5c9e537..899999c3c 100644 --- a/checks/junit.go +++ b/checks/junit.go @@ -144,7 +144,7 @@ func podExecf(ctx *context.Context, pod corev1.Pod, results pkg.Results, cmd str _cmd := fmt.Sprintf(cmd, args...) stdout, stderr, err := ctx.Kommons().ExecutePodf(pod.Namespace, pod.Name, containerName, "bash", "-c", _cmd) if stderr != "" || err != nil { - podFail(ctx, pod, results.Failf("error running %s: %v %v %v", _cmd, stdout, stderr, err)) + podFail(ctx, pod, results.Errorf("error running %s: %v %v %v", _cmd, stdout, stderr, err)) return "", false } return strings.TrimSpace(stdout), true @@ -191,7 +191,7 @@ func (c *JunitChecker) Check(ctx *context.Context, extConfig external.Check) pkg results = append(results, result) if ctx.Kommons() == nil { - return results.Failf("Kubernetes is not initialized") + return results.Errorf("Kubernetes is not initialized") } k8s := ctx.Kubernetes() diff --git a/checks/kubernetes.go b/checks/kubernetes.go index 1e003aade..9064ce686 100644 --- a/checks/kubernetes.go +++ b/checks/kubernetes.go @@ -42,13 +42,13 @@ func (c *KubernetesChecker) Check(ctx *context.Context, extConfig external.Check if check.KubeConfig != nil { val, err := ctx.GetEnvValueFromCache(*check.KubeConfig) if err != nil { - return results.Failf("failed to get kubeconfig from env: %v", err) + return results.Errorf("failed to get kubeconfig from env: %v", err) } if strings.HasPrefix(val, "/") { kClient, kube, err := pkg.NewKommonsClientWithConfigPath(val) if err != nil { - return results.Failf("failed to initialize kubernetes client from the provided kubeconfig: %v", err) + return results.Errorf("failed to initialize kubernetes client from the provided kubeconfig: %v", err) } ctx = ctx.WithDutyContext(ctx.WithKommons(kClient)) @@ -56,7 +56,7 @@ func (c *KubernetesChecker) Check(ctx *context.Context, extConfig external.Check } else { kClient, kube, err := pkg.NewKommonsClientWithConfig(val) if err != nil { - return results.Failf("failed to initialize kubernetes client from the provided kubeconfig: %v", err) + return results.Errorf("failed to initialize kubernetes client from the provided kubeconfig: %v", err) } ctx = ctx.WithDutyContext(ctx.WithKommons(kClient)) @@ -65,29 +65,29 @@ func (c *KubernetesChecker) Check(ctx *context.Context, extConfig external.Check } if ctx.Kommons() == nil { - return results.Failf("Kubernetes is not initialized") + return results.Errorf("Kubernetes is not initialized") } client, err := ctx.Kommons().GetClientByKind(check.Kind) if err != nil { - return results.Failf("Failed to get client for kind %s: %v", check.Kind, err) + return results.Errorf("Failed to get client for kind %s: %v", check.Kind, err) } namespaces, err := getNamespaces(ctx, check) if err != nil { - return results.Failf("Failed to get namespaces: %v", err) + return results.Errorf("Failed to get namespaces: %v", err) } var allResources []unstructured.Unstructured for _, namespace := range namespaces { resources, err := getResourcesFromNamespace(ctx, client, check, namespace) if err != nil { - return results.Failf("failed to get resources: %v. namespace: %v", err, namespace) + return results.Errorf("failed to get resources: %v. namespace: %v", err, namespace) } for _, filter := range check.Ignore { resources, err = filterResources(resources, filter) if err != nil { - results.Failf("failed to filter resources: %v. filter: %v", err, filter) + results.Errorf("failed to filter resources: %v. filter: %v", err, filter) return results } } @@ -97,7 +97,7 @@ func (c *KubernetesChecker) Check(ctx *context.Context, extConfig external.Check _resource := resource resourceHealth, err := health.GetResourceHealth(&_resource, nil) if err != nil { - results.Failf("error getting resource health (%s/%s/%s): %v", + results.Errorf("error getting resource health (%s/%s/%s): %v", resource.GetKind(), resource.GetNamespace(), resource.GetName(), err) } else { resource.Object["healthStatus"] = resourceHealth diff --git a/checks/kubernetes_resource.go b/checks/kubernetes_resource.go index b3f8f1a3f..b41ffa12b 100644 --- a/checks/kubernetes_resource.go +++ b/checks/kubernetes_resource.go @@ -63,49 +63,49 @@ func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.Kuberne results = append(results, result) if err := c.validate(ctx, check); err != nil { - return results.Failf("validation: %v", err) + return results.Invalidf("validation: %v", err) } if check.Kubeconfig != nil { ctx, err = c.applyKubeconfig(ctx, *check.Kubeconfig) if err != nil { - return results.Failf("failed to apply kube config: %v", err) + return results.Errorf("failed to apply kube config: %v", err) } } if err := templateKubernetesResourceCheck(ctx.Canary.GetPersistedID(), ctx.Canary.GetCheckID(check.GetName()), &check); err != nil { - return results.Failf("templating error: %v", err) + return results.Errorf("templating error: %v", err) } for i := range check.StaticResources { resource := check.StaticResources[i] if err := ctx.Kommons().ApplyUnstructured(utils.Coalesce(resource.GetNamespace(), ctx.Namespace), &resource); err != nil { - return results.Failf("failed to apply static resource %s: %v", resource.GetName(), err) + return results.Errorf("failed to apply static resource %s: %v", resource.GetName(), err) } } defer func() { if err := DeleteResources(ctx, check, false); err != nil { - results.Failf(err.Error()) + results.Errorf(err.Error()) } }() if check.ClearResources { if err := DeleteResources(ctx, check, false); err != nil { - results.Failf(err.Error()) + results.Errorf(err.Error()) } } for i := range check.Resources { resource := check.Resources[i] if err := ctx.Kommons().ApplyUnstructured(utils.Coalesce(resource.GetNamespace(), ctx.Namespace), &resource); err != nil { - return results.Failf("failed to apply resource (%s/%s/%s): %v", resource.GetKind(), resource.GetNamespace(), resource.GetName(), err) + return results.Errorf("failed to apply resource (%s/%s/%s): %v", resource.GetKind(), resource.GetNamespace(), resource.GetName(), err) } } if !check.WaitFor.Disable { if err := c.evalWaitFor(ctx, check); err != nil { - return results.Failf("%v", err) + return results.Errorf("%v", err) } } @@ -128,7 +128,7 @@ func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.Kuberne }, } if err := templater.Walk(&virtualCanary); err != nil { - return results.Failf("error templating checks: %v", err) + return results.Errorf("error templating checks: %v", err) } if wt, _ := check.CheckRetries.GetDelay(); wt > 0 { @@ -171,7 +171,7 @@ func (c *KubernetesResourceChecker) Check(ctx *context.Context, check v1.Kuberne return nil }) if retryErr != nil { - return results.Failf(retryErr.Error()) + return results.Errorf(retryErr.Error()) } } diff --git a/checks/ldap.go b/checks/ldap.go index 50cdae424..c75c27164 100644 --- a/checks/ldap.go +++ b/checks/ldap.go @@ -40,11 +40,11 @@ func (c *LdapChecker) Check(ctx *context.Context, extConfig external.Check) pkg. connection, err := ctx.GetConnection(check.Connection) if err != nil { - return results.Failf("failed to get connection: %v", err) + return results.Errorf("failed to get connection: %v", err) } if connection.URL == "" { - return results.Failf("Must specify a connection or URL") + return results.Invalidf("Must specify a connection or URL") } ld, err := ldap.DialURL(connection.URL, ldap.DialWithTLSConfig(&tls.Config{InsecureSkipVerify: check.SkipTLSVerify})) @@ -63,7 +63,7 @@ func (c *LdapChecker) Check(ctx *context.Context, extConfig external.Check) pkg. } res, err := ld.Search(req) if err != nil { - return results.Failf("Failed to search host %v error: %v", connection.URL, err) + return results.Errorf("Failed to search host %v error: %v", connection.URL, err) } if len(res.Entries) == 0 { diff --git a/checks/mongodb.go b/checks/mongodb.go index 54a35eae8..59e356f4a 100644 --- a/checks/mongodb.go +++ b/checks/mongodb.go @@ -5,7 +5,6 @@ import ( "time" "github.com/flanksource/canary-checker/api/context" - "github.com/flanksource/canary-checker/api/external" v1 "github.com/flanksource/canary-checker/api/v1" "github.com/flanksource/canary-checker/pkg" @@ -29,16 +28,14 @@ func (c *MongoDBChecker) Run(ctx *context.Context) pkg.Results { return results } -func (c *MongoDBChecker) Check(ctx *context.Context, extConfig external.Check) pkg.Results { - check := extConfig.(v1.MongoDBCheck) +func (c *MongoDBChecker) Check(ctx *context.Context, check v1.MongoDBCheck) pkg.Results { result := pkg.Success(check, ctx.Canary) var results pkg.Results results = append(results, result) - var err error connection, err := ctx.GetConnection(check.Connection) if err != nil { - return results.Failf("error getting connection: %v", err) + return results.Errorf("error getting connection: %v", err) } opts := options.Client(). @@ -57,7 +54,7 @@ func (c *MongoDBChecker) Check(ctx *context.Context, extConfig external.Check) p err = client.Ping(_ctx, readpref.Primary()) if err != nil { - return results.Error(err) + return results.Failf("failed to ping: %v", err) } return results diff --git a/checks/namespace.go b/checks/namespace.go index ae3f4c182..bdbe460b6 100644 --- a/checks/namespace.go +++ b/checks/namespace.go @@ -120,7 +120,7 @@ func (c *NamespaceChecker) Check(ctx *context.Context, extConfig external.Check) logger.Debugf("Running namespace check %s", check.Name) five := int64(5) if _, err := c.k8s.CoreV1().Nodes().List(ctx, metav1.ListOptions{TimeoutSeconds: &five}); err != nil { - return results.Failf("cannot connect to API server: %v", err) + return results.Errorf("cannot connect to API server: %v", err) } namespaceName := c.ng.NamespaceName(check.NamespaceNamePrefix) @@ -134,7 +134,7 @@ func (c *NamespaceChecker) Check(ctx *context.Context, extConfig external.Check) }, } if _, err := namespaces.Create(ctx, ns, metav1.CreateOptions{}); err != nil { - return results.Failf("unable to create namespace: %v", err) + return results.Errorf("unable to create namespace: %v", err) } defer func() { c.Cleanup(ns) // nolint: errcheck @@ -142,20 +142,20 @@ func (c *NamespaceChecker) Check(ctx *context.Context, extConfig external.Check) pod, err := c.newPod(check, ns) if err != nil { - return results.Failf("invalid pod spec: %v", err) + return results.Errorf("invalid pod spec: %v", err) } pods := c.k8s.CoreV1().Pods(ns.Name) if _, err := pods.Create(ctx, pod, metav1.CreateOptions{}); err != nil { - return results.Failf("unable to create pod: %v", err) + return results.Errorf("unable to create pod: %v", err) } pod, _ = c.WaitForPod(ns.Name, pod.Name, time.Millisecond*time.Duration(check.ScheduleTimeout), v1.PodRunning) created := pod.GetCreationTimestamp() conditions, err := c.getConditionTimes(ns, pod) if err != nil { - return results.Failf("could not list conditions: %v", err) + return results.Errorf("could not list conditions: %v", err) } scheduled := diff(conditions, v1.PodInitialized, v1.PodScheduled) @@ -166,7 +166,7 @@ func (c *NamespaceChecker) Check(ctx *context.Context, extConfig external.Check) logger.Tracef("%v", conditions) if err := c.createServiceAndIngress(check, ns, pod); err != nil { - return results.Failf("failed to create ingress and service: %v", err) + return results.Errorf("failed to create ingress and service: %v", err) } deadline := time.Now().Add(time.Duration(check.Deadline) * time.Millisecond) @@ -176,7 +176,7 @@ func (c *NamespaceChecker) Check(ctx *context.Context, extConfig external.Check) deleteOk := true deletion := NewTimer() if err := pods.Delete(c.ctx, pod.Name, metav1.DeleteOptions{}); err != nil { - return results.Failf("failed to delete pod: %v", err) + return results.Errorf("failed to delete pod: %v", err) } result.Pass = ingressResult.Pass && deleteOk result.Message = ingressResult.Message diff --git a/checks/opensearch.go b/checks/opensearch.go index b3994c119..514a2afee 100644 --- a/checks/opensearch.go +++ b/checks/opensearch.go @@ -33,11 +33,11 @@ func (t *OpenSearchChecker) check(ctx *context.Context, check v1.OpenSearchCheck connection, err := ctx.GetConnection(check.Connection) if err != nil { - return results.Failf("error getting connection: %v", err) + return results.Errorf("error getting connection: %v", err) } if connection.URL == "" { - return results.Failf("Must specify a URL") + return results.Invalidf("Must specify a URL") } cfg := opensearch.Config{ @@ -48,7 +48,7 @@ func (t *OpenSearchChecker) check(ctx *context.Context, check v1.OpenSearchCheck osClient, err := opensearch.NewClient(cfg) if err != nil { - return results.Failf("error creating the openSearch client: %v", err) + return results.Errorf("error creating the openSearch client: %v", err) } body := strings.NewReader(check.Query) @@ -58,7 +58,7 @@ func (t *OpenSearchChecker) check(ctx *context.Context, check v1.OpenSearchCheck osClient.Search.WithBody(body), ) if err != nil { - return results.Failf("error searching: %v", err) + return results.Errorf("error searching: %v", err) } defer res.Body.Close() @@ -73,7 +73,7 @@ func (t *OpenSearchChecker) check(ctx *context.Context, check v1.OpenSearchCheck var response OpenSearchResponse if err := json.NewDecoder(res.Body).Decode(&response); err != nil { - return results.Failf("error parsing the response body: %s", err) + return results.Errorf("error parsing the response body: %s", err) } if response.Hits.Total.Value != check.Results { diff --git a/checks/pod.go b/checks/pod.go index 85f9eeb60..2c2f38b04 100644 --- a/checks/pod.go +++ b/checks/pod.go @@ -161,14 +161,14 @@ func (c *PodChecker) Check(ctx *context.Context, extConfig external.Check) pkg.R five := int64(5) nodes, err := c.k8s.CoreV1().Nodes().List(ctx, metav1.ListOptions{TimeoutSeconds: &five}) if err != nil { - return results.Failf("cannot connect to API server: %v", err) + return results.Errorf("cannot connect to API server: %v", err) } nextNode, newIndex := c.nextNode(nodes, c.latestNodeIndex) c.latestNodeIndex = newIndex pod, err := c.newPod(podCheck, nextNode) if err != nil { - return results.Failf("invalid pod spec: %v", err) + return results.Errorf("invalid pod spec: %v", err) } if _, err := c.k8s.CoreV1().Pods(podCheck.Namespace).Create(ctx, pod, metav1.CreateOptions{}); err != nil { @@ -177,13 +177,13 @@ func (c *PodChecker) Check(ctx *context.Context, extConfig external.Check) pkg.R pod, err = c.WaitForPod(podCheck.Namespace, pod.Name, time.Millisecond*time.Duration(podCheck.ScheduleTimeout), v1.PodRunning) if err != nil { - return results.Failf("unable to fetch pod details: %v", err) + return results.Errorf("unable to fetch pod details: %v", err) } created := pod.GetCreationTimestamp() conditions, err := c.getConditionTimes(podCheck, pod) if err != nil { - return results.Failf("could not list conditions: %v", err) + return results.Errorf("could not list conditions: %v", err) } scheduled := diff(conditions, v1.PodInitialized, v1.PodScheduled) @@ -193,7 +193,7 @@ func (c *PodChecker) Check(ctx *context.Context, extConfig external.Check) pkg.R ctx.Debugf("%s created=%s, scheduled=%d, started=%d, running=%d wall=%s nodeName=%s", pod.Name, created, scheduled, started, running, startTimer, nextNode) if err := c.createServiceAndIngress(ctx, podCheck, pod); err != nil { - return results.Failf("failed to create service or ingress: %v", err) + return results.Errorf("failed to create service or ingress: %v", err) } deadline := time.Now().Add(time.Duration(podCheck.Deadline) * time.Millisecond) diff --git a/checks/prometheus.go b/checks/prometheus.go index 23439d17b..53c42a74e 100644 --- a/checks/prometheus.go +++ b/checks/prometheus.go @@ -34,7 +34,7 @@ func (c *PrometheusChecker) Check(ctx *context.Context, extConfig external.Check //nolint:staticcheck if check.Host != "" { - return results.Failf("host field is deprecated, use url field instead") + return results.Invalidf("host field is deprecated, use url field instead") } // Use global prometheus url if check's url is empty @@ -44,11 +44,11 @@ func (c *PrometheusChecker) Check(ctx *context.Context, extConfig external.Check connection, err := ctx.GetConnection(check.Connection) if err != nil { - return results.Failf("error getting connection: %v", err) + return results.Errorf("error getting connection: %v", err) } if connection.URL == "" { - return results.Failf("Must specify a URL") + return results.Invalidf("Must specify a URL") } promClient, err := prometheus.NewPrometheusAPI(connection.URL) diff --git a/checks/redis.go b/checks/redis.go index 95f2fba3d..7794e2ee6 100644 --- a/checks/redis.go +++ b/checks/redis.go @@ -48,7 +48,7 @@ func (c *RedisChecker) Check(ctx *context.Context, extConfig external.Check) pkg connection, err := ctx.GetConnection(check.Connection) if err != nil { - return results.Failf("error getting connection: %v", err) + return results.Errorf("error getting connection: %v", err) } redisOpts = &redis.Options{ diff --git a/checks/restic.go b/checks/restic.go index d7fd2883a..313c93713 100644 --- a/checks/restic.go +++ b/checks/restic.go @@ -50,7 +50,7 @@ func (c *ResticChecker) Check(ctx *context.Context, extConfig external.Check) pk if check.ConnectionName != "" { connection, err := ctx.HydrateConnectionByURL(check.ConnectionName) if err != nil { - return results.Failf("error getting restic connection: %v", err) + return results.Errorf("error getting restic connection: %v", err) } envVars[resticPasswordEnvKey] = connection.Password @@ -69,7 +69,7 @@ func (c *ResticChecker) Check(ctx *context.Context, extConfig external.Check) pk } else { password, err := ctx.GetEnvValueFromCache(*check.Password) if err != nil { - return results.Failf("error getting restic password from env: %v", err) + return results.Errorf("error getting restic password from env: %v", err) } envVars[resticPasswordEnvKey] = password } @@ -77,7 +77,7 @@ func (c *ResticChecker) Check(ctx *context.Context, extConfig external.Check) pk if check.AWSConnectionName != "" { connection, err := ctx.HydrateConnectionByURL(check.AWSConnectionName) if err != nil { - return results.Failf("error getting aws connection: %v", err) + return results.Errorf("error getting aws connection: %v", err) } envVars[resticAwsAccessKeyIDEnvKey] = connection.Username @@ -86,7 +86,7 @@ func (c *ResticChecker) Check(ctx *context.Context, extConfig external.Check) pk if !check.AccessKey.IsEmpty() { accessKey, err := ctx.GetEnvValueFromCache(*check.AccessKey) if err != nil { - return results.Failf("error getting aws access key from env: %v", err) + return results.Errorf("error getting aws access key from env: %v", err) } envVars[resticAwsAccessKeyIDEnvKey] = accessKey } @@ -94,7 +94,7 @@ func (c *ResticChecker) Check(ctx *context.Context, extConfig external.Check) pk if !check.SecretKey.IsEmpty() { secretKey, err := ctx.GetEnvValueFromCache(*check.SecretKey) if err != nil { - return results.Failf("error getting aws secret key from env: %v", err) + return results.Errorf("error getting aws secret key from env: %v", err) } envVars[resticAwsSecretAccessKey] = secretKey } diff --git a/checks/runchecks.go b/checks/runchecks.go index 72670f73b..95787253f 100644 --- a/checks/runchecks.go +++ b/checks/runchecks.go @@ -274,7 +274,7 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult if err != nil { r.ErrorMessage(err) } else if parsed, err := strconv.ParseBool(message); err != nil { - r.Failf("test expression did not return a boolean value. got %s", message) + r.Invalidf("test expression did not return a boolean value. got %s", message) } else if !parsed { r.Failf("") } diff --git a/checks/s3.go b/checks/s3.go index c95a4567a..caa71392d 100644 --- a/checks/s3.go +++ b/checks/s3.go @@ -72,12 +72,12 @@ func (c *S3Checker) Check(ctx *context.Context, extConfig external.Check) pkg.Re results = append(results, result) if err := check.AWSConnection.Populate(ctx); err != nil { - return results.Failf("failed to populate aws connection: %v", err) + return results.Errorf("failed to populate aws connection: %v", err) } cfg, err := GetAWSConfig(ctx, check.AWSConnection) if err != nil { - return results.Failf("Failed to get AWS config: %v", err) + return results.Errorf("failed to get AWS config: %v", err) } client := s3.NewFromConfig(cfg, func(o *s3.Options) { @@ -87,7 +87,7 @@ func (c *S3Checker) Check(ctx *context.Context, extConfig external.Check) pkg.Re listTimer := NewTimer() _, err = client.ListObjects(ctx, &s3.ListObjectsInput{Bucket: &check.BucketName}) if err != nil { - return results.Failf("Failed to list objects in bucket %s: %v", check.BucketName, err) + return results.Errorf("failed to list objects in bucket %s: %v", check.BucketName, err) } listHistogram.WithLabelValues(check.AWSConnection.Endpoint, check.BucketName).Observe(listTimer.Elapsed()) @@ -103,7 +103,7 @@ func (c *S3Checker) Check(ctx *context.Context, extConfig external.Check) pkg.Re Body: bytes.NewReader([]byte(data)), }) if err != nil { - return results.Failf("Failed to put object %s in bucket %s: %v", check.ObjectPath, check.BucketName, err) + return results.Errorf("failed to put object %s in bucket %s: %v", check.ObjectPath, check.BucketName, err) } updateHistogram.WithLabelValues(check.AWSConnection.Endpoint, check.BucketName).Observe(updateTimer.Elapsed()) @@ -112,7 +112,7 @@ func (c *S3Checker) Check(ctx *context.Context, extConfig external.Check) pkg.Re Key: &check.ObjectPath, }) if err != nil { - return results.Failf("Failed to get object %s in bucket %s: %v", check.ObjectPath, check.BucketName, err) + return results.Errorf("failed to get object %s in bucket %s: %v", check.ObjectPath, check.BucketName, err) } returnedData, _ := io.ReadAll(obj.Body) diff --git a/checks/sql.go b/checks/sql.go index e02d95388..0b7c0f1cb 100644 --- a/checks/sql.go +++ b/checks/sql.go @@ -147,7 +147,7 @@ func CheckSQL(ctx *context.Context, checker SQLChecker) pkg.Results { // nolint: connection, err := ctx.GetConnection(check.Connection) if err != nil { - return results.Failf("error getting connection: %v", err) + return results.Errorf("error getting connection: %v", err) } query := check.GetQuery() @@ -165,11 +165,10 @@ func CheckSQL(ctx *context.Context, checker SQLChecker) pkg.Results { // nolint: } details, err := querySQL(checker.GetDriver(), connection.URL, query) - result.AddDetails(details) - if err != nil { return results.Error(err) } + result.AddDetails(details) if details.Count < check.Result { return results.Failf("Query returned %d rows, expected %d", details.Count, check.Result) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 56f391926..0387603b7 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -131,7 +131,7 @@ func UnfoldGlobs(paths ...string) []string { for _, path := range paths { matched, err := filepath.Glob(path) if err != nil { - logger.Warnf("invalid glob pattern. path=%s; %w", path, err) + logger.Warnf("invalid glob pattern. path=%s; %v", path, err) continue }