Search in sources :

Example 46 with Project

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

the class WatchMojo method customizeConfig.

@Override
public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs) {
    try {
        Fabric8ServiceHub serviceHub = getFabric8ServiceHub();
        GeneratorContext ctx = new GeneratorContext.Builder().config(extractGeneratorConfig()).project(project).session(session).goalFinder(goalFinder).goalName("fabric8:watch").logger(log).mode(mode).strategy(buildStrategy).useProjectClasspath(useProjectClasspath).artifactResolver(serviceHub.getArtifactResolverService()).build();
        return GeneratorManager.generate(configs, ctx, false);
    } catch (MojoExecutionException e) {
        throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Fabric8ServiceHub(io.fabric8.maven.core.service.Fabric8ServiceHub) GeneratorContext(io.fabric8.maven.generator.api.GeneratorContext)

Example 47 with Project

use of io.fabric8.openshift.api.model.Project in project vertx-openshift-it by cescoffier.

the class OpenShiftTestAssistant method deployApplication.

public String deployApplication() throws IOException {
    applicationName = System.getProperty("app.name");
    List<? extends HasMetadata> entities = deploy("application", new File("target/classes/META-INF/fabric8/openshift.yml"));
    Optional<String> first = entities.stream().filter(hm -> hm instanceof DeploymentConfig).map(hm -> (DeploymentConfig) hm).map(dc -> dc.getMetadata().getName()).findFirst();
    if (applicationName == null && first.isPresent()) {
        applicationName = first.get();
    }
    Route route = client.adapt(OpenShiftClient.class).routes().inNamespace(project).withName(applicationName).get();
    assertThat(route).isNotNull();
    RestAssured.baseURI = "http://" + Objects.requireNonNull(route).getSpec().getHost();
    System.out.println("Route url: " + RestAssured.baseURI);
    return applicationName;
}
Also used : KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Awaitility.await(org.awaitility.Awaitility.await) Predicate(java.util.function.Predicate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Pod(io.fabric8.kubernetes.api.model.Pod) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) File(java.io.File) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Route(io.fabric8.openshift.api.model.Route) Map(java.util.Map) Optional(java.util.Optional) RestAssured(io.restassured.RestAssured) Comparator(java.util.Comparator) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) File(java.io.File) Route(io.fabric8.openshift.api.model.Route)

Example 48 with Project

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

the class Configuration method findNamespaceForEnvironment.

/**
 * Resolves a logical environment name for a project, such as <code>Testing</code> to the physical projcect/team specific
 * namespace value.
 *
 * It tries to find a fabric8.yml file in this folder or a parent folder and loads it and tries to use it to find the
 * namespace for the given environment or uses environment variables to resolve the environment name -> physical namespace
 * @return the namespace
 */
private static String findNamespaceForEnvironment(String environment, Map<String, String> map, KubernetesClient kubernetesClient, String developNamespace, boolean failOnMissingEnvironmentNamespace) {
    String namespace = null;
    if (!Strings.isNullOrBlank(environment)) {
        namespace = Environments.namespaceForEnvironment(kubernetesClient, environment, developNamespace);
        if (Strings.isNotBlank(namespace)) {
            return namespace;
        }
        String basedir = System.getProperty("basedir", ".");
        File folder = new File(basedir);
        ProjectConfig projectConfig = ProjectConfigs.findFromFolder(folder);
        if (projectConfig != null) {
            LinkedHashMap<String, String> environments = projectConfig.getEnvironments();
            if (environments != null) {
                namespace = environments.get(environment);
            }
        }
        String key = environment.toLowerCase() + ".namespace";
        if (Strings.isNullOrBlank(namespace)) {
            // lets try find an environment variable or system property
            namespace = getStringProperty(key, map, null);
        }
        if (Strings.isNullOrBlank(namespace)) {
            if (failOnMissingEnvironmentNamespace) {
                throw new IllegalStateException("A fabric8 environment '" + environment + "' has been specified, but no matching namespace was found in the fabric8.yml file or '" + key + "' system property");
            } else {
                return developNamespace;
            }
        }
    }
    return namespace;
}
Also used : ProjectConfig(io.fabric8.devops.ProjectConfig) File(java.io.File)

Example 49 with Project

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

the class Controller method applyProjectRequest.

/**
 * Returns true if the ProjectRequest is created
 */
public boolean applyProjectRequest(ProjectRequest entity) {
    String namespace = getOrCreateMetadata(entity).getName();
    LOG.info("Using project: " + namespace);
    String name = getName(entity);
    Objects.notNull(name, "No name for " + entity);
    OpenShiftClient openshiftClient = getOpenShiftClientOrNull();
    if (openshiftClient == null || !openshiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.PROJECT)) {
        LOG.warn("Cannot check for Project " + namespace + " as not running against OpenShift!");
        return false;
    }
    boolean exists = checkNamespace(name);
    // We may want to be more fine-grained on the phase of the project
    if (!exists) {
        try {
            Object answer = openshiftClient.projectrequests().create(entity);
            logGeneratedEntity("Created ProjectRequest: ", namespace, entity, answer);
            return true;
        } catch (Exception e) {
            onApplyError("Failed to create ProjectRequest: " + name + " due " + e.getMessage(), e);
        }
    }
    return false;
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) JSONObject(org.json.JSONObject) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 50 with Project

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

the class Controller method applyNamespace.

public void applyNamespace(String namespaceName, Map<String, String> labels) {
    if (Strings.isNullOrBlank(namespaceName)) {
        return;
    }
    OpenShiftClient openshiftClient = getOpenShiftClientOrNull();
    if (openshiftClient != null && openshiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.PROJECT)) {
        ProjectRequest entity = new ProjectRequest();
        ObjectMeta metadata = getOrCreateMetadata(entity);
        metadata.setName(namespaceName);
        String namespace = kubernetesClient.getNamespace();
        if (Strings.isNotBlank(namespace)) {
            Map<String, String> entityLabels = getOrCreateLabels(entity);
            if (labels != null) {
                entityLabels.putAll(labels);
            } else {
                // lets associate this new namespace with the project that it was created from
                entityLabels.put("project", namespace);
            }
        }
        applyProjectRequest(entity);
    } else {
        Namespace entity = new Namespace();
        ObjectMeta metadata = getOrCreateMetadata(entity);
        metadata.setName(namespaceName);
        String namespace = kubernetesClient.getNamespace();
        if (Strings.isNotBlank(namespace)) {
            Map<String, String> entityLabels = getOrCreateLabels(entity);
            if (labels != null) {
                entityLabels.putAll(labels);
            } else {
                // lets associate this new namespace with the project that it was created from
                entityLabels.put("project", namespace);
            }
        }
        applyNamespace(entity);
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ProjectRequest(io.fabric8.openshift.api.model.ProjectRequest) Namespace(io.fabric8.kubernetes.api.model.Namespace)

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