/
setup.go
98 lines (86 loc) · 2.41 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nacos
import (
"fmt"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"strconv"
"strings"
)
func init() {
caddy.RegisterPlugin("nacos", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
fmt.Println("register nacos plugin")
}
func setup(c *caddy.Controller) error {
fmt.Println("setup nacos plugin")
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
vs, _ := NacosParse(c)
vs.Next = next
Inited = true
return vs
})
return nil
}
func NacosParse(c *caddy.Controller) (*Nacos, error) {
fmt.Println("init nacos plugin...")
nacosImpl := Nacos{}
var serverHosts = make([]string, 0)
namespaceId := ""
for c.Next() {
nacosImpl.Zones = c.RemainingArgs()
if c.NextBlock() {
for {
switch v := c.Val(); v {
case "nacos_namespaceId":
namespaceId = c.RemainingArgs()[0]
case "nacos_server_host":
serverHosts = strings.Split(c.RemainingArgs()[0], ",")
//case "nacos_server":
// servers = strings.Split(c.RemainingArgs()[0], ",")
/* it is nacos_servera noop now */
//case "nacos_server_port":
// port, err := strconv.Atoi(c.RemainingArgs()[0])
// if err != nil {
// serverPort = port
// }
case "cache_ttl":
ttl, err := strconv.Atoi(c.RemainingArgs()[0])
if err != nil {
DNSTTL = uint32(ttl)
}
case "cache_dir":
CachePath = c.RemainingArgs()[0]
case "log_path":
LogPath = c.RemainingArgs()[0]
default:
if c.Val() != "}" {
return &Nacos{}, c.Errf("unknown property '%s'", c.Val())
}
}
if !c.Next() {
break
}
}
}
client := NewNacosClient(namespaceId, serverHosts)
nacosImpl.NacosClientImpl = client
nacosImpl.DNSCache = NewConcurrentMap()
return &nacosImpl, nil
}
return &Nacos{}, nil
}