Skip to content

Commit

Permalink
refactor(*): feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
shhdgit committed Nov 11, 2021
1 parent 08b9edc commit 1794cbe
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 66 deletions.
8 changes: 7 additions & 1 deletion pkg/apiserver/conprof/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@

package conprof

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

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

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

var Module = fx.Options(
fx.Provide(newService),
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/conprof/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func newService(lc fx.Lifecycle, p ServiceParams) *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(utils.MWForbidByFeatureSupport(FeatureFlagConprof.IsSupport(s.params.Config.FeatureVersion)))
{
endpoint.GET("/config", auth.MWAuthRequired(), s.reverseProxy("/config"), s.conprofConfig)
endpoint.POST("/config", auth.MWAuthRequired(), auth.MWRequireWritePriv(), s.reverseProxy("/config"), s.updateConprofConfig)
Expand Down
26 changes: 15 additions & 11 deletions pkg/apiserver/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (
"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"
apiutils "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"
"github.com/pingcap/tidb-dashboard/pkg/utils/version"
)

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

endpoint.Use(utils.MWConnectTiDB(s.params.TiDBClient))
endpoint.Use(apiutils.MWConnectTiDB(s.params.TiDBClient))
endpoint.GET("/databases", s.databasesHandler)
endpoint.GET("/tables", s.tablesHandler)
}
Expand All @@ -72,12 +72,16 @@ type InfoResponse struct { //nolint
// @Security JwtAuth
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) infoHandler(c *gin.Context) {
supportedFeatures := []string{}
if conprof.IsFeatureSupport(s.params.Config) {
supportedFeatures = append(supportedFeatures, "conprof")
featureFlags := []*utils.FeatureFlag{
conprof.FeatureFlagConprof,
user.FeatureFlagNonRootLogin,
}
if nonrootlogin.IsFeatureSupport(s.params.Config) {
supportedFeatures = append(supportedFeatures, "nonRootLogin")
supportedFeatures := []string{}
for _, ff := range featureFlags {
if !ff.IsSupport(s.params.Config.FeatureVersion) {
continue
}
supportedFeatures = append(supportedFeatures, ff.Name)
}

resp := InfoResponse{
Expand All @@ -102,7 +106,7 @@ type WhoAmIResponse struct {
// @Security JwtAuth
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) whoamiHandler(c *gin.Context) {
sessionUser := utils.GetSession(c)
sessionUser := apiutils.GetSession(c)
resp := WhoAmIResponse{
DisplayName: sessionUser.DisplayName,
IsShareable: sessionUser.IsShareable,
Expand All @@ -122,7 +126,7 @@ func (s *Service) databasesHandler(c *gin.Context) {
Databases string `gorm:"column:Database"`
}
var result []databaseSchemas
db := utils.GetTiDBConnection(c)
db := apiutils.GetTiDBConnection(c)
err := db.Raw("SHOW DATABASES").Scan(&result).Error
if err != nil {
_ = c.Error(err)
Expand Down Expand Up @@ -150,7 +154,7 @@ type tableSchema struct {
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) tablesHandler(c *gin.Context) {
var result []tableSchema
db := utils.GetTiDBConnection(c)
db := apiutils.GetTiDBConnection(c)
tx := db.Select([]string{"TABLE_NAME", "TIDB_TABLE_ID"}).Table("INFORMATION_SCHEMA.TABLES")
databaseName := c.Query("database_name")

Expand Down
34 changes: 0 additions & 34 deletions pkg/apiserver/nonrootlogin/feature_support.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package conprof
package user

import (
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/pkg/utils"
)

var (
supportedTiDBVersions = []string{">= 5.3.0"}
featureSupported *bool
)

func IsFeatureSupport(config *config.Config) (supported bool) {
if featureSupported != nil {
return *featureSupported
}

supported = utils.IsVersionSupport(config.FeatureVersion, supportedTiDBVersions)
featureSupported = &supported
return
}
var FeatureFlagNonRootLogin = utils.NewFeatureFlag("nonRootLogin", []string{">= 5.3.0"})
26 changes: 24 additions & 2 deletions pkg/apiserver/utils/semver_check.go → pkg/utils/feature_flag.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 PingCAP, Inc.
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -8,6 +8,7 @@
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Expand All @@ -21,10 +22,31 @@ import (
"github.com/pingcap/tidb-dashboard/pkg/utils/version"
)

type FeatureFlag struct {
Name string

supportedVersions []string
supported *bool
}

func NewFeatureFlag(name string, supportedVersions []string) *FeatureFlag {
return &FeatureFlag{Name: name, supportedVersions: supportedVersions}
}

func (ff *FeatureFlag) IsSupport(targetVersion string) bool {
if ff.supported != nil {
return *ff.supported
}

supported := isVersionSupport(targetVersion, ff.supportedVersions)
ff.supported = &supported
return supported
}

// IsVersionSupport checks if a semantic version fits within a set of constraints
// pdVersion, standaloneVersion examples: "v5.2.2", "v5.3.0", "v5.4.0-alpha-xxx", "5.3.0" (semver can handle `v` prefix by itself)
// constraints examples: "~5.2.2", ">= 5.3.0", see semver docs to get more information
func IsVersionSupport(standaloneVersion string, constraints []string) bool {
func isVersionSupport(standaloneVersion string, constraints []string) bool {
curVersion := standaloneVersion
if version.Standalone == "No" {
curVersion = version.PDVersion
Expand Down
63 changes: 63 additions & 0 deletions pkg/utils/feature_flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2021 Suhaha
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import (
"testing"

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

func Test_IsSupport(t *testing.T) {
ff := NewFeatureFlag("testFeature", []string{">= 5.3.0"})

require.Equal(t, false, ff.IsSupport("v5.2.2"))
// The results do not change because of caching. This is expected.
require.Equal(t, false, ff.IsSupport("v5.3.0"))

ff2 := NewFeatureFlag("testFeature", []string{">= 5.3.0"})
require.Equal(t, true, ff2.IsSupport("v5.3.0"))

ff3 := NewFeatureFlag("testFeature", []string{">= 5.3.0"})
require.Equal(t, true, ff3.IsSupport("v5.3.2"))
}

func Test_isVersionSupport(t *testing.T) {
type Args struct {
target string
supported []string
}
tests := []struct {
want bool
args Args
}{
{want: false, args: Args{target: "v4.2.0", supported: []string{">= 5.3.0"}}},
{want: false, args: Args{target: "v5.2.0", supported: []string{">= 5.3.0"}}},
{want: true, args: Args{target: "v5.3.0", supported: []string{">= 5.3.0"}}},
{want: false, args: Args{target: "v5.2.0-alpha-xxx", supported: []string{">= 5.3.0"}}},
{want: true, args: Args{target: "v5.3.0-alpha-xxx", supported: []string{">= 5.3.0"}}},
{want: true, args: Args{target: "v5.3.0", supported: []string{"= 5.3.0"}}},
{want: false, args: Args{target: "v5.3.1", supported: []string{"= 5.3.0"}}},
}

for _, tt := range tests {
isVersionSupport(tt.args.target, tt.args.supported)
}

version.Standalone = "No"
version.PDVersion = "v5.3.0"
require.Equal(t, true, isVersionSupport("v100.0.0", []string{"= 5.3.0"}))
}

0 comments on commit 1794cbe

Please sign in to comment.