This commit is contained in:
yusing
2026-02-16 08:59:01 +08:00
parent 15b9635ee1
commit e4e6f6b3e8
242 changed files with 3953 additions and 3502 deletions

View File

@@ -1,6 +1,7 @@
package types
import (
"errors"
"net/url"
"strconv"
"strings"
@@ -32,7 +33,7 @@ type (
DependsOn []string `json:"depends_on,omitempty"`
NoLoadingPage bool `json:"no_loading_page,omitempty"`
valErr gperr.Error
valErr error
} // @name IdlewatcherConfig
ContainerStopMethod string // @name ContainerStopMethod
ContainerSignal string // @name ContainerSignal
@@ -44,7 +45,7 @@ type (
} // @name IdlewatcherDockerConfig
ProxmoxConfig struct {
Node string `json:"node" validate:"required"`
VMID int `json:"vmid" validate:"required"`
VMID uint64 `json:"vmid" validate:"required"`
} // @name IdlewatcherProxmoxNodeConfig
)
@@ -57,21 +58,28 @@ const (
ContainerStopMethodKill ContainerStopMethod = "kill"
)
var (
ErrMissingProviderConfig = errors.New("missing idlewatcher provider config")
ErrInvalidStopMethod = errors.New("invalid stop method")
ErrInvalidStopSignal = errors.New("invalid stop signal")
ErrEmptyStartEndpoint = errors.New("start endpoint must not be empty if defined")
)
func (c *IdlewatcherConfig) Key() string {
if c.Docker != nil {
return c.Docker.ContainerID
}
return c.Proxmox.Node + ":" + strconv.Itoa(c.Proxmox.VMID)
return c.Proxmox.Node + ":" + strconv.FormatUint(c.Proxmox.VMID, 10)
}
func (c *IdlewatcherConfig) ContainerName() string {
if c.Docker != nil {
return c.Docker.ContainerName
}
return "lxc-" + strconv.Itoa(c.Proxmox.VMID)
return "lxc-" + strconv.FormatUint(c.Proxmox.VMID, 10)
}
func (c *IdlewatcherConfig) Validate() gperr.Error {
func (c *IdlewatcherConfig) Validate() error {
if c.IdleTimeout == 0 { // zero idle timeout means no idle watcher
c.valErr = nil
return nil
@@ -89,13 +97,13 @@ func (c *IdlewatcherConfig) Validate() gperr.Error {
return c.valErr
}
func (c *IdlewatcherConfig) ValErr() gperr.Error {
func (c *IdlewatcherConfig) ValErr() error {
return c.valErr
}
func (c *IdlewatcherConfig) validateProvider() error {
if c.Docker == nil && c.Proxmox == nil {
return gperr.New("missing idlewatcher provider config")
return ErrMissingProviderConfig
}
return nil
}
@@ -118,16 +126,16 @@ func (c *IdlewatcherConfig) validateStopMethod() error {
case ContainerStopMethodPause, ContainerStopMethodStop, ContainerStopMethodKill:
return nil
default:
return gperr.New("invalid stop method").Subject(string(c.StopMethod))
return gperr.PrependSubject(ErrInvalidStopMethod, string(c.StopMethod))
}
}
func (c *IdlewatcherConfig) validateStopSignal() error {
switch c.StopSignal {
case "", "SIGINT", "SIGTERM", "SIGQUIT", "SIGHUP", "INT", "TERM", "QUIT", "HUP":
case "", "SIGINT", "SIGTERM", "SIGKILL", "SIGQUIT", "SIGHUP", "INT", "TERM", "KILL", "QUIT", "HUP":
return nil
default:
return gperr.New("invalid stop signal").Subject(string(c.StopSignal))
return gperr.PrependSubject(ErrInvalidStopSignal, string(c.StopSignal))
}
}
@@ -141,7 +149,7 @@ func (c *IdlewatcherConfig) validateStartEndpoint() error {
c.StartEndpoint = c.StartEndpoint[:i]
}
if len(c.StartEndpoint) == 0 {
return gperr.New("start endpoint must not be empty if defined")
return ErrEmptyStartEndpoint
}
_, err := url.ParseRequestURI(c.StartEndpoint)
return err