From 0d9357594cfedaa32aaf8ef39877e9585b900242 Mon Sep 17 00:00:00 2001 From: CMGS Date: Wed, 6 Jul 2022 11:13:56 +0800 Subject: [PATCH] rewrite the config sample --- cluster/calcium/calcium_test.go | 2 +- core.yaml.sample | 86 ++++++++------ resources/cpumem/models/cpumem_test.go | 4 +- resources/manager.go | 2 +- resources/volume/models/volume_test.go | 2 +- types/config.go | 156 ++++++++++++------------- types/config_test.go | 4 +- 7 files changed, 137 insertions(+), 119 deletions(-) diff --git a/cluster/calcium/calcium_test.go b/cluster/calcium/calcium_test.go index c25ee4817..16cb279d5 100644 --- a/cluster/calcium/calcium_test.go +++ b/cluster/calcium/calcium_test.go @@ -32,7 +32,7 @@ func NewTestCluster() *Calcium { Git: types.GitConfig{ CloneTimeout: 300 * time.Second, }, - Scheduler: types.SchedConfig{ + Scheduler: types.SchedulerConfig{ MaxShare: -1, ShareBase: 100, }, diff --git a/core.yaml.sample b/core.yaml.sample index 6bbbeb241..1f0329859 100644 --- a/core.yaml.sample +++ b/core.yaml.sample @@ -1,31 +1,43 @@ -log_level: "DEBUG" -bind: ":5001" -statsd: "127.0.0.1:8125" -profile: ":12346" -global_timeout: 300s -connection_timeout: 10s -lock_timeout: 30s -cert_path: "/etc/eru/tls" -sentry_dsn: "https://examplePublicKey@o0.ingest.sentry.io/0" -max_concurrency: 20 - -auth: +log_level: "DEBUG" # required, default INFO +bind: ":5001" # required, default 5001 +lock_timeout: 30s # required, default 30s +global_timeout: 300s # required, default 300s +connection_timeout: 10s # required, default 10s +ha_keepalive_interval: 16s # required, default 16s +statsd: "127.0.0.1:8125" # optional +profile: ":12346" # optional +cert_path: "/etc/eru/tls" # optional, if you need connect to daemon without https +max_concurrency: 20 # optional, default 20 for pool size +store: etcd # optional, default etcd can choose redis +sentry_dsn: "https://examplePublicKey@o0.ingest.sentry.io/0" # optional + +wal_file: core.wal # required, default core.wal +wal_open_timeout: 8s # required, default 8s + +resource_plugins_dir: /etc/eru/plugins # optional, default /etc/eru/plugins +resource_plugins_timeout: 30s # optional, if need to use plugin, set it + +auth: # optional username: admin password: password -grpc: - max_concurrent_streams: 100 - max_recv_msg_size: 30 # will covert to MBytes - service_discovery_interval: 5s # WatchServiceStatus push interval - service_heartbeat_interval: 5s # RegisterService heartbeat +grpc: # required + max_concurrent_streams: 100 # default 100 + max_recv_msg_size: 20971520 # in MBytes # default 20971520 + service_discovery_interval: 5s # WatchServiceStatus push interval # default 15s + service_heartbeat_interval: 5s # RegisterService heartbeat # default 15s -store: etcd +git: # optional + scm_type: "github" + private_key: "***REMOVED***" # file path + token: "***REMOVED***" + clone_timeout: 300s etcd: - machines: + machines: # required - "http://127.0.0.1:2379" - prefix: "/core" - lock_prefix: "core/_lock" + prefix: "/eru" # required, default /eru + lock_prefix: "__lock__/eru" # required, default __lock__/eru ca: PATH_TO_CA key: PATH_TO_KEY cert: PATH_TO_CERT @@ -33,26 +45,34 @@ etcd: username: root password: root -git: - private_key: "***REMOVED***" - token: "***REMOVED***" - scm_type: "github" - clone_timeout: 300s +redis: # optional + addr: localhost:6379 + lock_prefix: "/lock" # default /lock + db: 0 docker: + version: "1.32" # required, default 1.32 + network_mode: "bridge" # required, default host + use_local_dns: true log: - type: "json-file" + type: "json-file" # required, default journald config: "max-size": "10m" - network_mode: "bridge" + hub: "hub.docker.com" namespace: "projecteru2" build_pod: "eru-test" - local_dns: true - -scheduler: - maxshare: -1 - sharebase: 100 + auth: + username: root + password: root virt: version: "v1" + +systemd: + runtime: "io.containerd.eru.v2" + +scheduler: + maxshare: -1 # required default -1 + sharebase: 100 # required default 100 + max_deploy_count: 10000 \ No newline at end of file diff --git a/resources/cpumem/models/cpumem_test.go b/resources/cpumem/models/cpumem_test.go index 03ec36746..aca6b84a0 100644 --- a/resources/cpumem/models/cpumem_test.go +++ b/resources/cpumem/models/cpumem_test.go @@ -19,7 +19,7 @@ func newTestCPUMem(t *testing.T) *CPUMem { Etcd: coretypes.EtcdConfig{ Prefix: "/cpumem", }, - Scheduler: coretypes.SchedConfig{ + Scheduler: coretypes.SchedulerConfig{ MaxShare: -1, ShareBase: 100, }, @@ -73,7 +73,7 @@ func TestNewCPUMem(t *testing.T) { Etcd: coretypes.EtcdConfig{ Machines: []string{"invalid-address"}, }, - Scheduler: coretypes.SchedConfig{ + Scheduler: coretypes.SchedulerConfig{ MaxShare: -1, ShareBase: 100, }, diff --git a/resources/manager.go b/resources/manager.go index 884247c0e..89536c71a 100644 --- a/resources/manager.go +++ b/resources/manager.go @@ -34,7 +34,7 @@ func NewPluginManager(config types.Config) (*PluginManager, error) { // LoadPlugins . func (pm *PluginManager) LoadPlugins(ctx context.Context) error { - if len(pm.config.ResourcePluginsDir) > 0 { + if len(pm.config.ResourcePluginsDir) > 0 { // it's not a slice ! pluginFiles, err := utils.ListAllExecutableFiles(pm.config.ResourcePluginsDir) if err != nil { log.Errorf(ctx, "[LoadPlugins] failed to list all executable files dir: %v, err: %v", pm.config.ResourcePluginsDir, err) diff --git a/resources/volume/models/volume_test.go b/resources/volume/models/volume_test.go index 63f33407c..ccdeeb519 100644 --- a/resources/volume/models/volume_test.go +++ b/resources/volume/models/volume_test.go @@ -18,7 +18,7 @@ func newTestVolume(t *testing.T) *Volume { Etcd: coretypes.EtcdConfig{ Prefix: "/Volume", }, - Scheduler: coretypes.SchedConfig{ + Scheduler: coretypes.SchedulerConfig{ MaxShare: -1, ShareBase: 100, MaxDeployCount: 1000, diff --git a/types/config.go b/types/config.go index bab2265c1..7a965a9c1 100644 --- a/types/config.go +++ b/types/config.go @@ -1,10 +1,10 @@ package types import ( - "crypto/sha1" // #nosec + // #nosec + "crypto/sha256" + "encoding/json" "fmt" - "strconv" - "strings" "time" ) @@ -18,50 +18,70 @@ const ( // Config holds eru-core config type Config struct { LogLevel string `yaml:"log_level" required:"true" default:"INFO"` - Bind string `yaml:"bind" required:"true" default:"5001"` // HTTP API address - LockTimeout time.Duration `yaml:"lock_timeout" required:"true" default:"30s"` // timeout for lock (ttl) - GlobalTimeout time.Duration `yaml:"global_timeout" required:"true" default:"300s"` // timeout for remove, run_and_wait and build, in second - ConnectionTimeout time.Duration `yaml:"connection_timeout" default:"10s"` // timeout for connections - HAKeepaliveInterval time.Duration `yaml:"ha_keepalive_interval" default:"16s"` // interval for node status watcher - Statsd string `yaml:"statsd"` // statsd host and port - Profile string `yaml:"profile"` // profile ip:port - CertPath string `yaml:"cert_path"` // docker cert files path - MaxConcurrency int64 `yaml:"max_concurrency" default:"20"` // concurrently call single runtime in the same time - Store string `yaml:"store" default:"etcd"` // store type - - ResourcePluginsDir string `yaml:"resource_plugins_dir" default:"/etc/eru/resource_plugins"` // resource plugins path - ResourcePluginsTimeout time.Duration `yaml:"resource_plugins_timeout" default:"30s"` // timeout for calling resource plugins - - Auth AuthConfig `yaml:"auth"` // grpc auth - GRPCConfig GRPCConfig `yaml:"grpc"` // grpc config + Bind string `yaml:"bind" required:"true" default:"5001"` // HTTP API address + LockTimeout time.Duration `yaml:"lock_timeout" required:"true" default:"30s"` // timeout for lock (ttl) + GlobalTimeout time.Duration `yaml:"global_timeout" required:"true" default:"300s"` // timeout for remove, run_and_wait and build, in second + ConnectionTimeout time.Duration `yaml:"connection_timeout" required:"true" default:"10s"` // timeout for connections + HAKeepaliveInterval time.Duration `yaml:"ha_keepalive_interval" required:"true" default:"16s"` // interval for node status watcher + Statsd string `yaml:"statsd"` // statsd host and port + Profile string `yaml:"profile"` // profile ip:port + CertPath string `yaml:"cert_path"` // docker cert files path + MaxConcurrency int64 `yaml:"max_concurrency" default:"20"` // concurrently call single runtime in the same time + Store string `yaml:"store" default:"etcd"` // store type + SentryDSN string `yaml:"sentry_dsn"` WALFile string `yaml:"wal_file" required:"true" default:"core.wal"` // WAL file path WALOpenTimeout time.Duration `yaml:"wal_open_timeout" required:"true" default:"8s"` // timeout for opening a WAL file - Git GitConfig `yaml:"git"` - Etcd EtcdConfig `yaml:"etcd"` - Redis RedisConfig `yaml:"redis"` - Docker DockerConfig `yaml:"docker"` - Scheduler SchedConfig `yaml:"scheduler"` - Virt VirtConfig `yaml:"virt"` - Systemd SystemdConfig `yaml:"systemd"` - SentryDSN string `yaml:"sentry_dsn"` + ResourcePluginsDir string `yaml:"resource_plugins_dir" default:"/etc/eru/plugins"` // resource plugins path + ResourcePluginsTimeout time.Duration `yaml:"resource_plugins_timeout" default:"30s"` // timeout for calling resource plugins + + Auth AuthConfig `yaml:"auth"` // grpc auth + GRPCConfig GRPCConfig `yaml:"grpc"` // grpc config + Git GitConfig `yaml:"git"` + Etcd EtcdConfig `yaml:"etcd"` + Redis RedisConfig `yaml:"redis"` + Docker DockerConfig `yaml:"docker"` + Virt VirtConfig `yaml:"virt"` + Systemd SystemdConfig `yaml:"systemd"` + Scheduler SchedulerConfig `yaml:"scheduler"` } // Identifier returns the id of this config // we consider the same storage as the same config -func (c Config) Identifier() string { - s := strings.Builder{} - _, _ = s.WriteString(c.Store) - for _, e := range c.Etcd.Machines { - _, _ = s.WriteString(e) +func (c Config) Identifier() (string, error) { + b, err := json.Marshal(c) + if err != nil { + return "", err } - _, _ = s.WriteString(c.Etcd.Prefix) - _, _ = s.WriteString(c.Redis.Addr) - _, _ = s.WriteString(strconv.Itoa(c.Redis.DB)) - h := sha1.New() // #nosec - _, _ = h.Write([]byte(s.String())) - return fmt.Sprintf("%x", h.Sum(nil)) + h := sha256.New() + h.Write(b) + return fmt.Sprintf("%x", h.Sum(nil)), nil +} + +// AuthConfig contains authorization information for connecting to a Registry +// Basically copied from https://github.com/moby/moby/blob/16a1736b9b93e44c898f95d670bbaf20a558103d/api/types/auth.go#L4 +// But use yaml instead of json +// And we use it as grpc simple auth +type AuthConfig struct { + Username string `yaml:"username,omitempty" json:"username,omitempty"` + Password string `yaml:"password,omitempty" json:"password,omitempty"` +} + +// GRPCConfig indicate grpc config +type GRPCConfig struct { + MaxConcurrentStreams int `yaml:"max_concurrent_streams,omitempty" json:"max_concurrent_streams,omitempty" required:"true" default:"100"` + MaxRecvMsgSize int `yaml:"max_recv_msg_size,omitempty" json:"max_recv_msg_size,omitempty" required:"true" default:"20971520"` + ServiceDiscoveryPushInterval time.Duration `yaml:"service_discovery_interval" required:"true" default:"15s"` + ServiceHeartbeatInterval time.Duration `yaml:"service_heartbeat_interval" required:"true" default:"15s"` +} + +// GitConfig holds eru-core git config +type GitConfig struct { + SCMType string `yaml:"scm_type"` // source code manager type [gitlab/github] + PrivateKey string `yaml:"private_key"` // private key to clone code + Token string `yaml:"token"` // token to call SCM API + CloneTimeout time.Duration `yaml:"clone_timeout" default:"300s"` // clone timeout } // EtcdConfig holds eru-core etcd config @@ -79,33 +99,26 @@ type EtcdConfig struct { // LockPrefix is used for lock type RedisConfig struct { Addr string `yaml:"addr" default:"localhost:6379"` // redis address - DB int `yaml:"db" default:"0"` // redis db LockPrefix string `yaml:"lock_prefix" default:"/lock"` // redis lock prefix -} - -// GitConfig holds eru-core git config -type GitConfig struct { - SCMType string `yaml:"scm_type"` // source code manager type [gitlab/github] - PrivateKey string `yaml:"private_key"` // private key to clone code - Token string `yaml:"token"` // token to call SCM API - CloneTimeout time.Duration `yaml:"clone_timeout" required:"true" default:"300s"` // clone timeout + DB int `yaml:"db" default:"0"` // redis db } // DockerConfig holds eru-core docker config type DockerConfig struct { - APIVersion string `yaml:"version" required:"true" default:"1.32"` // docker API version - NetworkMode string `yaml:"network_mode" required:"true" default:"host"` // docker network mode - Hub string `yaml:"hub"` // docker hub address - Namespace string `yaml:"namespace"` // docker hub prefix, will be set to $Hub/$HubPrefix/$appname - BuildPod string `yaml:"build_pod"` // podname used to build - UseLocalDNS bool `yaml:"local_dns"` // use node IP as dns - Log LogConfig `yaml:"log"` // docker log driver - AuthConfigs map[string]AuthConfig `yaml:"auths"` // docker registry credentials + APIVersion string `yaml:"version" required:"true" default:"1.32"` // docker API version + NetworkMode string `yaml:"network_mode" required:"true" default:"host"` // docker network mode + UseLocalDNS bool `yaml:"use_local_dns"` // use node IP as dns + Log LogConfig `yaml:"log"` // docker log driver + + Hub string `yaml:"hub"` // docker hub address + Namespace string `yaml:"namespace"` // docker hub prefix, will be set to $Hub/$HubPrefix/$appname + BuildPod string `yaml:"build_pod"` // podname used to build + AuthConfigs map[string]AuthConfig `yaml:"auths"` // docker registry credentials } // VirtConfig holds yavirtd config type VirtConfig struct { - APIVersion string `yaml:"version"` // Yavirtd API version + APIVersion string `yaml:"version" default:"v1"` // Yavirtd API version } // SystemdConfig is systemd config @@ -113,32 +126,15 @@ type SystemdConfig struct { Runtime string `yaml:"runtime" default:"io.containerd.eru.v2"` } +// SchedulerConfig holds scheduler config +type SchedulerConfig struct { + MaxShare int `yaml:"maxshare" required:"true" default:"-1"` // comlpex scheduler use maxshare + ShareBase int `yaml:"sharebase" required:"true" default:"100"` // how many pieces for one core + MaxDeployCount int `yaml:"max_deploy_count" default:"10000"` // max deploy count of each node +} + // LogConfig define log type type LogConfig struct { Type string `yaml:"type" required:"true" default:"journald"` // Log type, can be "journald", "json-file", "none" Config map[string]string `yaml:"config"` // Log configs } - -// SchedConfig holds scheduler config -type SchedConfig struct { - MaxShare int `yaml:"maxshare" required:"true" default:"-1"` // comlpex scheduler use maxshare - ShareBase int `yaml:"sharebase" required:"true" default:"100"` // how many pieces for one core - MaxDeployCount int `yaml:"max_deploy_count" required:"false" default:"10000"` // max deploy count of each node -} - -// AuthConfig contains authorization information for connecting to a Registry -// Basically copied from https://github.com/moby/moby/blob/16a1736b9b93e44c898f95d670bbaf20a558103d/api/types/auth.go#L4 -// But use yaml instead of json -// And we use it as grpc simple auth -type AuthConfig struct { - Username string `yaml:"username,omitempty" json:"username,omitempty"` - Password string `yaml:"password,omitempty" json:"password,omitempty"` -} - -// GRPCConfig indicate grpc config -type GRPCConfig struct { - MaxConcurrentStreams int `yaml:"max_concurrent_streams,omitempty" json:"max_concurrent_streams,omitempty" required:"true" default:"100"` - MaxRecvMsgSize int `yaml:"max_recv_msg_size,omitempty" json:"max_recv_msg_size,omitempty" required:"true" default:"20971520"` - ServiceDiscoveryPushInterval time.Duration `yaml:"service_discovery_interval" required:"true" default:"15s"` - ServiceHeartbeatInterval time.Duration `yaml:"service_heartbeat_interval" required:"true" default:"15s"` -} diff --git a/types/config_test.go b/types/config_test.go index b0e9a132f..cd9903c1e 100644 --- a/types/config_test.go +++ b/types/config_test.go @@ -14,5 +14,7 @@ func TestIdentifier(t *testing.T) { "2.2.2.2", }, } - assert.NotEmpty(t, config.Identifier()) + r, err := config.Identifier() + assert.NoError(t, err) + assert.NotEmpty(t, r) }