use of io.fabric8.openshift.api.model.BuildStrategyBuilder in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildServiceTest method createMockServer.
protected WebServerEventCollector<OpenShiftMockServer> createMockServer(BuildService.BuildServiceConfig config, boolean success, long buildDelay, boolean buildConfigExists, boolean imageStreamExists) {
OpenShiftMockServer mockServer = new OpenShiftMockServer(false);
WebServerEventCollector<OpenShiftMockServer> collector = new WebServerEventCollector<>(mockServer);
final String s2iBuildNameSuffix = Optional.ofNullable(config.getS2iBuildNameSuffix()).orElseGet(() -> config.getOpenshiftBuildStrategy() == OpenShiftBuildStrategy.s2i ? "-s2i" : "");
BuildConfig bc = new BuildConfigBuilder().withNewMetadata().withName(projectName + s2iBuildNameSuffix).endMetadata().withNewSpec().endSpec().build();
BuildConfig bcSecret = null;
if (config.getOpenshiftPullSecret() != null) {
bcSecret = new BuildConfigBuilder().withNewMetadata().withName(projectName + s2iBuildNameSuffix + "pullSecret").endMetadata().withNewSpec().withStrategy(new BuildStrategyBuilder().withType("Docker").withNewDockerStrategy().withNewPullSecret(config.getOpenshiftPullSecret()).endDockerStrategy().build()).endSpec().build();
}
ImageStream imageStream = new ImageStreamBuilder().withNewMetadata().withName(projectName).endMetadata().withStatus(new ImageStreamStatusBuilder().addNewTagLike(new NamedTagEventListBuilder().addNewItem().withImage("abcdef0123456789").endItem().build()).endTag().build()).build();
KubernetesList builds = new KubernetesListBuilder().withItems(new BuildBuilder().withNewMetadata().withName(projectName).endMetadata().build()).withNewMetadata().withResourceVersion("1").endMetadata().build();
String buildStatus = success ? "Complete" : "Fail";
Build build = new BuildBuilder().withNewMetadata().withResourceVersion("2").endMetadata().withNewStatus().withPhase(buildStatus).endStatus().build();
if (!buildConfigExists) {
mockServer.expect().get().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs/" + projectName + s2iBuildNameSuffix).andReply(collector.record("build-config-check").andReturn(404, "")).once();
mockServer.expect().get().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs/" + projectName + s2iBuildNameSuffix + "pullSecret").andReply(collector.record("build-config-check").andReturn(404, "")).once();
mockServer.expect().post().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs").andReply(collector.record("new-build-config").andReturn(201, bc)).once();
if (bcSecret != null) {
mockServer.expect().post().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs").andReply(collector.record("new-build-config").andReturn(201, bcSecret)).once();
}
} else {
mockServer.expect().patch().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs/" + projectName + s2iBuildNameSuffix).andReply(collector.record("patch-build-config").andReturn(200, bc)).once();
if (bcSecret != null) {
mockServer.expect().patch().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs/" + projectName + s2iBuildNameSuffix + "pullSecret").andReply(collector.record("patch-build-config").andReturn(200, bcSecret)).once();
}
}
mockServer.expect().get().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs/" + projectName + s2iBuildNameSuffix).andReply(collector.record("build-config-check").andReturn(200, bc)).always();
if (bcSecret != null) {
mockServer.expect().get().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs/" + projectName + s2iBuildNameSuffix + "pullSecret").andReply(collector.record("build-config-check").andReturn(200, bcSecret)).always();
}
if (!imageStreamExists) {
mockServer.expect().get().withPath("/apis/image.openshift.io/v1/namespaces/test/imagestreams/" + projectName).andReturn(404, "").once();
}
mockServer.expect().get().withPath("/apis/image.openshift.io/v1/namespaces/test/imagestreams/" + projectName).andReturn(200, imageStream).always();
mockServer.expect().post().withPath("/apis/image.openshift.io/v1/namespaces/test/imagestreams").andReturn(201, imageStream).once();
mockServer.expect().post().withPath("/apis/build.openshift.io/v1/namespaces/test/buildconfigs/" + projectName + s2iBuildNameSuffix + "/instantiatebinary?commit=").andReply(collector.record("pushed").andReturn(201, imageStream)).once();
mockServer.expect().get().withPath("/apis/build.openshift.io/v1/namespaces/test/builds").andReply(collector.record("check-build").andReturn(200, builds)).always();
mockServer.expect().get().withPath("/apis/build.openshift.io/v1/namespaces/test/builds?labelSelector=openshift.io/build-config.name%3D" + projectName + s2iBuildNameSuffix).andReturn(200, builds).always();
mockServer.expect().withPath("/apis/build.openshift.io/v1/namespaces/test/builds/" + projectName).andReturn(200, build).always();
mockServer.expect().withPath("/apis/build.openshift.io/v1/namespaces/test/builds?fieldSelector=metadata.name%3D" + projectName + "&watch=true").andUpgradeToWebSocket().open().waitFor(buildDelay).andEmit(new WatchEvent(build, "MODIFIED")).done().always();
return collector;
}
use of io.fabric8.openshift.api.model.BuildStrategyBuilder in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method createBuildStrategy.
private BuildStrategy createBuildStrategy(ImageConfiguration imageConfig, OpenShiftBuildStrategy osBuildStrategy, String openshiftPullSecret) {
if (osBuildStrategy == OpenShiftBuildStrategy.docker) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
Map<String, String> fromExt = buildConfig.getFromExt();
String fromName, fromKind, fromNamespace;
if (buildConfig.isDockerFileMode()) {
fromName = extractBaseFromDockerfile(buildConfig, config.getDockerBuildContext());
} else {
fromName = getMapValueWithDefault(fromExt, OpenShiftBuildStrategy.SourceStrategy.name, buildConfig.getFrom());
}
fromKind = getMapValueWithDefault(fromExt, OpenShiftBuildStrategy.SourceStrategy.kind, "DockerImage");
fromNamespace = getMapValueWithDefault(fromExt, OpenShiftBuildStrategy.SourceStrategy.namespace, "ImageStreamTag".equals(fromKind) ? "openshift" : null);
BuildStrategy buildStrategy = new BuildStrategyBuilder().withType("Docker").withNewDockerStrategy().withNewFrom().withKind(fromKind).withName(fromName).withNamespace(StringUtils.isEmpty(fromNamespace) ? null : fromNamespace).endFrom().withNoCache(checkForNocache(imageConfig)).withEnv(checkForEnv(imageConfig)).endDockerStrategy().build();
if (openshiftPullSecret != null) {
buildStrategy.getDockerStrategy().setPullSecret(new LocalObjectReferenceBuilder().withName(openshiftPullSecret).build());
}
return buildStrategy;
} else if (osBuildStrategy == OpenShiftBuildStrategy.s2i) {
BuildImageConfiguration buildConfig = imageConfig.getBuildConfiguration();
Map<String, String> fromExt = buildConfig.getFromExt();
String fromName, fromKind, fromNamespace;
if (buildConfig.isDockerFileMode()) {
fromName = extractBaseFromDockerfile(buildConfig, config.getDockerBuildContext());
} else {
fromName = getMapValueWithDefault(fromExt, OpenShiftBuildStrategy.SourceStrategy.name, buildConfig.getFrom());
}
fromKind = getMapValueWithDefault(fromExt, OpenShiftBuildStrategy.SourceStrategy.kind, "DockerImage");
fromNamespace = getMapValueWithDefault(fromExt, OpenShiftBuildStrategy.SourceStrategy.namespace, "ImageStreamTag".equals(fromKind) ? "openshift" : null);
BuildStrategy buildStrategy = new BuildStrategyBuilder().withType("Source").withNewSourceStrategy().withNewFrom().withKind(fromKind).withName(fromName).withNamespace(StringUtils.isEmpty(fromNamespace) ? null : fromNamespace).endFrom().withForcePull(config.isForcePullEnabled()).endSourceStrategy().build();
if (openshiftPullSecret != null) {
buildStrategy.getSourceStrategy().setPullSecret(new LocalObjectReferenceBuilder().withName(openshiftPullSecret).build());
}
return buildStrategy;
} else {
throw new IllegalArgumentException("Unsupported BuildStrategy " + osBuildStrategy);
}
}
Aggregations