Skip to content

Commit

Permalink
feat: new storage use s3
Browse files Browse the repository at this point in the history
Signed-off-by: 张启航 <[email protected]>
  • Loading branch information
ZhangSetSail committed Oct 8, 2024
1 parent e43020c commit 7d9a6ed
Show file tree
Hide file tree
Showing 125 changed files with 2,261 additions and 2,521 deletions.
4 changes: 1 addition & 3 deletions api/api_routers/version2/v2Routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ import (
"github.com/go-chi/chi"
"github.com/goodrain/rainbond/api/controller"
"github.com/goodrain/rainbond/api/middleware"
"github.com/goodrain/rainbond/cmd/api/option"
dbmodel "github.com/goodrain/rainbond/db/model"
)

// V2 v2
type V2 struct {
Cfg *option.Config
}

// Routes routes
func (v2 *V2) Routes() chi.Router {
r := chi.NewRouter()
license := middleware.NewLicense(v2.Cfg)
license := middleware.NewLicense()
r.Use(license.Verify)
r.Get("/show", controller.GetManager().Show)

Expand Down
60 changes: 18 additions & 42 deletions api/controller/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controller
import (
"bytes"
"fmt"
"io"
"github.com/goodrain/rainbond/pkg/component/storage"
"io/ioutil"
"net/http"
"os"
Expand All @@ -21,10 +21,10 @@ import (
"github.com/sirupsen/logrus"
)

//AppStruct -
// AppStruct -
type AppStruct struct{}

//ExportApp -
// ExportApp -
func (a *AppStruct) ExportApp(w http.ResponseWriter, r *http.Request) {

switch r.Method {
Expand Down Expand Up @@ -72,7 +72,7 @@ func (a *AppStruct) ExportApp(w http.ResponseWriter, r *http.Request) {

}

//Download -
// Download -
func (a *AppStruct) Download(w http.ResponseWriter, r *http.Request) {
format := strings.TrimSpace(chi.URLParam(r, "format"))
fileName := strings.TrimSpace(chi.URLParam(r, "fileName"))
Expand All @@ -83,11 +83,10 @@ func (a *AppStruct) Download(w http.ResponseWriter, r *http.Request) {
httputil.ReturnError(r, w, 404, fmt.Sprintf("Not found export app tar file: %s", tarFile))
return
}

http.ServeFile(w, r, tarFile)
storage.Default().StorageCli.ServeFile(w, r, tarFile)
}

//ImportID -
// ImportID -
func (a *AppStruct) ImportID(w http.ResponseWriter, r *http.Request) {
eventID := strings.TrimSpace(chi.URLParam(r, "eventID"))
if eventID == "" {
Expand Down Expand Up @@ -156,7 +155,7 @@ func (a *AppStruct) ImportID(w http.ResponseWriter, r *http.Request) {
}
}

//UploadID -
// UploadID -
func (a *AppStruct) UploadID(w http.ResponseWriter, r *http.Request) {
eventID := strings.TrimSpace(chi.URLParam(r, "eventID"))
if eventID == "" {
Expand All @@ -174,30 +173,17 @@ func (a *AppStruct) UploadID(w http.ResponseWriter, r *http.Request) {
}
httputil.ReturnSuccess(r, w, map[string]string{"path": dirName})
case "GET":
_, err := os.Stat(dirName)
err := storage.Default().StorageCli.MkdirAll(dirName)
if err != nil {
if !os.IsExist(err) {
err := os.MkdirAll(dirName, 0755)
if err != nil {
httputil.ReturnError(r, w, 502, "Failed to create directory by event id: "+err.Error())
return
}
}
httputil.ReturnError(r, w, 502, "Failed to create directory by event id: "+err.Error())
return
}
packages, err := ioutil.ReadDir(dirName)
packageArr, err := storage.Default().StorageCli.ReadDir(dirName)
if err != nil {
logrus.Errorf("read dir failure")
httputil.ReturnSuccess(r, w, map[string][]string{"packages": {}})
return
}

packageArr := make([]string, 0, 10)
for _, dir := range packages {
if dir.IsDir() {
continue
}
packageArr = append(packageArr, dir.Name())
}

httputil.ReturnSuccess(r, w, map[string][]string{"packages": packageArr})
case "DELETE":
cmd := exec.Command("rm", "-rf", dirName)
Expand All @@ -220,7 +206,7 @@ func (a *AppStruct) UploadID(w http.ResponseWriter, r *http.Request) {
}
}

//NewUpload -
// NewUpload -
func (a *AppStruct) NewUpload(w http.ResponseWriter, r *http.Request) {
eventID := strings.TrimSpace(chi.URLParam(r, "eventID"))
switch r.Method {
Expand All @@ -245,7 +231,7 @@ func (a *AppStruct) NewUpload(w http.ResponseWriter, r *http.Request) {

}

//Upload -
// Upload -
func (a *AppStruct) Upload(w http.ResponseWriter, r *http.Request) {
eventID := strings.TrimSpace(chi.URLParam(r, "eventID"))
switch r.Method {
Expand All @@ -254,7 +240,6 @@ func (a *AppStruct) Upload(w http.ResponseWriter, r *http.Request) {
httputil.ReturnError(r, w, 400, "Failed to parse eventID.")
return
}

logrus.Debug("Start receive upload file: ", eventID)
reader, header, err := r.FormFile("appTarFile")
if err != nil {
Expand All @@ -265,20 +250,11 @@ func (a *AppStruct) Upload(w http.ResponseWriter, r *http.Request) {
defer reader.Close()

dirName := fmt.Sprintf("%s/import/%s", handler.GetAppHandler().GetStaticDir(), eventID)
os.MkdirAll(dirName, 0755)

fileName := fmt.Sprintf("%s/%s", dirName, header.Filename)
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0644)
storage.Default().StorageCli.MkdirAll(dirName)
err = storage.Default().StorageCli.SaveFile(fileName, reader)
if err != nil {
logrus.Errorf("Failed to open file: %s", err.Error())
httputil.ReturnError(r, w, 502, "Failed to open file: "+err.Error())
}
defer file.Close()

logrus.Debug("Start write file to: ", fileName)
if _, err := io.Copy(file, reader); err != nil {
logrus.Errorf("Failed to write file:%s", err.Error())
httputil.ReturnError(r, w, 503, "Failed to write file: "+err.Error())
httputil.ReturnError(r, w, 503, "Failed to save file: "+err.Error())
}

logrus.Debug("successful write file to: ", fileName)
Expand All @@ -300,7 +276,7 @@ func (a *AppStruct) Upload(w http.ResponseWriter, r *http.Request) {

}

//ImportApp -
// ImportApp -
func (a *AppStruct) ImportApp(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
Expand Down
25 changes: 13 additions & 12 deletions api/controller/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package controller

import (
"fmt"
"github.com/goodrain/rainbond/config/configs"
"net/http"
"net/url"
"strconv"
Expand All @@ -28,7 +29,6 @@ import (
"github.com/goodrain/rainbond/api/handler"
api_model "github.com/goodrain/rainbond/api/model"
ctxutil "github.com/goodrain/rainbond/api/util/ctx"
"github.com/goodrain/rainbond/cmd/api/option"
"github.com/goodrain/rainbond/mq/client"
httputil "github.com/goodrain/rainbond/util/http"
"github.com/jinzhu/gorm"
Expand All @@ -39,7 +39,6 @@ import (
// GatewayStruct -
type GatewayStruct struct {
MQClient client.MQClient
cfg *option.Config
}

// HTTPRule is used to add, update or delete http rule which enables
Expand All @@ -55,7 +54,7 @@ func (g *GatewayStruct) HTTPRule(w http.ResponseWriter, r *http.Request) {
}
}

//GatewayCertificate k8s gateway certificate related operations
// GatewayCertificate k8s gateway certificate related operations
func (g *GatewayStruct) GatewayCertificate(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "PUT":
Expand All @@ -67,15 +66,15 @@ func (g *GatewayStruct) GatewayCertificate(w http.ResponseWriter, r *http.Reques
}
}

//BatchGatewayHTTPRoute k8s gateway http route batch operation
// BatchGatewayHTTPRoute k8s gateway http route batch operation
func (g *GatewayStruct) BatchGatewayHTTPRoute(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
g.batchGetGatewayHTTPRoute(w, r)
}
}

//GatewayHTTPRoute k8s gateway http route related operations
// GatewayHTTPRoute k8s gateway http route related operations
func (g *GatewayStruct) GatewayHTTPRoute(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
Expand Down Expand Up @@ -337,13 +336,14 @@ func (g *GatewayStruct) AddTCPRule(w http.ResponseWriter, r *http.Request) {
h := handler.GetGatewayHandler()
// verify request
values := url.Values{}
apiConfig := configs.Default().APIConfig
if req.ContainerPort == 0 {
values["container_port"] = []string{"The container_port field is required"}
}
if req.Port == 0 {
values["port"] = []string{"The port field is required"}
} else if req.Port <= g.cfg.MinExtPort {
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", g.cfg.MinExtPort)}
} else if req.Port <= apiConfig.MinExtPort {
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", apiConfig.MinExtPort)}
} else {
// check if the port exists
if h.TCPIPPortExists(req.IP, req.Port) {
Expand Down Expand Up @@ -385,8 +385,9 @@ func (g *GatewayStruct) updateTCPRule(w http.ResponseWriter, r *http.Request) {
h := handler.GetGatewayHandler()
// verify reqeust
values := url.Values{}
if req.Port != 0 && req.Port <= g.cfg.MinExtPort {
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", g.cfg.MinExtPort)}
apiConfig := configs.Default().APIConfig
if req.Port != 0 && req.Port <= apiConfig.MinExtPort {
values["port"] = []string{fmt.Sprintf("The port field should be greater than %d", apiConfig.MinExtPort)}
}
if len(req.RuleExtensions) > 0 {
for _, re := range req.RuleExtensions {
Expand All @@ -405,7 +406,7 @@ func (g *GatewayStruct) updateTCPRule(w http.ResponseWriter, r *http.Request) {
return
}

err := h.UpdateTCPRule(&req, g.cfg.MinExtPort)
err := h.UpdateTCPRule(&req, apiConfig.MinExtPort)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("Unexpected error occorred while "+
"updating tcp rule: %v", err))
Expand Down Expand Up @@ -472,7 +473,7 @@ func (g *GatewayStruct) Certificate(w http.ResponseWriter, r *http.Request) {
}
}

//updCertificate updates certificate and refresh http rules based on certificate id
// updCertificate updates certificate and refresh http rules based on certificate id
func (g *GatewayStruct) updCertificate(w http.ResponseWriter, r *http.Request) {
var req api_model.UpdCertificateReq
ok := httputil.ValidatorRequestStructAndErrorResponse(r, w, &req, nil)
Expand All @@ -493,7 +494,7 @@ func (g *GatewayStruct) updCertificate(w http.ResponseWriter, r *http.Request) {
httputil.ReturnSuccess(r, w, nil)
}

//GetGatewayIPs get gateway ips
// GetGatewayIPs get gateway ips
func GetGatewayIPs(w http.ResponseWriter, r *http.Request) {
ips := handler.GetGatewayHandler().GetGatewayIPs()
httputil.ReturnSuccess(r, w, ips)
Expand Down
6 changes: 3 additions & 3 deletions api/controller/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
package controller

import (
"github.com/goodrain/rainbond/cmd/api/option"
"github.com/goodrain/rainbond/config/configs"
httputil "github.com/goodrain/rainbond/util/http"
"net/http"
)

// LabelController implements Labeler.
type LabelController struct {
optconfig *option.Config
}

// Labels - get -> list labels
Expand All @@ -38,5 +37,6 @@ func (l *LabelController) Labels(w http.ResponseWriter, r *http.Request) {
}

func (l *LabelController) listLabels(w http.ResponseWriter, r *http.Request) {
httputil.ReturnSuccess(r, w, l.optconfig.EnableFeature)
config := configs.Default()
httputil.ReturnSuccess(r, w, config.APIConfig.EnableFeature)
}
21 changes: 7 additions & 14 deletions api/controller/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
package controller

import (
"github.com/goodrain/rainbond/pkg/component/mq"
"net/http"

"github.com/goodrain/rainbond/api/api"
"github.com/goodrain/rainbond/api/proxy"
"github.com/goodrain/rainbond/cmd/api/option"
mqclient "github.com/goodrain/rainbond/mq/client"
"github.com/goodrain/rainbond/worker/client"
)

Expand Down Expand Up @@ -57,8 +56,8 @@ type V2Manager interface {
var defaultV2Manager V2Manager

// CreateV2RouterManager 创建manager
func CreateV2RouterManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (err error) {
defaultV2Manager, err = NewManager(conf, statusCli)
func CreateV2RouterManager(statusCli *client.AppRuntimeSyncClient) (err error) {
defaultV2Manager, err = NewAPIManager(statusCli)
return err
}

Expand All @@ -67,18 +66,12 @@ func GetManager() V2Manager {
return defaultV2Manager
}

// NewManager new manager
func NewManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (*V2Routes, error) {
mqClient, err := mqclient.NewMqClient(conf.MQAPI)
if err != nil {
return nil, err
}
// NewAPIManager new manager
func NewAPIManager(statusCli *client.AppRuntimeSyncClient) (*V2Routes, error) {
var v2r V2Routes
v2r.TenantStruct.StatusCli = statusCli
v2r.TenantStruct.MQClient = mqClient
v2r.GatewayStruct.MQClient = mqClient
v2r.GatewayStruct.cfg = &conf
v2r.LabelController.optconfig = &conf
v2r.TenantStruct.MQClient = mq.Default().MqClient
v2r.GatewayStruct.MQClient = mq.Default().MqClient
eventServerProxy := proxy.CreateProxy("eventlog", "http", []string{"local=>rbd-eventlog:6363"})
v2r.EventLogStruct.EventlogServerProxy = eventServerProxy
return &v2r, nil
Expand Down
2 changes: 0 additions & 2 deletions api/controller/registry_auth_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ import (

"github.com/goodrain/rainbond/api/handler"
api_model "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/cmd/api/option"
"github.com/goodrain/rainbond/mq/client"
httputil "github.com/goodrain/rainbond/util/http"
)

// RegistryAuthSecretStruct -
type RegistryAuthSecretStruct struct {
MQClient client.MQClient
cfg *option.Config
}

// RegistryAuthSecret http handler for registry auth secret
Expand Down
Loading

0 comments on commit 7d9a6ed

Please sign in to comment.