Skip to content

Commit

Permalink
feat: add option to overwrite Docker read and open timeout values
Browse files Browse the repository at this point in the history
  • Loading branch information
yeikel committed Jan 29, 2025
1 parent 1cf9d09 commit a2ed997
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docker/lib/dependabot/docker/update_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,30 @@ def docker_repo_name
"library/#{dependency.name}"
end

# Defaults from https://github.com/deitch/docker_registry2/blob/bfde04144f0b7fd63c156a1aca83efe19ee78ffd/lib/registry/registry.rb#L26-L27
DEFAULT_DOCKER_OPEN_TIMEOUT_IN_SECONDS = 2
DEFAULT_DOCKER_READ_TIMEOUT_IN_SECONDS = 5

def docker_registry_client
@docker_registry_client ||=
DockerRegistry2::Registry.new(
"https://#{registry_hostname}",
user: registry_credentials&.fetch("username", nil),
password: registry_credentials&.fetch("password", nil),
read_timeout: 10,
read_timeout: docker_read_timeout_in_seconds,
open_timeout: docker_open_timeout_in_seconds,
http_options: { proxy: ENV.fetch("HTTPS_PROXY", nil) }
)
end

def docker_open_timeout_in_seconds
ENV.fetch("DEPENDABOT_DOCKER_OPEN_TIMEOUT_IN_SECONDS", DEFAULT_DOCKER_OPEN_TIMEOUT_IN_SECONDS).to_i
end

def docker_read_timeout_in_seconds
ENV.fetch("DEPENDABOT_DOCKER_READ_TIMEOUT_IN_SECONDS", DEFAULT_DOCKER_READ_TIMEOUT_IN_SECONDS).to_i
end

def sort_tags(candidate_tags, version_tag)
candidate_tags.sort do |tag_a, tag_b|
if comparable_version_from(tag_a) > comparable_version_from(tag_b)
Expand Down
34 changes: 34 additions & 0 deletions docker/spec/dependabot/docker/update_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,40 @@ def stub_tag_with_no_digest(tag)
end
end

describe ".docker_read_timeout_in_seconds" do
context "when DEPENDABOT_DOCKER_READ_TIMEOUT_IN_SECONDS is set" do
it "returns the provided value" do
override_value = 10
stub_const("ENV", ENV.to_hash.merge("DEPENDABOT_DOCKER_READ_TIMEOUT_IN_SECONDS" => override_value))
expect(checker.send(:docker_read_timeout_in_seconds)).to eq(override_value)
end
end

context "when ENV does not provide an override" do
it "falls back to a default value" do
expect(checker.send(:docker_read_timeout_in_seconds))
.to eq(Dependabot::Docker::UpdateChecker::DEFAULT_DOCKER_READ_TIMEOUT_IN_SECONDS)
end
end
end

describe ".docker_open_timeout_in_seconds" do
context "when DEPENDABOT_DOCKER_OPEN_TIMEOUT_IN_SECONDS is set" do
it "returns the provided value" do
override_value = 10
stub_const("ENV", ENV.to_hash.merge("DEPENDABOT_DOCKER_OPEN_TIMEOUT_IN_SECONDS" => override_value))
expect(checker.send(:docker_open_timeout_in_seconds)).to eq(override_value)
end
end

context "when ENV does not provide an override" do
it "falls back to a default value" do
expect(checker.send(:docker_open_timeout_in_seconds))
.to eq(Dependabot::Docker::UpdateChecker::DEFAULT_DOCKER_OPEN_TIMEOUT_IN_SECONDS)
end
end
end

private

def stub_same_sha_for(*tags)
Expand Down

0 comments on commit a2ed997

Please sign in to comment.