Search in sources :

Example 46 with ReplicaSet

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

the class KubernetesResourceUtil method getPodLabelSelector.

public static LabelSelector getPodLabelSelector(HasMetadata entity) {
    LabelSelector selector = null;
    if (entity instanceof Deployment) {
        Deployment resource = (Deployment) entity;
        DeploymentSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof ReplicaSet) {
        ReplicaSet resource = (ReplicaSet) entity;
        ReplicaSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) entity;
        DeploymentConfigSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof ReplicationController) {
        ReplicationController resource = (ReplicationController) entity;
        ReplicationControllerSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof DaemonSet) {
        DaemonSet resource = (DaemonSet) entity;
        DaemonSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof StatefulSet) {
        StatefulSet resource = (StatefulSet) entity;
        StatefulSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof Job) {
        Job resource = (Job) entity;
        JobSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    }
    return selector;
}
Also used : StatefulSetSpec(io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec) ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DaemonSetSpec(io.fabric8.kubernetes.api.model.extensions.DaemonSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DaemonSet(io.fabric8.kubernetes.api.model.extensions.DaemonSet) JobSpec(io.fabric8.kubernetes.api.model.JobSpec) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) Job(io.fabric8.kubernetes.api.model.Job) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 47 with ReplicaSet

use of io.fabric8.kubernetes.api.model.apps.ReplicaSet in project halyard by spinnaker.

the class KubernetesV1DistributedService method getRunningServiceDetails.

