Skip to content

Commit d4e0d50

Browse files
committed
feat: support finite state machine (fsm)
1 parent debeb3d commit d4e0d50

File tree

20 files changed

+285
-32
lines changed

20 files changed

+285
-32
lines changed

cmd/onex-nightwatch/app/options/options.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
package options
99

1010
import (
11+
"math"
12+
1113
"github.com/spf13/viper"
1214
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1315
"k8s.io/client-go/tools/clientcmd"
@@ -31,10 +33,12 @@ var _ app.CliOptions = (*Options)(nil)
3133

3234
// Options contains everything necessary to create and run a nightwatch server.
3335
type Options struct {
34-
HealthOptions *genericoptions.HealthOptions `json:"health" mapstructure:"health"`
35-
MySQLOptions *genericoptions.MySQLOptions `json:"mysql" mapstructure:"mysql"`
36-
RedisOptions *genericoptions.RedisOptions `json:"redis" mapstructure:"redis"`
37-
Metrics *genericoptions.MetricsOptions `json:"metrics" mapstructure:"metrics"`
36+
HealthOptions *genericoptions.HealthOptions `json:"health" mapstructure:"health"`
37+
MySQLOptions *genericoptions.MySQLOptions `json:"db" mapstructure:"db"`
38+
RedisOptions *genericoptions.RedisOptions `json:"redis" mapstructure:"redis"`
39+
UserWatcherMaxWorkers int64 `json:"user-watcher-max-workers" mapstructure:"user-watcher-max-workers"`
40+
DisableWatchers []string `json:"disable-watchers" mapstructure:"disable-watchers"`
41+
Metrics *genericoptions.MetricsOptions `json:"metrics" mapstructure:"metrics"`
3842
// Path to kubeconfig file with authorization and master location information.
3943
Kubeconfig string `json:"kubeconfig" mapstructure:"kubeconfig"`
4044
FeatureGates map[string]bool `json:"feature-gates"`
@@ -44,11 +48,13 @@ type Options struct {
4448
// NewOptions returns initialized Options.
4549
func NewOptions() *Options {
4650
o := &Options{
47-
HealthOptions: genericoptions.NewHealthOptions(),
48-
MySQLOptions: genericoptions.NewMySQLOptions(),
49-
RedisOptions: genericoptions.NewRedisOptions(),
50-
Metrics: genericoptions.NewMetricsOptions(),
51-
Log: log.NewOptions(),
51+
HealthOptions: genericoptions.NewHealthOptions(),
52+
MySQLOptions: genericoptions.NewMySQLOptions(),
53+
RedisOptions: genericoptions.NewRedisOptions(),
54+
UserWatcherMaxWorkers: math.MaxInt64,
55+
DisableWatchers: []string{},
56+
Metrics: genericoptions.NewMetricsOptions(),
57+
Log: log.NewOptions(),
5258
}
5359

5460
return o
@@ -57,7 +63,7 @@ func NewOptions() *Options {
5763
// Flags returns flags for a specific server by section name.
5864
func (o *Options) Flags() (fss cliflag.NamedFlagSets) {
5965
o.HealthOptions.AddFlags(fss.FlagSet("health"))
60-
o.MySQLOptions.AddFlags(fss.FlagSet("mysql"))
66+
o.MySQLOptions.AddFlags(fss.FlagSet("db"))
6167
o.RedisOptions.AddFlags(fss.FlagSet("redis"))
6268
o.Metrics.AddFlags(fss.FlagSet("metrics"))
6369
o.Log.AddFlags(fss.FlagSet("log"))
@@ -66,6 +72,8 @@ func (o *Options) Flags() (fss cliflag.NamedFlagSets) {
6672
// arrange these text blocks sensibly. Grrr.
6773
fs := fss.FlagSet("misc")
6874
fs.StringVar(&o.Kubeconfig, "kubeconfig", o.Kubeconfig, "Path to kubeconfig file with authorization and master location information.")
75+
fs.Int64Var(&o.UserWatcherMaxWorkers, "user-watcher-max-workers", o.UserWatcherMaxWorkers, "Specify the maximum concurrency event of user watcher.")
76+
fs.StringSliceVar(&o.DisableWatchers, "disable-watchers", o.DisableWatchers, "The list of watchers that should be disabled.")
6977
feature.DefaultMutableFeatureGate.AddFlag(fs)
7078

7179
return fss
@@ -77,6 +85,10 @@ func (o *Options) Complete() error {
7785
return err
7886
}
7987

88+
if o.UserWatcherMaxWorkers < 1 {
89+
o.UserWatcherMaxWorkers = math.MaxInt64
90+
}
91+
8092
_ = feature.DefaultMutableFeatureGate.SetFromMap(o.FeatureGates)
8193
return nil
8294
}
@@ -98,6 +110,8 @@ func (o *Options) Validate() error {
98110
func (o *Options) ApplyTo(c *nightwatch.Config) error {
99111
c.MySQLOptions = o.MySQLOptions
100112
c.RedisOptions = o.RedisOptions
113+
c.UserWatcherMaxWorkers = o.UserWatcherMaxWorkers
114+
c.DisableWatchers = o.DisableWatchers
101115
return nil
102116
}
103117

configs/onex.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ CREATE TABLE `uc_user` (
235235
`user_id` varchar(253) NOT NULL DEFAULT '' COMMENT '用户 ID',
236236
`username` varchar(253) NOT NULL DEFAULT '' COMMENT '用户名称',
237237
`status` tinyint(3) unsigned NOT NULL DEFAULT 1 COMMENT '用户状态,0-禁用;1-启用',
238+
`status` varchar(64) NOT NULL DEFAULT '' COMMENT '用户状态:registered,active,disabled,blacklisted,locked,deleted',
238239
`nickname` varchar(253) NOT NULL DEFAULT '' COMMENT '用户昵称',
239240
`password` varchar(64) NOT NULL DEFAULT '' COMMENT '用户加密后的密码',
240241
`email` varchar(253) NOT NULL DEFAULT '' COMMENT '用户电子邮箱',

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ require (
4646
github.com/jinzhu/copier v0.3.5
4747
github.com/kisielk/errcheck v1.5.0
4848
github.com/likexian/host-stat-go v0.0.0-20190516151207-c9cf36dd6ce9
49+
github.com/looplab/fsm v1.0.2
4950
github.com/mitchellh/go-wordwrap v1.0.1
5051
github.com/nicksnyder/go-i18n/v2 v2.2.1
5152
github.com/olekukonko/tablewriter v0.0.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,8 @@ github.com/likexian/host-stat-go v0.0.0-20190516151207-c9cf36dd6ce9/go.mod h1:Hn
12281228
github.com/likexian/simplejson-go v0.0.0-20190409170913-40473a74d76d/go.mod h1:Typ1BfnATYtZ/+/shXfFYLrovhFyuKvzwrdOnIDHlmg=
12291229
github.com/likexian/simplejson-go v0.0.0-20190419151922-c1f9f0b4f084/go.mod h1:U4O1vIJvIKwbMZKUJ62lppfdvkCdVd2nfMimHK81eec=
12301230
github.com/likexian/simplejson-go v0.0.0-20190502021454-d8787b4bfa0b/go.mod h1:3BWwtmKP9cXWwYCr5bkoVDEfLywacOv0s06OBEDpyt8=
1231+
github.com/looplab/fsm v1.0.2 h1:f0kdMzr4CRpXtaKKRUxwLYJ7PirTdwrtNumeLN+mDx8=
1232+
github.com/looplab/fsm v1.0.2/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=
12311233
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
12321234
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a h1:N9zuLhTvBSRt0gWSiJswwQ2HqDmtX/ZCDJURnKUt1Ik=
12331235
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a/go.mod h1:JKx41uQRwqlTZabZc+kILPrO/3jlKnQ2Z8b7YiVw5cE=

internal/controller/miner/controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/controller"
2727
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2828
"sigs.k8s.io/controller-runtime/pkg/handler"
29-
3029
"sigs.k8s.io/controller-runtime/pkg/source"
3130

3231
"github.com/superproj/onex/internal/controller/miner/apis/config"

internal/nightwatch/nightwatch.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
// trigger init functions in `internal/nightwatch/watcher/all`.
2626
_ "github.com/superproj/onex/internal/nightwatch/watcher/all"
2727
genericoptions "github.com/superproj/onex/pkg/options"
28+
stringsutil "github.com/superproj/onex/pkg/util/strings"
2829
)
2930

3031
var (
@@ -37,15 +38,20 @@ var (
3738
type nightWatch struct {
3839
runner *cron.Cron
3940
// distributed lock
40-
locker *redsync.Mutex
41-
config *watcher.Config
41+
locker *redsync.Mutex
42+
config *watcher.Config
43+
disableWatchers []string
4244
}
4345

4446
// Config is the configuration for the nightwatch server.
4547
type Config struct {
4648
MySQLOptions *genericoptions.MySQLOptions
4749
RedisOptions *genericoptions.RedisOptions
48-
Client clientset.Interface
50+
// The maximum concurrency event of user watcher.
51+
UserWatcherMaxWorkers int64
52+
// The list of watchers that should be disabled.
53+
DisableWatchers []string
54+
Client clientset.Interface
4955
}
5056

5157
// CompletedConfig same as Config, just to swap private object.
@@ -68,7 +74,11 @@ func (c *Config) CreateWatcherConfig() (*watcher.Config, error) {
6874
return nil, err
6975
}
7076

71-
return &watcher.Config{Store: storeClient, Client: c.Client}, nil
77+
return &watcher.Config{
78+
Store: storeClient,
79+
Client: c.Client,
80+
UserWatcherMaxWorkers: c.UserWatcherMaxWorkers,
81+
}, nil
7282
}
7383

7484
// New creates an asynchronous task instance.
@@ -104,7 +114,7 @@ func (c *Config) New() (*nightWatch, error) {
104114
return nil, err
105115
}
106116

107-
nw := &nightWatch{runner: runner, locker: locker, config: cfg}
117+
nw := &nightWatch{runner: runner, locker: locker, config: cfg, disableWatchers: c.DisableWatchers}
108118
if err := nw.addWatchers(); err != nil {
109119
return nil, err
110120
}
@@ -115,6 +125,10 @@ func (c *Config) New() (*nightWatch, error) {
115125
// addWatchers used to initialize all registered watchers and add the watchers as a Cron job.
116126
func (nw *nightWatch) addWatchers() error {
117127
for n, w := range watcher.ListWatchers() {
128+
if stringsutil.StringIn(n, nw.disableWatchers) {
129+
continue
130+
}
131+
118132
if err := w.Init(context.Background(), nw.config); err != nil {
119133
log.Errorw(err, "Failed to construct watcher", "watcher", n)
120134
return err

internal/nightwatch/watcher/all/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ package all
1010
import (
1111
_ "github.com/superproj/onex/internal/nightwatch/watcher/clean"
1212
_ "github.com/superproj/onex/internal/nightwatch/watcher/secretsclean"
13+
_ "github.com/superproj/onex/internal/nightwatch/watcher/user"
1314
)

internal/nightwatch/watcher/clean/watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ func (w *cleanWatcher) Init(ctx context.Context, config *watcher.Config) error {
4242
}
4343

4444
func init() {
45-
watcher.Register(&cleanWatcher{})
45+
watcher.Register("clean", &cleanWatcher{})
4646
}

internal/nightwatch/watcher/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ type Config struct {
2020

2121
// Client is the client for onex-apiserver.
2222
Client clientset.Interface
23+
24+
// Then maximum concurrency event of user watcher.
25+
UserWatcherMaxWorkers int64
2326
}

internal/nightwatch/watcher/secretsclean/watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ func (w *secretsCleanWatcher) Init(ctx context.Context, config *watcher.Config)
5050
}
5151

5252
func init() {
53-
watcher.Register(&secretsCleanWatcher{})
53+
watcher.Register("secretsclean", &secretsCleanWatcher{})
5454
}

0 commit comments

Comments
 (0)