2019-06-05 11:22:53 -04:00

123 lines
3.0 KiB
Go

package caddy
import (
"encoding/json"
"fmt"
"github.com/caddyserver/caddy2/modules/caddytls"
)
type serverRoute struct {
Matchers map[string]json.RawMessage `json:"match"`
Apply []map[string]string `json:"apply"`
Respond proxyConfig `json:"respond"`
}
type routeList []serverRoute
type proxyConfig struct {
Module string `json:"responder"`
LoadBalanceType string `json:"load_balance_type"`
Upstreams []upstreamConfig `json:"upstreams"`
}
type upstreamConfig struct {
Host string `json:"host"`
}
type httpServerConfig struct {
Listen []string `json:"listen"`
ReadTimeout string `json:"read_timeout"`
DisableAutoHTTPS bool `json:"disable_auto_https"`
// ReadHeaderTimeout caddy2.Duration `json:"read_header_timeout"`
// HiddenFiles []string `json:"hidden_files"` // TODO:... experimenting with shared/common state
TLSConnPolicies caddytls.ConnectionPolicies `json:"tls_connection_policies"`
Routes routeList `json:"routes"`
}
type httpErrorConfig struct {
Routes routeList `json:"routes"`
}
type serverConfig struct {
Server httpServerConfig `json:"ingress_server"`
}
type servers struct {
Servers serverConfig `json:"servers"`
}
type TLSConfig struct {
Module string `json:"module"`
Automation caddytls.AutomationConfig `json:"automation"`
}
type httpServer struct {
TLS TLSConfig `json:"tls"`
HTTP servers `json:"http"`
}
// StorageValues represents the config for certmagic storage providers.
type StorageValues struct {
Namespace string `json:"namespace"`
}
// Storage represents the certmagic storage configuration.
type Storage struct {
System string `json:"system"`
StorageValues
}
// Config represents a caddy2 config file.
type Config struct {
Storage Storage `json:"storage"`
Modules httpServer `json:"apps"`
}
// ControllerConfig represents ingress controller config received through cli arguments.
type ControllerConfig struct {
Email string
AutomaticTLS bool
TLSUseStaging bool
WatchNamespace string
}
// NewConfig returns a plain slate caddy2 config file.
func NewConfig(namespace string, cfg ControllerConfig) *Config {
autoPolicyBytes := json.RawMessage(fmt.Sprintf(`{"module": "acme", "email": "%v"}`, cfg.Email))
return &Config{
Storage: Storage{
System: "secret_store",
StorageValues: StorageValues{
Namespace: namespace,
},
},
Modules: httpServer{
TLS: TLSConfig{
Module: "acme",
Automation: caddytls.AutomationConfig{
Policies: []caddytls.AutomationPolicy{
caddytls.AutomationPolicy{
Hosts: nil,
Management: autoPolicyBytes,
},
},
},
},
HTTP: servers{
Servers: serverConfig{
Server: httpServerConfig{
DisableAutoHTTPS: !cfg.AutomaticTLS,
ReadTimeout: "30s",
Listen: []string{":80", ":443"},
TLSConnPolicies: caddytls.ConnectionPolicies{
&caddytls.ConnectionPolicy{},
},
},
},
},
},
}
}