MCPcopy
hub / github.com/fabiolb/fabio / Start

Method Start

bgp/bgp_nonwindows.go:105–165  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

103}
104
105func (bgph *BGPHandler) Start() error {
106 s := bgph.server
107 go s.Serve()
108
109 if len(bgph.config.GOBGPDCfgFile) > 0 {
110 initialCfg, err := bgpconfig.ReadConfigFile(bgph.config.GOBGPDCfgFile, "toml")
111 if err != nil {
112 // shouldn't happen if we called validate first.
113 return err
114 }
115 _, err = bgpconfig.InitialConfig(context.Background(), s, initialCfg, false)
116 if err != nil {
117 return fmt.Errorf("bgp: error initializing from gobgp config: %w", err)
118 }
119 } else {
120 // If we weren't passed a gobgp config file, configure using the values passed from the fabio
121 // config, and make sure we have a sane policy where we export our routes to peers but don't
122 // import from any peers.
123 err := bgph.startBGP(context.Background())
124 if err != nil {
125 return fmt.Errorf("bgp: error starting: %w", err)
126 }
127
128 err = bgph.setPolicies()
129 if err != nil {
130 return fmt.Errorf("bgp error setting policy: %w", err)
131 }
132
133 }
134
135 errCh := make(chan error, 1)
136 exit.Listen(func(sig os.Signal) {
137 log.Printf("[INFO] Stopping BGP")
138 err := s.StopBgp(context.Background(), &api.StopBgpRequest{})
139 errCh <- err
140 })
141
142 // monitor the change of the peer state
143 if err := s.WatchEvent(context.Background(), &api.WatchEventRequest{Peer: &api.WatchEventRequest_Peer{}}, func(r *api.WatchEventResponse) {
144 if p := r.GetPeer(); p != nil && p.Type == api.WatchEventResponse_PeerEvent_STATE {
145 log.Printf("[DEBUG] bgp event: %#v", p)
146 }
147 }); err != nil {
148 log.Printf("[ERROR] bgp watcher failed: %s", err)
149 }
150 if len(bgph.config.GOBGPDCfgFile) == 0 || len(bgph.config.Peers) > 0 {
151 // add peers
152 err := bgph.addNeighbors(context.Background(), bgph.config.Peers)
153 if err != nil {
154 return fmt.Errorf("bgp error adding neighbors: %w", err)
155 }
156 }
157 if len(bgph.config.AnycastAddresses) > 0 {
158 err := bgph.AddRoutes(context.Background(), bgph.config.AnycastAddresses)
159 if err != nil {
160 return fmt.Errorf("bgp error adding anycastaddresses: %w", err)
161 }
162 }

Callers

nothing calls this directly

Calls 6

startBGPMethod · 0.95
setPoliciesMethod · 0.95
addNeighborsMethod · 0.95
AddRoutesMethod · 0.95
ListenFunction · 0.92
ServeMethod · 0.65

Tested by

no test coverage detected