Skip to content

Commit

Permalink
Merge pull request #78 from replicatedhq/status-analyzer
Browse files Browse the repository at this point in the history
Status analyzer
  • Loading branch information
marccampbell authored Nov 8, 2019
2 parents c0415dd + 8a1e129 commit b0bbaa4
Show file tree
Hide file tree
Showing 8 changed files with 875 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/analyze/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func Analyze(analyzer *troubleshootv1beta1.Analyze, getFile getCollectedFileCont
if analyzer.ImagePullSecret != nil {
return analyzeImagePullSecret(analyzer.ImagePullSecret, findFiles)
}
if analyzer.DeploymentStatus != nil {
return deploymentStatus(analyzer.DeploymentStatus, getFile)
}
if analyzer.StatefulsetStatus != nil {
return statefulsetStatus(analyzer.StatefulsetStatus, getFile)
}

return nil, errors.New("invalid analyzer")
}
122 changes: 122 additions & 0 deletions pkg/analyze/common_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package analyzer

import (
"strconv"
"strings"

"github.com/pkg/errors"
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
)

func commonStatus(outcomes []*troubleshootv1beta1.Outcome, title string, readyReplicas int) (*AnalyzeResult, error) {
result := &AnalyzeResult{
Title: title,
}

// ordering from the spec is important, the first one that matches returns
for _, outcome := range outcomes {
if outcome.Fail != nil {
if outcome.Fail.When == "" {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return result, nil
}

match, err := compareActualToWhen(outcome.Fail.When, readyReplicas)
if err != nil {
return nil, errors.Wrap(err, "failed to parse fail range")
}

if match {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return result, nil
}
} else if outcome.Warn != nil {
if outcome.Warn.When == "" {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return result, nil
}

match, err := compareActualToWhen(outcome.Warn.When, readyReplicas)
if err != nil {
return nil, errors.Wrap(err, "failed to parse warn range")
}

if match {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return result, nil
}
} else if outcome.Pass != nil {
if outcome.Pass.When == "" {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI

return result, nil
}

match, err := compareActualToWhen(outcome.Pass.When, readyReplicas)
if err != nil {
return nil, errors.Wrap(err, "failed to parse pass range")
}

if match {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI

return result, nil
}
}
}

return result, nil
}

func compareActualToWhen(when string, actual int) (bool, error) {
parts := strings.Split(strings.TrimSpace(when), " ")

// we can make this a lot more flexible
if len(parts) != 2 {
return false, errors.New("unable to parse when range")
}

value, err := strconv.Atoi(parts[1])
if err != nil {
return false, errors.New("unable to parse when value")
}

switch parts[0] {
case "=":
fallthrough
case "==":
fallthrough
case "===":
return actual == value, nil

case "<":
return actual < value, nil

case ">":
return actual > value, nil

case "<=":
return actual <= value, nil

case ">=":
return actual >= value, nil
}

return false, errors.Errorf("unknown comparator: %q", parts[0])
}
Loading

0 comments on commit b0bbaa4

Please sign in to comment.