v0.5.0-rc5: added proxy.exclude label, refactored some code

This commit is contained in:
yusing 2024-09-17 17:56:41 +08:00
parent 83d1d027c6
commit be7a766cb2
8 changed files with 31 additions and 24 deletions

View file

@ -88,6 +88,7 @@
| Label | Description | Default | | Label | Description | Default |
| ----------------------- | -------------------------------------------------------- | ---------------- | | ----------------------- | -------------------------------------------------------- | ---------------- |
| `proxy.aliases` | comma separated aliases for subdomain and label matching | `container_name` | | `proxy.aliases` | comma separated aliases for subdomain and label matching | `container_name` |
| `proxy.exclude` | to be excluded from `go-proxy` | false |
| `proxy.<alias>.<field>` | set field for specific alias | N/A | | `proxy.<alias>.<field>` | set field for specific alias | N/A |
| `proxy.*.<field>` | set field for all aliases | N/A | | `proxy.*.<field>` | set field for all aliases | N/A |

View file

@ -114,7 +114,7 @@ func (p *Provider) LoadCert() E.NestedError {
p.tlsCert = &cert p.tlsCert = &cert
p.certExpiries = expiries p.certExpiries = expiries
logger.Infof("next renewal in %v", time.Until(p.ShouldRenewOn())) logger.Infof("next renewal in %v", U.FormatDuration(time.Until(p.ShouldRenewOn())))
return p.renewIfNeeded() return p.renewIfNeeded()
} }

View file

@ -2,17 +2,13 @@ package common
import ( import (
"os" "os"
"strings"
U "github.com/yusing/go-proxy/utils"
) )
var NoSchemaValidation = getEnvBool("GOPROXY_NO_SCHEMA_VALIDATION") var NoSchemaValidation = getEnvBool("GOPROXY_NO_SCHEMA_VALIDATION")
var IsDebug = getEnvBool("GOPROXY_DEBUG") var IsDebug = getEnvBool("GOPROXY_DEBUG")
func getEnvBool(key string) bool { func getEnvBool(key string) bool {
switch strings.ToLower(os.Getenv(key)) { return U.ParseBool(os.Getenv(key))
case "1", "true", "yes", "on":
return true
default:
return false
}
} }

View file

@ -60,7 +60,6 @@ const NSProxy = "proxy"
var _ = func() int { var _ = func() int {
RegisterNamespace(NSProxy, ValueParserMap{ RegisterNamespace(NSProxy, ValueParserMap{
"aliases": commaSepParser,
"path_patterns": yamlListParser, "path_patterns": yamlListParser,
"set_headers": yamlStringMappingParser, "set_headers": yamlStringMappingParser,
"hide_headers": yamlListParser, "hide_headers": yamlListParser,

View file

@ -1,3 +0,0 @@
package provider
const wildcardAlias = "*"

View file

@ -10,6 +10,7 @@ import (
E "github.com/yusing/go-proxy/error" E "github.com/yusing/go-proxy/error"
M "github.com/yusing/go-proxy/models" M "github.com/yusing/go-proxy/models"
PT "github.com/yusing/go-proxy/proxy/fields" PT "github.com/yusing/go-proxy/proxy/fields"
U "github.com/yusing/go-proxy/utils"
W "github.com/yusing/go-proxy/watcher" W "github.com/yusing/go-proxy/watcher"
) )
@ -72,6 +73,12 @@ func (p *DockerProvider) getEntriesFromLabels(container *types.Container, client
var mainAlias string var mainAlias string
var aliases PT.Aliases var aliases PT.Aliases
if exclude, ok := container.Labels[D.NSProxy+".exclude"]; ok {
if U.ParseBool(exclude) {
return M.NewProxyEntries(), E.Nil()
}
}
// set mainAlias to docker compose service name if available // set mainAlias to docker compose service name if available
if serviceName, ok := container.Labels["com.docker.compose.service"]; ok { if serviceName, ok := container.Labels["com.docker.compose.service"]; ok {
mainAlias = serviceName mainAlias = serviceName
@ -84,9 +91,9 @@ func (p *DockerProvider) getEntriesFromLabels(container *types.Container, client
mainAlias = containerName mainAlias = containerName
} }
if l, ok := container.Labels["proxy.aliases"]; ok { if l, ok := container.Labels[D.NSProxy+".aliases"]; ok {
aliases = PT.NewAliases(l) aliases = PT.NewAliases(l)
delete(container.Labels, "proxy.aliases") delete(container.Labels, D.NSProxy+"proxy.aliases")
} else { } else {
aliases = PT.NewAliases(mainAlias) aliases = PT.NewAliases(mainAlias)
} }
@ -157,3 +164,5 @@ func findFirstPort(c *types.Container) (string, E.NestedError) {
} }
return "", E.Failure("findFirstPort") return "", E.Failure("findFirstPort")
} }
const wildcardAlias = "*"

View file

@ -20,16 +20,16 @@ func FormatDuration(d time.Duration) string {
var parts []string var parts []string
if days > 0 { if days > 0 {
parts = append(parts, fmt.Sprintf("%d Day%s", days, pluralize(days))) parts = append(parts, fmt.Sprintf("%d day%s", days, pluralize(days)))
} }
if hours > 0 { if hours > 0 {
parts = append(parts, fmt.Sprintf("%d Hour%s", hours, pluralize(hours))) parts = append(parts, fmt.Sprintf("%d hour%s", hours, pluralize(hours)))
} }
if minutes > 0 { if minutes > 0 {
parts = append(parts, fmt.Sprintf("%d Minute%s", minutes, pluralize(minutes))) parts = append(parts, fmt.Sprintf("%d minute%s", minutes, pluralize(minutes)))
} }
if seconds > 0 { if seconds > 0 {
parts = append(parts, fmt.Sprintf("%d Second%s", seconds, pluralize(seconds))) parts = append(parts, fmt.Sprintf("%d second%s", seconds, pluralize(seconds)))
} }
// Join the parts with appropriate connectors // Join the parts with appropriate connectors
@ -42,6 +42,15 @@ func FormatDuration(d time.Duration) string {
return strings.Join(parts[:len(parts)-1], ", ") + " and " + parts[len(parts)-1] return strings.Join(parts[:len(parts)-1], ", ") + " and " + parts[len(parts)-1]
} }
func ParseBool(s string) bool {
switch strings.ToLower(s) {
case "1", "true", "yes", "on":
return true
default:
return false
}
}
func pluralize(n int64) string { func pluralize(n int64) string {
if n > 1 { if n > 1 {
return "s" return "s"

View file

@ -2,7 +2,6 @@ package utils
import ( import (
"github.com/santhosh-tekuri/jsonschema" "github.com/santhosh-tekuri/jsonschema"
"github.com/yusing/go-proxy/common"
) )
var schemaCompiler = func() *jsonschema.Compiler { var schemaCompiler = func() *jsonschema.Compiler {
@ -11,16 +10,13 @@ var schemaCompiler = func() *jsonschema.Compiler {
return c return c
}() }()
var schemaStorage = make(map[string] *jsonschema.Schema) var schemaStorage = make(map[string]*jsonschema.Schema)
func GetSchema(path string) *jsonschema.Schema { func GetSchema(path string) *jsonschema.Schema {
if common.NoSchemaValidation {
panic("bug: GetSchema called when schema validation disabled")
}
if schema, ok := schemaStorage[path]; ok { if schema, ok := schemaStorage[path]; ok {
return schema return schema
} }
schema := schemaCompiler.MustCompile(path) schema := schemaCompiler.MustCompile(path)
schemaStorage[path] = schema schemaStorage[path] = schema
return schema return schema
} }