Skip to content

Commit 4531d94

Browse files
authored
Merge pull request #3 from lreimer/master
Improved version command property mapping and tests.
2 parents 09119a4 + b359ed3 commit 4531d94

4 files changed

Lines changed: 53 additions & 3 deletions

File tree

docker-java-bridge-k8s/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Docker API to K8s Bridge
2+
3+
This implementation of the Docker API tries to bridge all calls to K8s. For example, a CreateContainer command should roughly translate to
4+
running a Pod in Kubernetes. The idea is kind of inspired by kubedock (https://github.com/joyrex2001/kubedock), a minimal implementation of
5+
the Docker API to run containers in a K8s cluster. Also, we take some inspiration from BuildKit CLI for
6+
Kubernetes (https://github.com/vmware-tanzu/buildkit-cli-for-kubectl).
7+

docker-java-bridge-k8s/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<version>13.0.1</version>
2929
</dependency>
3030

31+
<dependency>
32+
<groupId>commons-beanutils</groupId>
33+
<artifactId>commons-beanutils</artifactId>
34+
<version>1.9.4</version>
35+
</dependency>
36+
3137
<dependency>
3238
<groupId>org.slf4j</groupId>
3339
<artifactId>slf4j-api</artifactId>

docker-java-bridge-k8s/src/main/java/com/github/dockerjava/k8s/command/VersionCmdImpl.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22

33
import com.github.dockerjava.api.command.VersionCmd;
44
import com.github.dockerjava.api.exception.DockerException;
5+
import com.github.dockerjava.api.model.VersionPlatform;
56
import io.kubernetes.client.openapi.ApiClient;
67
import io.kubernetes.client.openapi.ApiException;
78
import io.kubernetes.client.openapi.models.VersionInfo;
89
import io.kubernetes.client.util.version.Version;
10+
import org.apache.commons.beanutils.BeanUtils;
11+
import org.apache.commons.beanutils.PropertyUtils;
12+
import org.apache.commons.lang.reflect.FieldUtils;
913
import org.apache.commons.lang3.builder.ToStringBuilder;
1014
import org.apache.commons.lang3.builder.ToStringStyle;
1115
import org.slf4j.Logger;
1216
import org.slf4j.LoggerFactory;
1317

1418
import java.io.IOException;
19+
import java.lang.reflect.InvocationTargetException;
20+
21+
import static org.apache.commons.beanutils.BeanUtils.setProperty;
22+
import static org.apache.commons.lang.reflect.FieldUtils.*;
1523

1624
public class VersionCmdImpl implements VersionCmd {
1725

@@ -35,6 +43,24 @@ public com.github.dockerjava.api.model.Version exec() {
3543
}
3644

3745
com.github.dockerjava.api.model.Version dockerVersion = new com.github.dockerjava.api.model.Version();
46+
try {
47+
writeField(dockerVersion, "goVersion", info.getGoVersion(), true);
48+
writeField(dockerVersion, "version", info.getGitVersion(), true);
49+
writeField(dockerVersion, "gitCommit", info.getGitCommit(), true);
50+
writeField(dockerVersion, "buildTime", info.getBuildDate(), true);
51+
52+
writeField(dockerVersion, "platform", new VersionPlatform().withName(info.getPlatform()), true);
53+
writeField(dockerVersion, "operatingSystem", info.getPlatform().split("/")[0], true);
54+
writeField(dockerVersion, "arch", info.getPlatform().split("/")[1], true);
55+
56+
writeField(dockerVersion, "apiVersion", info.getMajor() + "." + info.getMinor(), true);
57+
writeField(dockerVersion, "version", info.getMajor() + "." + info.getMinor(), true);
58+
59+
writeField(dockerVersion, "experimental", Boolean.FALSE, true);
60+
} catch (IllegalAccessException e) {
61+
throw new RuntimeException("Unable to set bean field.", e);
62+
}
63+
3864
return dockerVersion;
3965
}
4066

docker-java-bridge-k8s/src/test/java/com/github/dockerjava/k8s/command/VersionCmdImplTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,30 @@
1010

1111
import java.io.FileReader;
1212
import java.io.IOException;
13+
import java.nio.file.Paths;
1314

14-
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1517

1618
@ExtendWith(MockitoExtension.class)
1719
class VersionCmdImplTest {
1820

1921
@Test
2022
void execLocalKubeConfigClient() throws IOException {
21-
String kubeConfigPath = System.getenv("HOME") + "/.kube/config";
23+
String kubeConfigPath = Paths.get(System.getProperty("user.home"), ".kube", "config").toString();
2224
ApiClient client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath))).build();
2325

2426
VersionCmdImpl cmd = new VersionCmdImpl(client);
2527
Version version = cmd.exec();
26-
assertEquals(null, version.getGoVersion());
28+
29+
assertNotNull(version.getGoVersion());
30+
assertNotNull(version.getGitCommit());
31+
assertNotNull(version.getBuildTime());
32+
assertNotNull(version.getApiVersion());
33+
assertNotNull(version.getVersion());
34+
assertNotNull(version.getArch());
35+
assertNotNull(version.getOperatingSystem());
36+
assertNotNull(version.getPlatform());
37+
assertFalse(version.getExperimental());
2738
}
2839
}

0 commit comments

Comments
 (0)