Skip to content

Commit

Permalink
Add scripts for import step
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
  • Loading branch information
sergenyalcin committed May 7, 2024
1 parent 840f4e7 commit 701c15b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 462 deletions.
27 changes: 27 additions & 0 deletions hack/check_endpoints.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

function check_endpoints {
endpoints=( $("${KUBECTL}" -n "${CROSSPLANE_NAMESPACE}" get endpoints --no-headers | grep '^provider-' | awk '{print $1}') )
for endpoint in ${endpoints[@]}; do
port=$(${KUBECTL} -n "${CROSSPLANE_NAMESPACE}" get endpoints "$endpoint" -o jsonpath='{.subsets[*].ports[0].port}')
if [[ -z "${port}" ]]; then
echo "$endpoint - No served ports"
return 1
else
echo "$endpoint - Ports present"
fi
done
}

attempt=1
max_attempts=10
while [[ $attempt -le $max_attempts ]]; do
if check_endpoints; then
exit 0
else
printf "Retrying... (%d/%d)\n" "$attempt" "$max_attempts" >&2
fi
((attempt++))
sleep 5
done
exit 1
33 changes: 33 additions & 0 deletions hack/patch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

function patch {
kindgroup=$1;
name=$2;
if ${KUBECTL} --subresource=status patch "$kindgroup/$name" --type=merge -p '{"status":{"conditions":[]}}' ; then
return 0;
else
return 1;
fi;
};


kindgroup=$1;
name=$2;
attempt=1;
max_attempts=10;
while [[ $attempt -le $max_attempts ]]; do
if patch "$kindgroup" "$name"; then
echo "Successfully patched $kindgroup/$name";
${KUBECTL} annotate "$kindgroup/$name" uptest-old-id=$(${KUBECTL} get "$kindgroup/$name" -o=jsonpath='{.status.atProvider.id}') --overwrite;
break;
else
printf "Retrying... (%d/%d) for %s/%s\n" "$attempt" "$max_attempts" "$kindgroup" "$name" >&2;
fi;
((attempt++));
sleep 5;
done;
if [[ $attempt -gt $max_attempts ]]; then
echo "Failed to patch $kindgroup/$name after $max_attempts attempts";
exit 1;
fi;
exit 0;
81 changes: 4 additions & 77 deletions internal/templates/02-import.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,88 +8,15 @@ commands:
- command: sleep 10
- command: ${KUBECTL} scale deployment crossplane -n ${CROSSPLANE_NAMESPACE} --replicas=1 --timeout 10s
- script: ${KUBECTL} -n ${CROSSPLANE_NAMESPACE} get deploy --no-headers -o custom-columns=":metadata.name" | grep "provider-" | xargs ${KUBECTL} -n ${CROSSPLANE_NAMESPACE} scale deploy --replicas=1
- script: |
#!/bin/bash

function check_endpoints {
endpoints=$(${KUBECTL} -n ${CROSSPLANE_NAMESPACE} get endpoints --no-headers | grep '^provider-' | awk '{print $1}')

for endpoint in $endpoints; do
port=$(${KUBECTL} -n ${CROSSPLANE_NAMESPACE} get endpoints $endpoint -o jsonpath='{.subsets[*].ports[0].port}')
if [[ -z "${port}" ]]; then
echo "$endpoint - No served ports"
return 1
else
echo "$endpoint - Ports present"
fi
done
}

attempt=1
max_attempts=10

while [[ $attempt -le $max_attempts ]]; do
if check_endpoints; then
exit 0
else
printf "Retrying... (%d/%d)\n" "$attempt" "$max_attempts" >&2
fi
((attempt++))
sleep 5
done

exit 1
- script: |
#!/bin/bash

function patch {
kindgroup=$1
name=$2
if ${KUBECTL} --subresource=status patch "$kindgroup/$name" --type=merge -p '{"status":{"conditions":[]}}' ; then
return 0
else
return 1
fi
}

attempt=1
max_attempts=10

# Array of resources
resources=(
- script: curl -sL https://raw.githubusercontent.com/crossplane/uptest/main/hack/check_endpoints.sh -o /tmp/check_endpoints.sh && chmod +x /tmp/check_endpoints.sh
- script: curl -sL https://raw.githubusercontent.com/crossplane/uptest/main/hack/patch.sh -o /tmp/patch.sh && chmod +x /tmp/patch.sh
- script: /tmp/check_endpoints.sh
{{- range $resource := .Resources }}
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
{{- end -}}
{{- if not $resource.Namespace }}
"{{ $resource.KindGroup }}/{{ $resource.Name }}"
- script: /tmp/patch.sh {{ $resource.KindGroup }} {{ $resource.Name }}
{{- end }}
{{- end }}
)

for resource in "${resources[@]}"; do
kindgroup=$(echo "$resource" | cut -d '/' -f 1)
name=$(echo "$resource" | cut -d '/' -f 2)

attempt=1
while [[ $attempt -le $max_attempts ]]; do
if patch "$kindgroup" "$name"; then
echo "Successfully patched $kindgroup/$name"
${KUBECTL} annotate "$kindgroup/$name" uptest-old-id=$(${KUBECTL} get "$kindgroup/$name" -o=jsonpath='{.status.atProvider.id}') --overwrite
break
else
printf "Retrying... (%d/%d) for %s/%s\n" "$attempt" "$max_attempts" "$kindgroup" "$name" >&2
fi
((attempt++))
sleep 5
done

if [[ $attempt -gt $max_attempts ]]; then
echo "Failed to patch $kindgroup/$name after $max_attempts attempts"
exit 1
fi
done

echo "All resources patched successfully."
exit 0
- command: ${KUBECTL} annotate managed --all crossplane.io/paused=false --overwrite
Loading

0 comments on commit 701c15b

Please sign in to comment.