Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions internal/templates/00-apply.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,42 @@ spec:
- apply:
file: {{ .TestCase.TestDirectory }}
- script:
env:
- name: CHAINSAW_NAMESPACE
value: ($namespace)
content: |
echo "Running annotation script with retry logic"
annotate_resource() {
local namespace="$1"
local resource="$2"

if [ -n "$namespace" ]; then
${KUBECTL} annotate --namespace "$namespace" "$resource" upjet.upbound.io/test=true --overwrite
return $?
fi

if ${KUBECTL} annotate "$resource" upjet.upbound.io/test=true --overwrite; then
return 0
fi

if [ -n "$CHAINSAW_NAMESPACE" ]; then
${KUBECTL} annotate --namespace "$CHAINSAW_NAMESPACE" "$resource" upjet.upbound.io/test=true --overwrite
return $?
fi

return 1
}

retry_annotate() {
local max_attempts=10
local delay=5
local attempt=1
local cmd="$1"
local namespace="$1"
local resource="$2"

while [ $attempt -le $max_attempts ]; do
echo "Annotation attempt $attempt/$max_attempts for: $cmd"
if eval "$cmd"; then
echo "Annotation attempt $attempt/$max_attempts for: $resource"
if annotate_resource "$namespace" "$resource"; then
echo "Annotation successful on attempt $attempt"
return 0
else
Expand All @@ -61,7 +86,7 @@ spec:
{{- if eq $resource.KindGroup "secret." -}}
{{continue}}
{{- end }}
retry_annotate "${KUBECTL} annotate {{ if $resource.Namespace }}--namespace {{ $resource.Namespace }} {{ end }} {{ $resource.KindGroup }}/{{ $resource.Name }} upjet.upbound.io/test=true --overwrite"
retry_annotate "{{ $resource.Namespace }}" "{{ $resource.KindGroup }}/{{ $resource.Name }}"
{{- end }}
- name: Assert Status Conditions
description: |
Expand Down
94 changes: 88 additions & 6 deletions internal/templates/01-update.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,71 @@ spec:
{{- end }}
{{- if $resource.Root }}
- script:
env:
- name: CHAINSAW_NAMESPACE
value: ($namespace)
content: |
run_resource_kubectl() {
local namespace="$1"
local verb="$2"
local resource="$3"
shift 3

if [ -n "$namespace" ]; then
${KUBECTL} "$verb" --namespace "$namespace" "$resource" "$@"
return $?
fi

if ${KUBECTL} "$verb" "$resource" "$@"; then
return 0
fi

if [ -n "$CHAINSAW_NAMESPACE" ]; then
${KUBECTL} "$verb" --namespace "$CHAINSAW_NAMESPACE" "$resource" "$@"
return $?
fi

return 1
}

run_resource_status_patch() {
local namespace="$1"
local resource="$2"
shift 2

if [ -n "$namespace" ]; then
${KUBECTL} --subresource=status patch --namespace "$namespace" "$resource" "$@"
return $?
fi

if ${KUBECTL} --subresource=status patch "$resource" "$@"; then
return 0
fi

if [ -n "$CHAINSAW_NAMESPACE" ]; then
${KUBECTL} --subresource=status patch --namespace "$CHAINSAW_NAMESPACE" "$resource" "$@"
return $?
fi

return 1
}

retry_kubectl() {
local max_attempts=10
local delay=5
local attempt=1
local cmd="$1"
local namespace="$1"
local verb="$2"
local resource="$3"
shift 3

while [ $attempt -le $max_attempts ]; do
echo "Kubectl attempt $attempt/$max_attempts for: $cmd"
if eval "$cmd"; then
echo "Kubectl attempt $attempt/$max_attempts for: $verb $resource"
if [ "$verb" = "status-patch" ] && run_resource_status_patch "$namespace" "$resource" "$@"; then
echo "Kubectl operation successful on attempt $attempt"
return 0
fi
if [ "$verb" != "status-patch" ] && run_resource_kubectl "$namespace" "$verb" "$resource" "$@"; then
echo "Kubectl operation successful on attempt $attempt"
return 0
else
Expand All @@ -44,8 +99,8 @@ spec:
echo "Kubectl operation failed after $max_attempts attempts"
return 1
}
retry_kubectl "${KUBECTL} --subresource=status patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=merge -p '{\"status\":{\"conditions\":[]}}'"
retry_kubectl "${KUBECTL} patch {{ $resource.KindGroup }}/{{ $resource.Name }} --type=merge -p '{\"spec\":{\"forProvider\":{{ $resource.UpdateParameter }}}}'"
retry_kubectl "{{ $resource.Namespace }}" "status-patch" "{{ $resource.KindGroup }}/{{ $resource.Name }}" --type=merge -p '{"status":{"conditions":[]}}'
retry_kubectl "{{ $resource.Namespace }}" "patch" "{{ $resource.KindGroup }}/{{ $resource.Name }}" --type=merge -p '{"spec":{"forProvider":{{ $resource.UpdateParameter }}}}'
{{- end }}
{{- end }}
- name: Assert Updated Resource
Expand Down Expand Up @@ -73,6 +128,33 @@ spec:
status: "True"
{{- end }}
- script:
content: ${KUBECTL} get {{ if $resource.Namespace }}--namespace {{ $resource.Namespace }} {{ end }} {{ $resource.KindGroup }}/{{ $resource.Name }} -o=jsonpath='{.status.atProvider{{ $resource.UpdateAssertKey }}}' | grep -q "^{{ $resource.UpdateAssertValue }}$"
env:
- name: CHAINSAW_NAMESPACE
value: ($namespace)
content: |
run_resource_kubectl() {
local namespace="$1"
local verb="$2"
local resource="$3"
shift 3

if [ -n "$namespace" ]; then
${KUBECTL} "$verb" --namespace "$namespace" "$resource" "$@"
return $?
fi

if ${KUBECTL} "$verb" "$resource" "$@"; then
return 0
fi

if [ -n "$CHAINSAW_NAMESPACE" ]; then
${KUBECTL} "$verb" --namespace "$CHAINSAW_NAMESPACE" "$resource" "$@"
return $?
fi

return 1
}

run_resource_kubectl "{{ $resource.Namespace }}" "get" "{{ $resource.KindGroup }}/{{ $resource.Name }}" -o=jsonpath='{.status.atProvider{{ $resource.UpdateAssertKey }}}' | grep -q "^{{ $resource.UpdateAssertValue }}$"
{{- end }}
{{- end }}
Loading