# EKS TiDB Image API (Golang)

This service runs inside EKS Pods and uses the Kubernetes API to read or update the `data.image` field of an existing ConfigMap (or another key via parameters).

## API Endpoints

- `GET /api/v1/configmap/image`
  - Reads the image value from ConfigMap
  - Optional query params: `namespace`, `configmap_name`, `image_key`
- `PUT /api/v1/configmap/image`
  - Updates the image value in ConfigMap
  - JSON body:
    ```json
    {
      "image": "your-new-image:tag",
      "namespace": "pool",
      "configmap_name": "pool-config",
      "image_key": "image"
    }
    ```
- `GET /api/v1/canary-vip-tidb-pool/latest-pod-image`
  - Reads the latest Pod image in namespace `canary-vip-tidb-pool`
- `GET /healthz`
  - Health check endpoint

## Local Run (Optional)

For local debugging, the service reads `~/.kube/config` (or `KUBECONFIG`).

```bash
go run ./cmd/server
```

## Multi-Arch Image (EKS Mixed Instance Types)

If your EKS nodes include both `amd64` and `arm64`, use buildx to build and push a multi-arch image:

```bash
# 1) Initialize buildx (one-time setup)
docker buildx create --name multiarch-builder --use
docker buildx inspect --bootstrap

# 2) Build and push multi-arch image
docker buildx build \
  --platform linux/amd64,linux/arm64 \
  -t docker.io/ysy593/tidb-image-api:latest \
  --push .
```

## Pre-built Image

A pre-built multi-arch image is available at:

```
docker.io/ysy593/tidb-image-api:latest
```

## One-Click Deploy Script

Use `scripts/deploy.sh` to run ECR login, multi-arch buildx build/push, and Kubernetes apply in one command.

```bash
AWS_REGION=ap-southeast-1 \
AWS_ACCOUNT_ID=xxx\
ECR_REPO=tidb-image-api \
IMAGE_TAG=v1.0.0 \
./scripts/deploy.sh
```

Common optional variables:

- `PLATFORMS` (default `linux/amd64,linux/arm64`)
- `K8S_NAMESPACE` (default `pool`)
- `SKIP_BUILD=true` (skip image build/push)
- `SKIP_APPLY=true` (skip kubectl apply)

## Kubernetes Deployment

> The target ConfigMap already exists. No ConfigMap YAML is required in this project.

### Prerequisites

- The `k8s/all.yaml` file has been updated to use the pre-built image `docker.io/ysy593/tidb-image-api:latest`

### Deployment Steps

Apply merged resources:

```bash
kubectl apply -f k8s/all.yaml
```

## Public Access IP Whitelist

Edit `loadBalancerSourceRanges` in `k8s/all.yaml` and replace with your allowed source CIDRs:

```yaml
loadBalancerSourceRanges:
  - "1.2.3.4/32"
  - "5.6.7.0/24"
```

## Default Target ConfigMap Settings

You can set default target values via environment variables in `k8s/all.yaml`:

- `TARGET_NAMESPACE` (default `pool`)
- `TARGET_CONFIGMAP` (default `pool-config`)
- `IMAGE_KEY` (default `image`)

## API Usage Example

Latest tested endpoint:

```text
http://a3a7d34128d594d2d982d2e9e82d9ed2-9ea3dce10d13a2f7.elb.us-east-1.amazonaws.com
```

```bash
# Update image
curl -X PUT "http://a3a7d34128d594d2d982d2e9e82d9ed2-9ea3dce10d13a2f7.elb.us-east-1.amazonaws.com/api/v1/configmap/image" \
  -H "Content-Type: application/json" \
  -d '{
    "namespace": "canary-vip-tidb-pool",
    "configmap_name": "serverless-pool-config",
    "image_key": "tidb-image",
    "image": "385595570414.dkr.ecr.us-west-2.amazonaws.com/tidbcloud/serverless/tidb:20260209-114bedc955cef0951cd1c3c4f971a4342e269f23"
  }'

# Query image
curl "http://a3a7d34128d594d2d982d2e9e82d9ed2-9ea3dce10d13a2f7.elb.us-east-1.amazonaws.com/api/v1/configmap/image?namespace=canary-vip-tidb-pool&configmap_name=serverless-pool-config&image_key=tidb-image"

# Get latest pod image in canary-vip-tidb-pool namespace
curl "http://a3a7d34128d594d2d982d2e9e82d9ed2-9ea3dce10d13a2f7.elb.us-east-1.amazonaws.com/api/v1/canary-vip-tidb-pool/latest-pod-image"
```

### Example Response

```json
{
  "namespace": "canary-vip-tidb-pool",
  "configmap_name": "serverless-pool-config",
  "image_key": "tidb-image",
  "image_value": "385595570414.dkr.ecr.us-west-2.amazonaws.com/tidbcloud/serverless/tidb:20260209-114bedc955cef0951cd1c3c4f971a4342e269f23"
}
```

### Latest Pod Image Response

```json
{
  "namespace": "canary-vip-tidb-pool",
  "pod_name": "tidb-cluster-xxx-0",
  "image": "385595570414.dkr.ecr.us-west-2.amazonaws.com/tidbcloud/serverless/tidb:20260209-114bedc955cef0951cd1c3c4f971a4342e269f23"
}
```
