GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test-flux 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. set -e
  3. set -o pipefail
  4. BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)"
  5. PROFILE="flux-test"
  6. PROFILE_DIR="$BASEDIR/profiles/$PROFILE"
  7. KNOWN_HOSTS="$PROFILE_DIR/.known_hosts"
  8. CONFIG_REPO="$PROFILE_DIR/config"
  9. SSH_PRIVATE_KEY="$HOME/.minikube/machines/$PROFILE/id_rsa"
  10. #############################################################################
  11. # Setup
  12. #############################################################################
  13. # Clear profile temp dir and reset minikube profile
  14. rm -rf "$PROFILE_DIR"
  15. mkdir -p "$PROFILE_DIR"
  16. minikube delete --profile "$PROFILE" || true
  17. minikube start --profile "$PROFILE" --keep-context
  18. MINIKUBE_IP=$(minikube --profile "$PROFILE" ip)
  19. # Copy the latest Flux image into the minikube VM
  20. docker save "docker.io/weaveworks/flux:latest" | (eval $(minikube --profile "$PROFILE" docker-env) && docker load)
  21. # Create a central git repo inside the minikube VM and get the host key for ssh access
  22. minikube --profile "$PROFILE" ssh -- git init --bare /home/docker/flux.git
  23. ssh-keyscan $MINIKUBE_IP > "$KNOWN_HOSTS"
  24. # Deploy Flux in kube-system namespace, supplying it with the SSH keys it needs to access & authenticate the config repo
  25. kubectl --context "$PROFILE" -n kube-system create secret generic flux-git-deploy --from-file=identity="$SSH_PRIVATE_KEY"
  26. kubectl --context "$PROFILE" -n kube-system create configmap ssh-known-hosts --from-file=known_hosts="$KNOWN_HOSTS"
  27. sed -e "s/MINIKUBE_IP/$MINIKUBE_IP/" "$BASEDIR/flux-deploy-all.yaml" | kubectl --context "$PROFILE" -n kube-system apply -f-
  28. # Create a git repo in the local filesystem configured with the central git repo in minikube as origin
  29. git init "$CONFIG_REPO"
  30. git -C "$CONFIG_REPO" remote add origin ssh://docker@$MINIKUBE_IP/home/docker/flux.git
  31. export GIT_SSH_COMMAND="ssh -i $SSH_PRIVATE_KEY -o UserKnownHostsFile=$KNOWN_HOSTS"
  32. #############################################################################
  33. # Test Helpers
  34. #############################################################################
  35. fail() {
  36. echo
  37. echo "*** FAILED ***"
  38. echo
  39. exit 1
  40. }
  41. success() {
  42. echo
  43. echo "*** SUCCESS ***"
  44. echo
  45. exit 0
  46. }
  47. wait_for_sync() {
  48. echo -n "Waiting for sync"
  49. HEAD_REV=$(git -C "$CONFIG_REPO" rev-list -n 1 HEAD)
  50. for i in $(seq 1 $1); do
  51. git -C "$CONFIG_REPO" fetch --tags > /dev/null 2>&1
  52. FLUX_SYNC_REV=$(git -C "$CONFIG_REPO" rev-list -n 1 flux-sync 2>/dev/null || true)
  53. if [ "$FLUX_SYNC_REV" = "$HEAD_REV" ]; then
  54. echo
  55. return
  56. fi
  57. sleep 1
  58. echo -n .
  59. done
  60. fail
  61. }
  62. wait_for_upstream_commits() {
  63. echo -n "Waiting for $2 upstream commits"
  64. for i in $(seq 1 $1); do
  65. git -C "$CONFIG_REPO" fetch --tags > /dev/null 2>&1
  66. if [ "$(git -C "$CONFIG_REPO" rev-list --count HEAD..flux-sync)" -eq "2" ]; then
  67. echo
  68. return
  69. fi
  70. sleep 1
  71. echo -n .
  72. done
  73. fail
  74. }
  75. workloads() {
  76. curl -s "http://$MINIKUBE_IP:30080/api/flux/v6/services?namespace=$1"
  77. }
  78. workload_container() {
  79. jq --raw-output ".[]|select(.ID==\"$1\")|.Containers[]|select(.Name==\"$2\")|.Current.ID"
  80. }
  81. assert_workload_container() {
  82. if [ "$(workloads $1|workload_container $2 $3)" != "$4" ]; then
  83. fail
  84. fi
  85. }
  86. #############################################################################
  87. # Tests
  88. #############################################################################
  89. # Add the helloworld deployment to the config repo and push to the central config repo in minikube
  90. cp "$BASEDIR/helloworld-deployment.yaml" "$CONFIG_REPO"
  91. git -C "$CONFIG_REPO" add helloworld-deployment.yaml
  92. git -C "$CONFIG_REPO" commit -m 'Deploy helloworld'
  93. git -C "$CONFIG_REPO" push -u origin master
  94. # Wait two minutes for flux-sync tag to point to local HEAD
  95. wait_for_sync 120
  96. # Examine workloads via Flux API an assert container versions are correct
  97. assert_workload_container default default:deployment/helloworld helloworld quay.io/weaveworks/helloworld:master-a000001
  98. assert_workload_container default default:deployment/helloworld sidecar quay.io/weaveworks/sidecar:master-a000001
  99. # Now enable automation
  100. fluxctl --url http://$(minikube -p flux-test ip):30080/api/flux automate --workload=default:deployment/helloworld
  101. # Wait two minutes for two (automation and release) commits to appear
  102. wait_for_upstream_commits 120 2
  103. # Examine workload via Flux API an assert container versions are updated
  104. assert_workload_container default default:deployment/helloworld helloworld quay.io/weaveworks/helloworld:master-9a16ff945b9e
  105. assert_workload_container default default:deployment/helloworld sidecar quay.io/weaveworks/sidecar:master-a000002
  106. success