@@ -18,6 +18,7 @@ package sidecar
1818
1919import (
2020 "context"
21+ "encoding/json"
2122 "fmt"
2223 "io"
2324 "net"
@@ -27,6 +28,10 @@ import (
2728 "strings"
2829 "time"
2930
31+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+ "k8s.io/client-go/kubernetes"
33+ "k8s.io/client-go/rest"
34+
3035 "github.com/radondb/radondb-mysql-kubernetes/utils"
3136)
3237
@@ -93,15 +98,17 @@ func (s *server) healthHandler(w http.ResponseWriter, r *http.Request) {
9398
9499func (s * server ) backupHandler (w http.ResponseWriter , r * http.Request ) {
95100 w .Header ().Set ("Connection" , "keep-alive" )
101+ w .Header ().Set ("content-type" , "text/json" )
96102 if ! s .isAuthenticated (r ) {
97103 http .Error (w , "Not authenticated!" , http .StatusForbidden )
98104 return
99105 }
100- err := RunTakeBackupCommand (s .cfg )
106+ backName , Datetime , err := RunTakeBackupCommand (s .cfg )
101107 if err != nil {
102108 http .Error (w , err .Error (), http .StatusInternalServerError )
103109 } else {
104- w .Write ([]byte ("OK" ))
110+ msg , _ := json .Marshal (utils.JsonResult {Status : backupSuccessful , BackupName : backName , Date : Datetime })
111+ w .Write (msg )
105112 }
106113}
107114
@@ -204,6 +211,34 @@ func transportWithTimeout(connectTimeout time.Duration) http.RoundTripper {
204211 }
205212}
206213
214+ func setAnnonations (cfg * Config , backname string , DateTime string , BackupType string ) error {
215+ config , err := rest .InClusterConfig ()
216+ if err != nil {
217+ return err
218+ }
219+ // creates the clientset
220+ clientset , err := kubernetes .NewForConfig (config )
221+ if err != nil {
222+ return err
223+ }
224+
225+ job , err := clientset .BatchV1 ().Jobs (cfg .NameSpace ).Get (context .TODO (), cfg .JobName , metav1.GetOptions {})
226+ if err != nil {
227+ return err
228+ }
229+ if job .Annotations == nil {
230+ job .Annotations = make (map [string ]string )
231+ }
232+ job .Annotations [utils .JobAnonationName ] = backname
233+ job .Annotations [utils .JobAnonationDate ] = DateTime
234+ job .Annotations [utils .JobAnonationType ] = BackupType
235+ _ , err = clientset .BatchV1 ().Jobs (cfg .NameSpace ).Update (context .TODO (), job , metav1.UpdateOptions {})
236+ if err != nil {
237+ return err
238+ }
239+ return nil
240+ }
241+
207242// requestABackup connects to specified host and endpoint and gets the backup.
208243func requestABackup (cfg * Config , host string , endpoint string ) (* http.Response , error ) {
209244 log .Info ("initialize a backup" , "host" , host , "endpoint" , endpoint )
@@ -227,6 +262,13 @@ func requestABackup(cfg *Config, host string, endpoint string) (*http.Response,
227262 }
228263 return nil , fmt .Errorf ("fail to get backup: %s, code: %s" , err , status )
229264 }
265+ defer resp .Body .Close ()
266+ var result utils.JsonResult
267+ json .NewDecoder (resp .Body ).Decode (& result )
230268
269+ err = setAnnonations (cfg , result .BackupName , result .Date , "S3" ) // set annotation
270+ if err != nil {
271+ return nil , fmt .Errorf ("fail to set annotation: %s" , err )
272+ }
231273 return resp , nil
232274}
0 commit comments