|
| 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: fw-use-specified-nb |
| 6 | +spec: |
| 7 | + bindings: |
| 8 | + - name: fwname |
| 9 | + value: (join('-', ['ccm-fwtest', env('CLUSTER_NAME')])) |
| 10 | + namespace: "fw-use-specified-nb" |
| 11 | + steps: |
| 12 | + - name: Check if CCM is deployed |
| 13 | + try: |
| 14 | + - assert: |
| 15 | + file: ../assert-ccm-resources.yaml |
| 16 | + - name: Create firewall, Create pods and services |
| 17 | + try: |
| 18 | + - script: |
| 19 | + env: |
| 20 | + - name: FWLABEL |
| 21 | + value: ($fwname) |
| 22 | + content: | |
| 23 | + set -e |
| 24 | +
|
| 25 | + create_fw=$(curl -s --write-out "%{http_code}\n" --output /dev/null --request POST \ |
| 26 | + -H "Authorization: Bearer $LINODE_TOKEN" \ |
| 27 | + -H "Content-Type: application/json" \ |
| 28 | + -H "accept: application/json" \ |
| 29 | + "https://api.linode.com/v4/networking/firewalls" \ |
| 30 | + --data " |
| 31 | + { |
| 32 | + \"label\": \"$FWLABEL\", |
| 33 | + \"rules\": { |
| 34 | + \"inbound\": [{ |
| 35 | + \"action\": \"ACCEPT\", |
| 36 | + \"label\": \"inbound-rule123\", |
| 37 | + \"description\": \"inbound rule123\", |
| 38 | + \"ports\": \"4321\", |
| 39 | + \"protocol\": \"TCP\", |
| 40 | + \"addresses\": { |
| 41 | + \"ipv4\": [\"0.0.0.0/0\"] |
| 42 | + } |
| 43 | + }], |
| 44 | + \"inbound_policy\": \"ACCEPT\", |
| 45 | + \"outbound_policy\": \"ACCEPT\" |
| 46 | + } |
| 47 | + } |
| 48 | + " |
| 49 | + ) |
| 50 | +
|
| 51 | + if [[ $create_fw == "200" ]]; then |
| 52 | + echo "fw created" |
| 53 | + fi |
| 54 | + check: |
| 55 | + ($error == null): true |
| 56 | + (contains($stdout, 'fw created')): true |
| 57 | + - apply: |
| 58 | + file: create-pods-services.yaml |
| 59 | + catch: |
| 60 | + - describe: |
| 61 | + apiVersion: v1 |
| 62 | + kind: Pod |
| 63 | + - describe: |
| 64 | + apiVersion: v1 |
| 65 | + kind: Service |
| 66 | + - name: Check that loadbalancer ip is assigned |
| 67 | + try: |
| 68 | + - assert: |
| 69 | + resource: |
| 70 | + apiVersion: v1 |
| 71 | + kind: Service |
| 72 | + metadata: |
| 73 | + name: svc-test |
| 74 | + status: |
| 75 | + (loadBalancer.ingress[0].ip != null): true |
| 76 | + - name: Annotate service with nodebalancer id |
| 77 | + try: |
| 78 | + - script: |
| 79 | + env: |
| 80 | + - name: FWLABEL |
| 81 | + value: ($fwname) |
| 82 | + content: | |
| 83 | + set -e |
| 84 | + re='^[0-9]+$' |
| 85 | +
|
| 86 | + fwid=$(curl -s \ |
| 87 | + -H "Authorization: Bearer $LINODE_TOKEN" \ |
| 88 | + -H "Content-Type: application/json" \ |
| 89 | + -H "X-Filter: {\"label\": \"$FWLABEL\"}" \ |
| 90 | + "https://api.linode.com/v4/networking/firewalls" | jq .data[].id) |
| 91 | +
|
| 92 | + if ! [[ $fwid =~ $re ]]; then |
| 93 | + echo "Firewall id [$fwid] is incorrect, failed to fetch firewall" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | +
|
| 97 | + kubectl annotate svc svc-test -n $NAMESPACE service.beta.kubernetes.io/linode-loadbalancer-firewall-id=$fwid |
| 98 | + sleep 5 |
| 99 | +
|
| 100 | + for i in {1..10}; do |
| 101 | + nbid=$(KUBECONFIG=$KUBECONFIG NAMESPACE=$NAMESPACE LINODE_TOKEN=$LINODE_TOKEN ../scripts/get-nb-id.sh) |
| 102 | +
|
| 103 | + fwconfig=$(curl -s \ |
| 104 | + -H "Authorization: Bearer $LINODE_TOKEN" \ |
| 105 | + -H "Content-Type: application/json" \ |
| 106 | + "https://api.linode.com/v4/networking/firewalls/$fwid") |
| 107 | +
|
| 108 | + fw_attached_to_nb=$(echo $fwconfig | jq ".entities[] | select(.id == $nbid) | .id == $nbid") |
| 109 | +
|
| 110 | + if [[ $fw_attached_to_nb == "true" ]]; then |
| 111 | + echo "Conditions met" |
| 112 | + break |
| 113 | + fi |
| 114 | +
|
| 115 | + sleep 10 |
| 116 | + done |
| 117 | +
|
| 118 | + curl -s -X DELETE \ |
| 119 | + -H "Authorization: Bearer $LINODE_TOKEN" \ |
| 120 | + -H "Content-Type: application/json" \ |
| 121 | + "https://api.linode.com/v4/networking/firewalls/$fwid" |
| 122 | + check: |
| 123 | + (contains($stdout, 'Conditions met')): true |
0 commit comments