Skip to content

Commit e1aa7f0

Browse files
AMeceacalind
authored andcommitted
Refactor code, use constants
1 parent 58bae3d commit e1aa7f0

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pkg/sidecar/server.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import (
2525
"os/exec"
2626
)
2727

28+
const (
29+
backupStatusTrailer = "X-Backup-Status"
30+
backupSuccessfull = "Success"
31+
backupFailed = "Failed"
32+
)
33+
2834
type server struct {
2935
cfg *Config
3036
http.Server
@@ -79,7 +85,7 @@ func (s *server) backupHandler(w http.ResponseWriter, r *http.Request) {
7985

8086
w.Header().Set("Content-Type", "application/octet-stream")
8187
w.Header().Set("Connection", "keep-alive")
82-
w.Header().Set("Trailer", "Success")
88+
w.Header().Set("Trailer", backupStatusTrailer)
8389

8490
// nolint: gosec
8591
xtrabackup := exec.Command("xtrabackup", "--backup", "--slave-info", "--stream=xbstream",
@@ -115,12 +121,13 @@ func (s *server) backupHandler(w http.ResponseWriter, r *http.Request) {
115121

116122
if err := xtrabackup.Wait(); err != nil {
117123
log.Error(err, "failed waiting for xtrabackup to finish")
124+
w.Header().Set(backupStatusTrailer, backupFailed)
118125
http.Error(w, "xtrabackup failed", http.StatusInternalServerError)
119126
return
120127
}
121128

122129
// success
123-
w.Header().Set("Success", "true")
130+
w.Header().Set(backupStatusTrailer, backupSuccessfull)
124131
flusher.Flush()
125132
}
126133

@@ -170,7 +177,7 @@ func requestABackup(cfg *Config, host, endpoint string) (*http.Response, error)
170177
}
171178

172179
func checkBackupTrailers(resp *http.Response) error {
173-
if values, ok := resp.Trailer["Success"]; !ok || !stringInSlice("true", values) {
180+
if values, ok := resp.Trailer[backupStatusTrailer]; !ok || !stringInSlice(backupSuccessfull, values) {
174181
// backup is failed, remove from remote
175182
return fmt.Errorf("backup failed to be taken: no 'Success' trailer found")
176183
}

0 commit comments

Comments
 (0)