下面列出了io.fabric8.kubernetes.api.model.apps.DoneableDeployment#io.fabric8.kubernetes.client.dsl.RollableScalableResource 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void mockDelete(String resourceName, RollableScalableResource<StatefulSet, DoneableStatefulSet> resource) {
when(resource.cascading(true).delete()).thenAnswer(i -> {
LOGGER.debug("delete {} {}", resourceType, resourceName);
StatefulSet removed = db.remove(resourceName);
if (removed != null) {
fireWatchers(resourceName, removed, Watcher.Action.DELETED, "delete");
for (Map.Entry<String, Pod> pod : new HashMap<>(podDb).entrySet()) {
if (pod.getKey().matches(resourceName + "-[0-9]+")) {
mockPods.inNamespace(removed.getMetadata().getNamespace()).withName(pod.getKey()).cascading(true).delete();
}
}
}
return removed != null;
});
}
private List<RollableScalableResource<ReplicaSet, DoneableReplicaSet>> doGetLog() {
List<RollableScalableResource<ReplicaSet, DoneableReplicaSet>> rcs = new ArrayList<>();
DeploymentConfig deploymentConfig = fromServer().get();
String rcUid = deploymentConfig.getMetadata().getUid();
ReplicaSetOperationsImpl rsOperations = new ReplicaSetOperationsImpl((RollingOperationContext) context);
ReplicaSetList rcList = rsOperations.withLabels(deploymentConfig.getMetadata().getLabels()).list();
for (ReplicaSet rs : rcList.getItems()) {
OwnerReference ownerReference = KubernetesResourceUtil.getControllerUid(rs);
if (ownerReference != null && ownerReference.getUid().equals(rcUid)) {
rcs.add(rsOperations.withName(rs.getMetadata().getName()));
}
}
return rcs;
}
@Override
public boolean deleteResource(WebServer nginx) {
log.info("Execution deleteResource for: {}", nginx.getMetadata().getName());
log.info("Deleting ConfigMap {}", configMapName(nginx));
Resource<ConfigMap, DoneableConfigMap> configMap = kubernetesClient.configMaps()
.inNamespace(nginx.getMetadata().getNamespace())
.withName(configMapName(nginx));
if (configMap.get() != null) {
configMap.delete();
}
log.info("Deleting Deployment {}", deploymentName(nginx));
RollableScalableResource<Deployment, DoneableDeployment> deployment = kubernetesClient.apps().deployments()
.inNamespace(nginx.getMetadata().getNamespace())
.withName(deploymentName(nginx));
if (deployment.get() != null) {
deployment.cascading(true).delete();
}
log.info("Deleting Service {}", serviceName(nginx));
ServiceResource<Service, DoneableService> service = kubernetesClient.services()
.inNamespace(nginx.getMetadata().getNamespace())
.withName(serviceName(nginx));
if (service.get() != null) {
service.delete();
}
return true;
}
private Map<String, Integer> getActual() {
MixedOperation<ReplicationController, ReplicationControllerList, DoneableReplicationController, RollableScalableResource<ReplicationController, DoneableReplicationController>> aux1 =
client.replicationControllers();
FilterWatchListMultiDeletable<ReplicationController, ReplicationControllerList, Boolean, Watch, Watcher<ReplicationController>> aux2 =
"*".equals(namespace) ? aux1.inAnyNamespace() : aux1.inNamespace(namespace);
Map<String, String> labels =new HashMap<>(2);
labels.put(prefix + OPERATOR_KIND_LABEL, entityName);
labels.put(prefix + OPERATOR_RC_TYPE_LABEL, "worker");
List<ReplicationController> workerRcs = aux2.withLabels(labels).list().getItems();
Map<String, Integer> retMap = workerRcs
.stream()
.collect(Collectors.toMap(rc -> rc.getMetadata().getLabels().get(prefix + entityName),
rc -> rc.getSpec().getReplicas()));
return retMap;
}
@Override
protected void mockCreate(String resourceName, RollableScalableResource<Deployment, DoneableDeployment> resource) {
when(resource.create(any())).thenAnswer(invocation -> {
checkNotExists(resourceName);
Deployment deployment = invocation.getArgument(0);
LOGGER.debug("create {} {} -> {}", resourceType, resourceName, deployment);
deployment.getMetadata().setGeneration(Long.valueOf(0));
deployment.setStatus(new DeploymentStatusBuilder().withObservedGeneration(Long.valueOf(0)).build());
db.put(resourceName, copyResource(deployment));
for (int i = 0; i < deployment.getSpec().getReplicas(); i++) {
String uuid = UUID.randomUUID().toString();
String podName = deployment.getMetadata().getName() + "-" + uuid;
LOGGER.debug("create Pod {} because it's in Deployment {}", podName, resourceName);
Pod pod = new PodBuilder()
.withNewMetadataLike(deployment.getSpec().getTemplate().getMetadata())
.withUid(uuid)
.withNamespace(deployment.getMetadata().getNamespace())
.withName(podName)
.endMetadata()
.withNewSpecLike(deployment.getSpec().getTemplate().getSpec()).endSpec()
.build();
mockPods.inNamespace(deployment.getMetadata().getNamespace()).withName(podName).create(pod);
podsForDeployments.compute(deployment.getMetadata().getName(), (deploymentName, podsInDeployment) -> {
if (podsInDeployment == null) {
podsInDeployment = new ArrayList<>(2);
}
podsInDeployment.add(podName);
return podsInDeployment;
});
}
return deployment;
});
}
private MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>>
buildStatefulSets(MockBuilder<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> podMockBuilder, MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods,
MixedOperation<PersistentVolumeClaim, PersistentVolumeClaimList, DoneablePersistentVolumeClaim,
Resource<PersistentVolumeClaim, DoneablePersistentVolumeClaim>> mockPvcs) {
MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet,
DoneableStatefulSet>> result = new StatefulSetMockBuilder(podMockBuilder, ssDb, podDb, mockPods, mockPvcs).build();
return result;
}
@Override
@SuppressWarnings("unchecked")
protected void nameScopedMocks(String resourceName, RollableScalableResource<StatefulSet, DoneableStatefulSet> resource) {
super.nameScopedMocks(resourceName, resource);
EditReplacePatchDeletable<StatefulSet, StatefulSet, DoneableStatefulSet, Boolean> c = mock(EditReplacePatchDeletable.class);
when(c.withGracePeriod(anyLong())).thenReturn(resource);
when(resource.cascading(false)).thenReturn(c);
mockNoncascadingPatch(resourceName, c);
mockScale(resourceName, resource);
mockNoncascadingDelete(resourceName, c);
}
@Override
protected void mockCreate(String resourceName, RollableScalableResource<StatefulSet, DoneableStatefulSet> resource) {
when(resource.create(any())).thenAnswer(cinvocation -> {
checkNotExists(resourceName);
StatefulSet argument = cinvocation.getArgument(0);
LOGGER.debug("create {} {} -> {}", resourceType, resourceName, argument);
StatefulSet value = copyResource(argument);
value.setStatus(new StatefulSetStatus());
db.put(resourceName, value);
for (int i = 0; i < argument.getSpec().getReplicas(); i++) {
final int podNum = i;
String podName = argument.getMetadata().getName() + "-" + podNum;
LOGGER.debug("create Pod {} because it's in StatefulSet {}", podName, resourceName);
mockPods.inNamespace(argument.getMetadata().getNamespace()).createOrReplace(doCreatePod(argument, podName));
if (value.getSpec().getVolumeClaimTemplates().size() > 0) {
for (PersistentVolumeClaim pvcTemplate: value.getSpec().getVolumeClaimTemplates()) {
String pvcName = pvcTemplate.getMetadata().getName() + "-" + podName;
if (mockPvcs.inNamespace(argument.getMetadata().getNamespace()).withName(pvcName).get() == null) {
LOGGER.debug("create Pvc {} because it's in VolumeClaimTemplate of StatefulSet {}", pvcName, resourceName);
PersistentVolumeClaim pvc = new PersistentVolumeClaimBuilder()
.withNewMetadata()
.withLabels(argument.getSpec().getSelector().getMatchLabels())
.withNamespace(argument.getMetadata().getNamespace())
.withName(pvcName)
.endMetadata()
.build();
mockPvcs.inNamespace(argument.getMetadata().getNamespace()).withName(pvcName).create(pvc);
}
}
}
}
return argument;
});
}
public String getLog(Boolean isPretty) {
StringBuilder stringBuilder = new StringBuilder();
List<RollableScalableResource<ReplicaSet, DoneableReplicaSet>> rcList = doGetLog();
for (RollableScalableResource<ReplicaSet, DoneableReplicaSet> rcOperation : rcList) {
stringBuilder.append(rcOperation.getLog(isPretty));
}
return stringBuilder.toString();
}
/**
* Returns an unclosed Reader. It's the caller responsibility to close it.
* @return Reader
*/
@Override
public Reader getLogReader() {
List<RollableScalableResource<ReplicaSet, DoneableReplicaSet>> podResources = doGetLog();
if (podResources.size() > 1) {
throw new KubernetesClientException("Reading logs is not supported for multicontainer jobs");
} else if (podResources.size() == 1) {
return podResources.get(0).getLogReader();
}
return null;
}
@Override
public LogWatch watchLog(OutputStream out) {
List<RollableScalableResource<ReplicaSet, DoneableReplicaSet>> podResources = doGetLog();
if (podResources.size() > 1) {
throw new KubernetesClientException("Watching logs is not supported for multicontainer jobs");
} else if (podResources.size() == 1) {
return podResources.get(0).watchLog(out);
}
return null;
}
@Override
protected MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> operation() {
return getClient().apps().statefulSets();
}
@Override
protected MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> operation() {
return client.apps().statefulSets();
}
@Override
protected Class<RollableScalableResource> resourceType() {
return RollableScalableResource.class;
}
@Override
protected MixedOperation<Deployment, DeploymentList, DoneableDeployment, RollableScalableResource<Deployment, DoneableDeployment>> operation() {
return client.apps().deployments();
}
@Override
protected Class<RollableScalableResource> resourceType() {
return RollableScalableResource.class;
}
public DeploymentMockBuilder(Map<String, Deployment> depDb, MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods) {
super(Deployment.class, DeploymentList.class, DoneableDeployment.class, castClass(RollableScalableResource.class), depDb);
this.mockPods = mockPods;
}
/**
* Gets stateful set
*/
public RollableScalableResource<StatefulSet, DoneableStatefulSet> statefulSet(String statefulSetName) {
return client.apps().statefulSets().inNamespace(getNamespace()).withName(statefulSetName);
}
@Override
protected Operation<Deployment, DeploymentList, DoneableDeployment, RollableScalableResource<Deployment, DoneableDeployment>> resources() {
return new DeploymentOperationsImpl(client, config);
}
@Override
protected Operation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> resources() {
return new StatefulSetOperationsImpl(client, config);
}
@Override
protected Operation<ReplicaSet, ReplicaSetList, DoneableReplicaSet, RollableScalableResource<ReplicaSet, DoneableReplicaSet>> resources() {
return new ReplicaSetOperationsImpl(client, config);
}
@Override
protected Operation<ReplicationController, ReplicationControllerList, DoneableReplicationController, RollableScalableResource<ReplicationController, DoneableReplicationController>> resources() {
return new ReplicationControllerOperationsImpl(client, config);
}
@Override
public RollableScalableResource<ReplicationController, DoneableReplicationController> load(InputStream is) {
ReplicationController item = unmarshal(is, ReplicationController.class);
return new ReplicationControllerOperationsImpl((RollingOperationContext) context.withItem(item));
}
@Override
@Deprecated
public MixedOperation<Deployment, DeploymentList, DoneableDeployment, RollableScalableResource<Deployment, DoneableDeployment>> deployments() {
return new DeploymentOperationsImpl(httpClient, getConfiguration());
}
@Override
@Deprecated
public MixedOperation<ReplicaSet, ReplicaSetList, DoneableReplicaSet, RollableScalableResource<ReplicaSet, DoneableReplicaSet>> replicaSets() {
return new ReplicaSetOperationsImpl(httpClient, getConfiguration());
}
public MixedOperation<ReplicationController, ReplicationControllerList, DoneableReplicationController, RollableScalableResource<ReplicationController, DoneableReplicationController>> replicationControllers() {
return delegate.replicationControllers();
}
@Override
public MixedOperation<Deployment, DeploymentList, DoneableDeployment, RollableScalableResource<Deployment, DoneableDeployment>> deployments() {
return new DeploymentOperationsImpl(httpClient, getConfiguration());
}
@Override
public MixedOperation<ReplicaSet, ReplicaSetList, DoneableReplicaSet, RollableScalableResource<ReplicaSet, DoneableReplicaSet>> replicaSets() {
return new ReplicaSetOperationsImpl(httpClient, getConfiguration());
}
@Override
public MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> statefulSets() {
return new StatefulSetOperationsImpl(httpClient, getConfiguration());
}
protected abstract Operation<T, L, D, RollableScalableResource<T, D>> resources();