@Override
default RunningServiceDetails getRunningServiceDetails(AccountDeploymentDetails<KubernetesAccount> details, SpinnakerRuntimeSettings runtimeSettings) {
    ServiceSettings settings = runtimeSettings.getServiceSettings(getService());
    RunningServiceDetails res = new RunningServiceDetails();
    KubernetesClient client = KubernetesV1ProviderUtils.getClient(details);
    String name = getServiceName();
    String namespace = getNamespace(settings);
    RunningServiceDetails.LoadBalancer lb = new RunningServiceDetails.LoadBalancer();
    lb.setExists(client.services().inNamespace(namespace).withName(name).get() != null);
    res.setLoadBalancer(lb);
    List<Pod> pods = client.pods().inNamespace(namespace).withLabel("load-balancer-" + name, "true").list().getItems();
    pods.addAll(client.pods().inNamespace(namespace).withLabel("load-balancer-" + name, "false").list().getItems());
    Map<Integer, List<Instance>> instances = res.getInstances();
    for (Pod pod : pods) {
        String podName = pod.getMetadata().getName();
        String serverGroupName = podName.substring(0, podName.lastIndexOf("-"));
        Names parsedName = Names.parseName(serverGroupName);
        Integer version = parsedName.getSequence();
        if (version == null) {
            throw new IllegalStateException("Server group for service " + getServiceName() + " has unknown sequence (" + serverGroupName + ")");
        }
        String location = pod.getMetadata().getNamespace();
        String id = pod.getMetadata().getName();
        Instance instance = new Instance().setId(id).setLocation(location);
        List<ContainerStatus> containerStatuses = pod.getStatus().getContainerStatuses();
        if (!containerStatuses.isEmpty() && containerStatuses.stream().allMatch(ContainerStatus::getReady)) {
            instance.setHealthy(true);
        }
        if (!containerStatuses.isEmpty() && containerStatuses.stream().allMatch(s -> s.getState().getRunning() != null && s.getState().getTerminated() == null)) {
            instance.setRunning(true);
        }
        List<Instance> knownInstances = instances.getOrDefault(version, new ArrayList<>());
        knownInstances.add(instance);
        instances.put(version, knownInstances);
    }
    List<ReplicaSet> replicaSets = client.extensions().replicaSets().inNamespace(settings.getLocation()).list().getItems();
    for (ReplicaSet rs : replicaSets) {
        String rsName = rs.getMetadata().getName();
        Names parsedRsName = Names.parseName(rsName);
        if (!parsedRsName.getCluster().equals(getServiceName())) {
            continue;
        }
        instances.computeIfAbsent(parsedRsName.getSequence(), i -> new ArrayList<>());
    }
    return res;
}
Also used : KubernetesResourceDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesResourceDescription) KubernetesUtil(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.KubernetesUtil) SpinnakerMonitoringDaemonService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerMonitoringDaemonService) KubernetesAccount(com.netflix.spinnaker.halyard.config.model.v1.providers.kubernetes.KubernetesAccount) LogCollector(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.LogCollector) HalException(com.netflix.spinnaker.halyard.core.error.v1.HalException) DaemonTaskHandler(com.netflix.spinnaker.halyard.core.tasks.v1.DaemonTaskHandler) SidecarService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.SidecarService) Pair(org.apache.commons.lang3.tuple.Pair) Names(com.netflix.frigga.Names) Map(java.util.Map) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) Provider(com.netflix.spinnaker.halyard.config.model.v1.node.Provider) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SpinnakerService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.SpinnakerService) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Strings(io.fabric8.utils.Strings) DeploymentEnvironment(com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentEnvironment) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) ServiceInterfaceFactory(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceInterfaceFactory) KubernetesVolumeMount(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeMount) Set(java.util.Set) KubernetesProbe(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesProbe) GenerateService(com.netflix.spinnaker.halyard.deploy.services.v1.GenerateService) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) Collectors(java.util.stream.Collectors) List(java.util.List) KubernetesVolumeSource(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeSource) SocketUtils(org.springframework.util.SocketUtils) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) KubernetesContainerDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesContainerDescription) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) JobStatus(com.netflix.spinnaker.halyard.core.job.v1.JobStatus) Container(io.fabric8.kubernetes.api.model.Container) DeployKubernetesAtomicOperationDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.DeployKubernetesAtomicOperationDescription) CustomSizing(com.netflix.spinnaker.halyard.config.model.v1.node.CustomSizing) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) HashMap(java.util.HashMap) ArtifactService(com.netflix.spinnaker.halyard.deploy.services.v1.ArtifactService) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) KubernetesHandler(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHandler) AccountDeploymentDetails(com.netflix.spinnaker.halyard.deploy.deployment.v1.AccountDeploymentDetails) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) JobExecutor(com.netflix.spinnaker.halyard.core.job.v1.JobExecutor) KubernetesImageDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesImageDescription) KubernetesContainerPort(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesContainerPort) Instance(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails.Instance) KubernetesNamedServicePort(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.loadbalancer.KubernetesNamedServicePort) Volume(io.fabric8.kubernetes.api.model.Volume) ReplicaSetBuilder(io.fabric8.kubernetes.api.model.extensions.ReplicaSetBuilder) KubernetesLoadBalancerDescription(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.loadbalancer.KubernetesLoadBalancerDescription) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) KubernetesEnvVar(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesEnvVar) KubernetesSecretVolumeSource(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesSecretVolumeSource) JobRequest(com.netflix.spinnaker.halyard.core.job.v1.JobRequest) Pod(io.fabric8.kubernetes.api.model.Pod) SpinnakerRuntimeSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.SpinnakerRuntimeSettings) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Profile(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.profile.Profile) Paths(java.nio.file.Paths) KubernetesHttpGetAction(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHttpGetAction) KubernetesVolumeSourceType(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesVolumeSourceType) KubernetesTcpSocketAction(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesTcpSocketAction) DistributedService(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.DistributedService) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigSource(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource) Collections(java.util.Collections) KubernetesHandlerType(com.netflix.spinnaker.clouddriver.kubernetes.v1.deploy.description.servergroup.KubernetesHandlerType) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) Instance(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails.Instance) ServiceSettings(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings) Names(com.netflix.frigga.Names) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) RunningServiceDetails(com.netflix.spinnaker.halyard.deploy.spinnaker.v1.RunningServiceDetails) List(java.util.List) ArrayList(java.util.ArrayList) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet)

Example 48 with ReplicaSet

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

the class ReplicaSetHandlerTest method replicaSetHandlerTest.

