Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor feature flag to support more modules #1057

Merged
merged 23 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
github.com/vmihailenco/msgpack/v5 v5.3.5
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738
go.uber.org/atomic v1.9.0
go.uber.org/fx v1.10.0
go.uber.org/fx v1.12.0
go.uber.org/goleak v1.1.10
go.uber.org/zap v1.19.0
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/dig v1.8.0 h1:1rR6hnL/bu1EVcjnRDN5kx1vbIjEJDTGhSQ2B3ddpcI=
go.uber.org/dig v1.8.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
go.uber.org/fx v1.10.0 h1:S2K/H8oNied0Je/mLKdWzEWKZfv9jtxSDm8CnwK+5Fg=
go.uber.org/fx v1.10.0/go.mod h1:vLRicqpG/qQEzno4SYU86iCwfT95EZza+Eba0ItuxqY=
go.uber.org/dig v1.9.0 h1:pJTDXKEhRqBI8W7rU7kwT5EgyRZuSMVSFcZolOvKK9U=
go.uber.org/dig v1.9.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
go.uber.org/fx v1.12.0 h1:+1+3Cz9M0dFMPy9SW9XUIUHye8bnPUm7q7DroNGWYG4=
go.uber.org/fx v1.12.0/go.mod h1:egT3Kyg1JFYQkvKLZ3EsykxkNrZxgXS+gKoKo7abERY=
go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
Expand Down
7 changes: 4 additions & 3 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +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/pkg/utils/version"
"github.com/pingcap/tidb-dashboard/util/featureflag"
"github.com/pingcap/tidb-dashboard/util/rest"

// "github.com/pingcap/tidb-dashboard/pkg/apiserver/__APP_NAME__"
Expand All @@ -46,7 +48,6 @@ import (
"github.com/pingcap/tidb-dashboard/pkg/tidb"
"github.com/pingcap/tidb-dashboard/pkg/tikv"
"github.com/pingcap/tidb-dashboard/pkg/utils"
"github.com/pingcap/tidb-dashboard/pkg/utils/version"
)

