Search in sources :

Example 1 with ReplicaSet

use of io.fabric8.kubernetes.api.model.apps.ReplicaSet in project jointware by isdream.

the class KubernetesKeyValueStyleGeneratorTest method testKubernetesWithAllKind.

protected static void testKubernetesWithAllKind() throws Exception {
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new ServiceAccount());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new ThirdPartyResource());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new ResourceQuota());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Node());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new ConfigMap());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new NetworkPolicy());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new CustomResourceDefinition());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Ingress());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Service());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Namespace());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Secret());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new LimitRange());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Event());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new PersistentVolume());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new StatefulSet());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new PersistentVolumeClaim());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new DaemonSet());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new HorizontalPodAutoscaler());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Pod());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new ReplicaSet());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Job());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new ReplicationController());
    info(KUBERNETES_KIND, KubernetesDocumentKeyValueStyleGenerator.class.getName(), new Deployment());
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Pod(io.fabric8.kubernetes.api.model.Pod) ThirdPartyResource(io.fabric8.kubernetes.api.model.extensions.ThirdPartyResource) NetworkPolicy(io.fabric8.kubernetes.api.model.extensions.NetworkPolicy) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.CustomResourceDefinition) Node(io.fabric8.kubernetes.api.model.Node) Ingress(io.fabric8.kubernetes.api.model.extensions.Ingress) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) Namespace(io.fabric8.kubernetes.api.model.Namespace) Secret(io.fabric8.kubernetes.api.model.Secret) LimitRange(io.fabric8.kubernetes.api.model.LimitRange) ResourceQuota(io.fabric8.kubernetes.api.model.ResourceQuota) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) HorizontalPodAutoscaler(io.fabric8.kubernetes.api.model.HorizontalPodAutoscaler) Event(io.fabric8.kubernetes.api.model.Event) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) DaemonSet(io.fabric8.kubernetes.api.model.extensions.DaemonSet) PersistentVolume(io.fabric8.kubernetes.api.model.PersistentVolume) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) Job(io.fabric8.kubernetes.api.model.Job) KubernetesDocumentKeyValueStyleGenerator(com.github.isdream.chameleon.docs.KubernetesDocumentKeyValueStyleGenerator) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet)

Example 2 with ReplicaSet

use of io.fabric8.kubernetes.api.model.apps.ReplicaSet in project fabric8 by jboss-fuse.

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 3 with ReplicaSet

use of io.fabric8.kubernetes.api.model.apps.ReplicaSet in project fabric8 by jboss-fuse.

the class KubernetesAssert method deployments.

/**
 * Finds all the resources that create pod selections (Deployment, DeploymentConfig, ReplicaSet, ReplicationController)
 * and create a {@link HasPodSelectionAssert} to make assertions on their pods that they startup etc.
 *
 * @return the assertion object for the deployment
 */
public HasPodSelectionAssert deployments() {
    List<HasPodSelectionAssert> asserters = new ArrayList<>();
    List<HasMetadata> resources = new ArrayList<>();
    try {
        resources = KubernetesHelper.findKubernetesResourcesOnClasspath(new Controller(client));
    } catch (IOException e) {
        fail("Failed to load kubernetes resources on the classpath: " + e, e);
    }
    for (HasMetadata resource : resources) {
        HasPodSelectionAssert asserter = createPodSelectionAssert(resource);
        if (asserter != null) {
            asserters.add(asserter);
        }
    }
    String message = "No pod selection kinds found on the classpath such as Deployment, DeploymentConfig, ReplicaSet, ReplicationController";
    // TODO we don't yet support size > 1
    assertThat(asserters).describedAs(message).isNotEmpty();
    if (asserters.size() == 1) {
        return asserters.get(0);
    }
    return new MultiHasPodSelectionAssert(asserters);
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 4 with ReplicaSet

use of io.fabric8.kubernetes.api.model.apps.ReplicaSet in project fabric8 by jboss-fuse.

the class KubernetesAssert method replicas.

/**
 * Asserts that there is a ReplicaSet or ReplicationController of the given name
 *
 * @return the assertion object for the replicas
 */
public HasPodSelectionAssert replicas(String replicaName) {
    String namespace = namespace();
    String qualifiedName = namespace + "." + replicaName;
    ReplicaSet replicasSet = client.extensions().replicaSets().withName(replicaName).get();
    if (replicasSet != null) {
        assertThat(replicasSet).describedAs("ReplicaSet: " + qualifiedName).metadata().name().isEqualTo(replicaName);
        return new ReplicaSetPodsAssert(client, replicasSet);
    } else {
        ReplicationController replicationController = client.replicationControllers().withName(replicaName).get();
        assertThat(replicationController).describedAs("No ReplicaSet or ReplicationController called: " + qualifiedName).isNotNull();
        assertThat(replicationController).describedAs("ReplicationController: " + qualifiedName).metadata().name().isEqualTo(replicaName);
        return new ReplicationControllerPodsAssert(client, replicationController);
    }
}
Also used : ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet)

Example 5 with ReplicaSet

use of io.fabric8.kubernetes.api.model.apps.ReplicaSet in project fabric8 by jboss-fuse.

the class ReplicaSetPodsAssert method pods.

@Override
public PodSelectionAssert pods() {
    spec().isNotNull().selector().isNotNull();
    ReplicaSetSpec 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, "ReplicaSet " + KubernetesHelper.getName(actual));
}
Also used : ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) LabelSelectorRequirement(io.fabric8.kubernetes.api.model.LabelSelectorRequirement)

Aggregations

ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)17 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)14 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)12 ReplicaSet (io.fabric8.kubernetes.api.model.apps.ReplicaSet)10 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)10 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)8 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)7 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)7 ReplicationControllerSpec (io.fabric8.kubernetes.api.model.ReplicationControllerSpec)6 DeploymentConfigSpec (io.fabric8.openshift.api.model.DeploymentConfigSpec)6 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)5 ReplicaSetSpec (io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec)5 IOException (java.io.IOException)5 Controller (io.fabric8.kubernetes.api.Controller)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)4 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)4 Pod (io.fabric8.kubernetes.api.model.Pod)4 Secret (io.fabric8.kubernetes.api.model.Secret)4 Service (io.fabric8.kubernetes.api.model.Service)4