Debian Codegraph @

api/v0/detector/factory.go

raw symbols SCIP
1 package detector
2
3 import (
4 "encoding/binary"
5 "errors"
6 "fmt"
7 "io/ioutil"
8 "net"
9 "strconv"
10 "strings"
11 "sync"
12
13 "github.com/gogo/protobuf/proto"
14 log "github.com/golang/glog"
15 mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
16 util "github.com/mesos/mesos-go/api/v0/mesosutil"
17 "github.com/mesos/mesos-go/api/v0/upid"
18 )
19
20 var (
21 pluginLock sync.Mutex
22 plugins = map[string]PluginFactory{}
23 EmptySpecError = errors.New("empty master specification")
24
25 defaultFactory = PluginFactory(func(spec string, _ ...Option) (Master, error) {
26 if len(spec) == 0 {
27 return nil, EmptySpecError
28 }
29 if strings.Index(spec, "@") < 0 {
30 spec = "master@" + spec
31 }
32 if pid, err := upid.Parse(spec); err == nil {
33 return NewStandalone(CreateMasterInfo(pid)), nil
34 } else {
35 return nil, err
36 }
37 })
38 )
39
40 type PluginFactory func(string, ...Option) (Master, error)
41
42 // associates a plugin implementation with a Master specification prefix.
43 // packages that provide plugins are expected to invoke this func within
44 // their init() implementation. schedulers that wish to support plugins may
45 // anonymously import ("_") a package the auto-registers said plugins.
46 func Register(prefix string, f PluginFactory) error {
47 if prefix == "" {
48 return fmt.Errorf("illegal prefix: '%v'", prefix)
49 }
50 if f == nil {
51 return fmt.Errorf("nil plugin factories are not allowed")
52 }
53
54 pluginLock.Lock()
55 defer pluginLock.Unlock()
56
57 if _, found := plugins[prefix]; found {
58 return fmt.Errorf("detection plugin already registered for prefix '%s'", prefix)
59 }
60 plugins[prefix] = f
61 return nil
62 }
63
64 // Create a new detector given the provided specification. Examples are:
65 //
66 // - file://{path_to_local_file}
67 // - {ipaddress}:{port}
68 // - master@{ip_address}:{port}
69 // - master({id})@{ip_address}:{port}
70 //
71 // Support for the file:// prefix is intentionally hardcoded so that it may
72 // not be inadvertently overridden by a custom plugin implementation. Custom
73 // plugins are supported via the Register and MatchingPlugin funcs.
74 //
75 // Furthermore it is expected that master detectors returned from this func
76 // are not yet running and will only begin to spawn requisite background
77 // processing upon, or some time after, the first invocation of their Detect.
78 //
79 func New(spec string, options ...Option) (m Master, err error) {
80 if strings.HasPrefix(spec, "file://") {
81 var body []byte
82 path := spec[7:]
83 body, err = ioutil.ReadFile(path)
84 if err != nil {
85 log.V(1).Infof("failed to read from file at '%s'", path)
86 } else {
87 m, err = New(string(body), options...)
88 }
89 } else if f, ok := MatchingPlugin(spec); ok {
90 m, err = f(spec, options...)
91 } else {
92 m, err = defaultFactory(spec, options...)
93 }
94
95 return
96 }
97
98 func MatchingPlugin(spec string) (PluginFactory, bool) {
99 pluginLock.Lock()
100 defer pluginLock.Unlock()
101
102 for prefix, f := range plugins {
103 if strings.HasPrefix(spec, prefix) {
104 return f, true
105 }
106 }
107 return nil, false
108 }
109
110 // Super-useful utility func that attempts to build a mesos.MasterInfo from a
111 // upid.UPID specification. An attempt is made to determine the IP address of
112 // the UPID's Host and any errors during such resolution will result in a nil
113 // returned result. A nil result is also returned upon errors parsing the Port
114 // specification of the UPID.
115 //
116 // TODO(jdef) make this a func of upid.UPID so that callers can invoke somePid.MasterInfo()?
117 //
118 func CreateMasterInfo(pid *upid.UPID) *mesos.MasterInfo {
119 if pid == nil {
120 return nil
121 }
122 port, err := strconv.Atoi(pid.Port)
123 if err != nil {
124 log.Errorf("failed to parse port: %v", err)
125 return nil
126 }
127 //TODO(jdef) what about (future) ipv6 support?
128 var ipv4 net.IP
129 if ipv4 = net.ParseIP(pid.Host); ipv4 != nil {
130 // This is needed for the people cross-compiling from macos to linux.
131 // The cross-compiled version of net.LookupIP() fails to handle plain IPs.
132 // See https://github.com/mesos/mesos-go/api/v0/pull/117
133 } else if addrs, err := net.LookupIP(pid.Host); err == nil {
134 for _, ip := range addrs {
135 if ip = ip.To4(); ip != nil {
136 ipv4 = ip
137 break
138 }
139 }
140 if ipv4 == nil {
141 log.Errorf("host does not resolve to an IPv4 address: %v", pid.Host)
142 return nil
143 }
144 } else {
145 log.Errorf("failed to lookup IPs for host '%v': %v", pid.Host, err)
146 return nil
147 }
148 packedip := binary.BigEndian.Uint32(ipv4) // network byte order is big-endian
149 mi := util.NewMasterInfo(pid.ID, packedip, uint32(port))
150 mi.Pid = proto.String(pid.String())
151 if pid.Host != "" {
152 mi.Hostname = proto.String(pid.Host)
153 }
154 return mi
155 }