|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/gorilla/mux" |
| 7 | + "github.com/mattermost/rotator/model" |
| 8 | + rotator "github.com/mattermost/rotator/rotator" |
| 9 | +) |
| 10 | + |
| 11 | +// Register registers the API endpoints on the given router. |
| 12 | +func Register(rootRouter *mux.Router, context *Context) { |
| 13 | + apiRouter := rootRouter.PathPrefix("/api").Subrouter() |
| 14 | + |
| 15 | + initCluster(apiRouter, context) |
| 16 | +} |
| 17 | + |
| 18 | +// initCluster registers RDS cluster endpoints on the given router. |
| 19 | +func initCluster(apiRouter *mux.Router, context *Context) { |
| 20 | + addContext := func(handler contextHandlerFunc) *contextHandler { |
| 21 | + return newContextHandler(context, handler) |
| 22 | + } |
| 23 | + |
| 24 | + clustersRouter := apiRouter.PathPrefix("/rotate").Subrouter() |
| 25 | + clustersRouter.Handle("", addContext(handleRotateCluster)).Methods("POST") |
| 26 | +} |
| 27 | + |
| 28 | +// handleRotateCluster responds to POST /api/rotate, beginning the process of rotating a k8s cluster. |
| 29 | +// sample body: |
| 30 | +// { |
| 31 | +// "clusterID": "12345678", |
| 32 | +// "maxScaling": 2, |
| 33 | +// "rotateMasters": true, |
| 34 | +// "rotateWorkers": true, |
| 35 | +// "maxDrainRetries": 10, |
| 36 | +// "EvictGracePeriod": 60, |
| 37 | +// "WaitBetweenRotations": 60, |
| 38 | +// "WaitBetweenDrains": 60, |
| 39 | +// } |
| 40 | +func handleRotateCluster(c *Context, w http.ResponseWriter, r *http.Request) { |
| 41 | + |
| 42 | + rotateClusterRequest, err := model.NewRotateClusterRequestFromReader(r.Body) |
| 43 | + if err != nil { |
| 44 | + c.Logger.WithError(err).Error("failed to decode request") |
| 45 | + w.WriteHeader(http.StatusBadRequest) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + cluster := model.Cluster{ |
| 50 | + ClusterID: rotateClusterRequest.ClusterID, |
| 51 | + MaxScaling: rotateClusterRequest.MaxScaling, |
| 52 | + RotateMasters: rotateClusterRequest.RotateMasters, |
| 53 | + RotateWorkers: rotateClusterRequest.RotateWorkers, |
| 54 | + MaxDrainRetries: rotateClusterRequest.MaxDrainRetries, |
| 55 | + EvictGracePeriod: rotateClusterRequest.EvictGracePeriod, |
| 56 | + WaitBetweenRotations: rotateClusterRequest.WaitBetweenRotations, |
| 57 | + WaitBetweenDrains: rotateClusterRequest.WaitBetweenDrains, |
| 58 | + } |
| 59 | + |
| 60 | + rotatorMetada := rotator.RotatorMetadata{} |
| 61 | + |
| 62 | + go rotator.InitRotateCluster(&cluster, &rotatorMetada, c.Logger.WithField("cluster", cluster.ClusterID)) |
| 63 | + |
| 64 | + w.Header().Set("Content-Type", "application/json") |
| 65 | + w.WriteHeader(http.StatusAccepted) |
| 66 | + outputJSON(c, w, cluster) |
| 67 | +} |
0 commit comments