func Handler(s *Service) http.Handler {
Expand Down Expand Up @@ -99,6 +100,7 @@ func (s *Service) Start(ctx context.Context) error {

s.app = fx.New(
fx.Logger(utils.NewFxPrinter()),
fx.Supply(featureflag.NewRegistry(s.config.FeatureVersion)),
fx.Provide(
newAPIHandlerEngine,
s.provideLocals,
Expand All @@ -111,7 +113,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 @@ -123,6 +124,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 @@ -135,7 +137,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 Down
23 changes: 0 additions & 23 deletions pkg/apiserver/conprof/feature_support.go

This file was deleted.

4 changes: 3 additions & 1 deletion pkg/apiserver/conprof/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

package conprof

import "go.uber.org/fx"
import (
"go.uber.org/fx"
)

var Module = fx.Options(
fx.Provide(newService),
Expand Down
17 changes: 13 additions & 4 deletions pkg/apiserver/conprof/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"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/featureflag"
"github.com/pingcap/tidb-dashboard/util/rest"
)

Expand All @@ -44,11 +45,14 @@ type ngMonitoringAddrCacheEntity struct {
type ServiceParams struct {
fx.In

EtcdClient *clientv3.Client
Config *config.Config
EtcdClient *clientv3.Client
Config *config.Config
FeatureFlags *featureflag.Registry
}

type Service struct {
FeatureFlagConprof *featureflag.FeatureFlag

params ServiceParams
lifecycleCtx context.Context

Expand All @@ -57,21 +61,26 @@ type Service struct {
}

func newService(lc fx.Lifecycle, p ServiceParams) *Service {
s := &Service{params: p}
s := &Service{
FeatureFlagConprof: p.FeatureFlags.Register("conprof", ">= 5.3.0"),
params: p,
}

lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
s.lifecycleCtx = ctx
return nil
},
})

return s
}

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

endpoint.Use(utils.MWForbidByFeatureSupport(IsFeatureSupport(s.params.Config)))
endpoint.Use(s.FeatureFlagConprof.VersionGuard())
{
endpoint.GET("/config", auth.MWAuthRequired(), s.reverseProxy("/config"), s.conprofConfig)
endpoint.POST("/config", auth.MWAuthRequired(), auth.MWRequireWritePriv(), s.reverseProxy("/config"), s.updateConprofConfig)
Expand Down
20 changes: 6 additions & 14 deletions pkg/apiserver/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +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/nonrootlogin"
"github.com/pingcap/tidb-dashboard/pkg/apiserver/user"
"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/pkg/utils/version"
"github.com/pingcap/tidb-dashboard/util/featureflag"
)

type ServiceParams struct {
fx.In
Config *config.Config
LocalStore *dbstore.DB
TiDBClient *tidb.Client
Config *config.Config
LocalStore *dbstore.DB
TiDBClient *tidb.Client
FeatureFlags *featureflag.Registry
}

type Service struct {
Expand Down Expand Up @@ -61,19 +61,11 @@ type InfoResponse struct { //nolint
// @Security JwtAuth
// @Failure 401 {object} rest.ErrorResponse
func (s *Service) infoHandler(c *gin.Context) {
supportedFeatures := []string{}
if conprof.IsFeatureSupport(s.params.Config) {
supportedFeatures = append(supportedFeatures, "conprof")
}
if nonrootlogin.IsFeatureSupport(s.params.Config) {
supportedFeatures = append(supportedFeatures, "nonRootLogin")
}

resp := InfoResponse{
Version: version.GetInfo(),
EnableTelemetry: s.params.Config.EnableTelemetry,
EnableExperimental: s.params.Config.EnableExperimental,
SupportedFeatures: supportedFeatures,
SupportedFeatures: s.params.FeatureFlags.SupportedFeatures(),
}
c.JSON(http.StatusOK, resp)
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/apiserver/nonrootlogin/feature_support.go

This file was deleted.

12 changes: 8 additions & 4 deletions pkg/apiserver/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go.uber.org/zap"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/util/featureflag"
"github.com/pingcap/tidb-dashboard/util/rest"
)

Expand All @@ -30,6 +31,8 @@ var (
)

type AuthService struct {
FeatureFlagNonRootLogin *featureflag.FeatureFlag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature should be placed inside SQL auth, as only SQL auth needs it and other kind of auth does not care about the "non root login".


middleware *jwt.GinJWTMiddleware
authenticators map[utils.AuthType]Authenticator
}
Expand Down Expand Up @@ -71,7 +74,7 @@ func (a BaseAuthenticator) SignOutInfo(u *utils.SessionUser, redirectURL string)
return &SignOutInfo{}, nil
}

func NewAuthService() *AuthService {
func newAuthService(featureFlags *featureflag.Registry) *AuthService {
var secret *[32]byte

secretStr := os.Getenv("DASHBOARD_SESSION_SECRET")
Expand All @@ -88,8 +91,9 @@ func NewAuthService() *AuthService {
}

service := &AuthService{
middleware: nil,
authenticators: map[utils.AuthType]Authenticator{},
FeatureFlagNonRootLogin: featureFlags.Register("nonRootLogin", ">= 5.3.0"),
middleware: nil,
authenticators: map[utils.AuthType]Authenticator{},
}

middleware, err := jwt.New(&jwt.GinJWTMiddleware{
Expand Down Expand Up @@ -219,7 +223,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
12 changes: 12 additions & 0 deletions pkg/apiserver/user/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0.

package user

import (
"go.uber.org/fx"
)

var Module = fx.Options(
fx.Provide(newAuthService),
fx.Invoke(registerRouter),
)
2 changes: 0 additions & 2 deletions pkg/apiserver/utils/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ import (
var ErrNS = errorx.NewNamespace("error.api")

var ErrExpNotEnabled = ErrNS.NewType("experimental_feature_not_enabled")

var ErrFeatureNotSupported = ErrNS.NewType("feature_not_supported")
22 changes: 0 additions & 22 deletions pkg/apiserver/utils/mw_feature_support.go

This file was deleted.

37 changes: 0 additions & 37 deletions pkg/apiserver/utils/semver_check.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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice improvement.

}
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/dig v1.8.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
go.uber.org/fx v1.10.0/go.mod h1:vLRicqpG/qQEzno4SYU86iCwfT95EZza+Eba0ItuxqY=
go.uber.org/dig v1.9.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
go.uber.org/fx v1.12.0/go.mod h1:egT3Kyg1JFYQkvKLZ3EsykxkNrZxgXS+gKoKo7abERY=
go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
Expand Down
Loading