Docker's registry service validates and resolves image tags before fetching their manifests. A Backend Engineer candidate is shown this Go function and told a single bad request from a client recently took down the whole registry process.
func ProcessImageTag(registry string, image string, tag string) *Manifest {
if registry != "" {
if image != "" {
if tag != "" {
if len(tag) < 128 {
if tag != "latest" {
fullRef := registry + "/" + image + ":" + tag
manifest, _ := fetchManifest(fullRef)
log.Printf("fetched manifest for %s", fullRef)
return manifest
} else {
fullRef := registry + "/" + image + ":latest"
manifest, _ := fetchManifest(fullRef)
log.Printf("fetched manifest for %s", fullRef)
return manifest
}
} else {
panic("tag too long")
}
} else {
panic("tag required")
}
} else {
panic("image required")
}
} else {
panic("registry required")
}
return nil
}
Refactor this to improve its readability and maintainability. Explain what's wrong with the current version, including whatever caused the outage, and what you'd change.
Implement the validation portion as process_image_reference(registry, image, tag). Return a dictionary containing either reference or error; fetching the manifest is outside this function.
def process_image_reference(registry, image, tag):