-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyRouter.go
More file actions
133 lines (117 loc) · 3.22 KB
/
tinyRouter.go
File metadata and controls
133 lines (117 loc) · 3.22 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"flag"
"fmt"
"github.com/coreos/etcd/clientv3"
"google.golang.org/grpc/grpclog"
"io/ioutil"
"log"
"os"
"os/exec"
"time"
)
var config string
var calicoEtcdConfig calicoEctd
type calicoEctd struct {
Endpoints []string `json:"endpoints"`
EtcdCert string `json:"etcd_cert"`
EtcdCertKey string `json:"etcd_cert_key"`
EtcdCa string `json:"etcd_ca"`
}
func init() {
// get calico etcd server config file path
flag.StringVar(&config, "c", "./tinyRouter.json", "etcd connector info")
flag.Parse()
fmt.Println("etcd config path:", config)
// get calico etcd server config for connect
if config != "" {
filePtr, err := os.Open(config)
if err != nil {
fmt.Println(err)
return
}
decoder := json.NewDecoder(filePtr)
err = decoder.Decode(&calicoEtcdConfig)
if err != nil {
fmt.Println("Decoder failed", err.Error())
return
}
}
}
type calicoRouteSpec struct {
Cidr string `json:"cidr"`
Deleted string `json:"deleted"`
Node string `json:"node"`
State string `json:"state"`
}
type calicoStaticRoutes struct {
Spec calicoRouteSpec `json:"spec"`
}
func routeHandler(specByte []byte) {
t_struct := calicoStaticRoutes{}
err := json.Unmarshal(specByte, &t_struct)
if err != nil {
//fmt.Println(t_struct.Spec.Cidr, t_struct.Spec.Node, t_struct.Spec.Deleted, t_struct.Spec.State)
}
if t_struct.Spec.Deleted == "true" && t_struct.Spec.State == "pendingDeletion" {
fmt.Println("delete the static route", t_struct.Spec.Cidr, t_struct.Spec.Node)
routeCmd("del", t_struct.Spec.Cidr, t_struct.Spec.Node)
}
if t_struct.Spec.Deleted == "false" && t_struct.Spec.State == "confirmed" {
fmt.Println("add the static route", t_struct.Spec.Cidr, t_struct.Spec.Node)
routeCmd("add", t_struct.Spec.Cidr, t_struct.Spec.Node)
}
}
func main() {
calicoBgpRouteWatcher()
}
func routeCmd(action string, cidr string, gateway string) {
// ip route add 10.244.229.192/26 via 12.1.0.252
cmdStr := "ip route" + " " + action + " " + cidr + " " + "via" + " " + gateway
cmd := exec.Command("/bin/bash", "-c", cmdStr)
if err := cmd.Run(); err != nil {
fmt.Println("Linux command exec err please check your command", err.Error())
return
}
}
func calicoBgpRouteWatcher() {
cert, err := tls.LoadX509KeyPair(calicoEtcdConfig.EtcdCert, calicoEtcdConfig.EtcdCertKey)
if err != nil {
fmt.Printf("cert failed, err:%v", err)
return
}
caData, err := ioutil.ReadFile(calicoEtcdConfig.EtcdCa)
if err != nil {
return
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(caData)
_tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: pool,
}
cfg := clientv3.Config{
Endpoints: calicoEtcdConfig.Endpoints,
DialTimeout: 5 * time.Second,
TLS: _tlsConfig,
}
clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
cli, err := clientv3.New(cfg)
if err != nil {
log.Fatal(err)
}
defer cli.Close() // make sure to close the client
// watch
rch := cli.Watch(context.Background(), "/registry/crd.projectcalico.org/blockaffinities/", clientv3.WithPrefix())
for wresp := range rch {
for _, ev := range wresp.Events {
if ev.Type == 0 { // PUT
routeHandler(ev.Kv.Value)
}
}
}
}