Skip to content

Commit

Permalink
refine(feature): flag & manager
Browse files Browse the repository at this point in the history
  • Loading branch information
shhdgit committed Nov 14, 2021
1 parent 0b4dd21 commit 4857960
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 139 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ ifeq ($(DISTRO_BUILD_TAG),1)
BUILD_TAGS += distro
endif

LDFLAGS += -X "$(DASHBOARD_PKG)/util/versionutil.InternalVersion=$(shell grep -v '^\#' ./release-version)"
LDFLAGS += -X "$(DASHBOARD_PKG)/util/versionutil.Standalone=Yes"
LDFLAGS += -X "$(DASHBOARD_PKG)/util/versionutil.PDVersion=N/A"
LDFLAGS += -X "$(DASHBOARD_PKG)/util/versionutil.BuildTime=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
LDFLAGS += -X "$(DASHBOARD_PKG)/util/versionutil.BuildGitHash=$(shell git rev-parse HEAD)"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.InternalVersion=$(shell grep -v '^\#' ./release-version)"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.Standalone=Yes"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.PDVersion=N/A"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.BuildTime=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
LDFLAGS += -X "$(DASHBOARD_PKG)/pkg/utils/version.BuildGitHash=$(shell git rev-parse HEAD)"

default: server

Expand Down
4 changes: 2 additions & 2 deletions cmd/tidb-dashboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import (
keyvisualregion "github.com/pingcap/tidb-dashboard/pkg/keyvisual/region"
"github.com/pingcap/tidb-dashboard/pkg/swaggerserver"
"github.com/pingcap/tidb-dashboard/pkg/uiserver"
"github.com/pingcap/tidb-dashboard/pkg/utils/version"
_ "github.com/pingcap/tidb-dashboard/populate/distro"
"github.com/pingcap/tidb-dashboard/util/versionutil"
)

