Search in sources :

Example 1 with ClusterAccess

use of io.fabric8.maven.core.access.ClusterAccess in project fabric8-maven-plugin by fabric8io.

the class ClusterAccessTest method openshiftPlatformModeTest.

@Test
public void openshiftPlatformModeTest() 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);
    mode = clusterAccess.resolvePlatformMode(PlatformMode.openshift, logger);
    assertEquals(PlatformMode.openshift, mode);
    mode = clusterAccess.resolvePlatformMode(PlatformMode.DEFAULT, logger);
    assertEquals(PlatformMode.openshift, mode);
    mode = clusterAccess.resolvePlatformMode(null, logger);
    assertEquals(PlatformMode.openshift, mode);
}
Also used : RootPaths(io.fabric8.kubernetes.api.model.RootPaths) Test(org.junit.Test)

Example 2 with ClusterAccess

use of io.fabric8.maven.core.access.ClusterAccess in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method executeInternal.

public void executeInternal() throws MojoExecutionException, MojoFailureException {
    clusterAccess = new ClusterAccess(namespace);
    try {
        KubernetesClient kubernetes = clusterAccess.createDefaultClient(log);
        URL masterUrl = kubernetes.getMasterUrl();
        File manifest;
        if (KubernetesHelper.isOpenShift(kubernetes)) {
            manifest = openshiftManifest;
        } else {
            manifest = kubernetesManifest;
        }
        if (!Files.isFile(manifest)) {
            if (failOnNoKubernetesJson) {
                throw new MojoFailureException("No such generated manifest file: " + manifest);
            } else {
                log.warn("No such generated manifest file %s for this project so ignoring", manifest);
                return;
            }
        }
        String clusterKind = "Kubernetes";
        if (KubernetesHelper.isOpenShift(kubernetes)) {
            clusterKind = "OpenShift";
        }
        KubernetesResourceUtil.validateKubernetesMasterUrl(masterUrl);
        log.info("Using %s at %s in namespace %s with manifest %s ", clusterKind, masterUrl, clusterAccess.getNamespace(), manifest);
        Controller controller = createController();
        controller.setAllowCreate(createNewResources);
        controller.setServicesOnlyMode(servicesOnly);
        controller.setIgnoreServiceMode(ignoreServices);
        controller.setLogJsonDir(jsonLogDir);
        controller.setBasedir(getRootProjectFolder());
        controller.setIgnoreRunningOAuthClients(ignoreRunningOAuthClients);
        controller.setProcessTemplatesLocally(processTemplatesLocally);
        controller.setDeletePodsOnReplicationControllerUpdate(deletePodsOnReplicationControllerUpdate);
        controller.setRollingUpgrade(rollingUpgrades);
        controller.setRollingUpgradePreserveScale(isRollingUpgradePreserveScale());
        boolean openShift = KubernetesHelper.isOpenShift(kubernetes);
        if (openShift) {
            getLog().info("OpenShift platform detected");
        } else {
            disableOpenShiftFeatures(controller);
        }
        // lets check we have created the namespace
        String namespace = clusterAccess.getNamespace();
        controller.applyNamespace(namespace);
        controller.setNamespace(namespace);
        Set<HasMetadata> entities = KubernetesResourceUtil.loadResources(manifest);
        if (createExternalUrls) {
            if (controller.getOpenShiftClientOrNull() != null) {
                createRoutes(controller, entities);
            } else {
                createIngress(controller, kubernetes, entities);
            }
        }
        applyEntities(controller, kubernetes, namespace, manifest.getName(), entities);
    } catch (KubernetesClientException e) {
        KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
    } catch (MojoExecutionException e) {
        throw e;
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) URL(java.net.URL) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) File(java.io.File) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 3 with ClusterAccess

use of io.fabric8.maven.core.access.ClusterAccess in project fabric8-maven-plugin by fabric8io.

the class BuildMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip || skipBuild) {
        return;
    }
    clusterAccess = new ClusterAccess(namespace);
    // Platform mode is already used in executeInternal()
    super.execute();
}
Also used : ClusterAccess(io.fabric8.maven.core.access.ClusterAccess)

Example 4 with ClusterAccess

use of io.fabric8.maven.core.access.ClusterAccess in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method executeInternal.

