github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/types/config.go (about)

     1  package types
     2  
     3  import (
     4  	// #nosec
     5  	"crypto/sha256"
     6  	"encoding/json"
     7  	"fmt"
     8  	"time"
     9  )
    10  
    11  const (
    12  	// Etcd .
    13  	Etcd = "etcd"
    14  	// Redis .
    15  	Redis = "redis"
    16  )
    17  
    18  // Config holds eru-core config
    19  type Config struct {
    20  	Bind                string        `yaml:"bind" required:"true" default:"5001"`                 // HTTP API address
    21  	LockTimeout         time.Duration `yaml:"lock_timeout" required:"true" default:"30s"`          // timeout for lock (ttl)
    22  	GlobalTimeout       time.Duration `yaml:"global_timeout" required:"true" default:"300s"`       // timeout for remove, run_and_wait and build, in second
    23  	ConnectionTimeout   time.Duration `yaml:"connection_timeout" required:"true" default:"10s"`    // timeout for connections
    24  	HAKeepaliveInterval time.Duration `yaml:"ha_keepalive_interval" required:"true" default:"16s"` // interval for node status watcher
    25  	Statsd              string        `yaml:"statsd"`                                              // statsd host and port
    26  	Profile             string        `yaml:"profile"`                                             // profile ip:port
    27  	CertPath            string        `yaml:"cert_path"`                                           // docker cert files path
    28  	MaxConcurrency      int           `yaml:"max_concurrency" default:"100000"`                    // concurrently call single runtime in the same time
    29  	Store               string        `yaml:"store" default:"etcd"`                                // store type
    30  	SentryDSN           string        `yaml:"sentry_dsn"`                                          // sentry dsn
    31  	ProbeTarget         string        `yaml:"probe_target" required:"false" default:"8.8.8.8:80"`  // for getting outbound address
    32  
    33  	WALFile        string        `yaml:"wal_file" required:"true" default:"core.wal"`   // WAL file path
    34  	WALOpenTimeout time.Duration `yaml:"wal_open_timeout" required:"true" default:"8s"` // timeout for opening a WAL file
    35  
    36  	Auth           AuthConfig           `yaml:"auth"` // grpc auth
    37  	GRPCConfig     GRPCConfig           `yaml:"grpc"` // grpc config
    38  	Git            GitConfig            `yaml:"git"`
    39  	Etcd           EtcdConfig           `yaml:"etcd"`
    40  	Redis          RedisConfig          `yaml:"redis"`
    41  	Docker         DockerConfig         `yaml:"docker"`
    42  	Virt           VirtConfig           `yaml:"virt"`
    43  	Systemd        SystemdConfig        `yaml:"systemd"`
    44  	Scheduler      SchedulerConfig      `yaml:"scheduler"`
    45  	ResourcePlugin ResourcePluginConfig `yaml:"resource_plugin"`
    46  	Log            ServerLogConfig      `yaml:"log"`
    47  }
    48  
    49  // Identifier returns the ID of this config
    50  // we consider the same storage as the same config
    51  func (c Config) Identifier() (string, error) {
    52  	b, err := json.Marshal(c)
    53  	if err != nil {
    54  		return "", err
    55  	}
    56  	h := sha256.New()
    57  	h.Write(b)
    58  	return fmt.Sprintf("%x", h.Sum(nil)), nil
    59  }
    60  
    61  // AuthConfig contains authorization information for connecting to a Registry
    62  // Basically copied from https://github.com/moby/moby/blob/16a1736b9b93e44c898f95d670bbaf20a558103d/api/types/auth.go#L4
    63  // But use yaml instead of json
    64  // And we use it as grpc simple auth
    65  type AuthConfig struct {
    66  	Username string `yaml:"username,omitempty" json:"username,omitempty"`
    67  	Password string `yaml:"password,omitempty" json:"password,omitempty"`
    68  }
    69  
    70  // GRPCConfig indicate grpc config
    71  type GRPCConfig struct {
    72  	MaxConcurrentStreams         int           `yaml:"max_concurrent_streams,omitempty" json:"max_concurrent_streams,omitempty" required:"true" default:"100"`
    73  	MaxRecvMsgSize               int           `yaml:"max_recv_msg_size,omitempty" json:"max_recv_msg_size,omitempty" required:"true" default:"20971520"`
    74  	ServiceDiscoveryPushInterval time.Duration `yaml:"service_discovery_interval" required:"true" default:"15s"`
    75  	ServiceHeartbeatInterval     time.Duration `yaml:"service_heartbeat_interval" required:"true" default:"15s"`
    76  }
    77  
    78  // GitConfig holds eru-core git config
    79  type GitConfig struct {
    80  	SCMType      string        `yaml:"scm_type"`                     // source code manager type [gitlab/github]
    81  	PrivateKey   string        `yaml:"private_key"`                  // private key to clone code
    82  	Token        string        `yaml:"token"`                        // token to call SCM API
    83  	CloneTimeout time.Duration `yaml:"clone_timeout" default:"300s"` // clone timeout
    84  }
    85  
    86  // EtcdConfig holds eru-core etcd config
    87  type EtcdConfig struct {
    88  	Machines   []string   `yaml:"machines" required:"true"`                           // etcd cluster addresses
    89  	Prefix     string     `yaml:"prefix" required:"true" default:"/eru"`              // etcd lock prefix, all locks will be created under this dir
    90  	LockPrefix string     `yaml:"lock_prefix" required:"true" default:"__lock__/eru"` // etcd lock prefix, all locks will be created under this dir
    91  	Ca         string     `yaml:"ca"`                                                 // etcd ca
    92  	Key        string     `yaml:"key"`                                                // etcd key
    93  	Cert       string     `yaml:"cert"`                                               // etcd trusted_ca
    94  	Auth       AuthConfig `yaml:"auth"`                                               // etcd auth
    95  }
    96  
    97  // RedisConfig holds redis config
    98  // LockPrefix is used for lock
    99  type RedisConfig struct {
   100  	Addr       string `yaml:"addr" default:"localhost:6379"` // redis address
   101  	LockPrefix string `yaml:"lock_prefix" default:"/lock"`   // redis lock prefix
   102  	DB         int    `yaml:"db" default:"0"`                // redis db
   103  }
   104  
   105  // DockerConfig holds eru-core docker config
   106  type DockerConfig struct {
   107  	APIVersion  string    `yaml:"version" required:"true" default:"1.32"`      // docker API version
   108  	NetworkMode string    `yaml:"network_mode" required:"true" default:"host"` // docker network mode
   109  	UseLocalDNS bool      `yaml:"use_local_dns"`                               // use node IP as dns
   110  	Log         LogConfig `yaml:"log"`                                         // docker log driver
   111  
   112  	Hub         string                `yaml:"hub"`       // docker hub address
   113  	Namespace   string                `yaml:"namespace"` // docker hub prefix, will be set to $Hub/$HubPrefix/$appname
   114  	BuildPod    string                `yaml:"build_pod"` // podname used to build
   115  	AuthConfigs map[string]AuthConfig `yaml:"auths"`     // docker registry credentials
   116  }
   117  
   118  // VirtConfig holds yavirtd config
   119  type VirtConfig struct {
   120  	APIVersion string `yaml:"version" default:"v1"` // Yavirtd API version
   121  }
   122  
   123  // SystemdConfig is systemd config
   124  type SystemdConfig struct {
   125  	Runtime string `yaml:"runtime" default:"io.containerd.eru.v2"`
   126  }
   127  
   128  // SchedulerConfig holds scheduler config
   129  type SchedulerConfig struct {
   130  	MaxShare       int `yaml:"maxshare" required:"true" default:"-1"`   // comlpex scheduler use maxshare
   131  	ShareBase      int `yaml:"sharebase" required:"true" default:"100"` // how many pieces for one core
   132  	MaxDeployCount int `yaml:"max_deploy_count" default:"10000"`        // max deploy count of each node
   133  }
   134  
   135  // ResourcePluginConfig define Plugin config
   136  type ResourcePluginConfig struct {
   137  	Dir         string        `yaml:"dir" default:""`             // resource plugins path
   138  	CallTimeout time.Duration `yaml:"call_timeout" default:"30s"` // timeout for calling resource plugins
   139  	Whitelist   []string      `yaml:"whitelist"`                  // plugin whitelist
   140  }
   141  
   142  // LogConfig define log type
   143  type LogConfig struct {
   144  	Type   string            `yaml:"type" required:"true" default:"journald"` // Log type, can be "journald", "json-file", "none"
   145  	Config map[string]string `yaml:"config"`                                  // Log configs
   146  }
   147  
   148  type ServerLogConfig struct {
   149  	Level   string `yaml:"level" default:"info"`
   150  	UseJSON bool   `yaml:"use_json"`
   151  	// for file log
   152  	Filename   string `yaml:"filename"`
   153  	MaxSize    int    `yaml:"maxsize" default:"500"`
   154  	MaxAge     int    `yaml:"max_age" default:"28"`
   155  	MaxBackups int    `yaml:"max_backups" default:"3"`
   156  }