# NetworkPeer

## Upstream APIs

The NetworkPeer relies on the forked Crossplane repos to create, update, delete and manage the [AWS VpcPeering connections](https://github.com/tidbcloud/provider-aws/tree/pingcap/provider-aws/pkg/controller/vpcpeering) and [GCP VpcPeering connections](https://github.com/tidbcloud/provider-gcp/tree/pingcap/provider-gcp/pkg/controller/vpcpeering), respectively.  

Therefore you might need to check the `infra-provider`, `aws-provider` and `gcp-provider` pods to trace out the detailed logs for the troubleshooting.

Examples of getting logs for the nightly environment (pods are deployed under `infra` namespace)

Check logs from the `infra-provider` pod
```
kubectl logs <infra-provider's pod name> -n infra -c manager
```
Check logs from the `provider-aws` pod 
```
kubectl logs <aws-provider's pod name> -n infra
```
Check logs from the `Central worker`
```
kubectl logs <worker pod name> -n nightly
```

Easiest way is to search and query logs from the `Loki dashboard quick search` dashboard in our Grafana instance

### Usually if a Networkpeer object is in an abnormal state, a routine check might be helpful:

Check [this document](https://github.com/tidbcloud/infra-cd/blob/main/doc/dev.md) to retrieve the infra-api's context

- Describe the Networkpeer object
```
KUBECONFIG=<path to the infra-api's context> kubectl get networkpeer <networkpeer name> -n <networkpeer's namespace> -oyaml
```
Check the logs as per this networkpeer's name
- Describe the underlying crossplane objects

aws peering object
```
KUBECONFIG=<path to the infra-api's context> kubectl get vpcpeeringconnection <aws peering object name> -oyaml
```
gcp peering object
```
KUBECONFIG=<path to the infra-api's context> kubectl get peering <gcp peering object name> -oyaml
```
Check the logs as per the peering object's name



## Controller

The implementation of  the NetworkPeer controller is stored [here](https://github.com/tidbcloud/infra-api/blob/main/controllers/networkpeer/networkpeer_controller.go). It essentially implemented the [ExternalObjClient interface](https://github.com/tidbcloud/infra-api/blob/main/controllers/reconciler/reconciler.go) to Observe, Create, Update and Delete the NetworkPeer CR.

## Webhook

The [NetworkPeer webhook](https://github.com/tidbcloud/infra-api/blob/main/api/v1/networkpeer_webhook.go) is to validate the NetworkPeer object before sending it to the controller to create. 

Currently it does the following validations:
- If the NetworkPeer is valid as defined [here](https://github.com/tidbcloud/infra-api/blob/main/api/v1/networkpeer_types.go).
- If the source and target network CIDRs are overlapped.
- Detecting invalid values when creating the peering connection, a related issue is stated [here](https://jira.tidbcloud.com/browse/DM-2667). This issue was resolved by defining a PeeringCreator interface, and implementing it in the [main func](https://github.com/tidbcloud/infra-api/blob/362f5ea83154088803bb5629de2e76a6d2fbe38c/pkg/util/util.go#L39) of the infra-api provider. As a result, the webhook would validate and create the NetworkPeer by calling the cloud APIs directly, and reject the creation if there are any invalid parameters.

## Central Integration

Basically we switched the `Central` from calling the Cloud providers’ API to the `infra-api`:
- The funcs to call the `infra-api` from the `Central` are defined [here](https://github.com/tidbcloud/dbaas/blob/master/central/infra-api/networkpeer.go).
- The integration with the `Central` backend is [here](https://github.com/tidbcloud/dbaas/blob/master/central/model/cluster/vpc_peering.go).
- The workflow for the `Central` worker to sync the DB with the `infra-api` is defined [here](https://github.com/tidbcloud/dbaas/blob/f9d652caa259e12cfb245f351cb5e56fa5379636/central/model/cluster/cluster.go#L2338).

## Unit Test

- [NetworkPeer Controller Unit Test](https://github.com/tidbcloud/infra-api/blob/main/controllers/networkpeer/networkpeer_test.go)
- [NetworkPeer Webhook Unit Test](https://github.com/tidbcloud/infra-api/blob/main/api/v1/networkpeer_webhook_test.go)
- [NetworkPeer in Central Unit Test](https://github.com/tidbcloud/dbaas/blob/master/central/infra-api/networkpeer_test.go)

## Integration Test

- [NetworkPeer Controller Integration Test](https://github.com/tidbcloud/infra-api/blob/main/test/integration/network_controller_test.go)