@Test
public void replicaSetHandlerTest() {
    ContainerHandler containerHandler = getContainerHandler();
    PodTemplateHandler podTemplateHandler = new PodTemplateHandler(containerHandler);
    ReplicaSetHandler replicaSetHandler = new ReplicaSetHandler(podTemplateHandler);
    ResourceConfig config = new ResourceConfig.Builder().imagePullPolicy("IfNotPresent").controllerName("testing").withServiceAccount("test-account").withReplicas(5).volumes(volumes1).build();
    ReplicaSet replicaSet = replicaSetHandler.getReplicaSet(config, images);
    // Assertion
    assertNotNull(replicaSet.getSpec());
    assertNotNull(replicaSet.getMetadata());
    assertEquals(5, replicaSet.getSpec().getReplicas().intValue());
    assertNotNull(replicaSet.getSpec().getTemplate());
    assertEquals("testing", replicaSet.getMetadata().getName());
    assertEquals("test-account", replicaSet.getSpec().getTemplate().getSpec().getServiceAccountName());
    assertFalse(replicaSet.getSpec().getTemplate().getSpec().getVolumes().isEmpty());
    assertEquals("test", replicaSet.getSpec().getTemplate().getSpec().getVolumes().get(0).getName());
    assertEquals("/test/path", replicaSet.getSpec().getTemplate().getSpec().getVolumes().get(0).getHostPath().getPath());
    assertNotNull(replicaSet.getSpec().getTemplate().getSpec().getContainers());
}
Also used : ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) ReplicaSet(io.fabric8.kubernetes.api.model.apps.ReplicaSet) Test(org.junit.Test)

Example 49 with ReplicaSet

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

the class ImageEnricherTest method checkEnrichReplicaSet.

@Test
public void checkEnrichReplicaSet() throws Exception {
    KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new ReplicaSetBuilder().build());
    imageEnricher.create(PlatformMode.kubernetes, builder);
    assertCorrectlyGeneratedResources(builder.build(), "ReplicaSet");
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ReplicaSetBuilder(io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder) Test(org.junit.Test)

Example 50 with ReplicaSet

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

the class TriggersAnnotationEnricherTest method testReplicaSetEnrichment.

@Test
public void testReplicaSetEnrichment() throws IOException {
    KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new ReplicaSetBuilder().withNewSpec().withNewTemplate().withNewSpec().withContainers(createContainers("c1", "is", "c2", "a-docker-user/is:latest")).endSpec().endTemplate().endSpec().build());
    TriggersAnnotationEnricher enricher = new TriggersAnnotationEnricher(context);
    enricher.enrich(PlatformMode.kubernetes, builder);
    ReplicaSet res = (ReplicaSet) builder.build().getItems().get(0);
    String triggers = res.getMetadata().getAnnotations().get("image.openshift.io/triggers");
    assertNotNull(triggers);
    List<ImageChangeTrigger> triggerList = OBJECT_MAPPER.readValue(triggers, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, ImageChangeTrigger.class));
    assertEquals(1, triggerList.size());
    ImageChangeTrigger trigger = triggerList.get(0);
    assertEquals("ImageStreamTag", trigger.getFrom().getKind());
    assertEquals("is:latest", trigger.getFrom().getName());
    assertTrue(trigger.getAdditionalProperties().containsKey("fieldPath"));
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ReplicaSetBuilder(io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ImageChangeTrigger(io.fabric8.openshift.api.model.ImageChangeTrigger) ReplicaSet(io.fabric8.kubernetes.api.model.apps.ReplicaSet) Test(org.junit.Test)

Aggregations

ReplicaSet (io.fabric8.kubernetes.api.model.apps.ReplicaSet)50 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)26 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)22 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)22 Test (org.junit.Test)19 ReplicaSetBuilder (io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder)18 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)17 Test (org.junit.jupiter.api.Test)15 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)14 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)14 Pod (io.fabric8.kubernetes.api.model.Pod)13 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)10 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)9 ReplicationControllerSpec (io.fabric8.kubernetes.api.model.ReplicationControllerSpec)9 ReplicaSetSpec (io.fabric8.kubernetes.api.model.apps.ReplicaSetSpec)9 DeploymentConfigSpec (io.fabric8.openshift.api.model.DeploymentConfigSpec)9 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)8 Container (io.fabric8.kubernetes.api.model.Container)7 DaemonSet (io.fabric8.kubernetes.api.model.apps.DaemonSet)7 DeploymentSpec (io.fabric8.kubernetes.api.model.apps.DeploymentSpec)7