mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00

* feat: idle sleep for proxmox LXCs * refactor: replace deprecated docker api types * chore(api): remove debug task list endpoint * refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon * refactor: introduce Pool interface, move agent_pool to agent module * refactor: simplify api code * feat: introduce debug api * refactor: remove net.URL and net.CIDR types, improved unmarshal handling * chore: update Makefile for debug build tag, update README * chore: add gperr.Unwrap method * feat: relative time and duration formatting * chore: add ROOT_DIR environment variable, refactor * migration: move homepage override and icon cache to $BASE_DIR/data, add migration code * fix: nil dereference on marshalling service health * fix: wait for route deletion * chore: enhance tasks debuggability * feat: stdout access logger and MultiWriter * fix(agent): remove agent properly on verify error * fix(metrics): disk exclusion logic and added corresponding tests * chore: update schema and prettify, fix package.json and Makefile * fix: I/O buffer not being shrunk before putting back to pool * feat: enhanced error handling module * chore: deps upgrade * feat: better value formatting and handling --------- Co-authored-by: yusing <yusing@6uo.me>
189 lines
4.2 KiB
Go
Executable file
189 lines
4.2 KiB
Go
Executable file
package provider
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/docker/docker/client"
|
|
"github.com/rs/zerolog"
|
|
"github.com/yusing/go-proxy/internal/common"
|
|
"github.com/yusing/go-proxy/internal/docker"
|
|
"github.com/yusing/go-proxy/internal/gperr"
|
|
"github.com/yusing/go-proxy/internal/logging"
|
|
"github.com/yusing/go-proxy/internal/route"
|
|
U "github.com/yusing/go-proxy/internal/utils"
|
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
|
"github.com/yusing/go-proxy/internal/watcher"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type DockerProvider struct {
|
|
name, dockerHost string
|
|
l zerolog.Logger
|
|
}
|
|
|
|
const (
|
|
aliasRefPrefix = '#'
|
|
aliasRefPrefixAlt = '$'
|
|
)
|
|
|
|
var ErrAliasRefIndexOutOfRange = gperr.New("index out of range")
|
|
|
|
func DockerProviderImpl(name, dockerHost string) ProviderImpl {
|
|
if dockerHost == common.DockerHostFromEnv {
|
|
dockerHost = common.GetEnvString("DOCKER_HOST", client.DefaultDockerHost)
|
|
}
|
|
return &DockerProvider{
|
|
name,
|
|
dockerHost,
|
|
logging.With().Str("type", "docker").Str("name", name).Logger(),
|
|
}
|
|
}
|
|
|
|
func (p *DockerProvider) String() string {
|
|
return "docker@" + p.name
|
|
}
|
|
|
|
func (p *DockerProvider) ShortName() string {
|
|
return p.name
|
|
}
|
|
|
|
func (p *DockerProvider) IsExplicitOnly() bool {
|
|
return p.name[len(p.name)-1] == '!'
|
|
}
|
|
|
|
func (p *DockerProvider) Logger() *zerolog.Logger {
|
|
return &p.l
|
|
}
|
|
|
|
func (p *DockerProvider) NewWatcher() watcher.Watcher {
|
|
return watcher.NewDockerWatcher(p.dockerHost)
|
|
}
|
|
|
|
func (p *DockerProvider) loadRoutesImpl() (route.Routes, gperr.Error) {
|
|
containers, err := docker.ListContainers(p.dockerHost)
|
|
if err != nil {
|
|
return nil, gperr.Wrap(err)
|
|
}
|
|
|
|
errs := gperr.NewBuilder()
|
|
routes := make(route.Routes, len(containers))
|
|
|
|
for _, c := range containers {
|
|
container := docker.FromDocker(&c, p.dockerHost)
|
|
if container.IsExcluded {
|
|
continue
|
|
}
|
|
|
|
newEntries, err := p.routesFromContainerLabels(container)
|
|
if err != nil {
|
|
errs.Add(err.Subject(container.ContainerName))
|
|
}
|
|
for k, v := range newEntries {
|
|
if routes.Contains(k) {
|
|
errs.Addf("duplicated alias %s", k)
|
|
} else {
|
|
routes[k] = v
|
|
}
|
|
}
|
|
}
|
|
|
|
return routes, errs.Error()
|
|
}
|
|
|
|
// Returns a list of proxy entries for a container.
|
|
// Always non-nil.
|
|
func (p *DockerProvider) routesFromContainerLabels(container *docker.Container) (route.Routes, gperr.Error) {
|
|
if !container.IsExplicit && p.IsExplicitOnly() {
|
|
return nil, nil
|
|
}
|
|
|
|
routes := make(route.Routes, len(container.Aliases))
|
|
|
|
// init entries map for all aliases
|
|
for _, a := range container.Aliases {
|
|
routes[a] = &route.Route{
|
|
Metadata: route.Metadata{
|
|
Container: container,
|
|
},
|
|
}
|
|
}
|
|
|
|
errs := gperr.NewBuilder("label errors")
|
|
|
|
m, err := docker.ParseLabels(container.RouteConfig)
|
|
errs.Add(err)
|
|
|
|
var wildcardProps docker.LabelMap
|
|
|
|
for alias, entryMapAny := range m {
|
|
if len(alias) == 0 {
|
|
errs.Add(gperr.New("empty alias"))
|
|
continue
|
|
}
|
|
|
|
entryMap, ok := entryMapAny.(docker.LabelMap)
|
|
if !ok {
|
|
// try to deserialize to map
|
|
entryMap = make(docker.LabelMap)
|
|
yamlStr, ok := entryMapAny.(string)
|
|
if !ok {
|
|
// should not happen
|
|
panic(fmt.Errorf("invalid entry map type %T", entryMapAny))
|
|
}
|
|
if err := yaml.Unmarshal([]byte(yamlStr), &entryMap); err != nil {
|
|
errs.Add(gperr.Wrap(err).Subject(alias))
|
|
continue
|
|
}
|
|
}
|
|
|
|
if alias == docker.WildcardAlias {
|
|
wildcardProps = entryMap
|
|
continue
|
|
}
|
|
|
|
// check if it is an alias reference
|
|
switch alias[0] {
|
|
case aliasRefPrefix, aliasRefPrefixAlt:
|
|
index, err := strutils.Atoi(alias[1:])
|
|
if err != nil {
|
|
errs.Add(err)
|
|
break
|
|
}
|
|
if index < 1 || index > len(container.Aliases) {
|
|
errs.Add(ErrAliasRefIndexOutOfRange.Subject(strconv.Itoa(index)))
|
|
break
|
|
}
|
|
alias = container.Aliases[index-1]
|
|
}
|
|
|
|
// init entry if not exist
|
|
r, ok := routes[alias]
|
|
if !ok {
|
|
r = &route.Route{
|
|
Metadata: route.Metadata{
|
|
Container: container,
|
|
},
|
|
}
|
|
routes[alias] = r
|
|
}
|
|
|
|
// deserialize map into entry object
|
|
err := U.MapUnmarshalValidate(entryMap, r)
|
|
if err != nil {
|
|
errs.Add(err.Subject(alias))
|
|
} else {
|
|
routes[alias] = r
|
|
}
|
|
}
|
|
if wildcardProps != nil {
|
|
for _, re := range routes {
|
|
if err := U.MapUnmarshalValidate(wildcardProps, re); err != nil {
|
|
errs.Add(err.Subject(docker.WildcardAlias))
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return routes, errs.Error()
|
|
}
|