How to retry request to downstream based on response headers status code

The request here is that, I want to use retry feature in kong but it should be based on response headers.

Ex : if downstream system respond with status code as 502 then I want to retry 3times. - please let me know, do we have feasibility here ? if yes how to do that in Kubernets