Search in sources :

Example 91 with Project

use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.

the class DevOpsConnectors method createDevOpsConnector.

/**
 * Returns a DevOpsConnector for the given project repository
 */
public static DevOpsConnector createDevOpsConnector(ProjectRepository project) {
    DevOpsConnector connector = new DevOpsConnector();
    connector.setGitUrl(project.getGitUrl());
    String repoName = project.getRepoName();
    connector.setRepoName(repoName);
    String username = project.getUser();
    connector.setUsername(username);
    String buildName = ProjectRepositories.createBuildName(username, repoName);
    if (project.isGitHubProject()) {
        // lets default the issue tracker
        String url = project.getUrl();
        if (Strings.isNotBlank(url)) {
            connector.setIssueTrackerUrl(URLUtils.pathJoin(url, "issues"));
            connector.setTeamUrl(URLUtils.pathJoin(url, "graphs/contributors"));
            connector.setReleasesUrl(URLUtils.pathJoin(url, "tags"));
            connector.setRepositoryBrowseLink(url);
        }
        ProjectConfig config = ProjectConfigs.loadFromUrl(URLUtils.pathJoin(url, "blob/master/fabric8.yml"));
        if (config == null) {
            config = new ProjectConfig();
            // lets add a dummy build so we can at least build snapshots on demand in OpenShift
            config.setPipeline("maven/Deploy.groovy");
        }
        config.setBuildName(buildName);
        connector.setProjectConfig(config);
        connector.setRegisterWebHooks(false);
        System.out.println("Created config " + config.getBuildName() + " with flow " + config.getPipeline());
    }
    return connector;
}
Also used : ProjectConfig(io.fabric8.devops.ProjectConfig)

Example 92 with Project

use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.

the class SessionListener method preprocessEnvironment.

protected void preprocessEnvironment(KubernetesClient client, Controller controller, Configuration configuration, Session session) {
    if (configuration.isUseGoFabric8()) {
        // lets invoke gofabric8 to configure the security and secrets
        Logger logger = session.getLogger();
        Commands.assertCommand(logger, "oc", "project", session.getNamespace());
        Commands.assertCommand(logger, "gofabric8", "deploy", "-y", "--console=false", "--templates=false");
        Commands.assertCommand(logger, "gofabric8", "secrets", "-y");
    }
}
Also used : Logger(io.fabric8.arquillian.kubernetes.log.Logger)

Example 93 with Project

use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.

the class SessionListener method start.

