feat: allow no node and add run file

This commit is contained in:
mzz2017 2023-02-27 15:10:15 +08:00
parent a9709d2a57
commit 1737d3f7b2

View File

@ -14,13 +14,25 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"syscall" "syscall"
) )
const (
PidFilePath = "/var/run/dae.pid"
)
func init() {
runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "disable timestamp")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-runfile", "", false, "not generate /var/run/dae.pid")
}
var ( var (
cfgFile string cfgFile string
disableTimestamp bool disableTimestamp bool
disableRunFile bool
runCmd = &cobra.Command{ runCmd = &cobra.Command{
Use: "run", Use: "run",
@ -50,11 +62,6 @@ var (
} }
) )
func init() {
runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "disable timestamp")
}
func Run(log *logrus.Logger, conf *config.Config) (err error) { func Run(log *logrus.Logger, conf *config.Config) (err error) {
// New ControlPlane. // New ControlPlane.
@ -72,6 +79,9 @@ func Run(log *logrus.Logger, conf *config.Config) (err error) {
go func() { go func() {
<-readyChan <-readyChan
sdnotify.Ready() sdnotify.Ready()
if !disableRunFile {
_ = os.WriteFile(PidFilePath, []byte(strconv.Itoa(os.Getpid())), 0644)
}
}() }()
if listener, err = c.ListenAndServe(readyChan, conf.Global.TproxyPort); err != nil { if listener, err = c.ListenAndServe(readyChan, conf.Global.TproxyPort); err != nil {
log.Errorln("ListenAndServe:", err) log.Errorln("ListenAndServe:", err)
@ -124,6 +134,7 @@ loop:
if e := c.Close(); e != nil { if e := c.Close(); e != nil {
return fmt.Errorf("close control plane: %w", e) return fmt.Errorf("close control plane: %w", e)
} }
_ = os.Remove(PidFilePath)
return nil return nil
} }
@ -136,17 +147,23 @@ func newControlPlane(log *logrus.Logger, bpf interface{}, conf *config.Config) (
} }
} }
// Resolve subscriptions to nodes. // Resolve subscriptions to nodes.
resolvingfailed := false
for _, sub := range conf.Subscription { for _, sub := range conf.Subscription {
tag, nodes, err := internal.ResolveSubscription(log, filepath.Dir(cfgFile), string(sub)) tag, nodes, err := internal.ResolveSubscription(log, filepath.Dir(cfgFile), string(sub))
if err != nil { if err != nil {
log.Warnf(`failed to resolve subscription "%v": %v`, sub, err) log.Warnf(`failed to resolve subscription "%v": %v`, sub, err)
resolvingfailed = true
} }
if len(nodes) > 0 { if len(nodes) > 0 {
tagToNodeList[tag] = append(tagToNodeList[tag], nodes...) tagToNodeList[tag] = append(tagToNodeList[tag], nodes...)
} }
} }
if len(tagToNodeList) == 0 { if len(tagToNodeList) == 0 {
return nil, fmt.Errorf("no node found, which could because all subscription resolving failed") if resolvingfailed {
log.Warnln("No node found because all subscription resolving failed.")
} else {
log.Warnln("No node found.")
}
} }
if len(conf.Global.LanInterface) == 0 && len(conf.Global.WanInterface) == 0 { if len(conf.Global.LanInterface) == 0 && len(conf.Global.WanInterface) == 0 {