Skip to content

GitOps configuration repo demonstrating selective syncing with ArgoCD ApplicationSets. Manages Kubernetes apps across dev/staging/production environments where changes only trigger deployments for affected applications. Includes helper scripts, multi-env manifests, and comprehensive docs.

License

Notifications You must be signed in to change notification settings

dotbrains/argocd-selective-sync

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

ArgoCD Application Configuration with Per-App Hooks

This repository contains Kubernetes manifests and ArgoCD application configuration for deploying applications using GitOps practices with ArgoCD. It implements per-application selective syncing with dedicated post-sync hooks, ensuring that changes to a specific app only trigger syncs and validations for that individual application.

๐Ÿ“– For detailed information about per-app selective syncing, see SELECTIVE_SYNC_README.md

๐Ÿ“ Repository Structure

.
โ”œโ”€โ”€ README.md                           # This documentation
โ”œโ”€โ”€ SELECTIVE_SYNC_README.md            # Detailed per-app sync documentation
โ”œโ”€โ”€ application.yaml                    # ArgoCD ApplicationSet (manages all apps)
โ”œโ”€โ”€ apps/                              # Per-app ArgoCD application definitions
โ”‚   โ”œโ”€โ”€ dev/
โ”‚   โ”‚   โ”œโ”€โ”€ demo-app.yaml             # Dev demo-app with custom post-sync hook
โ”‚   โ”‚   โ””โ”€โ”€ api-service.yaml          # Dev api-service with custom post-sync hook
โ”‚   โ”œโ”€โ”€ staging/
โ”‚   โ”‚   โ”œโ”€โ”€ demo-app.yaml             # Staging demo-app with enhanced validation
โ”‚   โ”‚   โ””โ”€โ”€ api-service.yaml          # Staging api-service with validation
โ”‚   โ””โ”€โ”€ production/
โ”‚       โ”œโ”€โ”€ demo-app.yaml             # Production demo-app with comprehensive checks
โ”‚       โ””โ”€โ”€ api-service.yaml          # Production api-service with load balancer validation
โ”œโ”€โ”€ environments/                      # Environment-specific manifests
โ”‚   โ”œโ”€โ”€ dev/                          # Development environment
โ”‚   โ”‚   โ”œโ”€โ”€ demo-app/                 # Demo application
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ deployment.yaml      # Kubernetes Deployment
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ service.yaml         # Kubernetes Service
โ”‚   โ”‚   โ””โ”€โ”€ api-service/              # API service application
โ”‚   โ”‚       โ”œโ”€โ”€ deployment.yaml      # Kubernetes Deployment
โ”‚   โ”‚       โ””โ”€โ”€ service.yaml         # Kubernetes Service
โ”‚   โ”œโ”€โ”€ staging/                      # Staging environment
โ”‚   โ”‚   โ”œโ”€โ”€ demo-app/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ deployment.yaml
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ service.yaml
โ”‚   โ”‚   โ””โ”€โ”€ api-service/
โ”‚   โ”‚       โ”œโ”€โ”€ deployment.yaml
โ”‚   โ”‚       โ””โ”€โ”€ service.yaml
โ”‚   โ””โ”€โ”€ production/                   # Production environment
โ”‚       โ”œโ”€โ”€ demo-app/
โ”‚       โ”‚   โ”œโ”€โ”€ deployment.yaml
โ”‚       โ”‚   โ””โ”€โ”€ service.yaml
โ”‚       โ””โ”€โ”€ api-service/
โ”‚           โ”œโ”€โ”€ deployment.yaml
โ”‚           โ””โ”€โ”€ service.yaml
โ””โ”€โ”€ scripts/                           # Helpful management scripts
    โ”œโ”€โ”€ README.md                      # Scripts documentation
    โ”œโ”€โ”€ argocd-helper.sh               # Main helper script (recommended)
    โ”œโ”€โ”€ install-argocd.sh              # Install ArgoCD
    โ”œโ”€โ”€ deploy-applications.sh         # Deploy ApplicationSet
    โ”œโ”€โ”€ monitor-environments.sh        # Monitor per-app applications
    โ”œโ”€โ”€ add-environment.sh             # Add new environments (per-app structure)
    โ”œโ”€โ”€ cleanup-environments.sh        # Clean up per-app applications
    โ”œโ”€โ”€ test-per-app-hooks.sh          # Demo per-app hook behavior
    โ””โ”€โ”€ reset-argocd.sh                # Reset ArgoCD to clean state

