Skip to content

Commit

Permalink
chore(api): use direct routes (#2066)
Browse files Browse the repository at this point in the history
* chore(api): use direct routes

Signed-off-by: Yue Yang <g1enyy0ung@gmail.com>

* fix: makefile

Signed-off-by: Yue Yang <g1enyy0ung@gmail.com>

* fix(ci): imports

Signed-off-by: Yue Yang <g1enyy0ung@gmail.com>

* chore: address comments

Signed-off-by: Yue Yang <g1enyy0ung@gmail.com>

Co-authored-by: STRRL <str_ruiling@outlook.com>
Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
3 people committed Jul 2, 2021
1 parent e3b091d commit 2c98e64
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 143 deletions.
6 changes: 3 additions & 3 deletions Makefile
Expand Up @@ -25,14 +25,14 @@ endif

# Enable GO111MODULE=on explicitly, disable it with GO111MODULE=off when necessary.
export GO111MODULE := on
GOOS := $(if $(GOOS),$(GOOS),"")
GOOS := $(if $(GOOS),$(GOOS),"")
GOARCH := $(if $(GOARCH),$(GOARCH),"")
GOENV := GO15VENDOREXPERIMENT="1" CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH)
CGOENV := GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH)
CGOENV := GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH)
GO := $(GOENV) go
CGO := $(CGOENV) go
GOTEST := TEST_USE_EXISTING_CLUSTER=false NO_PROXY="${NO_PROXY},testhost" go test
SHELL := bash
SHELL := bash

PACKAGE_LIST := echo $$(go list ./... | grep -vE "chaos-mesh/test|pkg/ptrace|zz_generated|vendor") github.com/chaos-mesh/chaos-mesh/api/v1alpha1

Expand Down
16 changes: 8 additions & 8 deletions cmd/chaos-dashboard/main.go
Expand Up @@ -38,15 +38,15 @@ import (
)

var (
log = ctrl.Log.WithName("setup")
log = ctrl.Log.WithName("dashboard")
)

var (
printVersion bool
)

// @title Chaos Mesh Dashboard API
// @version 1.2
// @version 2.0
// @description Swagger docs for Chaos Mesh Dashboard. If you encounter any problems with API, please click on the issues link below to report bugs or questions.

// @contact.name Issues
Expand All @@ -66,25 +66,26 @@ func main() {
}

ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
mainLog := log.WithName("main")

dashboardConfig, err := config.EnvironChaosDashboard()
dashboardConfig, err := config.GetChaosDashboardEnv()
if err != nil {
log.Error(err, "main: invalid ChaosDashboardConfig")
mainLog.Error(err, "invalid ChaosDashboardConfig")
os.Exit(1)
}
dashboardConfig.Version = version.Get().GitVersion

persistTTLConfigParsed, err := config.ParsePersistTTLConfig(dashboardConfig.PersistTTL)
if err != nil {
log.Error(err, "main: invalid PersistTTLConfig")
mainLog.Error(err, "invalid PersistTTLConfig")
os.Exit(1)
}