type DashboardCLIConfig struct {
Expand Down Expand Up @@ -84,7 +84,7 @@ func NewCLIConfig() *DashboardCLIConfig {

flag.Parse()
if *showVersion {
versionutil.PrintStandaloneModeInfo()
version.PrintStandaloneModeInfo()
_ = log.Sync()
os.Exit(0)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import (
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user/sso"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user/sso/ssoauth"
"github.com/pingcap/tidb-dashboard/pkg/tiflash"
"github.com/pingcap/tidb-dashboard/util/versionutil"
"github.com/pingcap/tidb-dashboard/pkg/utils/version"
"github.com/pingcap/tidb-dashboard/util/feature"

// "github.com/pingcap/tidb-dashboard/pkg/apiserver/__APP_NAME__"
// NOTE: Don't remove above comment line, it is a placeholder for code generator.
Expand Down Expand Up @@ -103,6 +104,7 @@ func (s *Service) Start(ctx context.Context) error {
s.app = fx.New(
fx.Logger(utils.NewFxPrinter()),
fx.Provide(
feature.NewManagerProvider(s.config.FeatureVersion),
newAPIHandlerEngine,
s.provideLocals,
dbstore.NewDBStore,
Expand All @@ -114,7 +116,6 @@ func (s *Service) Start(ctx context.Context) error {
tikv.NewTiKVClient,
tiflash.NewTiFlashClient,
utils.NewSysSchema,
user.NewAuthService,
info.NewService,
clusterinfo.NewService,
logsearch.NewService,
Expand All @@ -126,6 +127,7 @@ func (s *Service) Start(ctx context.Context) error {
// __APP_NAME__.NewService,
// NOTE: Don't remove above comment line, it is a placeholder for code generator
),
user.Module,
codeauth.Module,
sqlauth.Module,
ssoauth.Module,
Expand All @@ -138,7 +140,6 @@ func (s *Service) Start(ctx context.Context) error {
debugapi.Module,
fx.Populate(&s.apiHandlerEngine),
fx.Invoke(
user.RegisterRouter,
info.RegisterRouter,
clusterinfo.RegisterRouter,
profiling.RegisterRouter,
Expand All @@ -160,7 +161,7 @@ func (s *Service) Start(ctx context.Context) error {
return err
}

versionutil.Print()
version.Print()

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/apiserver/conprof/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package conprof
import (
"go.uber.org/fx"

"github.com/pingcap/tidb-dashboard/util/versionutil"
"github.com/pingcap/tidb-dashboard/util/feature"
)

var FeatureFlagConprof = versionutil.NewFeatureFlag("conprof", []string{">= 5.3.0"})
var FeatureFlagConprof = feature.NewFlag("conprof", []string{">= 5.3.0"})

var Module = fx.Options(
fx.Provide(newService),
fx.Invoke(registerRouter),
fx.Invoke(registerRouter, FeatureFlagConprof.Register()),
)
17 changes: 8 additions & 9 deletions pkg/apiserver/conprof/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"golang.org/x/sync/singleflight"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/user"
apiutils "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/pkg/utils/topology"
"github.com/pingcap/tidb-dashboard/util/versionutil"
"github.com/pingcap/tidb-dashboard/util/feature"
)

var (
Expand Down Expand Up @@ -68,12 +68,11 @@ func newService(lc fx.Lifecycle, p ServiceParams) *Service {
}

// Register register the handlers to the service.
func registerRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
func registerRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service, fm *feature.Manager) {
endpoint := r.Group("/continuous_profiling")

endpoint.Use(apiutils.MWForbidByFeatureFlag([]*versionutil.FeatureFlag{
FeatureFlagConprof,
}, s.params.Config.FeatureVersion))
featureFlags := []*feature.Flag{FeatureFlagConprof}
endpoint.Use(fm.Guard(featureFlags))
{
endpoint.GET("/config", auth.MWAuthRequired(), s.reverseProxy("/config"), s.conprofConfig)
endpoint.POST("/config", auth.MWAuthRequired(), auth.MWRequireWritePriv(), s.reverseProxy("/config"), s.updateConprofConfig)
Expand All @@ -99,9 +98,9 @@ func (s *Service) reverseProxy(targetPath string) gin.HandlerFunc {
c.Request.URL.Path = targetPath
token := c.Query("token")
if token != "" {
queryStr, err := apiutils.ParseJWTString("conprof", token)
queryStr, err := utils.ParseJWTString("conprof", token)
if err != nil {
apiutils.MakeInvalidRequestErrorFromError(c, err)
utils.MakeInvalidRequestErrorFromError(c, err)
return
}
c.Request.URL.RawQuery = queryStr
Expand Down Expand Up @@ -287,7 +286,7 @@ func (s *Service) conprofGroupProfileDetail(c *gin.Context) {
// @Failure 500 {object} utils.APIError
func (s *Service) genConprofActionToken(c *gin.Context) {
q := c.Query("q")
token, err := apiutils.NewJWTString("conprof", q)
token, err := utils.NewJWTString("conprof", q)
if err != nil {
_ = c.Error(err)
return
Expand Down
45 changes: 17 additions & 28 deletions pkg/apiserver/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ import (
"github.com/thoas/go-funk"
"go.uber.org/fx"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/conprof"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user"
apiutils "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/pkg/dbstore"
"github.com/pingcap/tidb-dashboard/pkg/tidb"
"github.com/pingcap/tidb-dashboard/util/versionutil"
"github.com/pingcap/tidb-dashboard/pkg/utils/version"
"github.com/pingcap/tidb-dashboard/util/feature"
)

type ServiceParams struct {
fx.In
Config *config.Config
LocalStore *dbstore.DB
TiDBClient *tidb.Client
Config *config.Config
LocalStore *dbstore.DB
TiDBClient *tidb.Client
FeatureManager *feature.Manager
}

type Service struct {
Expand All @@ -41,16 +42,16 @@ func RegisterRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) {
endpoint.Use(auth.MWAuthRequired())
endpoint.GET("/whoami", s.whoamiHandler)

endpoint.Use(apiutils.MWConnectTiDB(s.params.TiDBClient))
endpoint.Use(utils.MWConnectTiDB(s.params.TiDBClient))
endpoint.GET("/databases", s.databasesHandler)
endpoint.GET("/tables", s.tablesHandler)
}

type InfoResponse struct { //nolint
Version *versionutil.Info `json:"version"`
EnableTelemetry bool `json:"enable_telemetry"`
EnableExperimental bool `json:"enable_experimental"`
SupportedFeatures []string `json:"supported_features"`
Version *version.Info `json:"version"`
EnableTelemetry bool `json:"enable_telemetry"`
EnableExperimental bool `json:"enable_experimental"`
SupportedFeatures []string `json:"supported_features"`
}

// @ID infoGet
Expand All @@ -60,23 +61,11 @@ type InfoResponse struct { //nolint
// @Security JwtAuth
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) infoHandler(c *gin.Context) {
featureFlags := []*versionutil.FeatureFlag{
conprof.FeatureFlagConprof,
user.FeatureFlagNonRootLogin,
}
supportedFeatures := []string{}
for _, ff := range featureFlags {
if !ff.IsSupported(s.params.Config.FeatureVersion) {
continue
}
supportedFeatures = append(supportedFeatures, ff.Name)
}

resp := InfoResponse{
Version: versionutil.GetInfo(),
Version: version.GetInfo(),
EnableTelemetry: s.params.Config.EnableTelemetry,
EnableExperimental: s.params.Config.EnableExperimental,
SupportedFeatures: supportedFeatures,
SupportedFeatures: s.params.FeatureManager.SupportedFeatures(),
}
c.JSON(http.StatusOK, resp)
}
Expand All @@ -94,7 +83,7 @@ type WhoAmIResponse struct {
// @Security JwtAuth
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) whoamiHandler(c *gin.Context) {
sessionUser := apiutils.GetSession(c)
sessionUser := utils.GetSession(c)
resp := WhoAmIResponse{
DisplayName: sessionUser.DisplayName,
IsShareable: sessionUser.IsShareable,
Expand All @@ -114,7 +103,7 @@ func (s *Service) databasesHandler(c *gin.Context) {
Databases string `gorm:"column:Database"`
}
var result []databaseSchemas
db := apiutils.GetTiDBConnection(c)
db := utils.GetTiDBConnection(c)
err := db.Raw("SHOW DATABASES").Scan(&result).Error
if err != nil {
_ = c.Error(err)
Expand Down Expand Up @@ -142,7 +131,7 @@ type tableSchema struct {
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) tablesHandler(c *gin.Context) {
var result []tableSchema
db := apiutils.GetTiDBConnection(c)
db := utils.GetTiDBConnection(c)
tx := db.Select([]string{"TABLE_NAME", "TIDB_TABLE_ID"}).Table("INFORMATION_SCHEMA.TABLES")
databaseName := c.Query("database_name")

Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (a BaseAuthenticator) SignOutInfo(u *utils.SessionUser, redirectURL string)
return &SignOutInfo{}, nil
}

func NewAuthService() *AuthService {
func newAuthService() *AuthService {
var secret *[32]byte

secretStr := os.Getenv("DASHBOARD_SESSION_SECRET")
Expand Down Expand Up @@ -219,7 +219,7 @@ func (s *AuthService) authForm(f AuthenticateForm) (*utils.SessionUser, error) {
return u, nil
}

func RegisterRouter(r *gin.RouterGroup, s *AuthService) {
func registerRouter(r *gin.RouterGroup, s *AuthService) {
endpoint := r.Group("/user")
endpoint.GET("/login_info", s.getLoginInfoHandler)
endpoint.POST("/login", s.loginHandler)
Expand Down
13 changes: 11 additions & 2 deletions pkg/apiserver/user/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

package user

import "github.com/pingcap/tidb-dashboard/util/versionutil"
import (
"go.uber.org/fx"

var FeatureFlagNonRootLogin = versionutil.NewFeatureFlag("nonRootLogin", []string{">= 5.3.0"})
"github.com/pingcap/tidb-dashboard/util/feature"
)

var FeatureFlagNonRootLogin = feature.NewFlag("nonRootLogin", []string{">= 5.3.0"})

var Module = fx.Options(
fx.Provide(newAuthService),
fx.Invoke(registerRouter, FeatureFlagNonRootLogin.Register()),
)
48 changes: 0 additions & 48 deletions pkg/apiserver/utils/mw_feature_flag.go

This file was deleted.

4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/tls"
"net/url"
"strings"

"github.com/pingcap/tidb-dashboard/pkg/utils/version"
)

const (
Expand Down Expand Up @@ -40,7 +42,7 @@ func Default() *Config {
TiDBTLSConfig: nil,
EnableTelemetry: true,
EnableExperimental: false,
FeatureVersion: "",
FeatureVersion: version.PDVersion,
}
}

Expand Down
8 changes: 4 additions & 4 deletions util/versionutil/version.go → pkg/utils/version/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0.

package versionutil
package version

import (
"fmt"
Expand All @@ -9,7 +9,7 @@ import (
"github.com/pingcap/log"
"go.uber.org/zap"

"github.com/pingcap/tidb-dashboard/util/distro"
"github.com/pingcap/tidb-dashboard/pkg/utils/distro"
)

type Info struct {
Expand All @@ -30,10 +30,10 @@ var (
)

func Print() {
log.Info(fmt.Sprintf("%s Dashboard started", distro.R().TiDB),
log.Info(fmt.Sprintf("%s Dashboard started", distro.Data("tidb")),
zap.String("internal-version", InternalVersion),
zap.String("standalone", Standalone),
zap.String(fmt.Sprintf("%s-version", strings.ToLower(distro.R().PD)), PDVersion),
zap.String(fmt.Sprintf("%s-version", strings.ToLower(distro.Data("pd"))), PDVersion),
zap.String("build-time", BuildTime),
zap.String("build-git-hash", BuildGitHash))
}
Expand Down
Loading

0 comments on commit 4857960

Please sign in to comment.