-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
103 lines (94 loc) · 3.06 KB
/
main.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
99
100
101
102
103
// Copyright 2020 Google LLC
//
// 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 main
import (
"encoding/binary"
"flag"
"fmt"
"net"
"os"
"github.com/aleofreddi/csi-sanlock-lvm/pkg/lvmctrld"
"k8s.io/klog"
)
var (
noLock = flag.Bool("no-lock", false, "disable locking, use the given host id. This option is mutually exclusive with lock-host-addr and lock-host-id")
lockAddr = flag.String("lock-host-addr", "", "enable locking, compute host id from the given ip address. This options is mutually exclusive with lock-host-id and no-lock")
lockId = flag.Uint("lock-host-id", 0, "enable locking, use the given host id. This option is mutually exclusive with lock-host-addr and no-lock")
listen = flag.String("listen", "tcp://0.0.0.0:9000", "listen address")
version string
commit string
)
func main() {
klog.InitFlags(nil)
flag.Parse()
klog.Infof("Starting lvmctrld %s (%s)", version, commit)
listener, err := bootstrap()
if err != nil {
klog.Errorf("Bootstrap failed: %v", err)
os.Exit(2)
}
if err = listener.Run(); err != nil {
klog.Errorf("Execution failed: %v", err)
os.Exit(3)
}
os.Exit(0)
}
func bootstrap() (*lvmctrld.Listener, error) {
if (*noLock == (*lockId != 0)) == ((*lockId != 0) == (*lockAddr != "")) {
return nil, fmt.Errorf("invalid lock configuration, expected one of: no-lock|lock-host-addr|lock-host-id flag")
}
// Start global lock if needed.
var id uint16
var err error
if *noLock {
id = 1
} else {
id, err = parseHostId(*lockId, *lockAddr)
if err != nil {
return nil, fmt.Errorf("failed to parse host id: %v", err)
}
if err = lvmctrld.StartLock(id, []string{}); err != nil {
return nil, fmt.Errorf("failed to start lock: %v", err)
}
}
// Start server.
listener, err := lvmctrld.NewListener(*listen, id)
if err != nil {
return nil, fmt.Errorf("failed to instance listener: %v", err)
}
if err = listener.Init(); err != nil {
return nil, fmt.Errorf("failed to initialize listener: %v", err)
}
return listener, nil
}
func parseHostId(hostId uint, hostAddr string) (uint16, error) {
if hostId != 0 {
if hostId > 2000 {
return 0, fmt.Errorf("invalid host id %d, expected a value in range [1, 2000]", hostId)
}
return uint16(hostId), nil
}
id, err := addressToHostId(hostAddr)
if err != nil {
return 0, fmt.Errorf("invalid address: %v", err)
}
return id, nil
}
func addressToHostId(ip string) (uint16, error) {
address := net.ParseIP(ip)
if address == nil {
return 0, fmt.Errorf("invalid ip address: %s", ip)
}
return uint16((binary.BigEndian.Uint32(address[len(address)-4:])&0x7ff)%1999 + 1), nil
}