Search in sources :

Example 51 with DeploymentConfig

use of io.fabric8.openshift.api.model.DeploymentConfig in project syndesis by syndesisio.

the class SupportUtil method addResourceDescriptors.

@SuppressWarnings({ "PMD.AvoidCatchingGenericException", "PMD.ExceptionAsFlowControl" })
protected void addResourceDescriptors(ZipOutputStream os) {
    Stream<BuildConfig> bcStream = client.buildConfigs().list().getItems().stream();
    Stream<DeploymentConfig> dcStream = client.deploymentConfigs().list().getItems().stream();
    Stream<ConfigMap> cmStream = client.configMaps().list().getItems().stream();
    Stream<ImageStreamTag> istStream = client.imageStreamTags().list().getItems().stream();
    Stream<? extends HasMetadata> stream = Stream.concat(bcStream, dcStream);
    stream = Stream.concat(stream, cmStream);
    stream = Stream.concat(stream, istStream);
    stream.forEach(res -> {
        HasMetadata resWithMetadata = (HasMetadata) res;
        try {
            ZipEntry ze = new ZipEntry("descriptors/" + resWithMetadata.getKind() + '/' + resWithMetadata.getMetadata().getName() + ".YAML");
            os.putNextEntry(ze);
            dumpAsYaml(resWithMetadata, os);
            os.closeEntry();
        } catch (Exception e) {
            LOG.error("Error adding resource {} {}", resWithMetadata.getKind(), resWithMetadata.getMetadata().getName(), e);
        }
    });
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ZipEntry(java.util.zip.ZipEntry) ImageStreamTag(io.fabric8.openshift.api.model.ImageStreamTag) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 52 with DeploymentConfig

use of io.fabric8.openshift.api.model.DeploymentConfig in project syndesis by syndesisio.

the class OpenShiftServiceImpl method isScaled.

@Override
public boolean isScaled(String name, int desiredReplicas) {
    String sName = openshiftName(name);
    DeploymentConfig dc = openShiftClient.deploymentConfigs().withName(sName).get();
    int allReplicas = 0;
    int availableReplicas = 0;
    if (dc != null && dc.getStatus() != null) {
        DeploymentConfigStatus status = dc.getStatus();
        allReplicas = nullSafe(status.getReplicas());
        availableReplicas = nullSafe(status.getAvailableReplicas());
    }
    return desiredReplicas == allReplicas && desiredReplicas == availableReplicas;
}
Also used : DeploymentConfigStatus(io.fabric8.openshift.api.model.DeploymentConfigStatus) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig)

Example 53 with DeploymentConfig

use of io.fabric8.openshift.api.model.DeploymentConfig in project syndesis by syndesisio.

the class OpenShiftServiceImpl method deploy.

@Override
public String deploy(String name, DeploymentData deploymentData) {
    final String sName = openshiftName(name);
    LOGGER.debug("Deploy {}", sName);
    ensureDeploymentConfig(sName, deploymentData);
    ensureSecret(sName, deploymentData);
    DeploymentConfig deployment = openShiftClient.deploymentConfigs().withName(sName).deployLatest();
    return String.valueOf(deployment.getStatus().getLatestVersion());
}
Also used : DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig)

Example 54 with DeploymentConfig

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

the class SessionListener method applyConfiguration.

