OpenshiftBuildService implements BuildService { @Override public void build(ImageConfiguration imageConfig) throws Fabric8ServiceException { String buildName = null; try { ImageName imageName = new ImageName(imageConfig.getName()); File dockerTar = createBuildArchive(imageConfig); KubernetesListBuilder builder = new KubernetesListBuilder(); String openshiftPullSecret = config.getOpenshiftPullSecret(); Boolean usePullSecret = checkOrCreatePullSecret(config, client, builder, openshiftPullSecret, imageConfig); if (usePullSecret) { buildName = updateOrCreateBuildConfig(config, client, builder, imageConfig, openshiftPullSecret); } else { buildName = updateOrCreateBuildConfig(config, client, builder, imageConfig, null); } checkOrCreateImageStream(config, client, builder, getImageStreamName(imageName)); applyResourceObjects(config, client, builder); Build build = startBuild(client, dockerTar, buildName); waitForOpenShiftBuildToComplete(client, build); addImageStreamToFile(getImageStreamFile(config), imageName, client); } catch (Fabric8ServiceException e) { throw e; } catch (Exception ex) { if (ex != null && ex.getCause() instanceof IOException) { log.error("Build for %s failed: %s", buildName, ex.getCause().getMessage()); logBuildFailure(client, buildName); } else { throw new Fabric8ServiceException("Unable to build the image using the OpenShift build service", ex); } } } OpenshiftBuildService(OpenShiftClient client, Logger log, ServiceHub dockerServiceHub, BuildServiceConfig config); @Override void build(ImageConfiguration imageConfig); @Override void postProcess(BuildServiceConfig config); static final String DEFAULT_S2I_SOURCE_TYPE; }
@Test public void testSuccessfulBuild() throws Exception { retryInMockServer(() -> { BuildService.BuildServiceConfig config = defaultConfig.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, false, false); OpenShiftMockServer mockServer = collector.getMockServer(); DefaultOpenShiftClient client = (DefaultOpenShiftClient) mockServer.createOpenShiftClient(); LOG.info("Current write timeout is : {}", client.getHttpClient().writeTimeoutMillis()); LOG.info("Current read timeout is : {}", client.getHttpClient().readTimeoutMillis()); LOG.info("Retry on failure : {}", client.getHttpClient().retryOnConnectionFailure()); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); service.build(image); assertTrue(mockServer.getRequestCount() > 8); collector.assertEventsRecordedInOrder("build-config-check", "new-build-config", "pushed"); assertEquals("{\"apiVersion\":\"build.openshift.io/v1\",\"kind\":\"BuildConfig\",\"metadata\":{\"name\":\"myapp-s2i-suffix2\"},\"spec\":{\"output\":{\"to\":{\"kind\":\"ImageStreamTag\",\"name\":\"myapp:latest\"}},\"source\":{\"type\":\"Binary\"},\"strategy\":{\"sourceStrategy\":{\"forcePull\":false,\"from\":{\"kind\":\"DockerImage\",\"name\":\"myapp\"}},\"type\":\"Source\"}}}", collector.getBodies().get(1)); collector.assertEventsNotRecorded("patch-build-config"); }); }
@Test public void testSuccessfulBuildNoS2iSuffix() throws Exception { retryInMockServer(() -> { BuildService.BuildServiceConfig config = defaultConfig .s2iBuildNameSuffix(null) .build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, false, false); OpenShiftMockServer mockServer = collector.getMockServer(); DefaultOpenShiftClient client = (DefaultOpenShiftClient) mockServer.createOpenShiftClient(); LOG.info("Current write timeout is : {}", client.getHttpClient().writeTimeoutMillis()); LOG.info("Current read timeout is : {}", client.getHttpClient().readTimeoutMillis()); LOG.info("Retry on failure : {}", client.getHttpClient().retryOnConnectionFailure()); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); service.build(image); assertTrue(mockServer.getRequestCount() > 8); collector.assertEventsRecordedInOrder("build-config-check", "new-build-config", "pushed"); assertEquals("{\"apiVersion\":\"build.openshift.io/v1\",\"kind\":\"BuildConfig\",\"metadata\":{\"name\":\"myapp-s2i\"},\"spec\":{\"output\":{\"to\":{\"kind\":\"ImageStreamTag\",\"name\":\"myapp:latest\"}},\"source\":{\"type\":\"Binary\"},\"strategy\":{\"sourceStrategy\":{\"forcePull\":false,\"from\":{\"kind\":\"DockerImage\",\"name\":\"myapp\"}},\"type\":\"Source\"}}}", collector.getBodies().get(1)); collector.assertEventsNotRecorded("patch-build-config"); }); }
@Test public void testDockerBuild() throws Exception { retryInMockServer(() -> { final io.fabric8.maven.docker.service.BuildService.BuildContext context = new io.fabric8.maven.docker.service.BuildService.BuildContext.Builder() .registryConfig(new RegistryService.RegistryConfig.Builder().build()) .build(); BuildService.BuildServiceConfig.Builder dockerConfig = new BuildService.BuildServiceConfig.Builder() .buildDirectory(baseDir) .dockerBuildContext(context) .buildRecreateMode(BuildRecreateMode.none) .s2iBuildNameSuffix("-docker") .openshiftBuildStrategy(OpenShiftBuildStrategy.docker) .dockerMojoParameters(dockerMojoParameters); BuildService.BuildServiceConfig config = dockerConfig.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, false, false); OpenShiftMockServer mockServer = collector.getMockServer(); DefaultOpenShiftClient client = (DefaultOpenShiftClient) mockServer.createOpenShiftClient(); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); service.build(image); assertTrue(mockServer.getRequestCount() > 8); collector.assertEventsRecordedInOrder("build-config-check", "new-build-config", "pushed"); assertEquals("{\"apiVersion\":\"build.openshift.io/v1\",\"kind\":\"BuildConfig\",\"metadata\":{\"name\":\"myapp-docker\"},\"spec\":{\"output\":{\"to\":{\"kind\":\"ImageStreamTag\",\"name\":\"myapp:latest\"}},\"source\":{\"type\":\"Binary\"},\"strategy\":{\"dockerStrategy\":{\"from\":{\"kind\":\"DockerImage\",\"name\":\"myapp\"}},\"type\":\"Docker\"}}}", collector.getBodies().get(1)); collector.assertEventsNotRecorded("patch-build-config"); }); }
@Test public void testDockerBuildNoS2iSuffix() throws Exception { retryInMockServer(() -> { final BuildContext context = new BuildContext.Builder() .registryConfig(new RegistryService.RegistryConfig.Builder().build()) .build(); final BuildService.BuildServiceConfig.Builder dockerConfig = new BuildService.BuildServiceConfig.Builder() .buildDirectory(baseDir) .dockerBuildContext(context) .buildRecreateMode(BuildRecreateMode.none) .openshiftBuildStrategy(OpenShiftBuildStrategy.docker) .dockerMojoParameters(dockerMojoParameters); final BuildService.BuildServiceConfig config = dockerConfig.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, false, false); OpenShiftMockServer mockServer = collector.getMockServer(); DefaultOpenShiftClient client = (DefaultOpenShiftClient) mockServer.createOpenShiftClient(); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); service.build(image); assertTrue(mockServer.getRequestCount() > 8); collector.assertEventsRecordedInOrder("build-config-check", "new-build-config", "pushed"); assertEquals("{\"apiVersion\":\"build.openshift.io/v1\",\"kind\":\"BuildConfig\",\"metadata\":{\"name\":\"myapp\"},\"spec\":{\"output\":{\"to\":{\"kind\":\"ImageStreamTag\",\"name\":\"myapp:latest\"}},\"source\":{\"type\":\"Binary\"},\"strategy\":{\"dockerStrategy\":{\"from\":{\"kind\":\"DockerImage\",\"name\":\"myapp\"}},\"type\":\"Docker\"}}}", collector.getBodies().get(1)); collector.assertEventsNotRecorded("patch-build-config"); }); }
@Test public void testDockerBuildFromExt() throws Exception { retryInMockServer(() -> { final io.fabric8.maven.docker.service.BuildService.BuildContext context = new io.fabric8.maven.docker.service.BuildService.BuildContext.Builder() .registryConfig(new RegistryService.RegistryConfig.Builder().build()) .build(); BuildService.BuildServiceConfig.Builder dockerConfig = new BuildService.BuildServiceConfig.Builder() .buildDirectory(baseDir) .dockerBuildContext(context) .buildRecreateMode(BuildRecreateMode.none) .s2iBuildNameSuffix("-docker") .openshiftBuildStrategy(OpenShiftBuildStrategy.docker) .dockerMojoParameters(dockerMojoParameters); BuildService.BuildServiceConfig config = dockerConfig.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, false, false); OpenShiftMockServer mockServer = collector.getMockServer(); DefaultOpenShiftClient client = (DefaultOpenShiftClient) mockServer.createOpenShiftClient(); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); Map<String,String> fromExt = ImmutableMap.of("name", "app:1.2-1", "kind", "ImageStreamTag", "namespace", "my-project"); ImageConfiguration fromExtImage = new ImageConfiguration.Builder() .name(projectName) .buildConfig(new BuildImageConfiguration.Builder() .fromExt(fromExt) .noCache(Boolean.TRUE) .build() ).build(); service.build(fromExtImage); assertTrue(mockServer.getRequestCount() > 8); collector.assertEventsRecordedInOrder("build-config-check", "new-build-config", "pushed"); assertEquals("{\"apiVersion\":\"build.openshift.io/v1\",\"kind\":\"BuildConfig\",\"metadata\":{\"name\":\"myapp-docker\"},\"spec\":{\"output\":{\"to\":{\"kind\":\"ImageStreamTag\",\"name\":\"myapp:latest\"}},\"source\":{\"type\":\"Binary\"},\"strategy\":{\"dockerStrategy\":{\"from\":{\"kind\":\"ImageStreamTag\",\"name\":\"app:1.2-1\",\"namespace\":\"my-project\"},\"noCache\":true},\"type\":\"Docker\"}}}", collector.getBodies().get(1)); collector.assertEventsNotRecorded("patch-build-config"); }); }
@Test public void testSuccessfulBuildSecret() throws Exception { retryInMockServer(() -> { BuildService.BuildServiceConfig config = defaultConfigSecret.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, false, false); OpenShiftMockServer mockServer = collector.getMockServer(); DefaultOpenShiftClient client = (DefaultOpenShiftClient) mockServer.createOpenShiftClient(); LOG.info("Current write timeout is : {}", client.getHttpClient().writeTimeoutMillis()); LOG.info("Current read timeout is : {}", client.getHttpClient().readTimeoutMillis()); LOG.info("Retry on failure : {}", client.getHttpClient().retryOnConnectionFailure()); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); service.build(image); assertTrue(mockServer.getRequestCount() > 8); collector.assertEventsRecordedInOrder("build-config-check", "new-build-config", "pushed"); collector.assertEventsNotRecorded("patch-build-config"); }); }
@Test(expected = Fabric8ServiceException.class) public void testFailedBuild() throws Exception { BuildService.BuildServiceConfig config = defaultConfig.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, false, 50, false, false); OpenShiftMockServer mockServer = collector.getMockServer(); OpenShiftClient client = mockServer.createOpenShiftClient(); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); service.build(image); }
@Test(expected = Fabric8ServiceException.class) public void testFailedBuildSecret() throws Exception { BuildService.BuildServiceConfig config = defaultConfigSecret.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, false, 50, false, false); OpenShiftMockServer mockServer = collector.getMockServer(); OpenShiftClient client = mockServer.createOpenShiftClient(); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); service.build(image); }
@Test public void testSuccessfulSecondBuild() throws Exception { retryInMockServer(() -> { BuildService.BuildServiceConfig config = defaultConfig.build(); WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true); OpenShiftMockServer mockServer = collector.getMockServer(); OpenShiftClient client = mockServer.createOpenShiftClient(); OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config); service.build(image); assertTrue(mockServer.getRequestCount() > 8); collector.assertEventsRecordedInOrder("build-config-check", "patch-build-config", "pushed"); collector.assertEventsNotRecorded("new-build-config"); }); }