Class: Kamal::Lint::Checks::ImageRegistryMismatch

Inherits:
Kamal::Lint::Check show all
Defined in:
lib/kamal/lint/checks/image_registry_mismatch.rb

Constant Summary collapse

DOCKER_HUB_HOSTS =

Docker Hub accepts unprefixed images (myorg/myapp resolves to docker.io/myorg/myapp), so we don't flag a missing prefix when the configured registry is Docker Hub under any of its canonical names.

%w[docker.io index.docker.io registry.hub.docker.com].freeze

Instance Attribute Summary

Attributes inherited from Kamal::Lint::Check

#context

Instance Method Summary collapse

Methods inherited from Kamal::Lint::Check

applies_to?, id, #initialize, severity, since, title, until_version

Constructor Details

This class inherits a constructor from Kamal::Lint::Check

Instance Method Details

#callObject

A leading path segment is treated as a registry host when it contains a . or : (e.g. ghcr.io, localhost:5000) — Kamal sees that as a host and won't add the configured server in front of it.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kamal/lint/checks/image_registry_mismatch.rb', line 20

def call
  image = parsed["image"]
  registry = parsed["registry"] || parsed.dig("builder", "registry") || {}
  server = registry["server"] if registry.is_a?(Hash)
  return [] unless image.is_a?(String) && server.is_a?(String) && !server.empty?

  normalized_server = server.sub(%r{/+\z}, "")
  return [] if DOCKER_HUB_HOSTS.include?(normalized_server)

  first_segment = image.split("/", 2).first.to_s
  looks_like_host = first_segment.include?(".") || first_segment.include?(":")

  # Unprefixed `org/repo` is the canonical Kamal style — the registry
  # server is prepended automatically. Only flag when the image already
  # carries a registry host that disagrees with `registry.server`.
  return [] unless looks_like_host
  return [] if image.start_with?("#{normalized_server}/")

  [ finding(
    message: "image `#{image}` is prefixed with a registry host that disagrees with `registry.server: #{server}`; Kamal will push to the wrong registry",
    line: context.line_for([ "image" ])
  ) ]
end