|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ethersphere/beekeeper/pkg/beekeeper" |
| 11 | + "github.com/ethersphere/beekeeper/pkg/config" |
| 12 | + "github.com/ethersphere/beekeeper/pkg/logging" |
| 13 | + "github.com/ethersphere/beekeeper/pkg/metrics" |
| 14 | + "github.com/ethersphere/beekeeper/pkg/orchestration" |
| 15 | + "github.com/opentracing/opentracing-go" |
| 16 | + "github.com/prometheus/client_golang/prometheus/push" |
| 17 | +) |
| 18 | + |
| 19 | +type CheckRunner struct { |
| 20 | + globalConfig config.CheckGlobalConfig |
| 21 | + checks map[string]config.Check |
| 22 | + cluster orchestration.Cluster |
| 23 | + metricsPusher *push.Pusher |
| 24 | + tracer opentracing.Tracer |
| 25 | + logger logging.Logger |
| 26 | +} |
| 27 | + |
| 28 | +func NewCheckRunner( |
| 29 | + globalConfig config.CheckGlobalConfig, |
| 30 | + checks map[string]config.Check, |
| 31 | + cluster orchestration.Cluster, |
| 32 | + metricsPusher *push.Pusher, |
| 33 | + tracer opentracing.Tracer, |
| 34 | + logger logging.Logger, |
| 35 | +) *CheckRunner { |
| 36 | + if logger == nil { |
| 37 | + logger = logging.New(io.Discard, 0) |
| 38 | + } |
| 39 | + return &CheckRunner{ |
| 40 | + globalConfig: globalConfig, |
| 41 | + checks: checks, |
| 42 | + cluster: cluster, |
| 43 | + metricsPusher: metricsPusher, |
| 44 | + tracer: tracer, |
| 45 | + logger: logger, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (c *CheckRunner) Run(ctx context.Context, checks []string) error { |
| 50 | + if len(checks) == 0 { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + validatedChecks := make([]checkRun, 0, len(checks)) |
| 55 | + |
| 56 | + // validate and prepare checks |
| 57 | + for _, checkName := range checks { |
| 58 | + checkName = strings.TrimSpace(checkName) |
| 59 | + // get configuration |
| 60 | + checkConfig, ok := c.checks[checkName] |
| 61 | + if !ok { |
| 62 | + return fmt.Errorf("check '%s' doesn't exist", checkName) |
| 63 | + } |
| 64 | + |
| 65 | + // choose checkType type |
| 66 | + checkType, ok := config.Checks[checkConfig.Type] |
| 67 | + if !ok { |
| 68 | + return fmt.Errorf("check %s not implemented", checkConfig.Type) |
| 69 | + } |
| 70 | + |
| 71 | + // create check options |
| 72 | + o, err := checkType.NewOptions(c.globalConfig, checkConfig) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("creating check %s options: %w", checkName, err) |
| 75 | + } |
| 76 | + |
| 77 | + // create check action |
| 78 | + chk := checkType.NewAction(c.logger) |
| 79 | + if r, ok := chk.(metrics.Reporter); ok && c.metricsPusher != nil { |
| 80 | + metrics.RegisterCollectors(c.metricsPusher, r.Report()...) |
| 81 | + } |
| 82 | + chk = beekeeper.NewActionMiddleware(c.tracer, chk, checkName) |
| 83 | + |
| 84 | + // append to validated checks |
| 85 | + validatedChecks = append(validatedChecks, checkRun{ |
| 86 | + name: checkName, |
| 87 | + typeName: checkConfig.Type, |
| 88 | + action: chk, |
| 89 | + options: o, |
| 90 | + timeout: checkConfig.Timeout, |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + type errorCheck struct { |
| 95 | + check string |
| 96 | + err error |
| 97 | + } |
| 98 | + |
| 99 | + var errors []errorCheck |
| 100 | + |
| 101 | + for _, check := range validatedChecks { |
| 102 | + c.logger.WithField("type", check.typeName).Infof("running check: %s", check.name) |
| 103 | + c.logger.Debugf("check options: %+v", check.options) |
| 104 | + |
| 105 | + if err := check.Run(ctx, c.cluster); err != nil { |
| 106 | + c.logger.WithField("type", check.typeName).Errorf("check '%s' failed: %v", check.name, err) |
| 107 | + errors = append(errors, errorCheck{ |
| 108 | + check: check.name, |
| 109 | + err: fmt.Errorf("check %s failed: %w", check.name, err), |
| 110 | + }) |
| 111 | + } else { |
| 112 | + c.logger.WithField("type", check.typeName).Infof("%s check completed successfully", check.name) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if len(errors) == 1 { |
| 117 | + return errors[0].err |
| 118 | + } else if len(errors) > 1 { |
| 119 | + var errStrings []string |
| 120 | + for _, e := range errors { |
| 121 | + errStrings = append(errStrings, fmt.Sprintf("[%s]: {%v}", e.check, e.err)) |
| 122 | + c.logger.Errorf("%s: %v", e.check, e.err) |
| 123 | + } |
| 124 | + return fmt.Errorf("multiple checks failed: %s", strings.Join(errStrings, "; ")) |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +type checkRun struct { |
| 131 | + name string |
| 132 | + typeName string |
| 133 | + action beekeeper.Action |
| 134 | + options interface{} |
| 135 | + timeout *time.Duration |
| 136 | +} |
| 137 | + |
| 138 | +func (c *checkRun) Run(ctx context.Context, cluster orchestration.Cluster) error { |
| 139 | + checkCtx, cancelCheck := createChildContext(ctx, c.timeout) |
| 140 | + defer cancelCheck() |
| 141 | + |
| 142 | + ch := make(chan error, 1) |
| 143 | + go func() { |
| 144 | + ch <- c.action.Run(checkCtx, cluster, c.options) |
| 145 | + close(ch) |
| 146 | + }() |
| 147 | + |
| 148 | + select { |
| 149 | + case <-checkCtx.Done(): |
| 150 | + deadline, ok := checkCtx.Deadline() |
| 151 | + if ok { |
| 152 | + return fmt.Errorf("%w: deadline %v", checkCtx.Err(), deadline) |
| 153 | + } |
| 154 | + return checkCtx.Err() |
| 155 | + case err := <-ch: |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + return nil |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func createChildContext(ctx context.Context, timeout *time.Duration) (context.Context, context.CancelFunc) { |
| 164 | + if timeout != nil { |
| 165 | + return context.WithTimeout(ctx, *timeout) |
| 166 | + } |
| 167 | + return context.WithCancel(ctx) |
| 168 | +} |
0 commit comments