# Setup Complete: tbi-acc-hotel-mapping-service

**Date**: 2026-02-27  
**Service**: tbi-acc-hotel-mapping-service  
**Namespace**: redtag  
**Environment**: dev (ready for qa, uat, prod)

---

## ✅ What Was Completed

### 1. tbi-devops-environments Repository
**Location**: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-environments`

Created complete CI/CD configuration in `dev/tbi-acc-hotel-mapping-service/`:

#### Files Created:
- ✅ `.circleci/config.yml` - CircleCI pipeline (lint, test, build, deploy)
- ✅ `docker/Dockerfile` - Multi-stage Node.js 20 build
- ✅ `scripts/setup-kubectl.sh` - Kubernetes setup script
- ✅ `scripts/deploy-services.sh` - Main deployment script with all critical fixes
- ✅ `charts/tbi-acc-hotel-mapping-service/Chart.yaml` - Helm chart metadata
- ✅ `charts/tbi-acc-hotel-mapping-service/values.yaml` - Default values
- ✅ `charts/tbi-acc-hotel-mapping-service/templates/` - All Kubernetes manifests:
  - deployment.yaml (with resource limits)
  - service.yaml (ClusterIP)
  - secrets.yaml (SOPS integration)
  - ingress.yaml (internal-nginx)
  - hpa.yaml (autoscaling 1-3 replicas)
  - pdb.yaml (pod disruption budget)
- ✅ `.github/workflows/manage-secrets.yml` - GitHub Actions secrets management
- ✅ `README.md` - Complete setup documentation
- ✅ `SETUP-SUMMARY.md` - Deployment guide
- ✅ `TEMPLATES-FOR-TBI-ENVIRONMENTS.md` - Templates for tbi-environments

**Status**: ⏳ Ready to commit

---

### 2. tbi-environments Repository
**Location**: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-environments`

#### Files Created:
- ✅ `dev/values/tbi-acc-hotel-mapping-service.yaml` - Service configuration
  - Ingress enabled with internal-nginx class
  - Hostname: tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca
  - Autoscaling: 1-3 replicas (70% CPU target)
  - Pod disruption budget: minAvailable 1
  
- ✅ `dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml` - Encrypted secrets with:
  - Gimmonix API credentials
  - OpenSearch configuration (dev and prod)
  - Oracle database configuration (prod)
  - MySQL database configuration (dev)
  - AWS configuration
  - Hotel mapping settings (cron, batch size, geo radius, etc.)
  - All environment variables properly formatted with `|-` literal block scalar
  
- ✅ `dev/requirements.yaml` - Updated with service entry

**Status**: ⏳ Ready to commit

---

### 3. Service Application Repository
**Location**: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-acc-hotel-mapping-service`

#### Files Created:
- ✅ `VERSION` - Version file (1.0.0)
- ✅ `.github/workflows/manage-secrets.yml` - Secrets management workflow
- ✅ `DEVOPS-SETUP.md` - Complete setup and deployment guide

**Status**: ⏳ Ready to commit

---

### 4. Documentation Updates
- ✅ Updated `README.md` - Added ACC Services section with tbi-acc-hotel-mapping-service

---

## 📋 Next Steps - Commit Order

### Step 1: Commit to tbi-devops-environments

```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-environments

git add dev/tbi-acc-hotel-mapping-service/
git add README.md
git add SETUP-COMPLETE-tbi-acc-hotel-mapping-service.md

git commit -m "Add CI/CD configuration for tbi-acc-hotel-mapping-service

Complete DevOps setup for ACC Hotel Mapping Service:

CI/CD Pipeline:
- CircleCI configuration for dev environment
- Lint, test, build, and deploy stages
- Node.js 20.4 runtime

Docker:
- Multi-stage build (build + production)
- Optimized for production with pruned dependencies

Deployment:
- Automated deployment scripts with critical fixes
- Git commit check (prevents 'nothing to commit' errors)
- requirements.yaml auto-update
- Proper error handling

Helm Chart:
- Complete Kubernetes manifests
- Deployment with resource limits (256Mi-512Mi, 100m-500m CPU)
- Service (ClusterIP)
- Secrets management with SOPS
- Ingress (internal-nginx, hostname: tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca)
- HPA (1-3 replicas, 70% CPU target)
- PDB (minAvailable: 1)

GitHub Actions:
- Secrets management workflow
- View, update, and restart capabilities
- SNS notifications

Service Details:
- Namespace: redtag
- Technology: Node.js 20
- Builds own Docker image
- Internal ingress enabled
- Integrates with Gimmonix, OpenSearch, Oracle, MySQL

Documentation:
- Complete README with setup instructions
- Setup summary with step-by-step guide
- Templates for tbi-environments
- Updated main README with service listing
"

