jeudi 14 octobre 2021

What is a suitable design pattern for implementing gating logic in CI/CD pipeline?

The applications/services on the CI/CD pipeline needs to pass through some checks/gates before getting deployed. It could be quality checks, vulnerability checks, compliance checks etc. I am implementing a REST API which needs to check for all the gates and return true only if all the checks are passed.It should also support checking for an individual gate.

I am using Golang for the implementation. I got it working using if blocks but it doesn't look neat nor scalable.

/api/v1/gate?type=vulnerability&service=xyz // Check just the vulnerability gate
/api/v1/gate?type=all&service=xyz           // Check all gates
if gateType == "vulnerability" || gateType == "all" {
  // Check for vulnerabilities
  passesCheck := CheckForVulnerabilities(gateType, service)
  if !passesCheck {
    return false
  }

if gateType == "compliance" || gateType == "all" {
  passesCheck := CheckForCompliance(gateType, service)
  if !passesCheck {
    return false
    }
}

return true

I definitely see a repeating pattern here and this implementation can be generalised. I was thinking on the lines of a middleware, where a requests goes through multiple registered middlewares and fails at the 1st failure but somehow its not fitting well with my requirement.

Can someone suggest a generic way of implementing this? I was looking at some design patterns and the "Chain of Responsibility" is the closest pattern I could relate to. Is that the right approach? Any suggestions and advice are welcome.

Aucun commentaire:

Enregistrer un commentaire