Skip to content

Commit 73e8901

Browse files
authored
Backport Subscription update logic for Rollouts E2E tests (#881)
Signed-off-by: Jonathan West <[email protected]>
1 parent 732a6b1 commit 73e8901

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

scripts/run-rollouts-e2e-tests.sh

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,150 @@
66

77
set -ex
88

9+
function wait_until_pods_running() {
10+
echo -n "Waiting until all pods in namespace $1 are up"
11+
12+
# Wait for there to be only a single Pod line in 'oc get pods' (there should be no more 'terminating' pods, etc)
13+
timeout="true"
14+
for i in {1..30}; do
15+
local num_pods="$(oc get pods --no-headers -n $1 | grep openshift-gitops-operator-controller-manager | wc -l 2>/dev/null)"
16+
17+
# Check the number of lines
18+
if [[ "$num_lines" == "1" ]]; then
19+
echo "Waiting for a single Pod entry in Namespace '$1': $num_pods"
20+
sleep 5
21+
else
22+
timeout="false"
23+
break
24+
fi
25+
done
26+
if [ "$timeout" == "true" ]; then
27+
echo -e "\n\nERROR: timeout waiting for expected number of pods"
28+
return 1
29+
fi
30+
31+
for i in {1..150}; do # timeout after 5 minutes
32+
local pods="$(oc get pods --no-headers -n $1 | grep openshift-gitops-operator-controller-manager 2>/dev/null)"
33+
# write it to tempfile
34+
TempFile=$(mktemp)
35+
oc get pods --no-headers -n $1 2>/dev/null >$TempFile
36+
37+
# All pods must be running
38+
local not_running=$(echo "${pods}" | grep -v Running | grep -v Completed | wc -l)
39+
if [[ -n "${pods}" && ${not_running} -eq 0 ]]; then
40+
local all_ready=1
41+
while read pod; do
42+
local status=($(echo ${pod} | cut -f2 -d' ' | tr '/' ' '))
43+
# All containers must be ready
44+
[[ -z ${status[0]} ]] && all_ready=0 && break
45+
[[ -z ${status[1]} ]] && all_ready=0 && break
46+
[[ ${status[0]} -lt 1 ]] && all_ready=0 && break
47+
[[ ${status[1]} -lt 1 ]] && all_ready=0 && break
48+
[[ ${status[0]} -ne ${status[1]} ]] && all_ready=0 && break
49+
done <${TempFile}
50+
if ((all_ready)); then
51+
echo -e "\nAll pods are up:\n${pods}"
52+
return 0
53+
fi
54+
fi
55+
echo -n "."
56+
sleep 2
57+
done
58+
echo -e "\n\nERROR: timeout waiting for pods to come up\n${pods}"
59+
return 1
60+
}
61+
62+
function enable_rollouts_cluster_scoped_namespaces() {
63+
64+
# This functions add this env var to operator:
65+
# - CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES="argo-rollouts,test-rom-ns-1,rom-ns-1"
66+
67+
if ! [ -z $NON_OLM ]; then
68+
oc set env deployment openshift-gitops-operator-controller-manager -n openshift-gitops-operator CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES="argo-rollouts,test-rom-ns-1,rom-ns-1"
69+
70+
elif [ -z $CI ]; then
71+
72+
oc patch -n openshift-gitops-operator subscription openshift-gitops-operator \
73+
--type merge --patch '{"spec": {"config": {"env": [{"name": "CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES", "value": "argo-rollouts,test-rom-ns-1,rom-ns-1"}]}}}'
74+
75+
else
76+
77+
oc patch -n openshift-gitops-operator subscription `subscription=gitops-operator- && oc get subscription --all-namespaces | grep $subscription | head -1 | awk '{print $2}'` \
78+
--type merge --patch '{"spec": {"config": {"env": [{"name": "CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES", "value": "argo-rollouts,test-rom-ns-1,rom-ns-1"}]}}}'
79+
fi
80+
81+
# Loop to wait until CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES is added to the OpenShift GitOps Operator Deployment
82+
for i in {1..30}; do
83+
if oc get deployment openshift-gitops-operator-controller-manager -n openshift-gitops-operator -o jsonpath='{.spec.template.spec.containers[0].env}' | grep -q '{"name":"CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES","value":"argo-rollouts,test-rom-ns-1,rom-ns-1"}'; then
84+
echo "CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES to be set"
85+
break
86+
else
87+
echo "Waiting for CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES to be set"
88+
sleep 5
89+
fi
90+
done
91+
92+
# Verify the variable is set
93+
if oc get deployment openshift-gitops-operator-controller-manager -n openshift-gitops-operator -o jsonpath='{.spec.template.spec.containers[0].env}' | grep -q '{"name":"CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES","value":"argo-rollouts,test-rom-ns-1,rom-ns-1"}'; then
94+
echo "CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES is set."
95+
else
96+
echo "ERROR: CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES was never set."
97+
exit 1
98+
fi
99+
100+
# Deployment is correct, now wait for Pods to start
101+
wait_until_pods_running "openshift-gitops-operator"
102+
103+
}
104+
105+
function disable_rollouts_cluster_scope_namespaces() {
106+
107+
# Remove the env var we previously added to operator
108+
109+
if ! [ -z $NON_OLM ]; then
110+
111+
oc set env deployment openshift-gitops-operator-controller-manager -n openshift-gitops-operator CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES=null
112+
113+
elif [ -z $CI ]; then
114+
115+
oc patch -n openshift-gitops-operator subscription openshift-gitops-operator \
116+
--type json --patch '[{"op": "remove", "path": "/spec/config"}]'
117+
else
118+
119+
oc patch -n openshift-gitops-operator subscription `subscription=gitops-operator- && oc get subscription --all-namespaces | grep $subscription | head -1 | awk '{print $2}'` \
120+
--type json --patch '[{"op": "remove", "path": "/spec/config"}]'
121+
fi
122+
123+
124+
# Loop to wait until CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES is removed from the OpenShift GitOps Operator Deplyoment
125+
for i in {1..30}; do
126+
if oc get deployment openshift-gitops-operator-controller-manager -n openshift-gitops-operator -o jsonpath='{.spec.template.spec.containers[0].env}' | grep -q '{"name":"CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES","value":"argo-rollouts,test-rom-ns-1,rom-ns-1"}'; then
127+
echo "Waiting for CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES to be removed"
128+
sleep 5
129+
else
130+
echo "CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES has been removed."
131+
break
132+
fi
133+
done
134+
135+
# Verify it has been removed.
136+
if oc get deployment openshift-gitops-operator-controller-manager -n openshift-gitops-operator -o jsonpath='{.spec.template.spec.containers[0].env}' | grep -q '{"name":"CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES","value":"argo-rollouts,test-rom-ns-1,rom-ns-1"}'; then
137+
echo "ERROR: CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES was not successfully removed."
138+
exit 1
139+
else
140+
echo "CLUSTER_SCOPED_ARGO_ROLLOUTS_NAMESPACES was successfuly removed."
141+
fi
142+
143+
# Wait for Pods to reflect the removal of the env var
144+
wait_until_pods_running "openshift-gitops-operator"
145+
}
146+
147+
148+
enable_rollouts_cluster_scoped_namespaces
149+
150+
trap disable_rollouts_cluster_scope_namespaces EXIT
151+
152+
9153
ROLLOUTS_TMP_DIR=$(mktemp -d)
10154

11155
cd $ROLLOUTS_TMP_DIR

0 commit comments

Comments
 (0)