Search in sources :

Example 61 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class Fabric8ServiceHub method init.

private void init() {
    Objects.requireNonNull(clusterAccess, "clusterAccess");
    Objects.requireNonNull(log, "log");
    this.resolvedMode = clusterAccess.resolvePlatformMode(platformMode, log);
    if (resolvedMode != PlatformMode.kubernetes && resolvedMode != PlatformMode.openshift) {
        throw new IllegalArgumentException("Unknown platform mode " + platformMode + " resolved as " + resolvedMode);
    }
    this.client = clusterAccess.createDefaultClient(log);
    if (this.controller == null) {
        this.controller = new Controller(this.client);
        this.controller.setThrowExceptionOnError(true);
    }
    // Lazily building services
    this.services.putIfAbsent(ClientToolsService.class, new LazyBuilder<ClientToolsService>() {

        @Override
        protected ClientToolsService build() {
            return new ClientToolsService(controller, log);
        }
    });
    this.services.putIfAbsent(PortForwardService.class, new LazyBuilder<PortForwardService>() {

        @Override
        protected PortForwardService build() {
            return new PortForwardService(getClientToolsService(), log, client);
        }
    });
    this.services.putIfAbsent(BuildService.class, new LazyBuilder<BuildService>() {

        @Override
        protected BuildService build() {
            BuildService buildService;
            // Creating platform-dependent services
            if (resolvedMode == PlatformMode.openshift) {
                // Openshift services
                buildService = new OpenshiftBuildService((OpenShiftClient) client, log, dockerServiceHub, buildServiceConfig);
            } else {
                // Kubernetes services
                buildService = new DockerBuildService(dockerServiceHub, buildServiceConfig);
            }
            return buildService;
        }
    });
    this.services.putIfAbsent(ArtifactResolverService.class, new LazyBuilder<ArtifactResolverService>() {

        @Override
        protected ArtifactResolverService build() {
            return new ArtifactResolverServiceMavenImpl(repositorySystem, mavenProject);
        }
    });
}
Also used : OpenshiftBuildService(io.fabric8.maven.core.service.openshift.OpenshiftBuildService) DockerBuildService(io.fabric8.maven.core.service.kubernetes.DockerBuildService) DockerBuildService(io.fabric8.maven.core.service.kubernetes.DockerBuildService) OpenshiftBuildService(io.fabric8.maven.core.service.openshift.OpenshiftBuildService) Controller(io.fabric8.kubernetes.api.Controller)

Example 62 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method applyResourceObjects.