@Override
public void executeInternal() throws MojoExecutionException, MojoFailureException {
    if (!basedir.isDirectory() || !basedir.exists()) {
        throw new MojoExecutionException("No directory for base directory: " + basedir);
    }
    // lets check for a git repo
    String gitRemoteURL = null;
    Repository repository = null;
    try {
        repository = GitUtils.findRepository(basedir);
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to find local git repository in current directory: " + e, e);
    }
    try {
        gitRemoteURL = GitUtils.getRemoteAsHttpsURL(repository);
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to get the current git branch: " + e, e);
    }
    try {
        clusterAccess = new ClusterAccess(this.namespace);
        if (Strings.isNullOrBlank(projectName)) {
            projectName = basedir.getName();
        }
        KubernetesClient kubernetes = clusterAccess.createDefaultClient(log);
        KubernetesResourceUtil.validateKubernetesMasterUrl(kubernetes.getMasterUrl());
        String namespace = clusterAccess.getNamespace();
        OpenShiftClient openShiftClient = getOpenShiftClientOrJenkinsShift(kubernetes, namespace);
        if (gitRemoteURL != null) {
            // lets check we don't already have this project imported
            String branch = repository.getBranch();
            BuildConfig buildConfig = findBuildConfigForGitRepo(openShiftClient, namespace, gitRemoteURL, branch);
            if (buildConfig != null) {
                logBuildConfigLink(kubernetes, namespace, buildConfig, log);
                throw new MojoExecutionException("Project already imported into build " + getName(buildConfig) + " for URI: " + gitRemoteURL + " and branch: " + branch);
            } else {
                Map<String, String> annotations = new HashMap<>();
                annotations.put(Annotations.Builds.GIT_CLONE_URL, gitRemoteURL);
                buildConfig = createBuildConfig(kubernetes, namespace, projectName, gitRemoteURL, annotations);
                openShiftClient.buildConfigs().inNamespace(namespace).create(buildConfig);
                ensureExternalGitSecretsAreSetupFor(kubernetes, namespace, gitRemoteURL);
                logBuildConfigLink(kubernetes, namespace, buildConfig, log);
            }
        } else {
            // lets create an import a new project
            UserDetails userDetails = createGogsUserDetails(kubernetes, namespace);
            userDetails.setBranch(branchName);
            BuildConfigHelper.CreateGitProjectResults createGitProjectResults;
            try {
                createGitProjectResults = BuildConfigHelper.importNewGitProject(kubernetes, userDetails, basedir, namespace, projectName, remoteName, "Importing project from mvn fabric8:import", false);
            } catch (WebApplicationException e) {
                Response response = e.getResponse();
                if (response.getStatus() > 400) {
                    String message = getEntityMessage(response);
                    log.warn("Could not create the git repository: %s %s", e, message);
                    log.warn("Are your username and password correct in the Secret %s/%s?", secretNamespace, gogsSecretName);
                    log.warn("To re-enter your password rerun this command with -Dfabric8.passsword.retry=true");
                    throw new MojoExecutionException("Could not create the git repository. " + "Are your username and password correct in the Secret " + secretNamespace + "/" + gogsSecretName + "?" + e + message, e);
                } else {
                    throw e;
                }
            }
            BuildConfig buildConfig = createGitProjectResults.getBuildConfig();
            openShiftClient.buildConfigs().inNamespace(namespace).create(buildConfig);
            logBuildConfigLink(kubernetes, namespace, buildConfig, log);
        }
    } catch (KubernetesClientException e) {
        KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) IOException(java.io.IOException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Response(javax.ws.rs.core.Response) Repository(org.eclipse.jgit.lib.Repository) UserDetails(io.fabric8.project.support.UserDetails) BuildConfigHelper(io.fabric8.project.support.BuildConfigHelper) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BuildConfigHelper.createBuildConfig(io.fabric8.project.support.BuildConfigHelper.createBuildConfig) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 5 with ClusterAccess

use of io.fabric8.maven.core.access.ClusterAccess in project fabric8-maven-plugin by fabric8io.

the class ResourceMojo method executeInternal.

public void executeInternal() throws MojoExecutionException, MojoFailureException {
    clusterAccess = new ClusterAccess(namespace);
    try {
        lateInit();
        // Resolve the Docker image build configuration
        resolvedImages = getResolvedImages(images, log);
        if (!skip && (!isPomProject() || hasFabric8Dir())) {
            // Extract and generate resources which can be a mix of Kubernetes and OpenShift resources
            KubernetesList resources = generateResources(resolvedImages);
            // Adapt list to use OpenShift specific resource objects
            KubernetesList openShiftResources = convertToOpenShiftResources(resources);
            writeResources(openShiftResources, ResourceClassifier.OPENSHIFT, generateRoute);
            File openShiftResourceDir = new File(this.targetDir, ResourceClassifier.OPENSHIFT.getValue());
            validateIfRequired(openShiftResourceDir, ResourceClassifier.OPENSHIFT);
            // Remove OpenShift specific stuff provided by fragments
            KubernetesList kubernetesResources = convertToKubernetesResources(resources, openShiftResources);
            writeResources(kubernetesResources, ResourceClassifier.KUBERNETES, generateRoute);
            File kubernetesResourceDir = new File(this.targetDir, ResourceClassifier.KUBERNETES.getValue());
            validateIfRequired(kubernetesResourceDir, ResourceClassifier.KUBERNETES);
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to generate fabric8 descriptor", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) IOException(java.io.IOException) File(java.io.File)

Aggregations

ClusterAccess (io.fabric8.maven.core.access.ClusterAccess)6 Test (org.junit.Test)6 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)5 RootPaths (io.fabric8.kubernetes.api.model.RootPaths)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 DockerBuildService (io.fabric8.maven.core.service.kubernetes.DockerBuildService)3 OpenshiftBuildService (io.fabric8.maven.core.service.openshift.OpenshiftBuildService)3 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)3 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 Controller (io.fabric8.kubernetes.api.Controller)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 Client (io.fabric8.kubernetes.client.Client)2 File (java.io.File)2 IOException (java.io.IOException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)1 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)1 DockerAccessException (io.fabric8.maven.docker.access.DockerAccessException)1 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)1