Search in sources :

Example 16 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method updateOrCreateBuildConfig.

private String updateOrCreateBuildConfig(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, ImageConfiguration imageConfig) {
    ImageName imageName = new ImageName(imageConfig.getName());
    String buildName = getS2IBuildName(config, imageName);
    String imageStreamName = getImageStreamName(imageName);
    String outputImageStreamTag = imageStreamName + ":" + (imageName.getTag() != null ? imageName.getTag() : "latest");
    BuildStrategy buildStrategyResource = createBuildStrategy(imageConfig, config.getOpenshiftBuildStrategy());
    BuildOutput buildOutput = new BuildOutputBuilder().withNewTo().withKind("ImageStreamTag").withName(outputImageStreamTag).endTo().build();
    // Fetch exsting build config
    BuildConfig buildConfig = client.buildConfigs().withName(buildName).get();
    if (buildConfig != null) {
        // lets verify the BC
        BuildConfigSpec spec = getBuildConfigSpec(buildConfig);
        validateSourceType(buildName, spec);
        if (config.getBuildRecreateMode().isBuildConfig()) {
            // Delete and recreate afresh
            client.buildConfigs().withName(buildName).delete();
            return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
        } else {
            // Update & return
            return updateBuildConfig(client, buildName, buildStrategyResource, buildOutput, spec);
        }
    } else {
        // Create afresh
        return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) BuildOutput(io.fabric8.openshift.api.model.BuildOutput) BuildStrategy(io.fabric8.openshift.api.model.BuildStrategy) OpenShiftBuildStrategy(io.fabric8.maven.core.config.OpenShiftBuildStrategy) BuildOutputBuilder(io.fabric8.openshift.api.model.BuildOutputBuilder) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) BuildConfigSpec(io.fabric8.openshift.api.model.BuildConfigSpec)

Example 17 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class OpenshiftBuildService method build.

@Override
public void build(ImageConfiguration imageConfig) throws Fabric8ServiceException {
    try {
        ImageName imageName = new ImageName(imageConfig.getName());
        File dockerTar = createBuildArchive(imageConfig);
        KubernetesListBuilder builder = new KubernetesListBuilder();
        // Check for buildconfig / imagestream and create them if necessary
        String buildName = updateOrCreateBuildConfig(config, client, builder, imageConfig);
        checkOrCreateImageStream(config, client, builder, getImageStreamName(imageName));
        applyResourceObjects(config, client, builder);
        // Start the actual build
        Build build = startBuild(client, dockerTar, buildName);
        // Wait until the build finishes
        waitForOpenShiftBuildToComplete(client, build);
        // Create a file with generated image streams
        addImageStreamToFile(getImageStreamFile(config), imageName, client);
    } catch (Fabric8ServiceException e) {
        throw e;
    } catch (Exception ex) {
        throw new Fabric8ServiceException("Unable to build the image using the OpenShift build service", ex);
    }
}
Also used : ImageName(io.fabric8.maven.docker.util.ImageName) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) Build(io.fabric8.openshift.api.model.Build) File(java.io.File) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 18 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class ResourceMojo method generateAppResources.

private KubernetesListBuilder generateAppResources(List<ImageConfiguration> images, EnricherManager enricherManager) throws IOException, MojoExecutionException {
    Path composeFilePath = checkComposeConfig();
    ComposeService composeUtil = new ComposeService(komposeBinDir, composeFilePath, log);
    try {
        File[] resourceFiles = KubernetesResourceUtil.listResourceFragments(resourceDir);
        File[] composeResourceFiles = composeUtil.convertToKubeFragments();
        File[] allResources = ArrayUtils.addAll(resourceFiles, composeResourceFiles);
        KubernetesListBuilder builder;
        // Add resource files found in the fabric8 directory
        if (allResources != null && allResources.length > 0) {
            if (resourceFiles != null && resourceFiles.length > 0) {
                log.info("using resource templates from %s", resourceDir);
            }
            if (composeResourceFiles != null && composeResourceFiles.length > 0) {
                log.info("using resource templates generated from compose file");
            }
            builder = readResourceFragments(allResources);
        } else {
            builder = new KubernetesListBuilder();
        }
        // Add locally configured objects
        if (resources != null) {
            // TODO: Allow also support resources to be specified via XML
            addConfiguredResources(builder, images);
        }
        // Create default resources for app resources only
        enricherManager.createDefaultResources(builder);
        // Enrich descriptors
        enricherManager.enrich(builder);
        return builder;
    } catch (ConstraintViolationException e) {
        String message = ValidationUtil.createValidationMessage(e.getConstraintViolations());
        log.error("ConstraintViolationException: %s", message);
        throw new MojoExecutionException(message, e);
    } catch (Fabric8ServiceException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        composeUtil.cleanComposeResources();
    }
}
Also used : Path(java.nio.file.Path) Fabric8ServiceException(io.fabric8.maven.core.service.Fabric8ServiceException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ComposeService(io.fabric8.maven.core.service.ComposeService) ConstraintViolationException(javax.validation.ConstraintViolationException) File(java.io.File)

Example 19 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder 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)

Example 20 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class PodAnnotationEnricher method adapt.

@Override
public void adapt(KubernetesListBuilder builder) {
    super.adapt(builder);
    List<HasMetadata> items = builder.getItems();
    for (HasMetadata item : items) {
        if (item instanceof Deployment) {
            Deployment deployment = (Deployment) item;
            ObjectMeta metadata = deployment.getMetadata();
            DeploymentSpec spec = deployment.getSpec();
            if (metadata != null && spec != null) {
                PodTemplateSpec template = spec.getTemplate();
                if (template != null) {
                    ObjectMeta templateMetadata = template.getMetadata();
                    if (templateMetadata == null) {
                        templateMetadata = new ObjectMeta();
                        template.setMetadata(templateMetadata);
                    }
                    templateMetadata.setAnnotations(MapUtil.mergeMaps(templateMetadata.getAnnotations(), metadata.getAnnotations()));
                }
            }
        }
    }
    builder.withItems(items);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment)

Aggregations

KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)30 Test (org.junit.Test)25 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)17 Expectations (mockit.Expectations)13 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 File (java.io.File)5 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 Secret (io.fabric8.kubernetes.api.model.Secret)3 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)3 IOException (java.io.IOException)3 Map (java.util.Map)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 Visitable (io.fabric8.kubernetes.api.builder.Visitable)2 Visitor (io.fabric8.kubernetes.api.builder.Visitor)2 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)2