# ArgoCD Deployment

This directory contains ArgoCD Application manifests for deploying Peikarband to Kubernetes.

## Files

- `application.yaml`: Production deployment (main branch → peikarband namespace)
- `application-staging.yaml`: Staging deployment (develop branch → peikarband-staging namespace)

## Prerequisites

1. ArgoCD installed in your cluster
2. Git repository access configured in ArgoCD
3. Docker registry credentials (if using private registry)

## Deployment

### 1. Add Git Repository to ArgoCD

```bash
# For HTTPS with token
argocd repo add https://git.peikarband.ir/ehsan-minadd/peikarband.git \
  --username YOUR_USERNAME \
  --password YOUR_ACCESS_TOKEN

# Or using argocd UI: Settings → Repositories → Connect Repo
```

### 2. Deploy Production

```bash
kubectl apply -f argocd/application.yaml
```

### 3. Deploy Staging

```bash
kubectl apply -f argocd/application-staging.yaml
```

## Sync Policy

Both applications use **automatic sync** with:
- **Auto-prune**: Remove resources deleted from Git
- **Self-heal**: Automatically sync when cluster state differs from Git
- **Retry logic**: 5 attempts with exponential backoff

## Monitoring

```bash
# Check application status
argocd app get peikarband
argocd app get peikarband-staging

# Watch sync progress
argocd app sync peikarband --watch

# View logs
argocd app logs peikarband
```

## Manual Sync

```bash
# Force sync
argocd app sync peikarband --force

# Sync with prune
argocd app sync peikarband --prune
```

## Rollback

```bash
# List history
argocd app history peikarband

# Rollback to specific revision
argocd app rollback peikarband <REVISION>
```

## Architecture

```
┌─────────────────────────────────────────────────┐
│                    ArgoCD                        │
│  ┌───────────────────┐  ┌──────────────────┐   │
│  │  Production App   │  │   Staging App    │   │
│  │  (main branch)    │  │ (develop branch) │   │
│  └─────────┬─────────┘  └────────┬─────────┘   │
└────────────┼─────────────────────┼──────────────┘
             │                     │
             ▼                     ▼
    ┌────────────────┐    ┌─────────────────┐
    │   namespace:   │    │    namespace:   │
    │   peikarband   │    │ peikarband-stg  │
    └────────────────┘    └─────────────────┘
```

## Environment Variables

Override via Helm values:

```yaml
# In values-production.yaml or values-staging.yaml
env:
  - name: DATABASE_URL
    value: "postgresql://..."
  - name: REDIS_URL
    value: "redis://..."
```

## Secrets Management

Secrets should be managed outside Git:

```bash
# Using kubectl
kubectl create secret generic peikarband-secrets \
  --from-literal=database-password=xxx \
  --namespace=peikarband

# Or using Sealed Secrets, External Secrets Operator, etc.
```

## Troubleshooting

### Application Out of Sync

```bash
argocd app sync peikarband --force
```

### Image Pull Errors

Check registry credentials:
```bash
kubectl get secret regcred -n peikarband -o yaml
```

### Health Check Failing

View pod logs:
```bash
kubectl logs -n peikarband -l app=peikarband --tail=100
```

### Helm Values Override Not Working

Verify values file path in Application manifest:
```bash
argocd app manifests peikarband | grep valueFiles
```

