Search in sources :

Example 41 with Check

use of io.fabric8.karaf.checks.Check 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 42 with Check

use of io.fabric8.karaf.checks.Check in project fabric8-maven-plugin by fabric8io.

the class ContainerHandlerTest method imagePullPolicyWithoutPolicySetTest.

@Test
public void imagePullPolicyWithoutPolicySetTest() {
    // project with version and ending in SNAPSHOT
    project1.setVersion("3.5-SNAPSHOT");
    // project with version but not ending in SNAPSHOT
    project2.setVersion("3.5-NEW");
    // creating container Handler for two
    ContainerHandler handler1 = new ContainerHandler(project1, envVarHandler, probeHandler);
    ContainerHandler handler2 = new ContainerHandler(project2, envVarHandler, probeHandler);
    // project without version
    ContainerHandler handler3 = new ContainerHandler(project, envVarHandler, probeHandler);
    images.clear();
    images.add(imageConfiguration1);
    // check if policy is not set then both in case of version is set or not
    ResourceConfig config2 = new ResourceConfig.Builder().imagePullPolicy("").build();
    containers = handler1.getContainers(config2, images);
    assertEquals("PullAlways", containers.get(0).getImagePullPolicy());
    containers = handler2.getContainers(config2, images);
    assertEquals("", containers.get(0).getImagePullPolicy());
    containers = handler3.getContainers(config2, images);
    assertEquals("", containers.get(0).getImagePullPolicy());
}
Also used : ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) Test(org.junit.Test)

Example 43 with Check

use of io.fabric8.karaf.checks.Check in project fabric8-maven-plugin by fabric8io.

the class ProfileUtil method findProfile.

/**
 * Find a profile. Profiles are looked up at various locations:
 *
 * <ul>
 *     <li>A given directory with the name profiles.yml (and variations, {@link #findProfile(String, File)}</li>
 * </ul>
 * @param profileArg the profile's name
 * @param resourceDir a directory to check for profiles.
 * @return the profile found or the default profile if none of this name is given
 * @throws IOException
 */
public static Profile findProfile(String profileArg, File resourceDir) throws IOException {
    try {
        String profile = profileArg == null ? DEFAULT_PROFILE : profileArg;
        Profile profileFound = lookup(profile, resourceDir);
        if (profileFound != null) {
            return profileFound;
        } else {
            throw new IllegalArgumentException("No profile '" + profile + "' defined");
        }
    } catch (IOException e) {
        throw new IOException("Error while looking up profile " + profileArg + ": " + e.getMessage(), e);
    }
}
Also used : Profile(io.fabric8.maven.core.config.Profile)

Example 44 with Check

use of io.fabric8.karaf.checks.Check 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 45 with Check

use of io.fabric8.karaf.checks.Check in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method ensureNamespaceExists.

private void ensureNamespaceExists(KubernetesClient kubernetes, String name) {
    name = convertToValidDnsLabel(name);
    // lets check namespace exists
    Namespace namespace = kubernetes.namespaces().withName(name).get();
    if (namespace == null) {
        Map<String, String> labels = new HashMap<>();
        labels.put("provider", "fabric8");
        labels.put("kind", "secrets");
        namespace = new NamespaceBuilder().withNewMetadata().withName(name).withLabels(labels).endMetadata().build();
        if (KubernetesHelper.isOpenShift(kubernetes)) {
            ProjectRequest projectRequest = new ProjectRequestBuilder().withMetadata(namespace.getMetadata()).build();
            OpenShiftClient openShiftClient = asOpenShiftClient(kubernetes);
            log.info("Creating ProjectRequest " + name + " with labels: " + labels);
            openShiftClient.projectrequests().create(projectRequest);
        } else {
            log.info("Creating Namespace " + name + " with labels: " + labels);
            kubernetes.namespaces().withName(name).create(namespace);
        }
    }
}
Also used : ProjectRequestBuilder(io.fabric8.openshift.api.model.ProjectRequestBuilder) HashMap(java.util.HashMap) ProjectRequest(io.fabric8.openshift.api.model.ProjectRequest) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Namespace(io.fabric8.kubernetes.api.model.Namespace) KubernetesHelper.getNamespace(io.fabric8.kubernetes.api.KubernetesHelper.getNamespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder)

Aggregations

Test (org.junit.Test)35 IOException (java.io.IOException)21 File (java.io.File)17 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)14 FabricService (io.fabric8.api.FabricService)11 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)11 Container (io.fabric8.api.Container)9 PatchException (io.fabric8.patch.management.PatchException)9 Expectations (mockit.Expectations)9 Profile (io.fabric8.api.Profile)8 Map (java.util.Map)7 TreeMap (java.util.TreeMap)7 Version (io.fabric8.api.Version)6 AuthConfig (io.fabric8.maven.docker.access.AuthConfig)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 Check (io.fabric8.karaf.checks.Check)5 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)5 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)5 Patch (io.fabric8.patch.management.Patch)5