# AWS Shared Provider

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

## How to build

### Pre-build

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

```bash
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.

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

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

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

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

```bash
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.

```bash
ssh-add -L
```

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

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

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

```bash
make build-in-docker
```

### Build and push image to registry

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

```bash
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/)

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

## How to run e2e test in eks

### At the beginning

Build and push image to ecr

```bash
make VAR_IMG_HUB=ecr VAR_PLATFORMS=linux/arm64 TARGETS=manager push
make VAR_IMG_HUB=ecr VAR_PLATFORMS=linux/arm64 TARGETS=tidb-gateway push
```

### Get dev base kubeconfig

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

### Get dev infra api kubeconfig

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

```bash
kubectl get secret --context ${BASE_CTX} gardener-provider-infra-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 free tier eks kubeconfig

```bash
aws eks update-kubeconfig --name dev-us-east-1-f01 --region us-east-1
```

### Run tests

```bash
make VAR_INFRA_KUBECONFIG=infra-api-kubeconfig.yaml e2e
```

There are currently five variables about kubeconfig

```bash
VAR_KUBECONFIG: to visit free tier EKS, default is $HOME/.kube/config
VAR_CONTEXT: default is arn:aws:eks:us-east-1:${awsAccountID}:cluster/dev-us-east-1-f01
VAR_INFRA_KUBECONFIG: to visit free tier EKS, default is $HOME/.kube/config
VAR_INFRA_CONTEXT: default is infra-dev
VAR_BASE_CONTEXT: default is arn:aws:eks:us-west-2:385595570414:cluster/dev-base-us-west-2
```

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

```bash
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.

### (Optional) Skip re-deploy manager

Sometimes problems are produced by e2e program itself. You may hope to only re-run e2e but not all components.

```bash
SKIP_DEPLOY=1 make e2e
```

### (Optional) Focus on specific test specs

To focus on specific tests, use the FOCUS as follows:

```bash
# Runs all tests match the "cluster" Describe block
$ FOCUS=cluster make e2e
```

### (Optional) Custom cluster name

Sometimes problems happended in test with default cluster name (your git username),
you can re-run in a clean test env with custom cluster name

```bash
RANDOM_CLUSTER={custom name} make e2e
```

### Clean up e2e test env manually

```bash
CLEANUP=1 make e2e
```

if you have run e2e with custom cluster name, then clean up with the name

```bash
RANDOM_CLUSTER={custom name} CLEANUP=1 make e2e
```
