Skip to content

Commit

Permalink
feat(console): support render ui template (#2272)
Browse files Browse the repository at this point in the history
* feat(console): support render template with config

* feat(console): support custom config

* feat(console): 合并industry-cloud到master (#2273)

* feat(console): 新建vm新增blockMultiQueue和cache参数 (#2269)

* feat(console): 新建vm新增blockMultiQueue和cache参数

* feat(console): change writethrough to writeback

* feat(console): 添加自定义配置

* feat(console): 修改auth logo 配置

* fix:  alarm vm_cpu_usage_rate can over 100 (#2190)

* feat(console): 添加差异化配置

* feat(console): support virtual monitor and alarm (#1918)

* feat(console): add vm monitor panel

* fix(console): fix create notify template error

* feat(console): support create vm alarm

* feat(console): change create vm network mode to virtio (#1920)

* feat(console): change network bridge model to virtio

* feat(console): support query vm by vm name

* fix(console): fix useFetch polling can't clear

* fix(console): fix log tabel type not right

* feat(console): change vm monitor expr

* feat(console): rewirite vm monitor chart

* feat(console): fix

* feat(console): 重新生成package-lock

* feat(console): 修复依赖错误

* feat(console): 处理webtty html

* feat(console): 全部default viisble设置为true

* feat(console): support render template with config

---------

Co-authored-by: jo-hnny <wuzhiqiang94@hotmail.com>
  • Loading branch information
Leo Ryu and jo-hnny committed May 5, 2023
1 parent 4f58b96 commit 6bd8cd3
Show file tree
Hide file tree
Showing 94 changed files with 3,794 additions and 2,198 deletions.
14 changes: 14 additions & 0 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 @@ -96,6 +97,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 +202,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 +218,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 = apiserver.DefaultTitle
}
if len(consoleConfig.LogoDir) == 0 {
consoleConfig.LogoDir = apiserver.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
87 changes: 86 additions & 1 deletion pkg/auth/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
package apiserver

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"time"

"tkestack.io/tke/pkg/auth/authentication/oidc/identityprovider/cloudindustry"
Expand All @@ -33,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 All @@ -54,14 +59,23 @@ import (
authrest "tkestack.io/tke/pkg/auth/registry/rest"
"tkestack.io/tke/pkg/auth/route"
"tkestack.io/tke/pkg/util/log"

"html/template"
)

const (
OIDCPath = "/oidc/"
AuthPath = "/auth/"
APIKeyPasswordPath = "/apis/auth.tkestack.io/v1/apikeys/default/password"

APIKeyPath = "/apis/auth.tkestack.io/v1/apikeys"
APIKeyPath = "/apis/auth.tkestack.io/v1/apikeys"
DefaultTitle = "TKEStack"
DefaultLogoDir = "default"
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 @@ -94,6 +108,7 @@ type ExtraConfig struct {
Authorizer authorizer.Authorizer
CasbinReloadInterval time.Duration
PrivilegedUsername string
ConsoleConfig *ConsoleConfig
}

// Config contains the core configuration instance of apiserver and
Expand All @@ -119,6 +134,39 @@ type APIServer struct {
GenericAPIServer *genericapiserver.GenericAPIServer
}

type ConsoleConfig struct {
Title string
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 @@ -137,6 +185,43 @@ 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
}

sourceRe := regexp.MustCompile(`\.tmpl\.html$`)
targetRe := regexp.MustCompile(`\.tmpl`)

for _, file := range files {
var buf bytes.Buffer
if !sourceRe.MatchString(file.Name()) {
continue
}
t, err := template.New(file.Name()).Delims("{%", "%}").ParseFiles(HtmlTmplDir + file.Name())
if err != nil {
return nil, err
}
if err = t.Execute(&buf, c.ExtraConfig.ConsoleConfig); err != nil {
return nil, err
}
// // remove .tmpl in file name
targetFileName := targetRe.ReplaceAllString(file.Name(), "")
if err = ioutil.WriteFile(HtmlTmplDir+targetFileName, buf.Bytes(), 0644); err != nil {
return nil, err
}
}

dexHandler := identityprovider.DexHander{}

hooks := c.registerHooks(&dexHandler, s)
Expand Down
62 changes: 62 additions & 0 deletions pkg/auth/web/static/default/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
17 changes: 17 additions & 0 deletions pkg/auth/web/static/tkeanywhere/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions pkg/auth/web/static/tkeanywhere/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6bd8cd3

Please sign in to comment.