Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
do etcd health check on bootstrap (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
ortuman authored Jan 19, 2022
1 parent 79775fc commit 8617cb5
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/jackal/jackal.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ package jackal

import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"time"

Expand Down Expand Up @@ -209,6 +212,9 @@ func (j *Jackal) Run() error {
j.hk = hook.NewHooks()

// init etcd
if err := j.checkEtcdHealth(cfg.Cluster.Etcd.Endpoints); err != nil {
return err
}
j.initLocker(cfg.Cluster.Etcd)
j.initKVStore(cfg.Cluster.Etcd)

Expand Down Expand Up @@ -264,6 +270,34 @@ func (j *Jackal) Run() error {
return j.shutdown()
}

func (j *Jackal) checkEtcdHealth(endpoints []string) error {
type healthResponse struct {
Health string `json:"health"`
}

var errHealthCheckFailedFn = func(err error) error {
return fmt.Errorf("etcd health check failed: %v", err)
}
for _, endpoint := range endpoints {
resp, err := http.Get(fmt.Sprintf("%s/health", endpoint))
if err != nil {
return errHealthCheckFailedFn(err)
}
var hResp healthResponse
if err := json.NewDecoder(resp.Body).Decode(&hResp); err != nil {
_ = resp.Body.Close()
return errHealthCheckFailedFn(err)
}
_ = resp.Body.Close()

healthy, _ := strconv.ParseBool(hResp.Health)
if !healthy {
return errHealthCheckFailedFn(fmt.Errorf("health = false, for endpoint %s", endpoint))
}
}
return nil
}

func (j *Jackal) initLocker(cfg etcd.Config) {
j.locker = etcd.NewLocker(cfg, j.logger)
j.registerStartStopper(j.locker)
Expand Down

0 comments on commit 8617cb5

Please sign in to comment.