This fix is an enhancement of external plugin enabling. Previously, it was already able to build a customerized coredns with plugins enabled selectively, without changing coredns source code. However, all default plugins are actually bundled because of the import rule: ``` "github.com/coredns/coredns/coremain" ``` The issue is best described with the following: ``` root@localhost:/go/src/github.com/coredns/coredns/sample# cat sample.go package main import ( _ "github.com/coredns/forward" "github.com/coredns/coredns/coremain" "github.com/coredns/coredns/core/dnsserver" ) var directives = []string{ "forward", "startup", "shutdown", } func init() { dnsserver.Directives = directives } func main() { coremain.Run() } root@localhost:/go/src/github.com/coredns/coredns/sample# root@localhost:/go/src/github.com/coredns/coredns/sample# go build -v sample.go root@localhost:/go/src/github.com/coredns/coredns/sample# ./sample -plugins root@localhost:/go/src/github.com/coredns/coredns/sample# ./sample -plugins Server types: dns Caddyfile loaders: flag default Other plugins: dns.auto dns.autopath dns.bind dns.cache dns.chaos dns.debug dns.dnssec dns.dnstap dns.erratic dns.errors dns.etcd dns.federation dns.file dns.forward dns.health dns.hosts dns.kubernetes dns.loadbalance dns.log dns.nsid dns.pprof dns.prometheus dns.proxy dns.reverse dns.rewrite dns.root dns.route53 dns.secondary dns.template .... ``` This fix moves zplugins.go to a different package/directory so that it is possible to "only import plugins as needed". The following is the new output after this fix: ``` root@localhost:/go/src/github.com/coredns/coredns/sample# ./sample -plugins Server types: dns Caddyfile loaders: flag default Other plugins: dns.forward dns.prometheus shutdown startup root@localhost:/go/src/github.com/coredns/coredns/sample# ``` Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
115 lines
2.4 KiB
Go
115 lines
2.4 KiB
Go
//+build ignore
|
|
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"go/format"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
mi := make(map[string]string, 0)
|
|
md := []string{}
|
|
|
|
file, err := os.Open(pluginFile)
|
|
if err != nil {
|
|
log.Fatalf("Failed to open %s: %q", pluginFile, err)
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
|
|
items := strings.Split(line, ":")
|
|
if len(items) != 2 {
|
|
// ignore empty lines
|
|
continue
|
|
}
|
|
name, repo := items[0], items[1]
|
|
|
|
if _, ok := mi[name]; ok {
|
|
log.Fatalf("Duplicate entry %q", name)
|
|
}
|
|
|
|
md = append(md, name)
|
|
mi[name] = pluginPath + repo // Default, unless overridden by 3rd arg
|
|
|
|
if _, err := os.Stat(pluginFSPath + repo); err != nil { // External package has been given
|
|
mi[name] = repo
|
|
}
|
|
}
|
|
|
|
genImports("core/plugin/zplugin.go", "plugin", mi)
|
|
genDirectives("core/dnsserver/zdirectives.go", "dnsserver", md)
|
|
}
|
|
|
|
func genImports(file, pack string, mi map[string]string) {
|
|
outs := header + "package " + pack + "\n\n" + "import ("
|
|
|
|
if len(mi) > 0 {
|
|
outs += "\n"
|
|
}
|
|
|
|
outs += "// Include all plugins.\n"
|
|
for _, v := range mi {
|
|
outs += `_ "` + v + `"` + "\n"
|
|
}
|
|
outs += ")\n"
|
|
|
|
if err := formatAndWrite(file, outs); err != nil {
|
|
log.Fatalf("Failed to format and write: %q", err)
|
|
}
|
|
}
|
|
|
|
func genDirectives(file, pack string, md []string) {
|
|
|
|
outs := header + "package " + pack + "\n\n"
|
|
outs += `
|
|
// Directives are registered in the order they should be
|
|
// executed.
|
|
//
|
|
// Ordering is VERY important. Every plugin will
|
|
// feel the effects of all other plugin below
|
|
// (after) them during a request, but they must not
|
|
// care what plugin above them are doing.
|
|
var Directives = []string{
|
|
`
|
|
|
|
for i := range md {
|
|
outs += `"` + md[i] + `",` + "\n"
|
|
}
|
|
|
|
outs += "}\n"
|
|
|
|
if err := formatAndWrite(file, outs); err != nil {
|
|
log.Fatalf("Failed to format and write: %q", err)
|
|
}
|
|
}
|
|
|
|
func formatAndWrite(file string, data string) error {
|
|
res, err := format.Source([]byte(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = ioutil.WriteFile(file, res, 0644); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
const (
|
|
pluginPath = "github.com/coredns/coredns/plugin/"
|
|
pluginFile = "plugin.cfg"
|
|
pluginFSPath = "plugin/" // Where the plugins are located on the file system
|
|
header = "// generated by directives_generate.go; DO NOT EDIT\n\n"
|
|
)
|