forked from ehazlett/stellar
/
service.go
66 lines (54 loc) · 1.41 KB
/
service.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
package nameserver
import (
"github.com/containerd/containerd"
"github.com/ehazlett/element"
"github.com/ehazlett/stellar"
"github.com/ehazlett/stellar/client"
ptypes "github.com/gogo/protobuf/types"
"google.golang.org/grpc"
api "github.com/ehazlett/stellar/api/services/nameserver/v1"
)
const (
serviceID = "stellar.services.nameserver.v1"
dsNameserverBucketName = "stellar." + stellar.APIVersion + ".services.nameserver"
)
var (
empty = &ptypes.Empty{}
)
type service struct {
containerdAddr string
namespace string
bridge string
upstreamDNSAddr string
agent *element.Agent
}
func New(cfg *stellar.Config, agent *element.Agent) (*service, error) {
srv := &service{
containerdAddr: cfg.ContainerdAddr,
namespace: cfg.Namespace,
bridge: cfg.Bridge,
upstreamDNSAddr: cfg.UpstreamDNSAddr,
agent: agent,
}
return srv, nil
}
func (s *service) Register(server *grpc.Server) error {
api.RegisterNameserverServer(server, s)
return nil
}
func (s *service) ID() string {
return serviceID
}
func (s *service) Start() error {
return s.startDNSServer()
}
func (s *service) containerd() (*containerd.Client, error) {
return stellar.DefaultContainerd(s.containerdAddr, s.namespace)
}
func (s *service) client() (*client.Client, error) {
peer, err := s.agent.LocalNode()
if err != nil {
return nil, err
}
return client.NewClient(peer.Addr)
}