# Import

## Controller

The Import controller implemented the [ExternalObjClient interface](https://github.com/tidbcloud/infra-api/blob/main/controllers/reconciler/reconciler.go) to Observe, Create, Update and Delete the Import CR.

An Import object is essentially a [Lightning](https://docs.pingcap.com/tidb/stable/tidb-lightning-overview) task which includes the pvc and secret to be used by this task, a Kubernetes Job and a Kubernetes Service to carry on this task within the Kubernetes cluster. The implementation is [here](https://github.com/tidbcloud/infra-api/blob/362f5ea83154088803bb5629de2e76a6d2fbe38c/controllers/imports/import_controller.go#L304). 

In order to set up a Lightning task, the [Lightning configuration](https://github.com/tidbcloud/infra-api/blob/362f5ea83154088803bb5629de2e76a6d2fbe38c/controllers/imports/import.go#L215) has to be generated.



## Central Integration

We switched the `Central` from calling the Cloud providers’ API to the `infra-api`:
- The definitions of the funcs to call the `infra-api` from the `Central` are [here](https://github.com/tidbcloud/dbaas/blob/master/central/infra-api/import.go).
- The integration with the `Central` backend is [here](https://github.com/tidbcloud/dbaas/blob/master/central/model/import/import.go).
- The workflow for the `Central` worker to sync the DB with `infra-api` is [here](https://github.com/tidbcloud/dbaas/blob/f9d652caa259e12cfb245f351cb5e56fa5379636/central/model/import/import.go#L611). A related issue to the sync func,which  was caused by the racing on access to the import DB tables. The fix for this issue is stated [this PR](https://github.com/tidbcloud/dbaas/pull/4485).

### The routine check for troubleshooting an import issue with the `Central`:

Check the workers' logs
```
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
## GitOps Integration

The PR is yet to be merged.

## Unit Test

- [Import Controller Unit Test](https://github.com/tidbcloud/infra-api/blob/main/controllers/imports/import_test.go)
- [Import in Central Unit Test](https://github.com/tidbcloud/dbaas/blob/master/central/infra-api/import_test.go)

## Integration Test

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