|
| 1 | +# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json |
| 2 | +apiVersion: chainsaw.kyverno.io/v1alpha1 |
| 3 | +kind: Test |
| 4 | +metadata: |
| 5 | + name: lb-update-port |
| 6 | + labels: |
| 7 | + all: |
| 8 | + lke: |
| 9 | +spec: |
| 10 | + namespace: "lb-update-port" |
| 11 | + steps: |
| 12 | + - name: Create pods and services |
| 13 | + try: |
| 14 | + - apply: |
| 15 | + file: create-pods-services.yaml |
| 16 | + catch: |
| 17 | + - describe: |
| 18 | + apiVersion: v1 |
| 19 | + kind: Pod |
| 20 | + - describe: |
| 21 | + apiVersion: v1 |
| 22 | + kind: Service |
| 23 | + - name: Check that loadbalancer ip is assigned |
| 24 | + try: |
| 25 | + - assert: |
| 26 | + resource: |
| 27 | + apiVersion: v1 |
| 28 | + kind: Service |
| 29 | + metadata: |
| 30 | + name: svc-test |
| 31 | + status: |
| 32 | + (loadBalancer.ingress[0].ip != null): true |
| 33 | + - name: Fetch loadbalancer ip and check both pods reachable |
| 34 | + try: |
| 35 | + - script: |
| 36 | + content: | |
| 37 | + set -e |
| 38 | + IP=$(kubectl get svc svc-test -n $NAMESPACE -o json | jq -r .status.loadBalancer.ingress[0].ip) |
| 39 | +
|
| 40 | + podnames=() |
| 41 | +
|
| 42 | + for i in {1..10}; do |
| 43 | + if [[ ${#podnames[@]} -lt 2 ]]; then |
| 44 | + output=$(curl -s $IP:80 | jq -e .podName || true) |
| 45 | +
|
| 46 | + if [[ "$output" == *"test-"* ]]; then |
| 47 | + unique=true |
| 48 | + for i in "${array[@]}"; do |
| 49 | + if [[ "$i" == "$output" ]]; then |
| 50 | + unique=false |
| 51 | + break |
| 52 | + fi |
| 53 | + done |
| 54 | + if [[ "$unique" == true ]]; then |
| 55 | + podnames+=($output) |
| 56 | + fi |
| 57 | + fi |
| 58 | + else |
| 59 | + break |
| 60 | + fi |
| 61 | + sleep 10 |
| 62 | + done |
| 63 | +
|
| 64 | + if [[ ${#podnames[@]} -lt 2 ]]; then |
| 65 | + echo "all pods failed to respond" |
| 66 | + else |
| 67 | + echo "all pods responded" |
| 68 | + fi |
| 69 | + check: |
| 70 | + ($error == null): true |
| 71 | + (contains($stdout, 'all pods responded')): true |
| 72 | + - name: Update service |
| 73 | + try: |
| 74 | + - apply: |
| 75 | + file: update-port-service.yaml |
| 76 | + - name: Check pods reachable on new port |
| 77 | + try: |
| 78 | + - script: |
| 79 | + content: | |
| 80 | + set -e |
| 81 | + #wait for changes to propagate to the LB |
| 82 | + sleep 30 |
| 83 | + IP=$(kubectl get svc svc-test -n $NAMESPACE -o json | jq -r .status.loadBalancer.ingress[0].ip) |
| 84 | +
|
| 85 | + podnames=() |
| 86 | +
|
| 87 | + for i in {1..10}; do |
| 88 | + if [[ ${#podnames[@]} -lt 2 ]]; then |
| 89 | + output=$(curl -s $IP:8080 | jq -e .podName || true) |
| 90 | +
|
| 91 | + if [[ "$output" == *"test-"* ]]; then |
| 92 | + unique=true |
| 93 | + for i in "${array[@]}"; do |
| 94 | + if [[ "$i" == "$output" ]]; then |
| 95 | + unique=false |
| 96 | + break |
| 97 | + fi |
| 98 | + done |
| 99 | + if [[ "$unique" == true ]]; then |
| 100 | + podnames+=($output) |
| 101 | + fi |
| 102 | + fi |
| 103 | + else |
| 104 | + break |
| 105 | + fi |
| 106 | + sleep 10 |
| 107 | + done |
| 108 | +
|
| 109 | + if [[ ${#podnames[@]} -lt 2 ]]; then |
| 110 | + echo "all pods failed to respond" |
| 111 | + else |
| 112 | + echo "all pods responded" |
| 113 | + fi |
| 114 | + check: |
| 115 | + ($error == null): true |
| 116 | + (contains($stdout, 'all pods responded')): true |
| 117 | + - name: Fetch firewall ID and check ports are updated |
| 118 | + try: |
| 119 | + - script: |
| 120 | + content: | |
| 121 | + set -e |
| 122 | +
|
| 123 | + for i in {1..10}; do |
| 124 | + nbid=$(KUBECONFIG=$KUBECONFIG NAMESPACE=$NAMESPACE LINODE_TOKEN=$LINODE_TOKEN ../scripts/get-nb-id.sh) |
| 125 | +
|
| 126 | + fw=$(curl -s --request GET \ |
| 127 | + -H "Authorization: Bearer $LINODE_TOKEN" \ |
| 128 | + -H "Content-Type: application/json" \ |
| 129 | + -H "accept: application/json" \ |
| 130 | + "https://api.linode.com/v4/nodebalancers/${nbid}/firewalls" || true) |
| 131 | +
|
| 132 | + if echo "$fw" | jq -e '.data[].rules.inbound[] | select(.ports | contains("8080"))' > /dev/null; then |
| 133 | + echo "firewall rule updated with new port" |
| 134 | + break |
| 135 | + fi |
| 136 | + sleep 10 |
| 137 | + done |
| 138 | + check: |
| 139 | + ($error == null): true |
| 140 | + (contains($stdout, 'firewall rule updated with new port')): true |
| 141 | + - name: Delete Pods |
| 142 | + try: |
| 143 | + - delete: |
| 144 | + ref: |
| 145 | + apiVersion: v1 |
| 146 | + kind: Pod |
| 147 | + - name: Delete Service |
| 148 | + try: |
| 149 | + - delete: |
| 150 | + ref: |
| 151 | + apiVersion: v1 |
| 152 | + kind: Service |
0 commit comments