Search in sources :

Example 36 with Project

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

the class ApplyMojo method overrideTemplateParameters.

/**
 * Before applying the given template lets allow template parameters to be overridden via the maven
 * properties - or optionally - via the command line if in interactive mode.
 */
protected static void overrideTemplateParameters(Template template, MavenProject project, Logger log) {
    List<io.fabric8.openshift.api.model.Parameter> parameters = template.getParameters();
    if (parameters != null && project != null) {
        Properties properties = getProjectAndFabric8Properties(project);
        boolean missingProperty = false;
        for (io.fabric8.openshift.api.model.Parameter parameter : parameters) {
            String parameterName = parameter.getName();
            String name = "fabric8.apply." + parameterName;
            String propertyValue = properties.getProperty(name);
            if (propertyValue != null) {
                log.info("Overriding template parameter " + name + " with value: " + propertyValue);
                parameter.setValue(propertyValue);
            } else {
                missingProperty = true;
                log.info("No property defined for template parameter: " + name);
            }
        }
        if (missingProperty) {
            log.debug("Current properties " + new TreeSet<>(properties.keySet()));
        }
    }
}
Also used : TreeSet(java.util.TreeSet) Parameter(org.apache.maven.plugins.annotations.Parameter) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) Properties(java.util.Properties)

Example 37 with Project

use of io.fabric8.openshift.api.model.Project 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 38 with Project

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

the class PushMojo method customizeConfig.

/**
 * Customization hook called by the base plugin.
 *
 * @param configs configuration to customize
 * @return the configuration customized by our generators.
 */
@Override
public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs) {
    try {
        ProcessorConfig generatorConfig = ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG, profile, resourceDir, generator);
        GeneratorContext ctx = new GeneratorContext.Builder().config(generatorConfig).project(project).session(session).goalFinder(goalFinder).goalName("fabric8:push").logger(log).mode(mode).strategy(buildStrategy).useProjectClasspath(false).build();
        return GeneratorManager.generate(configs, ctx, true);
    } catch (Exception e) {
        throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
    }
}
Also used : GeneratorContext(io.fabric8.maven.generator.api.GeneratorContext) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig)

Example 39 with Project

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

the class ResourceMojo method getResolvedImages.

private List<ImageConfiguration> getResolvedImages(List<ImageConfiguration> images, final Logger log) throws MojoExecutionException {
    List<ImageConfiguration> ret;
    ret = ConfigHelper.resolveImages(log, images, new ConfigHelper.Resolver() {

        @Override
        public List<ImageConfiguration> resolve(ImageConfiguration image) {
            return imageConfigResolver.resolve(image, project, session);
        }
    }, // no filter on image name yet (TODO: Maybe add this, too ?)
    null, new ConfigHelper.Customizer() {

        @Override
        public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs) {
            try {
                GeneratorContext ctx = new GeneratorContext.Builder().config(extractGeneratorConfig()).project(project).session(session).goalFinder(goalFinder).goalName("fabric8:resource").logger(log).mode(mode).strategy(buildStrategy).useProjectClasspath(useProjectClasspath).build();
                return GeneratorManager.generate(configs, ctx, true);
            } catch (Exception e) {
                throw new IllegalArgumentException("Cannot extract generator: " + e, e);
            }
        }
    });
    Date now = getBuildReferenceDate();
    storeReferenceDateInPluginContext(now);
    String minimalApiVersion = ConfigHelper.initAndValidate(ret, null, /* no minimal api version */
    new ImageNameFormatter(project, now), log);
    return ret;
}
Also used : ImageConfigResolver(io.fabric8.maven.docker.config.handler.ImageConfigResolver) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) ImageNameFormatter(io.fabric8.maven.docker.util.ImageNameFormatter) List(java.util.List) ArrayList(java.util.ArrayList) GeneratorContext(io.fabric8.maven.generator.api.GeneratorContext) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ConstraintViolationException(javax.validation.ConstraintViolationException) Date(java.util.Date)

Example 40 with Project

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

the class ResourceMojo method generateResources.

private KubernetesList generateResources(List<ImageConfiguration> images) throws IOException, MojoExecutionException {
    // Manager for calling enrichers.
    openshiftDependencyResources = new OpenShiftDependencyResources(log);
    loadOpenShiftOverrideResources();
    EnricherContext.Builder ctxBuilder = new EnricherContext.Builder().project(project).session(session).goalFinder(goalFinder).config(extractEnricherConfig()).resources(resources).images(resolvedImages).log(log).useProjectClasspath(useProjectClasspath).openshiftDependencyResources(openshiftDependencyResources);
    if (resources != null) {
        ctxBuilder.namespace(resources.getNamespace());
    }
    EnricherManager enricherManager = new EnricherManager(resources, ctxBuilder.build());
    // Generate all resources from the main resource diretory, configuration and enrich them accordingly
    KubernetesListBuilder builder = generateAppResources(images, enricherManager);
    // Add resources found in subdirectories of resourceDir, with a certain profile
    // applied
    addProfiledResourcesFromSubirectories(builder, resourceDir, enricherManager);
    return builder.build();
}
Also used : EnricherContext(io.fabric8.maven.enricher.api.EnricherContext) EnricherManager(io.fabric8.maven.plugin.enricher.EnricherManager)

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