private boolean applyConfiguration(KubernetesClient client, Controller controller, Configuration configuration, Session session, List<KubernetesList> kubeConfigs) throws Exception {
    Logger log = session.getLogger();
    Map<Integer, Callable<Boolean>> conditions = new TreeMap<>();
    Callable<Boolean> sessionPodsReady = new SessionPodsAreReady(client, session);
    Callable<Boolean> servicesReady = new SessionServicesAreReady(client, session, configuration);
    Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator());
    for (KubernetesList c : kubeConfigs) {
        entities.addAll(enhance(session, configuration, c).getItems());
    }
    if (containsImageStreamResources(entities)) {
    // no need to use a local image registry
    // as we are using OpenShift and
    } else {
        String registry = getLocalDockerRegistry();
        if (Strings.isNotBlank(registry)) {
            log.status("Adapting resources to pull images from registry: " + registry);
            addRegistryToImageNameIfNotPresent(entities, registry);
        } else {
            log.status("No local fabric8 docker registry found");
        }
    }
    List<Object> items = new ArrayList<>();
    items.addAll(entities);
    // Ensure services are processed first.
    Collections.sort(items, new Comparator<Object>() {

        @Override
        public int compare(Object left, Object right) {
            if (left instanceof Service) {
                return -1;
            } else if (right instanceof Service) {
                return 1;
            } else {
                return 0;
            }
        }
    });
    boolean isOpenshift = client.isAdaptable(OpenShiftClient.class);
    String namespace = session.getNamespace();
    String routeDomain = null;
    if (Strings.isNotBlank(configuration.getKubernetesDomain())) {
        routeDomain = configuration.getKubernetesDomain();
    }
    preprocessEnvironment(client, controller, configuration, session);
    Set<HasMetadata> extraEntities = new TreeSet<>(new HasMetadataComparator());
    for (Object entity : items) {
        if (entity instanceof Pod) {
            Pod pod = (Pod) entity;
            log.status("Applying pod:" + getName(pod));
            Set<Secret> secrets = generateSecrets(client, session, pod.getMetadata());
            String serviceAccountName = pod.getSpec().getServiceAccountName();
            if (Strings.isNotBlank(serviceAccountName)) {
                generateServiceAccount(client, session, secrets, serviceAccountName);
            }
            controller.applyPod(pod, session.getId());
            conditions.put(1, sessionPodsReady);
        } else if (entity instanceof Service) {
            Service service = (Service) entity;
            String serviceName = getName(service);
            log.status("Applying service:" + serviceName);
            controller.applyService(service, session.getId());
            conditions.put(2, servicesReady);
            if (isOpenshift) {
                Route route = Routes.createRouteForService(routeDomain, namespace, service, log);
                if (route != null) {
                    log.status("Applying route for:" + serviceName);
                    controller.applyRoute(route, "route for " + serviceName);
                    extraEntities.add(route);
                }
            }
        } else if (entity instanceof ReplicationController) {
            ReplicationController replicationController = (ReplicationController) entity;
            log.status("Applying replication controller:" + getName(replicationController));
            Set<Secret> secrets = generateSecrets(client, session, replicationController.getSpec().getTemplate().getMetadata());
            String serviceAccountName = replicationController.getSpec().getTemplate().getSpec().getServiceAccountName();
            if (Strings.isNotBlank(serviceAccountName)) {
                generateServiceAccount(client, session, secrets, serviceAccountName);
            }
            controller.applyReplicationController(replicationController, session.getId());
            conditions.put(1, sessionPodsReady);
        } else if (entity instanceof ReplicaSet || entity instanceof Deployment || entity instanceof DeploymentConfig) {
            log.status("Applying " + entity.getClass().getSimpleName() + ".");
            controller.apply(entity, session.getId());
            conditions.put(1, sessionPodsReady);
        } else if (entity instanceof OAuthClient) {
            OAuthClient oc = (OAuthClient) entity;
            // these are global so lets create a custom one for the new namespace
            ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(oc);
            String name = metadata.getName();
            if (isOpenshift) {
                OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
                OAuthClient current = openShiftClient.oAuthClients().withName(name).get();
                boolean create = false;
                if (current == null) {
                    current = oc;
                    create = true;
                }
                boolean updated = false;
                // lets add a new redirect entry
                List<String> redirectURIs = current.getRedirectURIs();
                String namespaceSuffix = "-" + namespace;
                String redirectUri = "http://" + name + namespaceSuffix;
                if (Strings.isNotBlank(routeDomain)) {
                    redirectUri += "." + Strings.stripPrefix(routeDomain, ".");
                }
                if (!redirectURIs.contains(redirectUri)) {
                    redirectURIs.add(redirectUri);
                    updated = true;
                }
                current.setRedirectURIs(redirectURIs);
                log.status("Applying OAuthClient:" + name);
                controller.setSupportOAuthClients(true);
                if (create) {
                    openShiftClient.oAuthClients().create(current);
                } else {
                    if (updated) {
                        // TODO this should work!
                        // openShiftClient.oAuthClients().withName(name).replace(current);
                        openShiftClient.oAuthClients().withName(name).delete();
                        current.getMetadata().setResourceVersion(null);
                        openShiftClient.oAuthClients().create(current);
                    }
                }
            }
        } else if (entity instanceof HasMetadata) {
            log.status("Applying " + entity.getClass().getSimpleName() + ":" + KubernetesHelper.getName((HasMetadata) entity));
            controller.apply(entity, session.getId());
        } else if (entity != null) {
            log.status("Applying " + entity.getClass().getSimpleName() + ".");
            controller.apply(entity, session.getId());
        }
    }
    entities.addAll(extraEntities);
    // Wait until conditions are meet.
    if (!conditions.isEmpty()) {
        Callable<Boolean> compositeCondition = new CompositeCondition(conditions.values());
        WaitStrategy waitStrategy = new WaitStrategy(compositeCondition, configuration.getWaitTimeout(), configuration.getWaitPollInterval());
        if (!waitStrategy.await()) {
            log.error("Timed out waiting for pods/services!");
            return false;
        } else {
            log.status("All pods/services are currently 'running'!");
        }
    } else {
        log.warn("No pods/services/replication controllers defined in the configuration!");
    }
    return true;
}
Also used : SessionPodsAreReady(io.fabric8.arquillian.kubernetes.await.SessionPodsAreReady) OAuthClient(io.fabric8.openshift.api.model.OAuthClient) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString) Logger(io.fabric8.arquillian.kubernetes.log.Logger) Callable(java.util.concurrent.Callable) HasMetadataComparator(io.fabric8.kubernetes.internal.HasMetadataComparator) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) Route(io.fabric8.openshift.api.model.Route) SessionServicesAreReady(io.fabric8.arquillian.kubernetes.await.SessionServicesAreReady) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) WaitStrategy(io.fabric8.arquillian.kubernetes.await.WaitStrategy) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) CompositeCondition(io.fabric8.arquillian.kubernetes.await.CompositeCondition)