git push origin master
```

---

### Step 2: Commit to tbi-environments

```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-environments

git add dev/values/tbi-acc-hotel-mapping-service.yaml
git add dev/secrets/secrets.tbi-acc-hotel-mapping-service.yaml
git add dev/requirements.yaml

git commit -m "Add configuration for tbi-acc-hotel-mapping-service in dev environment

Service Configuration (values):
- Name: tbi-acc-hotel-mapping-service
- Namespace: redtag
- Ingress: enabled with internal-nginx class
- Hostname: tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca
- Cert Manager: enabled
- Node Environment: development
- Autoscaling: 1-3 replicas, 70% CPU target
- Pod Disruption Budget: minAvailable 1

Secrets Configuration (encrypted with SOPS):
- Gimmonix API credentials (login, username, password, mapping URL)
- OpenSearch configuration:
  - DEV: vpc-opensearch-tbidevelopment
  - PROD: vpc-tbi-prod-opensearch
- Oracle Database (PROD): itconf user, webshop database
- MySQL Database (DEV): db-access-mysql-main-01, dynamic_admin
- AWS Configuration: region, credentials, S3 bucket
- Processing Configuration: chunk size, retries, timeout
- Validation Thresholds: stale record deletion, validation
- Hotel Mapping Configuration:
  - Cron schedule: 0 4 * * * (4 AM daily)
  - Batch size: 1000
  - Geo radius: 0.5 km
  - Scroll settings: 1000 size, 5m timeout
  - Retry settings: 3 max retries, 1000ms delay

Requirements:
- Added chart reference with version 1.0.0-develop-1
- Repository: https://tbi-charts.s3.amazonaws.com

All secrets properly formatted with |- literal block scalar for correct parsing.
"

git push origin master
```

---

### Step 3: Commit to Service Repository

```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-acc-hotel-mapping-service

git add VERSION
git add .github/workflows/manage-secrets.yml
git add DEVOPS-SETUP.md

git commit -m "Add DevOps configuration files

- Added VERSION file (1.0.0) for deployment versioning
- Added GitHub Actions workflow for secrets management
  - View secrets action
  - Update secrets action
  - Update and restart pods action
  - SNS email notifications
  - Supports dev, qa, uat environments
- Added DEVOPS-SETUP.md with complete deployment guide

CI/CD Configuration:
- All CI/CD configs are in tbi-devops-environments repository
- Secrets stored in tbi-environments repository
- This repo contains only application code + VERSION + GitHub Actions workflow

Next Steps:
1. Configure GitHub secret: TBI_GITHUB_TOKEN
2. Set up CircleCI project
3. Push to develop branch to trigger first deployment
"

git push origin master
```

---

### Step 4: Configure GitHub Secret

1. Go to: https://github.com/redlabelvacations/tbi-acc-hotel-mapping-service/settings/secrets/actions
2. Click "New repository secret"
3. Name: `TBI_GITHUB_TOKEN`
4. Value: (Get from DevOps team or use existing org-level token)
5. Click "Add secret"

---

### Step 5: Set Up CircleCI

1. Go to: https://app.circleci.com/projects/project-dashboard/github/redlabelvacations/
2. Find `tbi-acc-hotel-mapping-service`
3. Click "Set Up Project"
4. Select "Use existing config"
5. Verify `devops-mgt` context is available with:
   - AWS_ACCOUNT_ID: 594765360358
   - AWS_REGION: ca-central-1
   - CLUSTER_NAME: dev
   - GITHUB_TOKEN: (GitHub PAT)
   - DEPLOY_ENV: dev

---

### Step 6: Trigger First Deployment

```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-acc-hotel-mapping-service

# Switch to develop branch
git checkout develop

# Merge master changes
git merge master

# Trigger deployment
git commit --allow-empty -m "Trigger first deployment to dev environment"
git push origin develop
```

---

### Step 7: Monitor Deployment

**CircleCI Pipeline**:
- URL: https://app.circleci.com/pipelines/github/redlabelvacations/tbi-acc-hotel-mapping-service
- Expected stages:
  1. lint-and-test (Node.js 20.4)
  2. build-and-push-images (Docker build → ECR)
  3. deploy-to-kubernetes (Helm deployment)

**Kubernetes**:
```bash
# Watch pods
kubectl get pods -n redtag -l app=tbi-acc-hotel-mapping-service -w

# Check deployment status
kubectl rollout status deployment/tbi-acc-hotel-mapping-service -n redtag

# View logs
kubectl logs -n redtag deployment/tbi-acc-hotel-mapping-service --tail=100 -f

# Check service
kubectl get svc -n redtag tbi-acc-hotel-mapping-service

