Skip to content

Commit

Permalink
feat(console): support custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
leonarliu committed Apr 26, 2023
1 parent 52803dc commit 7750874
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 17 deletions.
20 changes: 18 additions & 2 deletions cmd/tke-auth-api/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"regexp"
"strings"
"time"

"tkestack.io/tke/pkg/auth/authentication/oidc/identityprovider/cloudindustry"

"github.com/casbin/casbin/v2"
Expand Down Expand Up @@ -76,8 +77,10 @@ import (
)

const (
license = "Apache 2.0"
title = "Tencent Kubernetes Engine Auth API"
license = "Apache 2.0"
title = "Tencent Kubernetes Engine Auth API"
defaultTitle = "TKEStack"
defaultLogoDir = "default"
)

// Config is the running configuration structure of the TKE controller manager.
Expand All @@ -96,6 +99,7 @@ type Config struct {
Authorizer authorizer.Authorizer
CasbinReloadInterval time.Duration
PrivilegedUsername string
ConsoleConfig *apiserver.ConsoleConfig
}

// CreateConfigFromOptions creates a running configuration instance based
Expand Down Expand Up @@ -200,6 +204,8 @@ func CreateConfigFromOptions(serverName string, opts *options.Options) (*Config,
return nil, err
}

setupDefaultConsoleConfig(opts.ConsoleConfig)

return &Config{
ServerName: serverName,
OIDCExternalAddress: dexConfig.Issuer,
Expand All @@ -214,9 +220,19 @@ func CreateConfigFromOptions(serverName string, opts *options.Options) (*Config,
Authorizer: aggregateAuthz,
PrivilegedUsername: opts.Authentication.PrivilegedUsername,
CasbinReloadInterval: opts.Authorization.CasbinReloadInterval,
ConsoleConfig: opts.ConsoleConfig,
}, nil
}

func setupDefaultConsoleConfig(consoleConfig *apiserver.ConsoleConfig) {
if len(consoleConfig.Title) == 0 {
consoleConfig.Title = defaultTitle
}
if len(consoleConfig.LogoDir) == 0 {
consoleConfig.LogoDir = defaultLogoDir
}
}

func setupAuthentication(genericAPIServerConfig *genericapiserver.Config, opts *apiserveroptions.AuthenticationWithAPIOptions, tokenAuthenticators []genericauthenticator.Token) error {
if err := authentication.SetupAuthentication(genericAPIServerConfig, opts); err != nil {
return nil
Expand Down
5 changes: 5 additions & 0 deletions cmd/tke-auth-api/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
genericapiserveroptions "k8s.io/apiserver/pkg/server/options"
apiserveroptions "tkestack.io/tke/pkg/apiserver/options"
storageoptions "tkestack.io/tke/pkg/apiserver/storage/options"
"tkestack.io/tke/pkg/auth/apiserver"
"tkestack.io/tke/pkg/util/cachesize"
"tkestack.io/tke/pkg/util/log"
)
Expand All @@ -40,6 +41,7 @@ type Options struct {
ETCD *storageoptions.ETCDStorageOptions
Auth *AuthOptions
Audit *genericapiserveroptions.AuditOptions
ConsoleConfig *apiserver.ConsoleConfig
}

// NewOptions creates a new Options with a default config.
Expand All @@ -54,6 +56,7 @@ func NewOptions(serverName string) *Options {
ETCD: storageoptions.NewETCDStorageOptions("/tke/auth-api"),
Auth: NewAuthOptions(),
Audit: genericapiserveroptions.NewAuditOptions(),
ConsoleConfig: apiserver.NewConsoleConfigOptions(),
}
}

Expand All @@ -68,6 +71,7 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
o.Authorization.AddFlags(fs)
o.Auth.AddFlags(fs)
o.Audit.AddFlags(fs)
o.ConsoleConfig.AddFlags(fs)
}

