mirror of
https://github.com/coredns/coredns.git
synced 2025-10-28 08:44:17 -04:00
middleware.cfg to configure middleware directives (#496)
* Use go generate to build middleware setup based on middleware.cfg Init default config * generated files * Move gen to an isolated area * rename files * PR review updates * undo readme
This commit is contained in:
4
Makefile
4
Makefile
@@ -48,6 +48,10 @@ clean:
|
|||||||
go clean
|
go clean
|
||||||
rm -f coredns
|
rm -f coredns
|
||||||
|
|
||||||
|
.PHONY: gen
|
||||||
|
gen:
|
||||||
|
go generate ./core/...
|
||||||
|
|
||||||
.PHONY: distclean
|
.PHONY: distclean
|
||||||
distclean: clean
|
distclean: clean
|
||||||
# Clean all dependencies and build artifacts
|
# Clean all dependencies and build artifacts
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
// Package core registers the server and all plugins we support.
|
// Package core registers the server and all plugins we support.
|
||||||
|
|
||||||
|
// Additional middleware packages
|
||||||
|
//go:generate go run ../gen/directives_generate.go ../middleware.cfg
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
//go:generate go run ../../gen/directives_generate.go ../../middleware.cfg
|
||||||
package dnsserver
|
package dnsserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -62,40 +63,3 @@ func RegisterDevDirective(name, before string) {
|
|||||||
}
|
}
|
||||||
fmt.Printf("[INFO] %s\n", msg)
|
fmt.Printf("[INFO] %s\n", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add here, and in core/coredns.go to use them.
|
|
||||||
|
|
||||||
// Directives are registered in the order they should be
|
|
||||||
// executed.
|
|
||||||
//
|
|
||||||
// Ordering is VERY important. Every middleware will
|
|
||||||
// feel the effects of all other middleware below
|
|
||||||
// (after) them during a request, but they must not
|
|
||||||
// care what middleware above them are doing.
|
|
||||||
var directives = []string{
|
|
||||||
"root",
|
|
||||||
"bind",
|
|
||||||
"trace",
|
|
||||||
"health",
|
|
||||||
"pprof",
|
|
||||||
|
|
||||||
"prometheus",
|
|
||||||
"errors",
|
|
||||||
"log",
|
|
||||||
"chaos",
|
|
||||||
"cache",
|
|
||||||
|
|
||||||
"rewrite",
|
|
||||||
"loadbalance",
|
|
||||||
|
|
||||||
"dnssec",
|
|
||||||
"file",
|
|
||||||
"auto",
|
|
||||||
"secondary",
|
|
||||||
"etcd",
|
|
||||||
"kubernetes",
|
|
||||||
"proxy",
|
|
||||||
"httpproxy",
|
|
||||||
"whoami",
|
|
||||||
"erratic",
|
|
||||||
}
|
|
||||||
|
|||||||
36
core/dnsserver/zdirectives.go
Normal file
36
core/dnsserver/zdirectives.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
// generated by directives_generate.go; DO NOT EDIT
|
||||||
|
package dnsserver
|
||||||
|
|
||||||
|
|
||||||
|
// Directives are registered in the order they should be
|
||||||
|
// executed.
|
||||||
|
//
|
||||||
|
// Ordering is VERY important. Every middleware will
|
||||||
|
// feel the effects of all other middleware below
|
||||||
|
// (after) them during a request, but they must not
|
||||||
|
// care what middleware above them are doing.
|
||||||
|
|
||||||
|
var directives = []string{
|
||||||
|
"root",
|
||||||
|
"bind",
|
||||||
|
"trace",
|
||||||
|
"health",
|
||||||
|
"pprof",
|
||||||
|
"prometheus",
|
||||||
|
"errors",
|
||||||
|
"log",
|
||||||
|
"chaos",
|
||||||
|
"cache",
|
||||||
|
"rewrite",
|
||||||
|
"loadbalance",
|
||||||
|
"dnssec",
|
||||||
|
"file",
|
||||||
|
"auto",
|
||||||
|
"secondary",
|
||||||
|
"etcd",
|
||||||
|
"kubernetes",
|
||||||
|
"proxy",
|
||||||
|
"httpprox",
|
||||||
|
"whoami",
|
||||||
|
"erratic",
|
||||||
|
}
|
||||||
4
core/zmiddleware.go
Normal file
4
core/zmiddleware.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// generated by directives_generate.go; DO NOT EDIT
|
||||||
|
package core
|
||||||
|
|
||||||
|
import ()
|
||||||
116
gen/directives_generate.go
Normal file
116
gen/directives_generate.go
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var middlewarePath = "github.com/miekg/coredns/middleware/"
|
||||||
|
var header = "// generated by directives_generate.go; DO NOT EDIT\n"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mwFile := os.Args[1]
|
||||||
|
|
||||||
|
mi := make(map[string]string, 0)
|
||||||
|
md := make(map[int]string, 0)
|
||||||
|
|
||||||
|
if file, err := os.Open(mwFile); err == nil {
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if !strings.HasPrefix(line, `//`) && !strings.HasPrefix(line, "#") {
|
||||||
|
items := strings.Split(line, ":")
|
||||||
|
if len(items) == 3 {
|
||||||
|
if priority, err := strconv.Atoi(items[0]); err == nil {
|
||||||
|
md[priority] = items[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if items[2] != "" {
|
||||||
|
if strings.Contains(items[2], "/") {
|
||||||
|
mi[items[1]] = items[2]
|
||||||
|
} else {
|
||||||
|
mi[items[1]] = middlewarePath + items[2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var orders []int
|
||||||
|
for k := range md {
|
||||||
|
orders = append(orders, k)
|
||||||
|
}
|
||||||
|
sort.Ints(orders)
|
||||||
|
|
||||||
|
if os.Getenv("GOPACKAGE") == "core" {
|
||||||
|
genImports("zmiddleware.go", mi)
|
||||||
|
}
|
||||||
|
if os.Getenv("GOPACKAGE") == "dnsserver" {
|
||||||
|
genDirectives("zdirectives.go", md)
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func genImports(file string, mi map[string]string) {
|
||||||
|
outs := header + "package " + os.Getenv("GOPACKAGE") + "\n\n" + "import ("
|
||||||
|
|
||||||
|
if len(mi) > 0 {
|
||||||
|
outs += "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range mi {
|
||||||
|
outs += " _ \"" + v + "\"\n"
|
||||||
|
}
|
||||||
|
outs += ")\n"
|
||||||
|
|
||||||
|
err := ioutil.WriteFile(file, []byte(outs), 0644)
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func genDirectives(file string, md map[int]string) {
|
||||||
|
|
||||||
|
outs := header + "package " + os.Getenv("GOPACKAGE") + "\n\n"
|
||||||
|
outs += `
|
||||||
|
// Directives are registered in the order they should be
|
||||||
|
// executed.
|
||||||
|
//
|
||||||
|
// Ordering is VERY important. Every middleware will
|
||||||
|
// feel the effects of all other middleware below
|
||||||
|
// (after) them during a request, but they must not
|
||||||
|
// care what middleware above them are doing.
|
||||||
|
|
||||||
|
var directives = []string{
|
||||||
|
`
|
||||||
|
|
||||||
|
var orders []int
|
||||||
|
for k := range md {
|
||||||
|
orders = append(orders, k)
|
||||||
|
}
|
||||||
|
sort.Ints(orders)
|
||||||
|
|
||||||
|
for _, k := range orders {
|
||||||
|
outs += " \"" + md[k] + "\",\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
outs += "}\n"
|
||||||
|
|
||||||
|
err := ioutil.WriteFile(file, []byte(outs), 0644)
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
43
middleware.cfg
Normal file
43
middleware.cfg
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# Directives are registered in the order they should be
|
||||||
|
# executed.
|
||||||
|
#
|
||||||
|
# Ordering is VERY important. Every middleware will
|
||||||
|
# feel the effects of all other middleware below
|
||||||
|
# (after) them during a request, but they must not
|
||||||
|
# care what middleware above them are doing.
|
||||||
|
|
||||||
|
|
||||||
|
# How to rebuild with updated middleware configurations:
|
||||||
|
# Modify the list below and run `make gen && make`
|
||||||
|
|
||||||
|
# The parser takes the input format of
|
||||||
|
# <order>:<middleware-name>:<package-name>
|
||||||
|
# OR
|
||||||
|
# <order>:<middleware-name>:
|
||||||
|
# External middleware example:
|
||||||
|
# 80:log:github.com/miekg/coredns/middleware/log
|
||||||
|
# Local middleware example:
|
||||||
|
# 80:log:
|
||||||
|
|
||||||
|
10:root:
|
||||||
|
20:bind:
|
||||||
|
30:trace:
|
||||||
|
40:health:
|
||||||
|
50:pprof:
|
||||||
|
60:prometheus:
|
||||||
|
70:errors:
|
||||||
|
80:log:
|
||||||
|
90:chaos:
|
||||||
|
100:cache:
|
||||||
|
110:rewrite:
|
||||||
|
120:loadbalance:
|
||||||
|
130:dnssec:
|
||||||
|
140:file:
|
||||||
|
150:auto:
|
||||||
|
160:secondary:
|
||||||
|
170:etcd:
|
||||||
|
180:kubernetes:
|
||||||
|
190:proxy:
|
||||||
|
200:httpprox:
|
||||||
|
210:whoami:
|
||||||
|
220:erratic:
|
||||||
Reference in New Issue
Block a user