This is a huge commit, it does following things:

1. refactor kubesphere dependency service client creation, we can
disable dependency by config
2. dependencies can be configured by configuration file
3. refactor cmd package using cobra.Command, so we can use hypersphere
to invoke command sepearately. Later we only need to build one image to
contains all kubesphere core components. One command to rule them all!
4. live reloading configuration currently not implemented
This commit is contained in:
Jeff
2019-09-03 15:20:22 +08:00
parent 52a1c2e619
commit 96d2ac4112
233 changed files with 26414 additions and 1927 deletions

View File

@@ -0,0 +1,57 @@
package redis
import (
"fmt"
"github.com/spf13/pflag"
"kubesphere.io/kubesphere/pkg/utils/net"
"kubesphere.io/kubesphere/pkg/utils/reflectutils"
)
type RedisOptions struct {
Host string
Port int
Password string
DB int
}
// NewRedisOptions returns options points to nowhere,
// because redis is not required for some components
func NewRedisOptions() *RedisOptions {
return &RedisOptions{
Host: "",
Port: 6379,
Password: "",
DB: 0,
}
}
func (r *RedisOptions) Validate() []error {
errors := make([]error, 0)
if r.Host != "" {
if !net.IsValidPort(r.Port) {
errors = append(errors, fmt.Errorf("--redis-port is out of range"))
}
}
return errors
}
func (r *RedisOptions) ApplyTo(options *RedisOptions) {
reflectutils.Override(options, r)
}
func (r *RedisOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&r.Host, "--redis-host", r.Host, ""+
"Redis service host address. If left blank, means redis is unnecessary, "+
"redis will be disabled")
fs.IntVar(&r.Port, "--redis-port", r.Port, ""+
"Redis service port number.")
fs.StringVar(&r.Password, "--redis-password", r.Password, ""+
"Redis service password if necessary, default to empty")
fs.IntVar(&r.DB, "--redis-db", r.DB, ""+
"Redis service database index, default to 0.")
}

View File

@@ -18,48 +18,49 @@
package redis
import (
"flag"
"log"
"os"
"os/signal"
"sync"
"syscall"
"fmt"
"github.com/go-redis/redis"
"k8s.io/klog"
)
var (
redisHost string
redisPassword string
redisDB int
redisClientOnce sync.Once
redisClient *redis.Client
)
func init() {
flag.StringVar(&redisHost, "redis-server", "localhost:6379", "redis server host")
flag.StringVar(&redisPassword, "redis-password", "", "redis password")
flag.IntVar(&redisDB, "redis-db", 0, "redis db")
type RedisClient struct {
client *redis.Client
}
func Client() *redis.Client {
func NewRedisClientOrDie(options *RedisOptions, stopCh <-chan struct{}) *RedisClient {
client, err := NewRedisClient(options, stopCh)
if err != nil {
panic(err)
}
redisClientOnce.Do(func() {
redisClient = redis.NewClient(&redis.Options{
Addr: redisHost,
Password: redisPassword,
DB: redisDB,
})
if err := redisClient.Ping().Err(); err != nil {
log.Fatalln(err)
}
c := make(chan os.Signal, 0)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
redisClient.Close()
}()
return client
}
func NewRedisClient(option *RedisOptions, stopCh <-chan struct{}) (*RedisClient, error) {
var r RedisClient
r.client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", option.Host, option.Port),
Password: option.Password,
DB: option.DB,
})
return redisClient
if err := r.client.Ping().Err(); err != nil {
klog.Error("unable to reach redis host", err)
r.client.Close()
return nil, err
}
go func() {
<-stopCh
if err := r.client.Close(); err != nil {
klog.Error(err)
}
}()
return &r, nil
}
func (r *RedisClient) Redis() *redis.Client {
return r.client
}