๐Ÿš€ What is ArgoCD?

ArgoCD is a declarative, GitOps continuous delivery tool for Kubernetes. It follows the GitOps pattern of using Git repositories as the source of truth for defining the desired application state.

๐Ÿ“‹ Prerequisites

  • Kubernetes cluster (local or remote)
  • kubectl configured to access your cluster
  • ArgoCD installed on your cluster

๐Ÿ› ๏ธ Installation & Setup

1. Install ArgoCD

# Create argocd namespace
kubectl create namespace argocd

# Install ArgoCD
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

# Wait for ArgoCD to be ready
kubectl wait --for=condition=available --timeout=300s deployment/argocd-server -n argocd

2. Access ArgoCD UI

# Get ArgoCD services
kubectl get svc -n argocd

# Port forward to access UI locally
kubectl port-forward svc/argocd-server -n argocd 8080:443

Open your browser and navigate to https://localhost:8080

3. Get Admin Password

# Get the initial admin password
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 --decode && echo

Login Credentials:

  • Username: admin
  • Password: (output from the command above)

4. Deploy ApplicationSet

Option A: Using the Helper Script (Recommended)

# Make scripts executable
chmod +x scripts/*.sh

# Deploy ApplicationSet and monitor creation
./scripts/argocd-helper.sh deploy

Option B: Manual Deployment

# Apply the ArgoCD ApplicationSet
kubectl apply -f application.yaml

๐Ÿ”ง Configuration Details

ArgoCD ApplicationSet (application.yaml)

This file defines an ApplicationSet that manages multiple applications with per-app selective syncing:

  • Generator: Uses matrix generator to create apps from environment ร— service combinations
  • Source Repository: Points to this GitHub repository
  • Per-App Targeting: Each ArgoCD Application watches only its specific path (environments/dev/demo-app/, etc.)
  • Namespaces: Each app deploys to its own namespace (dev-demo-app, dev-api-service, etc.)
  • Environment Labels: Applications are labeled with environment and service for easy filtering
  • Individual Sync Policies: Each app can have different sync policies and automation settings

Per-App Manifests

  • Demo App: Web application with different replica counts per environment (dev: 2, staging: 3, production: 5)
  • API Service: Backend service with environment-specific configurations and scaling
  • Environment Variations: Each environment has different resource limits, service types, and configurations

Per-App Post-Sync Hooks

Each individual application has its own custom post-sync validation:

  • Dev Apps: Basic health checks with quick validation
  • Staging Apps: Enhanced validation with longer wait times
  • Production Apps: Comprehensive validation with multiple checks, longer timeouts, and more retry attempts

Per-App Selective Sync Benefits

โœ… Ultimate Isolation: Changes to one app won't trigger syncs for any other app

โœ… Granular Control: Each app has its own sync policies, hooks, and validation logic

โœ… Resource Efficiency: Only the changed app runs validation/testing

โœ… Targeted Debugging: Hook failures are clearly tied to specific apps

โœ… Custom Validation: Each app type can have different testing requirements

โœ… Parallel Execution: Multiple apps can validate simultaneously when needed

๐ŸŽฏ Usage

Per-App Selective Updates

  1. Make changes to manifests in a specific app directory (environments/dev/demo-app/, environments/production/api-service/, etc.)
  2. Commit and push changes to this repository
  3. Only the affected app syncs - all other apps remain untouched
  4. Only that app's post-sync hook runs - targeted validation
  5. Monitor the deployment in the ArgoCD UI

Example Workflows

# Update only dev demo-app
vim environments/dev/demo-app/deployment.yaml
git add environments/dev/demo-app/
git commit -m "Scale dev demo-app to 3 replicas"
git push
# Result: Only dev-demo-app syncs and runs dev-demo-app-post-sync hook
#         dev-api-service and all other apps remain untouched

# Update only production api-service
vim environments/production/api-service/deployment.yaml
git add environments/production/api-service/
git commit -m "Update production api-service image"
git push
# Result: Only production-api-service syncs and runs comprehensive validation
#         production-demo-app and all other apps remain untouched

# Update same app across environments
vim environments/staging/demo-app/service.yaml
vim environments/production/demo-app/service.yaml
git add environments/staging/demo-app/ environments/production/demo-app/
git commit -m "Update demo-app service configuration"
git push
# Result: staging-demo-app and production-demo-app sync independently
#         All api-service apps in all environments remain untouched

๐ŸŽฅ Demo Per-App Hook Behavior

# Run the demo script to see exactly how per-app hooks work
./test-per-app-hooks.sh

This script shows you exactly which applications sync and which post-sync hooks run for different change scenarios.

๐Ÿ“Š Monitoring

Check Application Status

# Check all ArgoCD applications managed by the ApplicationSet
kubectl get applications -n argocd

# Check specific app deployments
kubectl get all -n dev-demo-app
kubectl get all -n dev-api-service
kubectl get all -n staging-demo-app
kubectl get all -n staging-api-service
kubectl get all -n production-demo-app
kubectl get all -n production-api-service

Monitor Per-App Post-Sync Hooks

# Check post-sync hook jobs for specific apps
kubectl get jobs -n dev-demo-app
kubectl get jobs -n production-api-service

# Check hook execution logs
kubectl logs -n dev-demo-app job/dev-demo-app-post-sync
kubectl logs -n production-api-service job/production-api-service-validation

# Watch hook execution in real-time
kubectl logs -f -n staging-demo-app job/staging-demo-app-post-sync

Access Applications

# Access demo-app in different environments
kubectl port-forward svc/argocd-demo-app-service -n dev-demo-app 8080:8080
kubectl port-forward svc/argocd-demo-app-service -n staging-demo-app 8081:8080
kubectl port-forward svc/argocd-demo-app-service -n production-demo-app 8082:8080

# Access api-service in different environments
kubectl port-forward svc/api-service -n dev-api-service 8090:80
kubectl port-forward svc/api-service -n staging-api-service 8091:80
kubectl port-forward svc/api-service -n production-api-service 8092:80

๐ŸŽฏ Per-App Selective Sync in Action

This repository implements per-application selective syncing - the ultimate improvement over traditional GitOps setups:

Traditional Approach Environment-Level Selective Sync Per-App Selective Sync (This Repo)
โŒ Single app watches entire repo โœ… Multiple apps watch environment paths ๐ŸŽ† Each app watches only its own path
โŒ Changes anywhere trigger all syncs โœ… Only affected environments sync ๐ŸŽ† Only affected app syncs
โŒ All hooks fire on any change โœ… Environment-specific hooks only ๐ŸŽ† App-specific hooks only
โŒ High resource usage and noise โœ… Moderate efficiency ๐ŸŽ† Maximum efficiency and precision
โŒ Difficult debugging โœ… Environment-level debugging ๐ŸŽ† App-level precision debugging

๐ŸŽ† Example: The Power of Per-App Hooks

Scenario: You have 10 microservices across 3 environments (30 total applications)

  • Traditional: Update 1 service โ†’ 30 apps sync + 30 post-sync hooks run ๐Ÿ˜ฑ
  • Environment-Level: Update 1 service โ†’ 3 apps sync + 3 hooks run ๐Ÿ˜
  • Per-App (This Repo): Update 1 service โ†’ 1 app syncs + 1 hook runs ๐ŸŽ‰

๐Ÿ“š Learn more: See SELECTIVE_SYNC_README.md for detailed implementation guide

๐Ÿ› ๏ธ Helpful Scripts

This repository includes a comprehensive set of scripts to make managing your ArgoCD setup easy:

๐ŸŽฏ Quick Commands

# Make scripts executable (first time only)
chmod +x scripts/*.sh

# Demo per-app hook behavior
./scripts/test-per-app-hooks.sh

# Reset ArgoCD to clean state (removes all applications)
./scripts/reset-argocd.sh

# Main helper script (shows all available commands)
./scripts/argocd-helper.sh help

# Quick status check for all apps
./scripts/argocd-helper.sh status

# Monitor all applications
./scripts/argocd-helper.sh monitor

# Add a new environment (will create apps for each service)
./scripts/argocd-helper.sh add-env qa --replicas 3

๐Ÿ“ Available Scripts

Script Purpose Example
argocd-helper.sh Main entry point ./scripts/argocd-helper.sh help
install-argocd.sh Install ArgoCD ./scripts/install-argocd.sh
deploy-applications.sh Deploy ApplicationSet ./scripts/deploy-applications.sh
monitor-environments.sh Monitor per-app sync status ./scripts/monitor-environments.sh --watch
add-environment.sh Add new environment (per-app) ./scripts/add-environment.sh qa --replicas 3
cleanup-environments.sh Clean up per-app resources ./scripts/cleanup-environments.sh dev-demo-app
test-per-app-hooks.sh Demo per-app hook behavior ./scripts/test-per-app-hooks.sh
reset-argocd.sh Reset ArgoCD to clean state ./scripts/reset-argocd.sh

๐Ÿ“„ Full documentation: See scripts/README.md for complete usage guide

๐Ÿ”„ Reset ArgoCD to Clean State

If you need to start fresh or clean up all applications, use the reset script:

# Reset ArgoCD to clean state (removes all applications and namespaces)
./scripts/reset-argocd.sh

What the Reset Script Does:

  • โ— Removes all ApplicationSets (both old and new)
  • โ— Deletes all ArgoCD Applications
  • โ— Cleans up all application namespaces
  • โœ… Preserves ArgoCD core components (server, controller, repo-server)
  • โœ… Keeps ArgoCD configuration and secrets

When to Use Reset:

  • ๐Ÿ†• Development: Clean up after testing different configurations
  • ๐Ÿ“ฆ Migration: Moving from old environment-based to new per-app structure
  • ๐Ÿ›ฎ Troubleshooting: Start fresh when applications are stuck or corrupted
  • ๐Ÿงน Maintenance: Periodic cleanup of unused applications

After Reset:

Your ArgoCD will be in a clean state, ready to:

  • Deploy new ApplicationSets with ./scripts/deploy-applications.sh
  • Access ArgoCD UI with kubectl port-forward svc/argocd-server -n argocd 8080:443
  • Create new applications using your per-app structure

๐ŸŽ† Per-App Features Highlight

This setup provides the ultimate GitOps granularity:

๐ŸŽฏ Individual Application Control

  • 6 separate ArgoCD Applications: dev-demo-app, dev-api-service, staging-demo-app, staging-api-service, production-demo-app, production-api-service
  • Independent sync policies: Dev apps auto-heal, production apps require manual approval for safety
  • Isolated namespaces: Each app deploys to its own Kubernetes namespace

๐Ÿ”ง Custom Post-Sync Hooks

  • Dev applications: Basic health checks (10s wait, 3 retries)
  • Staging applications: Enhanced validation (15s wait, more comprehensive checks)
  • Production applications: Comprehensive validation (30s wait, 5 retries, multiple health checks)

๐Ÿš€ Real-World Benefits

  • ๐Ÿ’ฐ Cost Savings: No unnecessary compute for unrelated validations
  • โšก Speed: Only changed apps validate, dramatically faster deployment feedback
  • ๐ŸŽฏ Precision: Failures are immediately tied to specific apps
  • ๐Ÿ”ง Flexibility: Each app can have completely different validation logic
  • ๐Ÿ”„ Parallel Processing: Multiple app changes can validate simultaneously

๐Ÿ”„ Reset ArgoCD

  • Complete cleanup: Removes all Applications, ApplicationSets, and application namespaces
  • Safe reset: Preserves ArgoCD core components and configuration
  • Fresh start: Returns ArgoCD to clean state for new deployments

๐Ÿ”„ GitOps Workflow

  1. Developer makes changes to application code
  2. CI/CD builds and pushes new container image
  3. Update image tag in environment-specific deployment.yaml
  4. ArgoCD detects changes and syncs only the affected environment
  5. Application is updated with zero impact on other environments

๐Ÿ›ก๏ธ Security Best Practices

  • Change the default admin password after first login
  • Use RBAC to control access to applications
  • Enable SSO for production environments
  • Regularly update ArgoCD to the latest version

๐Ÿ”— Useful Links

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

GitOps configuration repo demonstrating selective syncing with ArgoCD ApplicationSets. Manages Kubernetes apps across dev/staging/production environments where changes only trigger deployments for affected applications. Includes helper scripts, multi-env manifests, and comprehensive docs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%