Skip to content

Instantly share code, notes, and snippets.

@mikebridge
Last active October 10, 2024 23:32
Show Gist options
  • Save mikebridge/ac813742a69e8d611a7b48684c75e426 to your computer and use it in GitHub Desktop.
Save mikebridge/ac813742a69e8d611a7b48684c75e426 to your computer and use it in GitHub Desktop.
ArgoCD setup and teardown for minikube
#!/bin/bash
ARGOCD_NAMESPACE="argocd"
ARGOCD_ADMIN_PORT="8080"
check_argocd_installed() {
if command -v argocd >/dev/null 2>&1; then
echo "- Argo CD CLI is installed."
else
echo "*** Argo CD CLI is not installed."
echo "*** You can install it using 'brew install argocd' or follow the official installation guide."
exit 1
fi
}
namespace_exists() {
kubectl get namespace $ARGOCD_NAMESPACE >/dev/null 2>&1
}
create_argocd_namespace() {
if namespace_exists $ARGOCD_NAMESPACE; then
echo "- Namespace $ARGOCD_NAMESPACE already exists."
else
echo "- Creating namespace: $ARGOCD_NAMESPACE..."
kubectl create namespace $ARGOCD_NAMESPACE
fi
}
argocd_installed() {
kubectl get deployment argocd-server -n $ARGOCD_NAMESPACE >/dev/null 2>&1
}
install_argocd() {
if argocd_installed; then
echo "- Argo CD is already installed."
else
echo "- Installing Argo CD..."
kubectl apply -n $ARGOCD_NAMESPACE -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
fi
}
wait_for_argocd_pods() {
echo "- Waiting for Argo CD pods to be ready..."
kubectl wait --for=condition=Ready pods --all -n $ARGOCD_NAMESPACE --timeout=180s
}
port_forward_running() {
# Check if port 8080 is already being forwarded (only works if it's the same port)
lsof -i :${ARGOCD_ADMIN_PORT} >/dev/null 2>&1
}
# Function to port-forward Argo CD API server if it's not already forwarded
port_forward_argocd() {
if port_forward_running; then
echo "Port-forwarding is already set up for Argo CD API server on port 8080."
else
echo "Port-forwarding Argo CD API server to localhost:8080..."
kubectl port-forward svc/argocd-server -n $ARGOCD_NAMESPACE ${ARGOCD_ADMIN_PORT}:443 &
fi
}
# Function to retrieve the Argo CD admin password
get_admin_password() {
echo "Retrieving Argo CD admin password..."
kubectl -n $ARGOCD_NAMESPACE get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
echo
}
ssl_file_exists() {
[ -f argocd-server-cert.pem ]
}
export_ssl_cert() {
if ssl_file_exists; then
echo "SSL certificate for Argo CD API server already exported."
else
echo "Exporting SSL certificate for Argo CD API server..."
openssl s_client -showcerts -connect localhost:8080 </dev/null 2>/dev/null | openssl x509 -outform PEM > argocd-server-cert.pem
fi
echo "To trust the SSL cert on MacOS, "
echo " - open Keychain Access,"
echo " - then under System select 'Keychains' and under 'Category' select 'Certificates'"
echo " - then File -> Import Items, and select the pem file"
echo " - then find the 'Argo CD Server' CERT, double-click it"
echo " - set 'When using this certificate' to 'Always Trust'"
echo " - close your browser and re-start it."
}
echo "Starting Argo CD setup..."
check_argocd_installed
create_argocd_namespace
install_argocd
wait_for_argocd_pods
port_forward_argocd
export_ssl_cert
get_admin_password
echo "** Argo CD setup completed successfully **"
cat << EOF
You can access the Argo CD UI at https://localhost:8080
Login (necessary to change the password):
argocd login localhost:8080 --username admin --password MY_PASSWORD
To change your admin password:
argocd account update-password
To list contexts:
kubectl config get-contexts -o name
To add a context (e.g. minikube) to the cluster:
argocd cluster add minikube
EOF
#!/usr/bin/env bash
ARGOCD_NAMESPACE="argocd"
delete_argocd_namespace() {
echo "Deleting Argo CD namespace: $ARGOCD_NAMESPACE..."
kubectl delete namespace $ARGOCD_NAMESPACE --wait
}
delete_argocd_crds() {
echo "Deleting Argo CD CRDs..."
kubectl delete crd applications.argoproj.io appprojects.argoproj.io argocds.argoproj.io argocdexports.argoproj.io
}
delete_remaining_services() {
echo "Deleting any remaining Argo CD services..."
kubectl get svc --all-namespaces -l app.kubernetes.io/part-of=argocd -o jsonpath='{.items[*].metadata.name}' | xargs -n 1 -I {} kubectl delete svc {} --all-namespaces
}
delete_argocd_namespace
delete_argocd_crds
delete_remaining_services
echo "Argo CD and all related services have been deleted successfully."
@mikebridge
Copy link
Author

mikebridge commented Oct 10, 2024

This script should idempotently set up the "getting started" instructions for argocd on MacOS / minikube.

Once you have argo set up, you can install the example app
in the default namespace from the CLI:

argocd app create guestbook \
    --repo https://github.com/argoproj/argocd-example-apps.git \
    --path guestbook \
    --dest-server https://kubernetes.default.svc \
    --dest-namespace default

Then, once you've sync-ed it via the admin UI, expose it on port 8081:

 1015  kubectl port-forward svc/guestbook-ui 8081:80 -n default

If you want kubectl to use the argocd context:

kubectl config set-context \
    --current \
    --namespace=argocd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment