mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-11 03:06:51 +02:00
feat: idle sleep for proxmox LXCs
This commit is contained in:
@@ -111,7 +111,7 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container)
|
||||
|
||||
errs := gperr.NewBuilder("label errors")
|
||||
|
||||
m, err := docker.ParseLabels(container.Labels)
|
||||
m, err := docker.ParseLabels(container.RouteConfig)
|
||||
errs.Add(err)
|
||||
|
||||
var wildcardProps docker.LabelMap
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
D "github.com/yusing/go-proxy/internal/docker"
|
||||
"github.com/yusing/go-proxy/internal/route"
|
||||
T "github.com/yusing/go-proxy/internal/route/types"
|
||||
@@ -69,10 +68,10 @@ func TestApplyLabel(t *testing.T) {
|
||||
Labels: map[string]string{
|
||||
D.LabelAliases: "a,b",
|
||||
D.LabelIdleTimeout: "",
|
||||
D.LabelStopMethod: common.StopMethodDefault,
|
||||
D.LabelStopMethod: "stop",
|
||||
D.LabelStopSignal: "SIGTERM",
|
||||
D.LabelStopTimeout: common.StopTimeoutDefault,
|
||||
D.LabelWakeTimeout: common.WakeTimeoutDefault,
|
||||
D.LabelStopTimeout: "1h",
|
||||
D.LabelWakeTimeout: "10s",
|
||||
"proxy.*.no_tls_verify": "true",
|
||||
"proxy.*.scheme": "https",
|
||||
"proxy.*.host": "app",
|
||||
@@ -110,20 +109,16 @@ func TestApplyLabel(t *testing.T) {
|
||||
ExpectEqual(t, a.Middlewares, middlewaresExpect)
|
||||
ExpectEqual(t, len(b.Middlewares), 0)
|
||||
|
||||
ExpectEqual(t, a.Container.IdleTimeout, "")
|
||||
ExpectEqual(t, b.Container.IdleTimeout, "")
|
||||
|
||||
ExpectEqual(t, a.Container.StopTimeout, common.StopTimeoutDefault)
|
||||
ExpectEqual(t, b.Container.StopTimeout, common.StopTimeoutDefault)
|
||||
|
||||
ExpectEqual(t, a.Container.StopMethod, common.StopMethodDefault)
|
||||
ExpectEqual(t, b.Container.StopMethod, common.StopMethodDefault)
|
||||
|
||||
ExpectEqual(t, a.Container.WakeTimeout, common.WakeTimeoutDefault)
|
||||
ExpectEqual(t, b.Container.WakeTimeout, common.WakeTimeoutDefault)
|
||||
|
||||
ExpectEqual(t, a.Container.StopSignal, "SIGTERM")
|
||||
ExpectEqual(t, b.Container.StopSignal, "SIGTERM")
|
||||
ExpectEqual(t, a.Container.IdlewatcherConfig.IdleTimeout, 0)
|
||||
ExpectEqual(t, b.Container.IdlewatcherConfig.IdleTimeout, 0)
|
||||
ExpectEqual(t, a.Container.IdlewatcherConfig.StopTimeout, time.Hour)
|
||||
ExpectEqual(t, b.Container.IdlewatcherConfig.StopTimeout, time.Hour)
|
||||
ExpectEqual(t, a.Container.IdlewatcherConfig.StopMethod, "stop")
|
||||
ExpectEqual(t, b.Container.IdlewatcherConfig.StopMethod, "stop")
|
||||
ExpectEqual(t, a.Container.IdlewatcherConfig.WakeTimeout, 10*time.Second)
|
||||
ExpectEqual(t, b.Container.IdlewatcherConfig.WakeTimeout, 10*time.Second)
|
||||
ExpectEqual(t, a.Container.IdlewatcherConfig.StopSignal, "SIGTERM")
|
||||
ExpectEqual(t, b.Container.IdlewatcherConfig.StopSignal, "SIGTERM")
|
||||
|
||||
ExpectEqual(t, a.Homepage.Show, true)
|
||||
ExpectEqual(t, a.Homepage.Icon.Value, "png/adguard-home.png")
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||
|
||||
@@ -11,6 +13,9 @@ import (
|
||||
"github.com/yusing/go-proxy/internal/gperr"
|
||||
"github.com/yusing/go-proxy/internal/homepage"
|
||||
idlewatcher "github.com/yusing/go-proxy/internal/idlewatcher/types"
|
||||
"github.com/yusing/go-proxy/internal/logging"
|
||||
gpnet "github.com/yusing/go-proxy/internal/net"
|
||||
"github.com/yusing/go-proxy/internal/proxmox"
|
||||
"github.com/yusing/go-proxy/internal/task"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||
@@ -43,6 +48,8 @@ type (
|
||||
Homepage *homepage.ItemConfig `json:"homepage,omitempty"`
|
||||
AccessLog *accesslog.Config `json:"access_log,omitempty"`
|
||||
|
||||
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
|
||||
|
||||
Metadata `deserialize:"-"`
|
||||
}
|
||||
|
||||
@@ -55,13 +62,13 @@ type (
|
||||
LisURL *url.URL `json:"lurl,omitempty"`
|
||||
ProxyURL *url.URL `json:"purl,omitempty"`
|
||||
|
||||
Idlewatcher *idlewatcher.Config `json:"idlewatcher,omitempty"`
|
||||
|
||||
impl route.Route
|
||||
}
|
||||
Routes map[string]*Route
|
||||
)
|
||||
|
||||
const DefaultHost = "localhost"
|
||||
|
||||
func (r Routes) Contains(alias string) bool {
|
||||
_, ok := r[alias]
|
||||
return ok
|
||||
@@ -81,6 +88,70 @@ func (r *Route) Validate() (err gperr.Error) {
|
||||
}
|
||||
}
|
||||
|
||||
if r.Idlewatcher != nil && r.Idlewatcher.Proxmox != nil {
|
||||
node := r.Idlewatcher.Proxmox.Node
|
||||
vmid := r.Idlewatcher.Proxmox.VMID
|
||||
if node == "" {
|
||||
return gperr.Errorf("node (proxmox node name) is required")
|
||||
}
|
||||
if vmid <= 0 {
|
||||
return gperr.Errorf("vmid (lxc id) is required")
|
||||
}
|
||||
if r.Host == DefaultHost {
|
||||
containerName := r.Idlewatcher.ContainerName()
|
||||
// get ip addresses of the vmid
|
||||
node, ok := proxmox.Nodes.Get(node)
|
||||
if !ok {
|
||||
return gperr.Errorf("proxmox node %s not found in pool", node)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
ips, err := node.LXCGetIPs(ctx, vmid)
|
||||
if err != nil {
|
||||
return gperr.Errorf("failed to get ip addresses of vmid %d: %w", vmid, err)
|
||||
}
|
||||
|
||||
if len(ips) == 0 {
|
||||
return gperr.Multiline().
|
||||
Addf("no ip addresses found for %s", containerName).
|
||||
Adds("make sure you have set static ip address for container instead of dhcp").
|
||||
Subject(containerName)
|
||||
}
|
||||
|
||||
l := logging.With().Str("container", containerName).Logger()
|
||||
|
||||
l.Info().Msg("checking if container is running")
|
||||
running, err := node.LXCIsRunning(ctx, vmid)
|
||||
if err != nil {
|
||||
return gperr.New("failed to check container state").With(err)
|
||||
}
|
||||
|
||||
if !running {
|
||||
l.Info().Msg("starting container")
|
||||
if err := node.LXCAction(ctx, vmid, proxmox.LXCStart); err != nil {
|
||||
return gperr.New("failed to start container").With(err)
|
||||
}
|
||||
}
|
||||
|
||||
l.Info().Msgf("finding reachable ip addresses")
|
||||
for _, ip := range ips {
|
||||
if ok, _ := gpnet.PingWithTCPFallback(ctx, ip, r.Port.Proxy); ok {
|
||||
r.Host = ip.String()
|
||||
l.Info().Msgf("using ip %s", r.Host)
|
||||
break
|
||||
}
|
||||
}
|
||||
if r.Host == DefaultHost {
|
||||
return gperr.Multiline().
|
||||
Addf("no reachable ip addresses found, tried %d IPs", len(ips)).
|
||||
AddLines(ips).
|
||||
Subject(containerName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
errs := gperr.NewBuilder("entry validation failed")
|
||||
|
||||
if r.Scheme == route.SchemeFileServer {
|
||||
@@ -190,6 +261,10 @@ func (r *Route) HealthMonitor() health.HealthMonitor {
|
||||
}
|
||||
|
||||
func (r *Route) IdlewatcherConfig() *idlewatcher.Config {
|
||||
cont := r.Container
|
||||
if cont != nil && cont.IdlewatcherConfig != nil {
|
||||
return cont.IdlewatcherConfig
|
||||
}
|
||||
return r.Idlewatcher
|
||||
}
|
||||
|
||||
@@ -255,7 +330,8 @@ func (r *Route) UseLoadBalance() bool {
|
||||
}
|
||||
|
||||
func (r *Route) UseIdleWatcher() bool {
|
||||
return r.Idlewatcher != nil && r.Idlewatcher.IdleTimeout > 0
|
||||
cfg := r.IdlewatcherConfig()
|
||||
return cfg != nil && cfg.IdleTimeout > 0
|
||||
}
|
||||
|
||||
func (r *Route) UseHealthCheck() bool {
|
||||
@@ -276,7 +352,7 @@ func (r *Route) Finalize() {
|
||||
if r.Host == "" {
|
||||
switch {
|
||||
case !isDocker:
|
||||
r.Host = "localhost"
|
||||
r.Host = DefaultHost
|
||||
case cont.PrivateHostname != "":
|
||||
r.Host = cont.PrivateHostname
|
||||
case cont.PublicHostname != "":
|
||||
|
||||
Reference in New Issue
Block a user