Search in sources :

Example 1 with EditReplacePatchDeletable

use of io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable in project strimzi by strimzi.

the class MockKube method buildStatefulSets.

private MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> buildStatefulSets(MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods) {
    return new AbstractMockBuilder<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>>(StatefulSet.class, StatefulSetList.class, DoneableStatefulSet.class, castClass(RollableScalableResource.class), ssDb) {

        @Override
        protected void nameScopedMocks(RollableScalableResource<StatefulSet, DoneableStatefulSet> resource, String resourceName) {
            mockGet(resourceName, resource);
            // mockCreate("endpoint", endpointDb, resourceName, resource);
            mockCascading(resource);
            mockPatch(resourceName, resource);
            mockDelete(resourceName, resource);
            mockIsReady(resourceName, resource);
            when(resource.create(any())).thenAnswer(cinvocation -> {
                checkNotExists(resourceName);
                StatefulSet argument = cinvocation.getArgument(0);
                LOGGER.debug("create {} {} -> {}", resourceType, resourceName, argument);
                ssDb.put(resourceName, copyResource(argument));
                for (int i = 0; i < argument.getSpec().getReplicas(); i++) {
                    String podName = argument.getMetadata().getName() + "-" + i;
                    podDb.put(podName, new PodBuilder().withNewMetadata().withNamespace(argument.getMetadata().getNamespace()).withName(podName).endMetadata().build());
                }
                return argument;
            });
            EditReplacePatchDeletable<StatefulSet, StatefulSet, DoneableStatefulSet, Boolean> c = mock(EditReplacePatchDeletable.class);
            when(resource.cascading(false)).thenReturn(c);
            when(c.patch(any())).thenAnswer(patchInvocation -> {
                StatefulSet argument = patchInvocation.getArgument(0);
                return doPatch(resourceName, argument);
            });
            when(resource.scale(anyInt(), anyBoolean())).thenAnswer(invocation -> {
                checkDoesExist(resourceName);
                StatefulSet ss = copyResource(ssDb.get(resourceName));
                int newScale = invocation.getArgument(0);
                ss.getSpec().setReplicas(newScale);
                return doPatch(resourceName, ss);
            });
            when(resource.scale(anyInt())).thenAnswer(invocation -> {
                checkDoesExist(resourceName);
                StatefulSet ss = copyResource(ssDb.get(resourceName));
                int newScale = invocation.getArgument(0);
                ss.getSpec().setReplicas(newScale);
                return doPatch(resourceName, ss);
            });
            when(resource.isReady()).thenAnswer(i -> {
                LOGGER.debug("{} {} is ready", resourceType, resourceName);
                return true;
            });
            mockPods.inNamespace(any()).withName(any()).watch(new Watcher<Pod>() {

                @Override
                public void eventReceived(Action action, Pod resource) {
                    if (action == Action.DELETED) {
                        String podName = resource.getMetadata().getName();
                        String podNamespace = resource.getMetadata().getNamespace();
                        StatefulSet statefulSet = ssDb.get(resourceName);
                        if (podName.startsWith(resourceName + "-") && Integer.parseInt(podName.substring(podName.lastIndexOf("-") + 1)) < statefulSet.getSpec().getReplicas()) {
                            mockPods.inNamespace(podNamespace).withName(podName).create(resource);
                        }
                    }
                }

                @Override
                public void onClose(KubernetesClientException e) {
                }
            });
        }

        private StatefulSet doPatch(String resourceName, StatefulSet argument) {
            int oldScale = ssDb.get(resourceName).getSpec().getReplicas();
            int newScale = argument.getSpec().getReplicas();
            if (newScale > oldScale) {
                LOGGER.debug("scaling up {} {} from {} to {}", resourceType, resourceName, oldScale, newScale);
                Pod examplePod = mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(argument.getMetadata().getName() + "-0").get();
                for (int i = oldScale; i < newScale; i++) {
                    String newPodName = argument.getMetadata().getName() + "-" + i;
                    mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(newPodName).create(new PodBuilder(examplePod).editMetadata().withName(newPodName).endMetadata().build());
                }
                ssDb.put(resourceName, copyResource(argument));
            } else if (newScale < oldScale) {
                ssDb.put(resourceName, copyResource(argument));
                LOGGER.debug("scaling down {} {} from {} to {}", resourceType, resourceName, oldScale, newScale);
                for (int i = oldScale - 1; i >= newScale; i--) {
                    String newPodName = argument.getMetadata().getName() + "-" + i;
                    mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(newPodName).delete();
                }
            } else {
                ssDb.put(resourceName, copyResource(argument));
            }
            return argument;
        }
    }.build();
}
Also used : DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) KubernetesResourceList(io.fabric8.kubernetes.api.model.KubernetesResourceList) Doneable(io.fabric8.kubernetes.api.model.Doneable) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) LoggerFactory(org.slf4j.LoggerFactory) Watcher(io.fabric8.kubernetes.client.Watcher) DoneablePersistentVolumeClaim(io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim) Resource(io.fabric8.kubernetes.client.dsl.Resource) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PersistentVolumeClaimList(io.fabric8.kubernetes.api.model.PersistentVolumeClaimList) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) ExtensionsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.ExtensionsAPIGroupDSL) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) DoneableDeployment(io.fabric8.kubernetes.api.model.extensions.DoneableDeployment) DoneableEndpoints(io.fabric8.kubernetes.api.model.DoneableEndpoints) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) DeploymentList(io.fabric8.kubernetes.api.model.extensions.DeploymentList) EndpointsList(io.fabric8.kubernetes.api.model.EndpointsList) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Service(io.fabric8.kubernetes.api.model.Service) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) OngoingStubbing(org.mockito.stubbing.OngoingStubbing) ConfigMapList(io.fabric8.kubernetes.api.model.ConfigMapList) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.when(org.mockito.Mockito.when) DoneableConfigMap(io.fabric8.kubernetes.api.model.DoneableConfigMap) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) PodList(io.fabric8.kubernetes.api.model.PodList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) Watcher(io.fabric8.kubernetes.client.Watcher) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 ConfigMapList (io.fabric8.kubernetes.api.model.ConfigMapList)1 Doneable (io.fabric8.kubernetes.api.model.Doneable)1 DoneableConfigMap (io.fabric8.kubernetes.api.model.DoneableConfigMap)1 DoneableEndpoints (io.fabric8.kubernetes.api.model.DoneableEndpoints)1 DoneablePersistentVolumeClaim (io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim)1 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)1 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)1 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)1 EndpointsList (io.fabric8.kubernetes.api.model.EndpointsList)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)1 KubernetesResourceList (io.fabric8.kubernetes.api.model.KubernetesResourceList)1 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)1 PersistentVolumeClaimList (io.fabric8.kubernetes.api.model.PersistentVolumeClaimList)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 Service (io.fabric8.kubernetes.api.model.Service)1 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)1