Example 55 with DeploymentConfig

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

the class DeploymentPodsAssert method pods.

@Override
public PodSelectionAssert pods() {
    spec().isNotNull().selector().isNotNull();
    DeploymentSpec spec = this.actual.getSpec();
    Integer replicas = spec.getReplicas();
    LabelSelector selector = spec.getSelector();
    Map<String, String> matchLabels = selector.getMatchLabels();
    List<LabelSelectorRequirement> matchExpressions = selector.getMatchExpressions();
    return new PodSelectionAssert(client, replicas, matchLabels, matchExpressions, "DeploymentConfig " + KubernetesHelper.getName(actual));
}
Also used : DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) LabelSelectorRequirement(io.fabric8.kubernetes.api.model.LabelSelectorRequirement)

Aggregations

DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)41 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)15 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)14 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)13 Service (io.fabric8.kubernetes.api.model.Service)13 Pod (io.fabric8.kubernetes.api.model.Pod)11 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)11 IOException (java.io.IOException)11 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)9 Test (org.junit.Test)9 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 DeploymentConfigSpec (io.fabric8.openshift.api.model.DeploymentConfigSpec)8 Route (io.fabric8.openshift.api.model.Route)8 List (java.util.List)8 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)7 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)7 ImageStream (io.fabric8.openshift.api.model.ImageStream)7 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Controller (io.fabric8.kubernetes.api.Controller)6