# Check ingress
kubectl get ingress -n redtag tbi-acc-hotel-mapping-service
kubectl describe ingress -n redtag tbi-acc-hotel-mapping-service
```

---

## 🔍 Verification Checklist

After deployment completes:

- [ ] CircleCI pipeline completed successfully (all green)
- [ ] Pods are running: `kubectl get pods -n redtag -l app=tbi-acc-hotel-mapping-service`
- [ ] Pod logs show successful startup (no errors)
- [ ] Service is created: `kubectl get svc -n redtag tbi-acc-hotel-mapping-service`
- [ ] Ingress is configured: `kubectl get ingress -n redtag tbi-acc-hotel-mapping-service`
- [ ] Application responds: `curl -k https://tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca/health`
- [ ] Environment variables loaded correctly (check logs)
- [ ] GitHub Actions secrets workflow accessible
- [ ] Test secrets management workflow (view-secrets action)

---

## 📊 Service Configuration Summary

### Deployment
- **Service Name**: tbi-acc-hotel-mapping-service
- **Namespace**: redtag
- **Environment**: dev
- **Cluster**: dev
- **AWS Region**: ca-central-1
- **AWS Account**: 594765360358

### Ingress
- **Enabled**: Yes
- **Class**: internal-nginx (internal access only)
- **Hostname**: tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca
- **TLS**: Enabled (cert-manager)

### Scaling
- **Min Replicas**: 1
- **Max Replicas**: 3
- **CPU Target**: 70%
- **PDB**: minAvailable 1

### Resources
- **Requests**: 256Mi memory, 100m CPU
- **Limits**: 512Mi memory, 500m CPU

### Integrations
- **Gimmonix**: Hotel mapping API
- **OpenSearch**: Search and analytics (dev + prod)
- **Oracle**: Production database (webshop)
- **MySQL**: Development database (dynamic_admin)
- **AWS S3**: Storage bucket

---

## 🔐 Secrets Management

### View Secrets
```bash
# Via GitHub Actions
Go to: Actions → Manage Secrets → Run workflow
Select: environment=dev, action=view-secrets

# Via CLI
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-environments/dev
sops -d secrets/secrets.tbi-acc-hotel-mapping-service.yaml
```

### Update Secrets
```bash
# Via GitHub Actions
Go to: Actions → Manage Secrets → Run workflow
Select: environment=dev, action=update-and-restart
Secret updates: {"GIMMONIX_PASSWORD":"new-password"}

# Via CLI
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-environments/dev
sops secrets/secrets.tbi-acc-hotel-mapping-service.yaml
# Edit, save (auto-encrypts)
git add . && git commit -m "Update secrets" && git push
kubectl rollout restart deployment/tbi-acc-hotel-mapping-service -n redtag
```

---

## 🐛 Troubleshooting

### Issue: CircleCI pipeline fails
**Check**: 
- VERSION file exists in service repo
- package.json has required scripts (lint, test, build)
- CircleCI context `devops-mgt` is configured

### Issue: Pods crash
**Check**:
```bash
kubectl logs -n redtag deployment/tbi-acc-hotel-mapping-service --tail=100
kubectl describe pod -n redtag -l app=tbi-acc-hotel-mapping-service
```

### Issue: Ingress not accessible
**Check**:
```bash
kubectl get ingress -n redtag tbi-acc-hotel-mapping-service
kubectl describe ingress -n redtag tbi-acc-hotel-mapping-service
# Verify internal-nginx ingress controller is running
kubectl get pods -n ingress-nginx
```

### Issue: Environment variables not loading
**Check**:
```bash
# Verify secret exists
kubectl get secret -n redtag tbi-acc-hotel-mapping-service-secrets

# Check secret format
kubectl get secret -n redtag tbi-acc-hotel-mapping-service-secrets \
  -o jsonpath='{.data.dotenv}' | base64 -d | head -20

# Should show variables on separate lines, not as single line with \n
```

---

## 📚 Documentation