controllerRuntimeStopCh := ctrl.SetupSignalHandler()
ctrlRuntimeStopCh := ctrl.SetupSignalHandler()
app := fx.New(
fx.Provide(
func() (<-chan struct{}, *config.ChaosDashboardConfig, *ttlcontroller.TTLconfig) {
return controllerRuntimeStopCh, dashboardConfig, persistTTLConfigParsed
return ctrlRuntimeStopCh, dashboardConfig, persistTTLConfigParsed
},
dbstore.NewDBStore,
collector.NewServer,
Expand All @@ -97,5 +98,4 @@ func main() {
)

app.Run()
<-controllerRuntimeStopCh
}
44 changes: 0 additions & 44 deletions pkg/apiserver/archive/archive_test.go
Expand Up @@ -55,10 +55,6 @@ func (m *MockExperimentStore) ListMeta(ctx context.Context, kind, namespace, nam
var err error
if kind == "testKind" {
expMeta := &core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: "testUID",
Kind: "testKind",
Name: "testName",
Expand All @@ -84,10 +80,6 @@ func (m *MockExperimentStore) FindByUID(ctx context.Context, UID string) (*core.
jsonStr, _ := json.Marshal(chaos)
res = &core.Experiment{
ExperimentMeta: core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: UID,
Kind: v1alpha1.KindPodChaos,
Name: "testName",
Expand All @@ -104,10 +96,6 @@ func (m *MockExperimentStore) FindByUID(ctx context.Context, UID string) (*core.
jsonStr, _ := json.Marshal(chaos)
res = &core.Experiment{
ExperimentMeta: core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: UID,
Kind: v1alpha1.KindIOChaos,
Name: "testName",
Expand All @@ -124,10 +112,6 @@ func (m *MockExperimentStore) FindByUID(ctx context.Context, UID string) (*core.
jsonStr, _ := json.Marshal(chaos)
res = &core.Experiment{
ExperimentMeta: core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: UID,
Kind: v1alpha1.KindNetworkChaos,
Name: "testName",
Expand All @@ -144,10 +128,6 @@ func (m *MockExperimentStore) FindByUID(ctx context.Context, UID string) (*core.
jsonStr, _ := json.Marshal(chaos)
res = &core.Experiment{
ExperimentMeta: core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: UID,
Kind: v1alpha1.KindTimeChaos,
Name: "testName",
Expand All @@ -164,10 +144,6 @@ func (m *MockExperimentStore) FindByUID(ctx context.Context, UID string) (*core.
jsonStr, _ := json.Marshal(chaos)
res = &core.Experiment{
ExperimentMeta: core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: UID,
Kind: v1alpha1.KindKernelChaos,
Name: "testName",
Expand All @@ -184,10 +160,6 @@ func (m *MockExperimentStore) FindByUID(ctx context.Context, UID string) (*core.
jsonStr, _ := json.Marshal(chaos)
res = &core.Experiment{
ExperimentMeta: core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: UID,
Kind: v1alpha1.KindStressChaos,
Name: "testName",
Expand All @@ -202,10 +174,6 @@ func (m *MockExperimentStore) FindByUID(ctx context.Context, UID string) (*core.
case "testOtherChaos":
res = &core.Experiment{
ExperimentMeta: core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: UID,
Kind: "OtherChaos",
Name: "testName",
Expand All @@ -231,10 +199,6 @@ func (m *MockExperimentStore) FindMetaByUID(ctx context.Context, UID string) (*c
switch UID {
case "tsetUID":
res = &core.ExperimentMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: "testUID",
Kind: "testKind",
Name: "testName",
Expand Down Expand Up @@ -281,10 +245,6 @@ func (m *MockScheduleStore) ListMeta(ctx context.Context, namespace, name string
var err error
if name == "testScheduleName" {
schMeta := &core.ScheduleMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: "testUID",
Kind: "testKind",
Name: "testScheduleName",
Expand All @@ -310,10 +270,6 @@ func (m *MockScheduleStore) FindByUID(ctx context.Context, UID string) (*core.Sc
jsonStr, _ := json.Marshal(sch)
res = &core.Schedule{
ScheduleMeta: core.ScheduleMeta{
ID: 0,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
DeletedAt: nil,
UID: UID,
Kind: v1alpha1.KindPodChaos,
Name: "testName",
Expand Down
38 changes: 17 additions & 21 deletions pkg/apiserver/server.go
Expand Up @@ -49,7 +49,7 @@ func serverRegister(r *gin.Engine, conf *config.ChaosDashboardConfig) {
go r.Run(listenAddr)
}

func newEngine() *gin.Engine {
func newEngine(config *config.ChaosDashboardConfig) *gin.Engine {
r := gin.Default()

// default is "/debug/pprof/"
Expand All @@ -70,20 +70,28 @@ func newEngine() *gin.Engine {
v.RegisterValidation("RequiredFieldEqual", apivalidator.RequiredFieldEqualValid, true)
}

moveToUIRoot := func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/dashboard")
}

r.GET("/", moveToUIRoot)
ui := uiserver.AssetsFS()
if ui != nil {
newDashboardRouter(r, ui)
r.GET("/", func(c *gin.Context) {
c.FileFromFS("/", ui)
})
// `/:foo/*bar` from https://en.wikipedia.org/wiki/Foobar, the name itself has no meaning.
//
// This handle just internally redirects all no-exact routes to the root directory of static files because the UI is a single page application and only has one entry (index.html).
r.GET("/:foo/*bar", func(c *gin.Context) {
c.FileFromFS("/", ui)
})

renderStatic := func(c *gin.Context) {
c.FileFromFS(c.Request.URL.Path, ui)
}
r.GET("/static/*any", renderStatic)
r.GET("/favicon.ico", renderStatic)
} else {
r.GET("/dashboard", func(c *gin.Context) {
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Dashboard UI is not built. Please run `UI=1 make`.")
})
}
r.NoRoute(moveToUIRoot)

return r
}
Expand All @@ -96,15 +104,3 @@ func newAPIRouter(r *gin.Engine) *gin.RouterGroup {

return api
}

func newDashboardRouter(r *gin.Engine, ui http.FileSystem) {
renderRequest := func(c *gin.Context) {
c.FileFromFS(c.Request.URL.Path, ui)
}

r.GET("/dashboard/*any", func(c *gin.Context) {
c.FileFromFS("/", ui)
})
r.GET("/static/*any", renderRequest)
r.GET("/favicon.ico", renderRequest)
}
5 changes: 2 additions & 3 deletions pkg/config/dashboard/dashboard.go
Expand Up @@ -37,7 +37,6 @@ type ChaosDashboardConfig struct {
// EnableFilterNamespace will filter namespace with annotation. Only the pods/containers in namespace
// annotated with `chaos-mesh.org/inject=enabled` will be injected
EnableFilterNamespace bool `envconfig:"ENABLE_FILTER_NAMESPACE" default:"false"`

// SecurityMode will use the token login by the user if set to true
SecurityMode bool `envconfig:"SECURITY_MODE" default:"true" json:"security_mode"`
DNSServerCreate bool `envconfig:"DNS_SERVER_CREATE" default:"false" json:"dns_server_create"`
Expand All @@ -60,8 +59,8 @@ type DatabaseConfig struct {
Secret string `envconfig:"DATABASE_SECRET"`
}

// EnvironChaosDashboard returns the settings from the environment.
func EnvironChaosDashboard() (*ChaosDashboardConfig, error) {
// GetChaosDashboardEnv gets all env variables related to dashboard.
func GetChaosDashboardEnv() (*ChaosDashboardConfig, error) {
cfg := ChaosDashboardConfig{}
err := envconfig.Process("", &cfg)
return &cfg, err
Expand Down
23 changes: 11 additions & 12 deletions pkg/core/experiment.go
Expand Up @@ -20,6 +20,8 @@ import (

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/jinzhu/gorm"

"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
)

Expand Down Expand Up @@ -64,18 +66,15 @@ type Experiment struct {

// ExperimentMeta defines the metadata of an experiment. Use in db.
type ExperimentMeta struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `sql:"index" json:"deleted_at"`
UID string `gorm:"index:uid" json:"uid"`
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Action string `json:"action"`
StartTime time.Time `json:"start_time"`
FinishTime time.Time `json:"finish_time"`
Archived bool `json:"archived"`
gorm.Model
UID string `gorm:"index:uid" json:"uid"`
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Action string `json:"action"`
StartTime time.Time `json:"start_time"`
FinishTime time.Time `json:"finish_time"`
Archived bool `json:"archived"`
}

// ExperimentInfo defines a form data of Experiment from API.
Expand Down
23 changes: 11 additions & 12 deletions pkg/core/schedule.go
Expand Up @@ -17,6 +17,8 @@ import (
"context"
"time"

"github.com/jinzhu/gorm"

"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
)

Expand Down Expand Up @@ -61,18 +63,15 @@ type Schedule struct {

// ScheduleMeta defines the metadata of a schedule instance. Use in db.
type ScheduleMeta struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `sql:"index" json:"deleted_at"`
UID string `gorm:"index:uid" json:"uid"`
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Action string `json:"action"`
StartTime time.Time `json:"start_time"`
FinishTime time.Time `json:"finish_time"`
Archived bool `json:"archived"`
gorm.Model
UID string `gorm:"index:schedule_uid" json:"uid"`
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Action string `json:"action"`
StartTime time.Time `json:"start_time"`
FinishTime time.Time `json:"finish_time"`
Archived bool `json:"archived"`
}

// ScheduleInfo defines a form data of schedule from API.
Expand Down
12 changes: 6 additions & 6 deletions pkg/core/workflow.go
Expand Up @@ -19,6 +19,7 @@ import (
"strings"
"time"

"github.com/jinzhu/gorm"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -49,15 +50,14 @@ const (

// WorkflowMeta defines the root structure of a workflow.
type WorkflowMeta struct {
ID uint `gorm:"primary_key" json:"id"`
Namespace string `json:"namespace"`
Name string `json:"name"`
// the entry node name
Entry string `json:"entry"`
gorm.Model
UID string `gorm:"index:workflow_uid" json:"uid"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Entry string `json:"entry"` // the entry node name
CreatedAt string `json:"created_at"`
EndTime string `json:"end_time"`
Status WorkflowStatus `json:"status,omitempty"`
UID string `gorm:"index:uid" json:"uid"`
Archived bool `json:"-"`
}

Expand Down

0 comments on commit 2c98e64

Please sign in to comment.