public void start(@Observes final Start event, KubernetesClient client, Controller controller, Configuration configuration) throws Exception {
    Objects.requireNonNull(client, "KubernetesClient most not be null!");
    Session session = event.getSession();
    final Logger log = session.getLogger();
    String namespace = session.getNamespace();
    System.setProperty(Constants.KUBERNETES_NAMESPACE, namespace);
    log.status("Using Kubernetes at: " + client.getMasterUrl());
    log.status("Creating kubernetes resources inside namespace: " + namespace);
    log.info("if you use OpenShift then type this switch namespaces:     oc project " + namespace);
    log.info("if you use kubernetes then type this to switch namespaces: kubectl namespace " + namespace);
    clearTestResultDirectories(session);
    controller.setNamespace(namespace);
    controller.setThrowExceptionOnError(true);
    controller.setRecreateMode(true);
    controller.setIgnoreRunningOAuthClients(true);
    if (configuration.isCreateNamespaceForTest()) {
        createNamespace(client, controller, session);
    } else {
        String namespaceToUse = configuration.getNamespace();
        checkNamespace(client, controller, session, configuration);
        updateConfigMapStatus(client, session, Constants.RUNNING_STATUS);
        namespace = namespaceToUse;
        controller.setNamespace(namespace);
    }
    List<KubernetesList> kubeConfigs = new LinkedList<>();
    shutdownHook = new ShutdownHook(client, controller, configuration, session, kubeConfigs);
    Runtime.getRuntime().addShutdownHook(shutdownHook);
    try {
        URL configUrl = configuration.getEnvironmentConfigUrl();
        List<String> dependencies = !configuration.getEnvironmentDependencies().isEmpty() ? configuration.getEnvironmentDependencies() : resolver.resolve(session);
        if (configuration.isEnvironmentInitEnabled()) {
            for (String dependency : dependencies) {
                log.info("Found dependency: " + dependency);
                loadDependency(log, kubeConfigs, dependency, controller, configuration, namespace);
            }
            OpenShiftClient openShiftClient = controller.getOpenShiftClientOrNull();
            if (configUrl == null) {
                // lets try find the default configuration generated by the new fabric8-maven-plugin
                String resourceName = "kubernetes.yml";
                if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE) && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.ROUTE)) {
                    resourceName = "openshift.yml";
                }
                configUrl = findConfigResource("/META-INF/fabric8/" + resourceName);
            }
            if (configUrl != null) {
                log.status("Applying kubernetes configuration from: " + configUrl);
                String configText = readAsString(configUrl);
                Object dto = null;
                String configPath = configUrl.getPath();
                if (configPath.endsWith(".yml") || configPath.endsWith(".yaml")) {
                    dto = loadYaml(configText, KubernetesResource.class);
                } else {
                    dto = loadJson(configText);
                }
                dto = expandTemplate(controller, configuration, log, namespace, configUrl.toString(), dto);
                KubernetesList kubeList = KubernetesHelper.asKubernetesList(dto);
                List<HasMetadata> items = kubeList.getItems();
                kubeConfigs.add(kubeList);
            }
            // Lets also try to load the image stream for the project.
            if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE)) {
                File targetDir = new File(System.getProperty("basedir", ".") + "/target");
                if (targetDir.exists() && targetDir.isDirectory()) {
                    File[] files = targetDir.listFiles();
                    if (files != null) {
                        for (File file : files) {
                            if (file.getName().endsWith("-is.yml")) {
                                loadDependency(log, kubeConfigs, file.toURI().toURL().toString(), controller, configuration, namespace);
                            }
                        }
                    }
                }
            // 
            }
        }
        if (!configuration.isEnvironmentInitEnabled() || applyConfiguration(client, controller, configuration, session, kubeConfigs)) {
            displaySessionStatus(client, session);
        } else {
            throw new IllegalStateException("Failed to apply kubernetes configuration.");
        }
    } catch (Exception e) {
        try {
            cleanupSession(client, controller, configuration, session, kubeConfigs, Constants.ERROR_STATUS);
        } catch (MultiException me) {
            throw e;
        } finally {
            if (shutdownHook != null) {
                Runtime.getRuntime().removeShutdownHook(shutdownHook);
            }
        }
        throw new RuntimeException(e);
    }
}
Also used : Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString) Logger(io.fabric8.arquillian.kubernetes.log.Logger) URL(java.net.URL) MultiException(io.fabric8.utils.MultiException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) File(java.io.File) MultiException(io.fabric8.utils.MultiException) Util.cleanupSession(io.fabric8.arquillian.utils.Util.cleanupSession)

Example 94 with Project

use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.

the class BuildConfigHelper method createBuildConfig.

public static BuildConfig createBuildConfig(KubernetesClient kubernetesClient, String namespace, String projectName, String cloneUrl, Map<String, String> annotations) {
    LOG.info("Creating a BuildConfig for namespace: " + namespace + " project: " + projectName);
    String jenkinsUrl = null;
    try {
        jenkinsUrl = getJenkinsServiceUrl(kubernetesClient, namespace);
    } catch (Exception e) {
    // ignore missing Jenkins service issue
    }
    BuildConfig buildConfig = Builds.createDefaultBuildConfig(projectName, cloneUrl, jenkinsUrl);
    Map<String, String> currentAnnotations = KubernetesHelper.getOrCreateAnnotations(buildConfig);
    currentAnnotations.putAll(annotations);
    return buildConfig;
}
Also used : BuildConfig(io.fabric8.openshift.api.model.BuildConfig) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 95 with Project

use of io.fabric8.openshift.api.model.Project in project fabric8 by fabric8io.

the class BuildConfigHelper method createAndApplyBuildConfig.

/**
 * Returns the created BuildConfig for the given project name and git repository
 */
public static BuildConfig createAndApplyBuildConfig(KubernetesClient kubernetesClient, String namespace, String projectName, String cloneUrl, Map<String, String> annotations) {
    BuildConfig buildConfig = createBuildConfig(kubernetesClient, namespace, projectName, cloneUrl, annotations);
    Controller controller = new Controller(kubernetesClient);
    controller.setNamespace(namespace);
    controller.applyBuildConfig(buildConfig, "from project " + projectName);
    return buildConfig;
}
Also used : BuildConfig(io.fabric8.openshift.api.model.BuildConfig) Controller(io.fabric8.kubernetes.api.Controller)

Aggregations

Test (org.junit.Test)51 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)29 File (java.io.File)14 Expectations (mockit.Expectations)14 ArrayList (java.util.ArrayList)13 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)11 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)11 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)9 IOException (java.io.IOException)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 VolumeConfig (io.fabric8.maven.core.config.VolumeConfig)7 GeneratorContext (io.fabric8.maven.generator.api.GeneratorContext)6 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)6 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)5 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)5 MojoFailureException (org.apache.maven.plugin.MojoFailureException)5 MavenProject (org.apache.maven.project.MavenProject)5 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)4 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3