private void applyResourceObjects(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder) throws Exception {
    // Adding a workaround to handle intermittent Socket closed errors while
    // building on OpenShift. See https://github.com/fabric8io/fabric8-maven-plugin/issues/1133
    // for more details.
    int nTries = 0;
    boolean bResourcesCreated = false;
    Exception buildException = null;
    do {
        try {
            if (config.getEnricherTask() != null) {
                config.getEnricherTask().execute(builder);
            }
            if (builder.hasItems()) {
                KubernetesList k8sList = builder.build();
                client.lists().create(k8sList);
            }
            // If we are here, it means resources got created successfully.
            bResourcesCreated = true;
        } catch (Exception aException) {
            // Retry only when Exception is of socket closed message.
            if (aException.getMessage() != null && aException.getMessage().contains("Socket closed")) {
                log.warn("Problem encountered while applying resource objects, retrying..");
                buildException = aException;
                nTries++;
                Thread.sleep(RESOURCE_CREATION_RETRY_TIMEOUT_IN_MILLIS);
                // Make a connection to cluster again.
                client = clusterAccess.createDefaultClient(log);
            } else {
                // and simply throw as it is.
                throw new MojoExecutionException(aException.getMessage());
            }
        }
    } while (nTries < RESOURCE_CREATION_RETRIES && !bResourcesCreated);
    if (!bResourcesCreated)
        throw new MojoExecutionException(buildException.getMessage());
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 63 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class ClusterAccessTest method createClientTestOpenshift.

@Test
public void createClientTestOpenshift() throws Exception {
    paths.add("/oapi");
    paths.add("/oapi/v1");
    RootPaths rootpaths = new RootPaths();
    rootpaths.setPaths(paths);
    mockServer.expect().get().withPath("/").andReturn(200, rootpaths).always();
    ClusterAccess clusterAccess = new ClusterAccess(null, client);
    Client outputClient = clusterAccess.createDefaultClient(logger);
    assertTrue(outputClient instanceof OpenShiftClient);
}
Also used : RootPaths(io.fabric8.kubernetes.api.model.RootPaths) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Client(io.fabric8.kubernetes.client.Client) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Test(org.junit.Test)

Example 64 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildServiceTest method checkTarPackage.

@Test
public void checkTarPackage() throws Exception {
    int nTries = 0;
    boolean bTestComplete = false;
    do {
        try {
            nTries++;
            BuildService.BuildServiceConfig config = defaultConfig.build();
            WebServerEventCollector<OpenShiftMockServer> collector = createMockServer(config, true, 50, true, true);
            OpenShiftMockServer mockServer = collector.getMockServer();
            OpenShiftClient client = mockServer.createOpenShiftClient();
            final OpenshiftBuildService service = new OpenshiftBuildService(client, logger, dockerServiceHub, config);
            ImageConfiguration imageWithEnv = new ImageConfiguration.Builder(image).buildConfig(new BuildImageConfiguration.Builder(image.getBuildConfiguration()).env(Collections.singletonMap("FOO", "BAR")).build()).build();
            service.createBuildArchive(imageWithEnv);
            final List<ArchiverCustomizer> customizer = new LinkedList<>();
            new Verifications() {

                {
                    archiveService.createDockerBuildArchive(withInstanceOf(ImageConfiguration.class), withInstanceOf(MojoParameters.class), withCapture(customizer));
                    assertTrue(customizer.size() == 1);
                }
            };
            customizer.get(0).customize(tarArchiver);
            final List<File> file = new LinkedList<>();
            new Verifications() {

                {
                    String path;
                    tarArchiver.addFile(withCapture(file), path = withCapture());
                    assertEquals(".s2i/environment", path);
                }
            };
            assertEquals(1, file.size());
            List<String> lines;
            try (FileReader reader = new FileReader(file.get(0))) {
                lines = IOUtils.readLines(reader);
            }
            assertTrue(lines.contains("FOO=BAR"));
            bTestComplete = true;
        } catch (Fabric8ServiceException exception) {
            Throwable rootCause = getRootCause(exception);
            logger.warn("A problem encountered while running test {}, retrying..", exception.getMessage());
            // Let's wait for a while, and then retry again
            if (rootCause != null && rootCause instanceof IOException) {
                continue;
            }
        }
    } while (nTries < MAX_TIMEOUT_RETRIES && !bTestComplete);
}
Also used : BuildService(io.fabric8.maven.core.service.BuildService) ArchiverCustomizer(io.fabric8.maven.docker.assembly.ArchiverCustomizer) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) IOException(java.io.IOException) Verifications(mockit.Verifications) LinkedList(java.util.LinkedList) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) MojoParameters(io.fabric8.maven.docker.util.MojoParameters) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.Test)

Example 65 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project kie-wb-common by kiegroup.

the class OpenShiftAccessInterfaceImpl method getOpenShiftClient.

@Override
public OpenShiftClient getOpenShiftClient(final ProviderId providerId) {
    if (!clientMap.containsKey(providerId.getId())) {
        checkInstanceOf("providerId", providerId, OpenShiftProvider.class);
        ProviderConfig providerConfig = ((OpenShiftProvider) providerId).getConfig();
        OpenShiftClient client = newOpenShiftClient(providerConfig);
        clientMap.put(providerId.getId(), client);
    }
    return clientMap.get(providerId.getId());
}
Also used : ProviderConfig(org.guvnor.ala.config.ProviderConfig) OpenShiftProviderConfig(org.guvnor.ala.openshift.config.OpenShiftProviderConfig) OpenShiftClient(org.guvnor.ala.openshift.access.OpenShiftClient) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftProvider(org.guvnor.ala.openshift.model.OpenShiftProvider)

Aggregations

OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)65 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 IOException (java.io.IOException)18 Test (org.junit.Test)17 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)15 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)14 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)12 API (org.wso2.carbon.apimgt.core.models.API)12 FileNotFoundException (java.io.FileNotFoundException)11 Service (io.fabric8.kubernetes.api.model.Service)10 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)10 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)10 BeforeTest (org.testng.annotations.BeforeTest)9 Test (org.testng.annotations.Test)9 Controller (io.fabric8.kubernetes.api.Controller)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Route (io.fabric8.openshift.api.model.Route)8 JSONObject (org.json.JSONObject)8