Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add getter for checking if dockerHost in DefaultDockerClientConfig.Bu…
…ilder has default value
  • Loading branch information
kiview committed Aug 23, 2021
commit 28323adee5ebc9f79e0cedf984123eb878b9e614
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ public final Builder withDockerHost(String dockerHost) {
return this;
}

/**
* Considered default if dockerHost is set to the default value.
*/
public final boolean hasDefaultDockerHost() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's change it to isDockerHostSetExplicitly(), to detect user's intention to override it (even if set to the default value)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, will invert the logic (changed it to usesImplicitDockerHost() before).
As you can see, the checks are all over the place in DefaultDockerClientConfig now, I lack the idea of a clean way to capture all places atm.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered removing docker.host from DEFAULT_PROPERTIES, using dockerHost != null to detect explicit value, and then passing dockerHost != null ? dockerHost : DEFAULT_DOCKER_HOST to DefaultDockerClientConfig from build()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would work nicely if dockerHost could be null in Builder 😐
But I will check further in this in this direction.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't it be? :)

@kiview kiview Aug 23, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can now. However, it changes the contract of Builder.withDockerHost(), meaning it will only do parsing of host URI on build().

I would consider this an acceptable tradeoff for the dramatically simplified changeset.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep the validation, and call it from withProperties only if the value is not null

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, works nicely.

URI defaultDockerHost = URI.create(DEFAULT_PROPERTIES.getProperty(DOCKER_HOST));
return this.dockerHost.equals(defaultDockerHost);
}

public final Builder withApiVersion(RemoteApiVersion apiVersion) {
this.apiVersion = apiVersion.getVersion();
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,49 @@ public void withDockerTlsVerify() throws Exception {
assertThat((Boolean) field.get(builder), is(true));
}

@Test
public void hasDefaultDockerHostRespectsSetter() {
DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder();
assertThat(builder.hasDefaultDockerHost(), is(true));

builder.withDockerHost("tcp://foo");
assertThat(builder.hasDefaultDockerHost(), is(false));
}

@Test
public void hasDefaultDockerHostRespectsSystemProperties() {
Properties systemProperties = new Properties();
systemProperties.put(DefaultDockerClientConfig.DOCKER_HOST, "tcp://foo");

DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder(Collections.emptyMap(), systemProperties);

assertThat(builder.hasDefaultDockerHost(), is(false));
}

@Test
public void hasDefaultDockerHostRespectsEnv() {
Map<String, String> env = new HashMap<>();
env.put(DefaultDockerClientConfig.DOCKER_HOST, "tcp://foo");

DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder(env, new Properties());

assertThat(builder.hasDefaultDockerHost(), is(false));
}

@Test
public void hasDefaultDockerHostConsideredAsDefaultIfSetToDefault() {
String defaultDockerHost = DefaultDockerClientConfig.DEFAULT_PROPERTIES
.getProperty(DefaultDockerClientConfig.DOCKER_HOST);

Map<String, String> env = new HashMap<>();
env.put(DefaultDockerClientConfig.DOCKER_HOST, defaultDockerHost);

DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder(env, new Properties());

assertThat(builder.hasDefaultDockerHost(), is(true));
}


@Test
public void testGetAuthConfigurationsFromDockerCfg() throws URISyntaxException {
File cfgFile = new File(Resources.getResource("com.github.dockerjava.core/registry.v1").toURI());
Expand Down