Skip to content

Instantly share code, notes, and snippets.

@ldelossa
Created February 23, 2022 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldelossa/3f35a16982eefcf894f2b0976cc34fd5 to your computer and use it in GitHub Desktop.
Save ldelossa/3f35a16982eefcf894f2b0976cc34fd5 to your computer and use it in GitHub Desktop.
package gobgp
import (
"fmt"
v2alpha1api "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
)
// workDiff is a helper structure which provides fields and a method set
// for computing a diff of work to achieve a given
// *v2alpha1api.CiliumBGPPeeringPolicy
type workDiff struct {
// incoming CiliumBGPPeeringConfigurations mapped by their
// local ASN.
seen map[int]*v2alpha1api.CiliumBGPPeeringConfiguration
// Local ASNs which BgpServers must be instantiated, configured,
// and added to the manager. Lookup peering configuration via `map`
// field.
add []int
// Local ASNs which BgpServers exist for but current policy has marked
// for removal. Lookup peering configuration va Manager's LocalASNMap
remove []int
// Local ASNs which BgpServers exist for but policy associated with server
// may have been updated and needs further evaluation. Lookup peering configuration
// via `map` field.
eval []int
}
// Diff computes the work diff for given an incoming *v2alpha1api.CiliumBGPPeeringPolicy and
// the current LocalASNMap state.
//
// Once Diff is invoked the appropriate field will contain BgpServers to add, remove, or re-evaluate
// in the workDiff's respective fields.
func (wd *workDiff) Diff(m LocalASNMap, policy *v2alpha1api.CiliumBGPPeeringPolicy) error {
if err := wd.addDiff(m, policy); err != nil {
return fmt.Errorf("encountered error creating work diff (add): %v", err)
}
if err := wd.removeDiff(m, policy); err != nil {
return fmt.Errorf("encountered error creating work diff (remove): %v", err)
}
return nil
}
// addDiff will populate the `seen` field of the workDiff, compute BgpServers which must be
// created, configured, and registered, and mark existing BgpServers for re-evaluation of their
// configuration.
//
// since addDiff populates the `seen` field of a diff, this method should always be called first
// when computing a work diff.
func (wd *workDiff) addDiff(m LocalASNMap, policy *v2alpha1api.CiliumBGPPeeringPolicy) error {
for _, config := range policy.Spec.Peers {
if _, ok := wd.seen[config.LocalASN]; !ok {
wd.seen[config.LocalASN] = &config
} else {
return fmt.Errorf("encountered duplicate local ASNs")
}
if _, ok := m[config.LocalASN]; !ok {
wd.add = append(wd.add, config.LocalASN)
} else {
wd.eval = append(wd.eval, config.LocalASN)
}
}
return nil
}
// removeDiff will populate the `remove` field of a work diff, indicating which existing
// BgpServers must disconnected and removed from the Manager.
func (wd *workDiff) removeDiff(m LocalASNMap, policy *v2alpha1api.CiliumBGPPeeringPolicy) error {
for k, _ := range m {
if _, ok := wd.seen[k]; !ok {
wd.remove = append(wd.remove, k)
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment