-
Notifications
You must be signed in to change notification settings - Fork 26
/
bgp.go
118 lines (108 loc) · 3.49 KB
/
bgp.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package metal
import (
"context"
"errors"
"fmt"
"strings"
metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)
type bgp struct {
project string
client *metal.BGPApiService
k8sclient kubernetes.Interface
localASN int
bgpPass string
}
func newBGP(client *metal.BGPApiService, k8sclient kubernetes.Interface, metalConfig Config) (*bgp, error) {
b := &bgp{
client: client,
k8sclient: k8sclient,
project: metalConfig.ProjectID,
localASN: metalConfig.LocalASN,
bgpPass: metalConfig.BGPPass,
}
// enable BGP
klog.V(2).Info("bgp.init(): enabling BGP on project")
if err := b.enableBGP(); err != nil {
return nil, fmt.Errorf("failed to enable BGP on project %s: %w", b.project, err)
}
klog.V(2).Info("bgp.init(): BGP enabled")
return b, nil
}
// enableBGP enable bgp on the project
func (b *bgp) enableBGP() error {
// first check if it is enabled before trying to create it
bgpConfig, _, err := b.client.
FindBgpConfigByProject(context.Background(), b.project).
Execute()
// if we already have a config, just return
// we need some extra handling logic because the API always returns 200, even if
// not BGP config is in place.
// We treat it as valid config already exists only if ALL of the above is true:
// - no error
// - bgpConfig struct exists
// - bgpConfig struct has non-blank ID
// - bgpConfig struct does not have Status=="disabled"
if err == nil && bgpConfig != nil && bgpConfig.GetId() != "" && bgpConfig.GetStatus() != metal.BGPCONFIGSTATUS_DISABLED {
b.localASN = int(bgpConfig.GetAsn())
b.bgpPass = bgpConfig.GetMd5()
return nil
}
// we did not have a valid one, so create it
req := metal.BgpConfigRequestInput{
Asn: int64(b.localASN),
Md5: &b.bgpPass,
DeploymentType: "local",
UseCase: metal.PtrString("kubernetes-load-balancer"),
}
_, err = b.client.
RequestBgpConfig(context.Background(), b.project).
BgpConfigRequestInput(req).
Execute()
return err
}
// ensureNodeBGPEnabled check if the node has bgp enabled, and set it if it does not
func ensureNodeBGPEnabled(id string, client *metal.APIClient) error {
// if we are rnning ccm properly, then the provider ID will be on the node object
id, err := deviceIDFromProviderID(id)
if err != nil {
return err
}
// fortunately, this is idempotent, so just create
req := metal.BGPSessionInput{
AddressFamily: metal.BGPSESSIONINPUTADDRESSFAMILY_IPV4.Ptr(),
}
_, response, err := client.DevicesApi.
CreateBgpSession(context.Background(), id).
BGPSessionInput(req).
Execute()
// if we already had one, then we can ignore the error
// this really should be a 409, but 422 is what is returned
if response.StatusCode == 422 && strings.Contains(fmt.Sprintf("%s", err), "already has session") {
err = nil
}
return err
}
// getNodeBGPConfig get the BGP config for a specific node
func getNodeBGPConfig(providerID string, client *metal.APIClient) (peer *metal.BgpNeighborData, err error) {
id, err := deviceIDFromProviderID(providerID)
if err != nil {
return nil, err
}
bgpSessions, _, err := client.DevicesApi.
GetBgpNeighborData(context.Background(), id).
Execute()
if err != nil {
return nil, fmt.Errorf("failed to get device neighbours for device %s: %w", id, err)
}
bgpNeighbours := bgpSessions.GetBgpNeighbors()
// we need the ipv4 neighbour
for _, n := range bgpNeighbours {
if n.GetAddressFamily() == 4 {
return &n, nil
}
}
return nil, errors.New("no matching ipv4 neighbour found")
}