-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nofify): Add support for Healthchecks notifications
- Loading branch information
Showing
16 changed files
with
244 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cmd | ||
|
||
var finalizers []func(err error) | ||
|
||
func OnFinalize(y ...func(err error)) { | ||
finalizers = append(finalizers, y...) | ||
} | ||
|
||
func PostRun(err error) { | ||
for _, x := range finalizers { | ||
x(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package consts | ||
|
||
const ( | ||
AnalyzeKey = "restore.analyze" | ||
HaltOnErrorKey = "restore.halt-on-error" | ||
SpinnerKey = "spinner.name" | ||
KubeconfigKey = "kubernetes.kubeconfig" | ||
JobPodLabelsKey = "kubernetes.job-pod-labels" | ||
NoJobKey = "kubernetes.no-job" | ||
LogLevelKey = "log.level" | ||
LogFormatKey = "log.format" | ||
LogRedactKey = "log.redact" | ||
RemoteGzipKey = "remote-gzip" | ||
PortForwardAddrKey = "port-forward.address" | ||
AnalyzeKey = "restore.analyze" | ||
HaltOnErrorKey = "restore.halt-on-error" | ||
SpinnerKey = "spinner.name" | ||
KubeconfigKey = "kubernetes.kubeconfig" | ||
JobPodLabelsKey = "kubernetes.job-pod-labels" | ||
NoJobKey = "kubernetes.no-job" | ||
LogLevelKey = "log.level" | ||
LogFormatKey = "log.format" | ||
LogRedactKey = "log.redact" | ||
RemoteGzipKey = "remote-gzip" | ||
PortForwardAddrKey = "port-forward.address" | ||
HealthchecksPingUrlKey = "healthchecks.ping-url" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package notifier | ||
|
||
import "context" | ||
|
||
type contextKey uint8 | ||
|
||
const notifierContextKey contextKey = iota | ||
|
||
func NewContext(ctx context.Context, n Notifier) context.Context { | ||
return context.WithValue(ctx, notifierContextKey, n) | ||
} | ||
|
||
func FromContext(ctx context.Context) (Notifier, bool) { | ||
notifier, ok := ctx.Value(notifierContextKey).(Notifier) | ||
return notifier, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package notifier | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
"strings" | ||
|
||
"github.com/clevyr/kubedb/internal/util" | ||
) | ||
|
||
func NewHealthchecks(url string) (Notifier, error) { | ||
if url == "" { | ||
return nil, fmt.Errorf("healthchecks %w", ErrEmptyUrl) | ||
} | ||
|
||
return &Healthchecks{ | ||
url: url, | ||
}, nil | ||
} | ||
|
||
type Healthchecks struct { | ||
url string | ||
} | ||
|
||
func (h Healthchecks) SendStatus(status Status, log string) error { | ||
var statusStr string | ||
switch status { | ||
case StatusStart: | ||
statusStr = "start" | ||
case StatusFailure: | ||
statusStr = "fail" | ||
} | ||
|
||
u, err := url.Parse(h.url) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u.Path = path.Join(u.Path, statusStr) | ||
|
||
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(log)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer util.ReadAndClose(resp.Body) | ||
|
||
switch resp.StatusCode { | ||
case http.StatusOK: | ||
case http.StatusCreated: | ||
default: | ||
return fmt.Errorf("%w: %s", ErrInvalidResponse, resp.Status) | ||
} | ||
return nil | ||
} | ||
|
||
func (h Healthchecks) Started() error { | ||
return h.SendStatus(StatusStart, "") | ||
} | ||
|
||
func (h Healthchecks) Finished(err error) error { | ||
if err == nil { | ||
return h.SendStatus(StatusSuccess, "") | ||
} else { | ||
return h.SendStatus(StatusFailure, "Error: "+err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package notifier | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Status uint8 | ||
|
||
const ( | ||
StatusSuccess Status = iota | ||
StatusFailure | ||
StatusStart | ||
) | ||
|
||
var ( | ||
ErrInvalidResponse = errors.New("invalid http response") | ||
ErrUnknownHandler = errors.New("unknown handler") | ||
ErrEmptyUrl = errors.New("url must be set") | ||
) | ||
|
||
type Notifier interface { | ||
Started() error | ||
Finished(err error) error | ||
} | ||
|
||
func New(handler, url string) (Notifier, error) { | ||
switch strings.ToLower(handler) { | ||
case "healthchecks": | ||
return NewHealthchecks(url) | ||
default: | ||
return nil, fmt.Errorf("%w: %s", ErrUnknownHandler, handler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package util | ||
|
||
import "io" | ||
|
||
func ReadAndClose(r io.ReadCloser) { | ||
_, _ = io.Copy(io.Discard, r) | ||
_ = r.Close() | ||
} |
Oops, something went wrong.