# Shared Provider

It's an [Infra API](https://github.com/tidbcloud/infra-api) provider for DBaaS shared tier.

## How to build

### Pre-build

At beginning, you should check whether you have permission to clone [Infra API](https://github.com/tidbcloud/infra-api).

```
$ git clone git@github.com:tidbcloud/infra-api.git
```

### Build in local

You should set [GOPRIVATE](https://golang.org/ref/mod#private-modules) for [Infra API](https://github.com/tidbcloud/infra-api)
to avoid downloading this module from proxy. You can run `go help private` for more help.

```
$ export GOPRIVATE=github.com/tidbcloud
```

Then you should set git config to use `ssh` instead of `https`.

```
$ git config --global url."git@github.com:tidbcloud".insteadOf "https://github.com/tidbcloud"
```

At last, just run `make build` to build binary.

```
$ make build
```

### Build in docker

We use [--ssh](https://docs.docker.com/develop/develop-images/build_enhancements/#using-ssh-to-access-private-data-in-builds) to access Infra API in docker builds.

You should check whether your public key for github has been added into ssh-agent.

```
$ ssh-add -L
```

If there is no ssh-agent(`$SSH_AUTH_SOCK` is empty), you should run it by this command.

```
$ eval $(ssh-agent)
```

Then, you can run the below command to build docker image.

```
$ make build-in-docker
```

### Build and push image to registry

After build, you can build docker image and push to remote registry

```
$ make VAR_IMG_HUB=ecr push
```

Now only `ecr` is supported.

You can also build [multi-arch image](https://docs.docker.com/desktop/multi-arch/)

```
$ make VAR_IMG_HUB=ecr VAR_PLATFORMS=linux/arm64,linux/amd64 push
```

## How to deploy

```
$ make deploy
```

## How to run e2e test in eks

### At the beginning

Build and push image to ecr

```
$ make VAR_IMG_HUB=ecr TARGETS=manager push
```

NOTE: now only manager e2e is supported

### Get dev base kubeconfig

```
$ aws eks update-kubeconfig --name dev-base-us-west-2 --region us-west-2
```

### Get dev infra api kubeconfig

```
BASE_CTX=`kubectl config get-contexts --output=name | grep dev-base-us-west-2`
```

```
$ kubectl get secret --context ${BASE_CTX} api-server-kubeconfig --template={{'.data.config'}} -n infra | base64 -d > infra-api-kubeconfig.yaml; KUBECONFIG=infra-api-kubeconfig.yaml kubectl config set-cluster infra --server=https://us-west-2.infra.shared.aws.tidbcloud.com:443
```
### Get shared tier eks kubeconfig

```
$ aws eks update-kubeconfig --name dev-us-east-1-s01 --region us-east-1
```

### Run tests

```
$ make VAR_INFRA_API_KUBECONFIG=infra-api-kubeconfig.yaml e2e
```

There are currently five variables about kubeconfig

```
VAR_BASE_KUBECONFIG: to visit base EKS, default is $HOME/.kube/config
VAR_BASE_CONTEXT: default is arn:aws:eks:us-west-2:385595570414:cluster/dev-base-us-west-2
VAR_INFRA_API_KUBECONFIG: to visit infra api, default is $HOME/.kube/config
VAR_INFRA_API_CONTEXT: default is infra-dev
VAR_EKS_KUBECONFIG: to visit shared tier EKS, default is $HOME/.kube/config
VAR_EKS_CONTEXT: default is arn:aws:eks:us-east-1:${awsAccountID}:cluster/dev-us-east-1-s01
VAR_GKE_KUBECONFIG: to visit shared tier GKE, default is $HOME/.kube/config
VAR_GKE_CONTEXT: default is (TODO)
```

### (Optional) Merge infra-api kubeconfig into $HOME/.kube/config

```
$ KUBECONFIG=$HOME/.kube/config:infra-api-kubeconfig.yaml kubectl config view --flatten --raw > kubeconfig.yaml
```

IMPORTANT: this command cannot resolve conflicts. Manually editing is recommended.