// ApplyFlags parsing parameters from the command line or configuration file
Expand All @@ -83,6 +87,7 @@ func (o *Options) ApplyFlags() []error {
errs = append(errs, o.Authentication.ApplyFlags()...)
errs = append(errs, o.Authorization.ApplyFlags()...)
errs = append(errs, o.Auth.ApplyFlags()...)
errs = append(errs, o.ConsoleConfig.ApplyFlags()...)

return errs
}
Expand Down
1 change: 1 addition & 0 deletions cmd/tke-auth-api/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func createAPIServerConfig(cfg *config.Config) *apiserver.Config {
Authorizer: cfg.Authorizer,
CasbinReloadInterval: cfg.CasbinReloadInterval,
PrivilegedUsername: cfg.PrivilegedUsername,
ConsoleConfig: cfg.ConsoleConfig,
},
}
}
Expand Down
50 changes: 37 additions & 13 deletions pkg/auth/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (

dexstorage "github.com/dexidp/dex/storage"
"github.com/emicklei/go-restful"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"k8s.io/apiserver/pkg/server/mux"

"github.com/casbin/casbin/v2"
Expand Down Expand Up @@ -66,10 +68,12 @@ const (
AuthPath = "/auth/"
APIKeyPasswordPath = "/apis/auth.tkestack.io/v1/apikeys/default/password"

APIKeyPath = "/apis/auth.tkestack.io/v1/apikeys"
defaultTitle = "TKEStack"
defaultLogoDir = ""
htmlTmplDir = "web/auth/templates/"
APIKeyPath = "/apis/auth.tkestack.io/v1/apikeys"
htmlTmplDir = "web/auth/templates/"
flagConsoleTitle = "title"
flagConsoleLogoDir = "logo-dir"
configConsoleTitle = "console_config.title"
configConsoleLogoDir = "console_config.logo_dir"
)

func IgnoreAuthPathPrefixes() []string {
Expand Down Expand Up @@ -133,6 +137,34 @@ type ConsoleConfig struct {
LogoDir string
}

// NewAuthOptions creates a AuthOptions object with default parameters.
func NewConsoleConfigOptions() *ConsoleConfig {
return &ConsoleConfig{}
}

// AddFlags adds flags for console to the specified FlagSet object.
func (o *ConsoleConfig) AddFlags(fs *pflag.FlagSet) {
fs.String(flagConsoleTitle, o.Title,
"Custom console title.")
_ = viper.BindPFlag(configConsoleTitle, fs.Lookup(flagConsoleTitle))

fs.String(flagConsoleLogoDir, o.LogoDir,
"Custom console logo dir.")
_ = viper.BindPFlag(configConsoleLogoDir, fs.Lookup(flagConsoleLogoDir))

}

// ApplyFlags parsing parameters from the command line or configuration file
// to the options instance.
func (o *ConsoleConfig) ApplyFlags() []error {
var errs []error

o.Title = viper.GetString(configConsoleTitle)
o.LogoDir = viper.GetString(configConsoleLogoDir)

return errs
}

// Complete fills in any fields not set that are required to have valid data.
// It's mutating the receiver.
func (cfg *Config) Complete() CompletedConfig {
Expand All @@ -151,14 +183,6 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
return nil, err
}

consoleConfig := new(ConsoleConfig)
if c.ExtraConfig.ConsoleConfig != nil {
consoleConfig = c.ExtraConfig.ConsoleConfig
} else {
consoleConfig.Title = defaultTitle
consoleConfig.LogoDir = defaultLogoDir
}

files, err := ioutil.ReadDir(htmlTmplDir)
if err != nil {
return nil, err
Expand All @@ -170,7 +194,7 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
if err != nil {
return nil, err
}
if err = t.Execute(&buf, consoleConfig); err != nil {
if err = t.Execute(&buf, c.ExtraConfig.ConsoleConfig); err != nil {
return nil, err
}
// // remove .tmpl in file name
Expand Down
36 changes: 36 additions & 0 deletions pkg/gateway/apis/config/v1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions pkg/gateway/apis/config/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions pkg/gateway/apis/config/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pkg/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ import (
"tkestack.io/tke/pkg/gateway/websocket"
"tkestack.io/tke/pkg/gateway/webtty"

// "tkestack.io/tke/pkg/util/template"
"html/template"
)

const (
defaultTitle = "TKEStack"
defaultLogDir = ""
defaultLogDir = "default"
)

// ExtraConfig contains the additional configuration of apiserver.
Expand Down

0 comments on commit 7750874

Please sign in to comment.