Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shihyuho committed Mar 13, 2019
1 parent 069d4a0 commit f9eb064
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 69 deletions.
16 changes: 7 additions & 9 deletions api/protobuf-spec/softleader/captainkube/v2/rmc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ option java_package = "tw.com.softleader.captainkube.v2";
option go_package = "captainkube.v2";

import "chart.proto";
import "image.proto";

message RmcRequest {
Chart chart = 1;
ReTag retag = 2;
repeated string set = 3;
string constraint = 4;
bool verbose = 5;
string timeout = 6;
bool color = 7;
bool force = 8;
bool dryRun = 9;
repeated string set = 2;
string constraint = 3;
bool verbose = 4;
string timeout = 5;
bool color = 6;
bool force = 7;
bool dryRun = 8;
}
7 changes: 0 additions & 7 deletions cmd/capctl/app/rmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ const (
$ {{.}} rmc CHART... -e CAPTAIN_ENDPOINT -c ^
$ {{.}} rmc CHART... -e CAPTAIN_ENDPOINT -c ~
傳入 '--retag-from' 及 '--retag-to' 可以刪除 retag 後的 image
$ {{.}} rmc CHART... -e CAPTAIN_ENDPOINT -f hub.softleader.com.tw -t client-registry:5000
如果需要在 rmc 前修改 values.yaml 中任何參數, 可以傳入 '--set key1=val1,key2=val2'
$ {{.}} rmc CHART... -e CAPTAIN_ENDPOINT --set ingress.enabled=true
Expand All @@ -50,15 +46,13 @@ type rmcCmd struct {
charts []string
constraint string
endpoint *ctx.Endpoint // captain 的 endpoint ip
retag *ctx.ReTag
dryRun bool
set []string
}

func newRmcCmd() *cobra.Command {
c := rmcCmd{
endpoint: activeCtx.Endpoint,
retag: activeCtx.ReTag,
}

cmd := &cobra.Command{
Expand All @@ -83,7 +77,6 @@ func newRmcCmd() *cobra.Command {
f.StringArrayVar(&c.set, "set", []string{}, "set values (can specify multiple or separate values with commas: key1=val1,key2=val2)")
f.BoolVar(&c.hex, "hex", false, "convert and upload chart into hex string")
c.endpoint.AddFlags(f)
c.retag.AddFlags(f)

return cmd
}
Expand Down
6 changes: 1 addition & 5 deletions cmd/captain/app/server/captain_rmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (s *CaptainServer) Rmc(req *pb.RmcRequest, stream pb.Captain_RmcServer) err
endpoints.Each(func(e *caplet.Endpoint) {
if err := e.CallRmi(log, newRmiRequest(
tpls,
req.GetRetag(),
req.GetConstraint(),
req.GetVerbose(),
req.GetForce(),
Expand All @@ -64,7 +63,7 @@ func (s *CaptainServer) Rmc(req *pb.RmcRequest, stream pb.Captain_RmcServer) err
return nil
}

func newRmiRequest(tpls chart.Templates, retag *pb.ReTag, constraint string, verbose, force, dryRun bool) (req *pb.RmiRequest) {
func newRmiRequest(tpls chart.Templates, constraint string, verbose, force, dryRun bool) (req *pb.RmiRequest) {
req = &pb.RmiRequest{
Verbose: verbose,
Force: force,
Expand All @@ -77,9 +76,6 @@ func newRmiRequest(tpls chart.Templates, retag *pb.ReTag, constraint string, ver
Repo: img.Repo,
Tag: constraint + img.Tag,
}
if i.Host == retag.From {
i.Host = retag.To
}
req.Images = append(req.Images, i)
}
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/capui/app/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ func NewCapUIServer(cmd *capUICmd) (r *gin.Engine) {
installRoute.POST("/", install.Chart)
}

cleanUpRoute := r.Group("/cleanup")
{
cleanup := &CleanUp{
cmd,
}
cleanUpRoute.GET("/", cleanup.View)
cleanUpRoute.POST("/", cleanup.Clean)
}

contextsRoute := r.Group("/contexts")
{
ctxs := &Contexts{}
Expand Down
136 changes: 136 additions & 0 deletions cmd/capui/app/service_cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package app

import (
"bytes"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/softleader/captain-kube/pkg/captain"
"github.com/softleader/captain-kube/pkg/ctx"
"github.com/softleader/captain-kube/pkg/dur"
pb "github.com/softleader/captain-kube/pkg/proto"
"github.com/softleader/captain-kube/pkg/sio"
"github.com/softleader/captain-kube/pkg/utils"
"io"
"mime/multipart"
"net/http"
"strconv"
)

// CleanUpRequest 代表頁面送進來的 form request
type CleanUpRequest struct {
Prune bool `form:"prune"`
Set string `form:"set"`
Verbose bool `form:"verbose"`
Force bool `form:"force"`
Ctx string `form:"ctx"`
Timeout string `form:"timeout"`
DryRun bool `form:"dryRun"`
Constraint string `form:"constraint"`
}

// CleanUp 定義了 route 的相關 call back function
type CleanUp struct {
*capUICmd
}

// View 轉到 CleanUp 頁面
func (s *CleanUp) View(c *gin.Context) {
c.HTML(http.StatusOK, "cleanup.html", gin.H{
"requestURI": c.Request.RequestURI,
"config": &s,
"context": &activeContext,
})
}

// Clean 接收 clean up 的資訊
func (s *CleanUp) Clean(c *gin.Context) {
log := logrus.New() // 這個是這次請求要往前吐的 log
log.SetFormatter(&utils.PlainFormatter{})
log.SetOutput(sio.NewSSEventWriter(c))
if v, _ := strconv.ParseBool(c.Request.FormValue("verbose")); v {
log.SetLevel(logrus.DebugLevel)
}

var form CleanUpRequest
if err := c.Bind(&form); err != nil {
log.Errorln("binding form data error:", err)
logrus.Errorln("binding form data error:", err)
return
}

mForm, err := c.MultipartForm()
if err != nil {
log.Errorln("loading form files error:", err)
logrus.Errorln("loading form files error:", err)
return
}

// 而頁面到送進來的過程中, global activeCtx 有可能改變
// 所以這邊必須以頁面的 ctx 為主
selectedCtx, err := newContext(log, form.Ctx)
if err != nil {
log.Errorln(err)
logrus.Errorln(err)
return
}

if form.Prune {
if err := captain.CallPrune(log, selectedCtx.Endpoint.String(), form.Verbose, false, dur.Parse(form.Timeout)); err != nil {
log.Errorln(err)
logrus.Errorln(err)
return
}
}

if len(mForm.File) > 0 {
if form.DryRun {
log.Warnln("running in dry-run mode, specify the '-v' flag if you want to turn on verbose output")
}

// ps. 在讀完request body後才可以開始response, 否則body會close
files := mForm.File["files"]
for _, file := range files {
if err := s.rmc(log, selectedCtx, &form, file); err != nil {
log.Errorln(err)
logrus.Errorln(err)
return
}
}
}
}

func (s *CleanUp) rmc(log *logrus.Logger, activeCtx *ctx.Context, form *CleanUpRequest, fileHeader *multipart.FileHeader) error {
file, err := fileHeader.Open()
if err != nil {
log.Errorln(err)
logrus.Errorln(err)
return err
}
defer file.Close()
log.Debugf("received form: %+v", form)

buf := bytes.NewBuffer(nil)
read, err := io.Copy(buf, file)
if err != nil {
return err
}
log.Debugf("received chart size: %v", read)

req := &pb.RmcRequest{
Chart: &pb.Chart{
FileName: fileHeader.Filename,
Content: buf.Bytes(),
FileSize: fileHeader.Size,
},
Timeout: form.Timeout,
DryRun: form.DryRun,
Force: form.Force,
Set: []string{form.Set},
Color: false,
Verbose: form.Verbose,
Constraint: form.Constraint,
}

log.Println("cleaning up:", fileHeader.Filename)
return captain.CallRmc(log, activeCtx.Endpoint.String(), req, dur.Parse(form.Timeout))
}
2 changes: 1 addition & 1 deletion cmd/capui/app/service_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *Install) install(log *logrus.Logger, activeCtx *ctx.Context, form *Inst
if err != nil {
return fmt.Errorf("failed to open file stream: %s", err)
}

defer file.Close()
log.Debugf("received form: %+v", form)

buf := bytes.NewBuffer(nil)
Expand Down
56 changes: 23 additions & 33 deletions pkg/proto/rmc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f9eb064

Please sign in to comment.