Search in sources :

Example 1 with BuildStrategyBuilder

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;
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) ImageStream(io.fabric8.openshift.api.model.ImageStream) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) WebServerEventCollector(io.fabric8.maven.core.util.WebServerEventCollector) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) BuildStrategyBuilder(io.fabric8.openshift.api.model.BuildStrategyBuilder) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Build(io.fabric8.openshift.api.model.Build) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) OpenshiftBuildConfig(io.fabric8.maven.core.config.OpenshiftBuildConfig) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent)

Example 2 with BuildStrategyBuilder

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);
    }
}
Also used : BuildStrategyBuilder(io.fabric8.openshift.api.model.BuildStrategyBuilder) BuildStrategy(io.fabric8.openshift.api.model.BuildStrategy) OpenShiftBuildStrategy(io.fabric8.maven.core.config.OpenShiftBuildStrategy) LocalObjectReferenceBuilder(io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder) Map(java.util.Map) HashMap(java.util.HashMap) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration)

Aggregations

BuildStrategyBuilder (io.fabric8.openshift.api.model.BuildStrategyBuilder)2 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)1 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)1 LocalObjectReferenceBuilder (io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder)1 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)1 OpenShiftBuildStrategy (io.fabric8.maven.core.config.OpenShiftBuildStrategy)1 OpenshiftBuildConfig (io.fabric8.maven.core.config.OpenshiftBuildConfig)1 WebServerEventCollector (io.fabric8.maven.core.util.WebServerEventCollector)1 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)1 Build (io.fabric8.openshift.api.model.Build)1 BuildBuilder (io.fabric8.openshift.api.model.BuildBuilder)1 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)1 BuildConfigBuilder (io.fabric8.openshift.api.model.BuildConfigBuilder)1 BuildStrategy (io.fabric8.openshift.api.model.BuildStrategy)1 ImageStream (io.fabric8.openshift.api.model.ImageStream)1 ImageStreamBuilder (io.fabric8.openshift.api.model.ImageStreamBuilder)1 ImageStreamStatusBuilder (io.fabric8.openshift.api.model.ImageStreamStatusBuilder)1 NamedTagEventListBuilder (io.fabric8.openshift.api.model.NamedTagEventListBuilder)1 OpenShiftMockServer (io.fabric8.openshift.client.server.mock.OpenShiftMockServer)1 HashMap (java.util.HashMap)1