Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the controller for API BGPPolicy #6203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hongliangl
Copy link
Contributor

@hongliangl hongliangl commented Apr 9, 2024

This commit implements the controller of API BGPPolicy, designed to advertise
Service IPs, Egress IPs, and Pod IPs to BGP peers from selected Nodes.

According to the spec of BGPPolicy, the Node selector is used to select Nodes
to which a BGPPolicy is applied. Multiple BGPPolicies can be applied to the
same Node. However, only one BGPPolicy can be effective on a Node, with others
serving as alternatives. The first BGPPolicy applied to a Node will be the
effective one, and the latter ones will serve as alternatives. The effective one
may be changed in the following cases:

  • The current effective BGPPolicy is updated and not applied to the Node.
  • The current effective BGPPolicy is deleted.
  • The antrea-agent is rebooted, and an original alternative BGPPolicy is synced
    first and becomes effective.

The BGP server instance is only started for the effective BGPPolicy on a Node.
Currently, the BGP server instance is implemented by goBGP, and more BGP
server implementations might be added later. If the effective BGPPolicy is changed,
the corresponding BGP server instance will be terminated by calling the Stop()
method, and a new BGP server instance will be created and started by calling the
Start() method for the new effective BGPPolicy.

To start a BGP server instance, ASN, routerID, and listen port must be specified.
ASN and listen port are specified in the spec of the effective BGPPolicy. For
routerID, if the cluster is IPv4 only or dual stack, the IPv4 NodeIP is used as
the routerID; if the cluster is IPv6 only, the routerID must be specified in the
Node annotation antrea.io/bgp-route-id. Additionally, a new BGP server instance
should be created and started when any of ASN, routerID, or listen port changes.

The information of the BGP peers is specified in the effective BGPPolicy. The
unique identification of a BGP peer is the peer IP address.

To reconcile the latest BGP peers:

  • Get the BGP peers to be added and add them by calling the AddPeers method of
    the BGP server instance.
  • Get the BGP peers to be deleted and delete them by calling the RemovePeers
    method of the BGP server instance.
  • Get the remaining BGP peers and calculate the updated BGP peers, then update
    them by calling the UpdatePeers method of the BGP server instance.

The information of the IPs to be advertised can be calculated from the spec of
the effective BGPPolicy. Currently, we advertise the IPs and CIDRs to all the
BGP peers.

To reconcile the latest IPs to all BGP peers:

  • If the BGP server instance is newly created and started, advertise all the IPs
    by calling the AdvertiseRoutes method.
  • If the BGP server instance is not newly created and started:
    • Get the IPs/CIDRs to be added and advertise them by calling the
      AdvertiseRoutes method.
    • Get the IPs/CIDRs to be removed and withdraw them by calling the
      WithdrawRoutes method.

The feature is gated by the alpha BGPPolicy feature gate.

Depends on #6009

  • goBGP integration tests
  • e2e tests
  • unit tests
  • refine code comments
  • documents
  • goBGP logger

@hongliangl hongliangl added kind/feature Categorizes issue or PR as related to a new feature. action/release-note Indicates a PR that should be included in release notes. labels Apr 9, 2024
@rajnkamr rajnkamr added the area/transit/bgp Issues or PRs related to BGP support. label Apr 11, 2024
@hongliangl hongliangl added this to the Antrea v2.1 release milestone May 24, 2024
@hongliangl hongliangl force-pushed the 20240222-bgp-controller-v2 branch 2 times, most recently from f209e2a to 3063737 Compare May 30, 2024 10:40
@hongliangl hongliangl force-pushed the 20240222-bgp-controller-v2 branch 3 times, most recently from 57773d9 to c9b727a Compare June 6, 2024 12:25
@hongliangl hongliangl force-pushed the 20240222-bgp-controller-v2 branch 11 times, most recently from 095d7da to 5086b34 Compare June 19, 2024 07:23
@hongliangl hongliangl marked this pull request as ready for review June 19, 2024 07:26
@hongliangl hongliangl force-pushed the 20240222-bgp-controller-v2 branch 2 times, most recently from 3bab09b to 69c31a2 Compare June 20, 2024 05:55
@hongliangl hongliangl changed the title Add BGPPolicy controller Implement the controller for API BGPPolicy Jun 20, 2024
@luolanzone
Copy link
Contributor

Hi @hongliangl could you explain more about advertise Service IPs, Egress IPs, and Pod IPs to BGP peers from selected Nodes.? According to the API, there is enum of ipTypes, Does it mean users can choose what's kind of Service can be advertised? Will there be a default one? It would be better to provide a sample BGPPolicy and explain the behavior to help reviewers. Thanks.