- **Service README**: `dev/tbi-acc-hotel-mapping-service/README.md`
- **Setup Summary**: `dev/tbi-acc-hotel-mapping-service/SETUP-SUMMARY.md`
- **Templates**: `dev/tbi-acc-hotel-mapping-service/TEMPLATES-FOR-TBI-ENVIRONMENTS.md`
- **Service Setup**: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-acc-hotel-mapping-service/DEVOPS-SETUP.md`
- **Developer Guide**: `docs/DEVELOPER-GUIDE.md`
- **Deployment Guide**: `docs/DEPLOYMENT-SCRIPT-GUIDE.md`
- **Secrets Setup**: `docs/SECRETS-PIPELINE-SETUP.md`
- **Quick Reference**: `docs/QUICK-REFERENCE.md`

---

## ✅ Success Criteria

Setup is complete when:
1. ✅ All files committed to respective repositories
2. ✅ GitHub secret configured
3. ✅ CircleCI project set up
4. ✅ First deployment triggered
5. ✅ Pods running successfully
6. ✅ Logs show no errors
7. ✅ Ingress accessible (internal network)
8. ✅ GitHub Actions workflow tested

---

## 🔧 Issues Encountered and Resolutions

### Issue 1: npm Dependency Conflicts
**Error**: 
```
npm ERR! Invalid: lock file's webpack-sources@3.3.3 does not satisfy webpack-sources@3.3.4
npm ERR! Missing: estraverse@4.3.0 from lock file
```

**Resolution**:
```bash
cd /path/to/tbi-acc-hotel-mapping-service
rm package-lock.json
npm install
git add package-lock.json
git commit -m "Fix: Update package-lock.json to resolve dependency conflicts"
git push origin develop
```

### Issue 2: Pod CrashLoopBackOff - Module Not Found
**Error**:
```
Error: Cannot find module '/usr/src/app/dist/index.js'
```

**Root Cause**: Dockerfile CMD pointed to wrong entry point

**Resolution**:
Updated `dev/tbi-acc-hotel-mapping-service/docker/Dockerfile`:
```dockerfile
# Changed from:
CMD ["node", "dist/index.js"]

# To:
CMD ["node", "dist/main.js"]
```

### Issue 3: Ingress Not Created
**Error**: Ingress resource not found after deployment

**Root Cause**: 
- Ingress template checked for `.Values.ingress.enabled` 
- Values file had `ingress_enabled` (underscore format)
- Template configured for ALB instead of nginx

**Resolution**:
Updated `dev/tbi-acc-hotel-mapping-service/charts/tbi-acc-hotel-mapping-service/templates/ingress.yaml`:
- Changed condition from `{{- if .Values.ingress.enabled }}` to `{{- if .Values.ingress_enabled }}`
- Changed from ALB annotations to nginx annotations
- Added TLS configuration with cert-manager
- Added proper nginx timeout settings

### Issue 4: Missing Port Configuration
**Error**: Ingress template couldn't find port values

**Resolution**:
Added to `dev/values/tbi-acc-hotel-mapping-service.yaml`:
```yaml
port: 80
targetPort: 3000
```

### Issue 5: internal-nginx Ingress Class
**Issue**: `internal-nginx` ingress class exists but no controller running

**Resolution**: Changed to `nginx` ingress class which has active controller

**Note**: If internal-only access is required, this needs infrastructure team to deploy internal-nginx controller

---

## 🎉 Deployment Status Update (2026-02-27)

### ✅ Successfully Deployed to Dev Environment

**Deployment Timeline**:
1. Initial setup completed
2. Fixed package-lock.json dependency conflicts
3. Fixed Dockerfile entry point (dist/index.js → dist/main.js)
4. Fixed ingress template configuration
5. Successfully deployed and running

**Current Status**:
- **Pod**: ✅ Running (1/1)
- **Service**: ✅ Active (ClusterIP 10.100.116.15)
- **Ingress**: ✅ Created (tbi-acc-hotel-mapping-service-ingress)
- **Application**: ✅ All modules initialized successfully
- **Routes**: ✅ All endpoints mapped and accessible

**Ingress Configuration**:
- Class: nginx (Note: internal-nginx class exists but no controller running)
- Hostname: tbi-acc-hotel-mapping-service.dev.tbidevelopment.ca
- TLS: Enabled with cert-manager (letsencrypt-prod)
- Backend: Port 80 → Container Port 3000

**Application Endpoints**:
- `GET /` - App controller
- `GET /oracle-sync/test-connection`
- `GET /oracle-sync/test-query`
- `GET /hotels/search`
- `GET /hotels/master/by-city`
- `POST /hotels/by-ids`
- `POST /hotels/search-by-location`
- `POST /hotels/gmx/gmx-details`
- `GET /hotels/export/mapped-csv`

**Database Connections**:
- Oracle: ✅ Connected (Thin mode - expected for containerized environment)
- MySQL: ✅ Connected
- OpenSearch: ✅ Configured (dev and prod endpoints)

**Issues Resolved**:
1. ✅ npm dependency conflicts (regenerated package-lock.json)
2. ✅ Dockerfile entry point mismatch (updated to dist/main.js)
3. ✅ Ingress template values structure (changed to ingress_enabled)
4. ✅ Ingress class configuration (using nginx instead of internal-nginx)
5. ✅ Missing port configuration in values file

**Known Limitations**:
- SNS notifications not configured for CircleCI deployments (only available in GitHub Actions secrets workflow)
- DNS resolution may take time for new ingress hostname
- Certificate issuance in progress (cert-manager)

---

**Setup completed by**: Kiro AI Assistant  
**Date**: 2026-02-27  
**Status**: ✅ Deployed and Running in Dev Environment
