-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep-deploy-krkn.yaml
199 lines (185 loc) · 9.04 KB
/
step-deploy-krkn.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
apiVersion: tekton.dev/v1alpha1
kind: StepAction
metadata:
name: operator-sdk-run-bundle
spec:
image: quay.io/krkn-chaos/krkn-hub:$(params.chaostest)
params:
- name: KUBECONFIG
description: Path to Kubeconfig secret
- name: chaostest
description: Namespace to deploy app to for testing
- name: credentials
description: A volume to which the remote cluster credentials will be written.
env:
- name: NAMESPACE
value: $(params.chaostest)
- name: KUBECONFIG
value: $(params.KUBECONFIG)
- name: KRKN_KUBE_CONFIG
value: $(params.KUBECONFIG)
- name: DISTRIBUTION
value: 'openshift'
- name: TEST
value: $(params.chaostest)
volumeMounts:
- name: "$(params.credentials)"
mountPath: /credentials
script: |
export
export DURATION=60
export DISTRIBUTION='openshift'
echo $TEST
test=$TEST
function get_pod_name(){
readarray -t pod_array < <(oc get pods -n $NAMESPACE --field-selector=status.phase=Running -o custom-columns=:metadata.name --no-headers=true)
if [[ ${#pod_array[@]} -eq 0 ]] ;then
exit 1
fi
random_index=$((RANDOM % ${#pod_array[@]}))
pod_test=${pod_array[$random_index]}
echo $pod_test
}
function get_pod_label(){
pod_test=$1
output=$(kubectl get pods $pod_test -n $NAMESPACE -o custom-columns=:metadata.labels --no-headers=true)
pod_label=''
max=500;for i in $(echo $output | sed 's/^map\[//; s/\]$//'| sed 's/:/=/g');do min=$(kubectl get pods -l $i -n $NAMESPACE -o custom-columns=:metadata.name --no-headers=true|wc -l) ; if [[ min -eq 1 ]] || [[ min -le max ]];then max=$min;pod_label=$i;fi;done;
echo $pod_label
}
if [[ ${test} == 'application-outages' ]] ;then
echo "in app outage"
pod_test=$(get_pod_name)
if [[ -z $pod_test ]] ; then
echo "no pod found"
exit 1
fi
export POD_SELECTOR=$(oc get pods -n $NAMESPACE $pod_test -o jsonpath='{.metadata.labels}')
echo $POD_SELECTOR
elif [[ ${test} == 'container-scenarios' ]] ;then
echo "in container-scenarios"
pod_test=$(get_pod_name)
if [[ -z $pod_test ]] ; then
echo "no pod found"
exit 1
fi
export LABEL_SELECTOR=$(get_pod_label ${pod_test})
echo $LABEL_SELECTOR
readarray -t con_array < <(kubectl get pod $pod_test -n $NAMESPACE -o jsonpath='{.status.containerStatuses[?(@.state.running)].name}')
random_index=$((RANDOM % ${#con_array[@]}))
export CONTAINER_NAME=${con_array[$random_index]}
echo $CONTAINER_NAME
elif [[ ${test} == 'pod-scenarios' ]] ;then
echo "in pod-scenarios"
pod_test=$(get_pod_name)
if [[ -z $pod_test ]] ; then
echo "no pod found"
exit 1
fi
export POD_LABEL=$(get_pod_label ${pod_test})
echo $POD_LABEL
elif [[ ${test} == 'service-disruption-scenarios' ]] ;then
echo "in service-disruption-scenarios"
elif [[ ${test} == 'pvc-scenarios' ]] ;then
echo "in pvc-scenarios"
readarray -t pvc_array < <(kubectl get pvc -n $NAMESPACE -o jsonpath='{range .items[?(@.status.phase=="Bound")]}{.metadata.name}{"\n"}{end}')
random_index=$((RANDOM % ${#pvc_array[@]}))
export PVC_NAME=${pvc_array[$random_index]}
echo $PVC_NAME
elif [[ ${test} == 'time-scenarios' ]] ;then
echo "in time-scenarios"
pod_test=$(get_pod_name)
if [[ -z $pod_test ]] ; then
echo "no pod found"
exit 1
fi
export LABEL_SELECTOR=$(get_pod_label ${pod_test})
echo $LABEL_SELECTOR
elif [[ ${test} == 'pod-network-chaos' ]] ;then
echo "in pod-network-chaos"
pod_test=$(get_pod_name)
if [[ -z $pod_test ]] ; then
echo "no pod found"
exit 1
fi
export LABEL_SELECTOR=$(get_pod_label ${pod_test})
echo $LABEL_SELECTOR
elif [[ ${test} == 'node-cpu-hog' ]] ;then
echo "in node-cpu-hog"
pod_test=$(get_pod_name)
if [[ -z $pod_test ]] ; then
echo "no pod found"
exit 1
fi
host_ip=$(oc get pods -n $NAMESPACE $pod_test -o custom-columns=:status.hostIP --no-headers=true)
node_name=$(kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.addresses[?(@.address=="$host_ip")].address}{"\n"}{end}' | grep $host_ip | awk '{print $1}')
export NODE_SELECTORS="kubernetes.io/hostname=${node_name}"
echo $NODE_SELECTORS
elif [[ ${test} == 'network-chaos' ]] ;then
echo "in network-chaos"
pod_test=$(get_pod_name)
if [[ -z $pod_test ]] ; then
echo "no pod found"
exit 1
fi
host_ip=$(oc get pods -n $NAMESPACE $pod_test -o custom-columns=:status.hostIP --no-headers=true)
export NODE_NAME=$(kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.addresses[?(@.address=="$host_ip")].address}{"\n"}{end}' | grep $host_ip | awk '{print $1}')
echo $NODE_NAME
else
echo "invalid testcase name"
exit 1
fi
start_time=$(date +%s)
/home/krkn/run.sh
TOKEN=$(oc create token prometheus-k8s -n openshift-monitoring)
URL=$(oc get route thanos-querier --template='{{.spec.host}}' -n openshift-monitoring)
QUERY="kube_pod_status_phase{namespace='$NAMESPACE', phase='Running'} == 1 and kube_pod_status_phase{namespace='$NAMESPACE', phase='Running'} != nan"
Bearer_TOKEN="Authorization: Bearer $TOKEN"
start_promql="https://${URL}/api/v1/query_range?start=${start_time}&end=${start_time}&step=1m&format=csv"
end_promql="https://${URL}/api/v1/query_range?start=$(date +%s)&end=$(date +%s)&step=1m&format=csv"
namespace="sum(kube_namespace_status_phase{namespace='$NAMESPACE',phase='Active'})"
deployment="sum(kube_deployment_status_condition{namespace='$NAMESPACE',condition='Available'})"
replicaset="count(kube_replicaset_status_ready_replicas{namespace='$NAMESPACE'})"
statefulset="sum(kube_statefulset_status_replicas_ready{namespace='$NAMESPACE'})"
service="sum(kube_service_info{namespace='$NAMESPACE'})"
network_policy="count(kube_networkpolicy_spec_egress_rules{namespace='$NAMESPACE'})"
persistent_volume="sum(kube_persistentvolumeclaim_status_phase{namespace='$NAMESPACE',phase='Bound'})"
pod="sum(kube_pod_status_phase{namespace='$NAMESPACE', phase='Running'} == 1 and kube_pod_status_phase{namespace='$NAMESPACE', phase='Running'} != nan)"
end_time=$(date +%s)
diff_time=$((end_time - start_time))
difference_in_minutes=$((diff_time/ 60))
for i in namespace deployment replicaset statefulset service network_policy persistent_volume pod;do
old=$(curl -s -k -H "$Bearer_TOKEN" "$start_promql" -d "query=${!i}"|jq -r '.data.result[0].values[0][1]')
new=$(curl -s -k -H "$Bearer_TOKEN" "$end_promql" -d "query=${!i}"|jq -r '.data.result[0].values[0][1]')
if [[ $old == $new ]]; then
echo "no change in ${i} count"
else
echo "Change in ${i} count"
fi
done
for i in $( curl -s -k -H "$Bearer_TOKEN" "$start_promql" -d "query=$QUERY"| jq -r '.data.result[].metric.pod'); do
#oc delete pod ${i} -n test1
pod_query="kube_pod_info{namespace='$NAMESPACE',pod='$i'}"
old=$(curl -s -k -H "$Bearer_TOKEN" "$start_promql" -d "query=$pod_query" | jq -r '.data.result[].metric.pod_ip')
new=$(curl -s -k -H "$Bearer_TOKEN" "$end_promql" -d "query=$pod_query" | jq -r '.data.result[].metric.pod_ip')
if [[ $old == $new ]]; then
echo "no change in ip of the pod $i"
a=1
else
echo "Change in ip of the pod $i"
a=1
fi
old=$(curl -s -k -H "$Bearer_TOKEN" "$start_promql" -d "query=$pod_query" | jq -r '.data.result[].metric.host_ip')
new=$(curl -s -k -H "$Bearer_TOKEN" "$end_promql" -d "query=$pod_query" | jq -r '.data.result[].metric.host_ip')
if [[ $old == $new ]]; then
echo "no change in host ip of the pod $i"
a=1
else
echo "Change in host ip of the pod" $i
a=1
fi
done
for i in $( curl -s -k -H "$Bearer_TOKEN" "$end_promql" -d "query=$QUERY"| jq -r '.data.result[].metric.pod'); do
kubectl logs -n $NAMESPACE $i --since=${difference_in_minutes}m
done
kubectl get events -n $NAMESPACE -o json | jq --argjson timestamp "$start_time" '.items[] | select((.metadata.creationTimestamp | fromdateiso8601) >= $timestamp) | {timestamp: .metadata.creationTimestamp, type: .type, reason: .reason, message: .message}'