@hongliangl
Copy link
Contributor Author

Hi @hongliangl could you explain more about advertise Service IPs, Egress IPs, and Pod IPs to BGP peers from selected Nodes.? According to the API, there is enum of ipTypes, Does it mean users can choose what's kind of Service can be advertised? Will there be a default one? It would be better to provide a sample BGPPolicy and explain the behavior to help reviewers. Thanks.

will add

bgpPolicy:
# The name of the Secret storing passwords of the BGP peers. For each BGP peer, the Secret key is generated by
# concatenating its IP address and AS number, e.g., `192.168.1.1-65521`.
secretName: {{ .Values.bgpPolicy.secretName | quote }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we allow user to change this Secret name? I mean how would Antrea guarantee the ClusterRole being updated after this is changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If users only modify the name but don't update the ClusterRole, we don't have a good solution to resolve that. The error logs may help users. What we can do is to document it well to remind users. I will refine the comment here to remind users about that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other similar cases, we just hardcode the secret name and grant the secret's permission to agent, and no one has ever complained it. I think it could follow that, instead of making the secret name configurable.

pkg/agent/controller/networkpolicy/l7engine/reconciler.go Outdated Show resolved Hide resolved
}

// Stop the stale BGP server if it exists.
if bpState.bgpServer != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to stop stale BGP server after new BGP server? Won't be port conflict if other configs changes but not port?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I should stop the server first.

pkg/agent/controller/bgp/controller.go Outdated Show resolved Hide resolved
pkg/agent/controller/bgp/controller.go Outdated Show resolved Hide resolved
pkg/agent/controller/bgp/controller.go Show resolved Hide resolved

func (c *Controller) getRouterID() (string, error) {
var routerID string
// For IPv6 only environment, the BGP routerID should be specified by K8s Node annotation `antrea. io/ bgp-route-id`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// For IPv6 only environment, the BGP routerID should be specified by K8s Node annotation `antrea. io/ bgp-route-id`.
// For IPv6 only environment, the BGP routerID should be specified by K8s Node annotation `antrea.io/bgp-route-id`.

Question: if this is provided by users, how to avoid duplicate IDs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot avoid duplicate IDs.

This commit implements the controller of API `BGPPolicy`, designed to advertise
Service IPs, Egress IPs, and Pod IPs to BGP peers from selected Nodes.

According to the spec of `BGPPolicy`, the Node selector is used to select Nodes
to which a `BGPPolicy` is applied. Multiple `BGPPolicies` can be applied to the
same Node. However, only one `BGPPolicy` can be effective on a Node, with others
serving as alternatives. The first `BGPPolicy` applied to a Node will be the
effective one, and the latter ones will serve as alternatives. The effective one
may be changed in the following cases:

- The current effective BGPPolicy is updated and not applied to the Node.
- The current effective BGPPolicy is deleted.
- The antrea-agent is rebooted, and an original alternative BGPPolicy is synced
  first and becomes effective.

The BGP server instance is only started for the effective BGPPolicy on a Node.
If the effective BGPPolicy is changed, the corresponding BGP server instance will
be terminated by calling the `Stop` method, and a new BGP server instance will
be created and started by calling the `Start` method for the new effective
BGPPolicy.

To start a BGP server instance, ASN, routerID, and listen port must be specified.
ASN and listen port are specified in the spec of the effective BGPPolicy. For
routerID, if the cluster is IPv4 only or dual stack, the IPv4 NodeIP is used as
the routerID; if the cluster is IPv6 only, the routerID must be specified in the
Node annotation `antrea.io/bgp-route-id`. Additionally, a new BGP server instance
should be created and started when any of ASN, routerID, or listen port changes.

The information of the BGP peers is specified in the effective BGPPolicy. The
unique identification of a BGP peer is the peer IP address.

To reconcile the latest BGP peers:

- Get the BGP peers to be added and add them by calling the `AddPeer` method of
  the BGP server instance.
- Get the BGP peers to be deleted and delete them by calling the `RemovePeer`
  method of the BGP server instance.
- Get the remaining BGP peers and calculate the updated BGP peers, then update
  them by calling the `UpdatePeer` method of the BGP server instance.

The information of the IPs to be advertised can be calculated from the spec of
the effective BGPPolicy. Currently, we advertise the IPs and CIDRs to all the
BGP peers.

To reconcile the latest IPs to all BGP peers:

- If the BGP server instance is newly created and started, advertise all the IPs
  by calling the `AdvertiseRoutes` method.
- If the BGP server instance is not newly created and started:
  - Get the IPs/CIDRs to be added and advertise them by calling the
    `AdvertiseRoutes` method.
  - Get the IPs/CIDRs to be removed and withdraw them by calling the
    `WithdrawRoutes` method.

The feature is gated by the alpha `BGPPolicy` feature gate.

Signed-off-by: Hongliang Liu <lhongliang@vmware.com>
Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't finished reviewing all code, but I feel the patch spends too many words and code for the unexpected case: multiple policies applied to a Node, making it like a normal case.
It's not a meaningful configuration and we should discourage users to do it, and there is no need to explain how we deal with such configurations in detail. "We could just have a note for users: If more than one BGPPolicy applies to a Node, only one will take effect."

bgpPolicy:
# The name of the Secret storing passwords of the BGP peers. For each BGP peer, the Secret key is generated by
# concatenating its IP address and AS number, e.g., `192.168.1.1-65521`.
secretName: {{ .Values.bgpPolicy.secretName | quote }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other similar cases, we just hardcode the secret name and grant the secret's permission to agent, and no one has ever complained it. I think it could follow that, instead of making the secret name configurable.

@@ -0,0 +1,6 @@
apiVersion: v1
kind: Secret
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why it needs to create the secret in advance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that we should let users create the Secret? Is it a convention or something else?

@@ -31,7 +31,7 @@ edit the Agent configuration in the
## List of Available Features

| Feature Name | Component | Default | Stage | Alpha Release | Beta Release | GA Release | Extra Requirements | Notes |
| ----------------------------- | ------------------ | ------- | ----- | ------------- | ------------ | ---------- | ------------------ | --------------------------------------------- |
|-------------------------------| ------------------ | ------- | ----- |---------------| ------------ | ---------- |--------------------| --------------------------------------------- |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure which is correct, having space or not having space. But this change makes them inconsistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIll rever this.

)

const (
bgpRouteIDAnnotation = "antrea.io/bgp-route-id"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have comment to understand why we need annotation for IPv6 in another thread. But if this is really needed, please move it to pkg/agent/types/annotations.go and name it "bgp.antrea.io/router-id" (it should be router, not route, right?)

pkg/agent/controller/bgp/controller.go Show resolved Hide resolved
Comment on lines +81 to +83
newBGPServerFn = func(globalConfig *bgp.GlobalConfig) bgp.Interface {
return gobgp.NewGoBGPServer(globalConfig)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
newBGPServerFn = func(globalConfig *bgp.GlobalConfig) bgp.Interface {
return gobgp.NewGoBGPServer(globalConfig)
}
newBGPServerFn = gobgp.NewGoBGPServer

And I think it's better to make it a member variable of Controller and assign gobgp.NewGoBGPServer to it by default, then you don't need to revert it in tests.

Comment on lines +102 to +103
// peers maps IP families to concatenated strings of BGP peer IP addresses and ASNs.
// Example: "192.168.77.100-65000", "2001::1-65000".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update.

Comment on lines +104 to +106
peerKeys sets.Set[string]
// peerConfigs maps concatenated string of BGP peer IP addresses and ASN to the configuration of the peer.
peerConfigs map[string]bgp.PeerConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't peerKeys redundant with peerConfigs' keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

peerKeys can be removed. If so, we still need to collect all the keys of the current and the stale peerConfigs to two sets, and then get the peers to be added or deleted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be easily got by sets.KeySet(peerConfigs), it's not a common "optimization" to use a separate set to store another map's keys, which may not be even more efficient as it still needs to maintain an extra set when writing.

Comment on lines +305 to +307
for i := 0; i < defaultWorkers; i++ {
go wait.Until(c.worker, time.Second, stopCh)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point to process multiple policies in parallel and have many code to handle the race condition.
In normal scenario, we expect only zero or single policy applied to the Node, we should just get one policy and implement it, instead of dealing with multiple policies like it's a normal case. If all jobs of implementing a single policy should be done sequentially, it just needs a single worker with a dummy key.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The worker's implementation in my mind:

func syncBGPPolicy() error {
  // Get 0 or 1 policy applied to the Node.
  bgpPolicy, exists := c.getBGPPolicy() 
  if !exists {
    // cleanup
  }
  // Implement the bgpPolicy
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should we choose a BGPPolicy if users apply multiple ones to a Node anyway?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it hard to get one item from a list of items?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/release-note Indicates a PR that should be included in release notes. area/transit/bgp Issues or PRs related to BGP support. kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants