下面列出了io.fabric8.kubernetes.api.model.policy.PodDisruptionBudgetBuilder#io.fabric8.kubernetes.api.model.policy.PodDisruptionBudget 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private PodDisruptionBudget buildPodDisruptionBudget(final RabbitMQCustomResource resource) {
final String namespace = resource.getMetadata().getNamespace();
return new PodDisruptionBudgetBuilder()
.withNewMetadata()
.withName(String.format("%s-poddisruptionbudget", resource.getName()))
.withNamespace(namespace)
.withOwnerReferences(
new OwnerReference(
resource.getApiVersion(),
true,
true,
resource.getKind(),
resource.getName(),
resource.getMetadata().getUid()
)
)
.endMetadata()
.withNewSpec()
.withMaxUnavailable(new IntOrString(1))
.withNewSelector()
.withMatchLabels(Collections.singletonMap(Labels.Kubernetes.INSTANCE, resource.getName()))
.endSelector()
.endSpec()
.build();
}
/**
* Creates the PodDisruptionBudget
*
* @return The default PodDisruptionBudget
*/
protected PodDisruptionBudget createPodDisruptionBudget() {
return new PodDisruptionBudgetBuilder()
.withNewMetadata()
.withName(name)
.withLabels(getLabelsWithStrimziName(name, templatePodDisruptionBudgetLabels).toMap())
.withNamespace(namespace)
.withAnnotations(templatePodDisruptionBudgetAnnotations)
.withOwnerReferences(createOwnerReference())
.endMetadata()
.withNewSpec()
.withNewMaxUnavailable(templatePodDisruptionBudgetMaxUnavailable)
.withSelector(new LabelSelectorBuilder().withMatchLabels(getSelectorLabels().toMap()).build())
.endSpec()
.build();
}
@Test
public void testPodDisruptionBudget() {
KafkaBridge resource = new KafkaBridgeBuilder(this.resource)
.editSpec()
.withNewTemplate()
.withNewPodDisruptionBudget()
.withMaxUnavailable(2)
.endPodDisruptionBudget()
.endTemplate()
.endSpec()
.build();
KafkaBridgeCluster kbc = KafkaBridgeCluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = kbc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(2)));
}
@Test
public void testPodDisruptionBudget() {
KafkaMirrorMaker resource = new KafkaMirrorMakerBuilder(this.resource)
.editSpec()
.withNewTemplate()
.withNewPodDisruptionBudget()
.withMaxUnavailable(2)
.endPodDisruptionBudget()
.endTemplate()
.endSpec()
.build();
KafkaMirrorMakerCluster mmc = KafkaMirrorMakerCluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = mmc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(2)));
}
@Test
public void testPodDisruptionBudget() {
KafkaConnect resource = new KafkaConnectBuilder(this.resource)
.editSpec()
.withNewTemplate()
.withNewPodDisruptionBudget()
.withMaxUnavailable(2)
.endPodDisruptionBudget()
.endTemplate()
.endSpec()
.build();
KafkaConnectCluster kc = KafkaConnectCluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = kc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(2)));
}
@Test
public void testPodDisruptionBudget() {
Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
image, healthDelay, healthTimeout, metricsCmJson, configurationJson, emptyMap()))
.editSpec()
.editZookeeper()
.withNewTemplate()
.withNewPodDisruptionBudget()
.withMaxUnavailable(2)
.endPodDisruptionBudget()
.endTemplate()
.endZookeeper()
.endSpec()
.build();
ZookeeperCluster zc = ZookeeperCluster.fromCrd(kafkaAssembly, VERSIONS);
PodDisruptionBudget pdb = zc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(2)));
}
@Test
public void testPodDisruptionBudget() {
KafkaMirrorMaker2 resource = new KafkaMirrorMaker2Builder(this.resource)
.editSpec()
.withNewTemplate()
.withNewPodDisruptionBudget()
.withMaxUnavailable(2)
.endPodDisruptionBudget()
.endTemplate()
.endSpec()
.build();
KafkaMirrorMaker2Cluster kmm2 = KafkaMirrorMaker2Cluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = kmm2.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(2)));
}
@Test
public void testPodDisruptionBudget() {
KafkaConnectS2I resource = new KafkaConnectS2IBuilder(this.resource)
.editSpec()
.withNewTemplate()
.withNewPodDisruptionBudget()
.withMaxUnavailable(2)
.endPodDisruptionBudget()
.endTemplate()
.endSpec()
.build();
KafkaConnectS2ICluster kc = KafkaConnectS2ICluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = kc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(2)));
}
@Test
public void testPodDisruptionBudget() {
Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
.editSpec()
.editKafka()
.withNewTemplate()
.withNewPodDisruptionBudget()
.withMaxUnavailable(2)
.endPodDisruptionBudget()
.endTemplate()
.endKafka()
.endSpec()
.build();
KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);
PodDisruptionBudget pdb = kc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(2)));
}
@Override
protected Future<ReconcileResult<PodDisruptionBudget>> internalPatch(String namespace, String name, PodDisruptionBudget current, PodDisruptionBudget desired, boolean cascading) {
Promise<ReconcileResult<PodDisruptionBudget>> promise = Promise.promise();
internalDelete(namespace, name).onComplete(delRes -> {
if (delRes.succeeded()) {
internalCreate(namespace, name, desired).onComplete(createRes -> {
if (createRes.succeeded()) {
promise.complete(createRes.result());
} else {
promise.fail(createRes.cause());
}
});
} else {
promise.fail(delRes.cause());
}
});
return promise.future();
}
@Test
public void testGet() {
server.expect().withPath("/apis/policy/v1beta1/namespaces/test/poddisruptionbudgets/poddisruptionbudget1").andReturn(200, new PodDisruptionBudgetBuilder().build()).once();
server.expect().withPath("/apis/policy/v1beta1/namespaces/ns1/poddisruptionbudgets/poddisruptionbudget2").andReturn(200, new PodDisruptionBudgetBuilder().build()).once();
KubernetesClient client = server.getClient();
PodDisruptionBudget podDisruptionBudget = client.policy().podDisruptionBudget().withName("poddisruptionbudget1").get();
assertNotNull(podDisruptionBudget);
podDisruptionBudget = client.policy().podDisruptionBudget().withName("poddisruptionbudget2").get();
assertNull(podDisruptionBudget);
podDisruptionBudget = client.policy().podDisruptionBudget().inNamespace("ns1").withName("poddisruptionbudget2").get();
assertNotNull(podDisruptionBudget);
}
private RabbitMQCluster(
final String name,
final String namespace,
final Secret adminSecret,
final Secret erlangCookieSecret,
final Service mainService,
final Service discoveryService,
final Optional<Service> loadBalancerService,
final Optional<Service> nodePortService,
final StatefulSet statefulSet,
final PodDisruptionBudget podDisruptionBudget,
final List<ShovelSpec> shovels,
final List<RabbitMQUser> users,
final List<PolicySpec> policies,
final List<OperatorPolicySpec> operatorPolicies
) {
this.name = name;
this.namespace = namespace;
this.adminSecret = adminSecret;
this.erlangCookieSecret = erlangCookieSecret;
this.mainService = mainService;
this.discoveryService = discoveryService;
this.loadBalancerService = loadBalancerService;
this.nodePortService = nodePortService;
this.statefulSet = statefulSet;
this.podDisruptionBudget = podDisruptionBudget;
this.shovels = shovels;
this.users = users;
this.policies = policies;
this.operatorPolicies = operatorPolicies;
}
@Test
public void testDefaultPodDisruptionBudget() {
KafkaBridge resource = new KafkaBridgeBuilder(this.resource).build();
KafkaBridgeCluster kbc = KafkaBridgeCluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = kbc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(1)));
}
@Test
public void testDefaultPodDisruptionBudget() {
KafkaMirrorMaker resource = new KafkaMirrorMakerBuilder(this.resource).build();
KafkaMirrorMakerCluster mmc = KafkaMirrorMakerCluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = mmc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(1)));
}
@Test
public void testDefaultPodDisruptionBudget() {
KafkaConnect resource = new KafkaConnectBuilder(this.resource).build();
KafkaConnectCluster kc = KafkaConnectCluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = kc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(1)));
}
@Test
public void testPodDisruptionBudget() {
int maxUnavailable = 2;
CruiseControlSpec cruiseControlSpec = new CruiseControlSpecBuilder()
.withImage(ccImage)
.withNewTemplate()
.withNewPodDisruptionBudget()
.withMaxUnavailable(maxUnavailable)
.endPodDisruptionBudget()
.endTemplate()
.build();
Kafka resource =
new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas, image, healthDelay, healthTimeout))
.editSpec()
.editKafka()
.withVersion(version)
.endKafka()
.withCruiseControl(cruiseControlSpec)
.endSpec()
.build();
CruiseControl cc = CruiseControl.fromCrd(resource, VERSIONS);
Deployment dep = cc.generateDeployment(true, null, null, null);
List<Container> containers = dep.getSpec().getTemplate().getSpec().getContainers();
Container ccContainer = containers.stream().filter(container -> ccImage.equals(container.getImage())).findFirst().get();
PodDisruptionBudget pdb = cc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(maxUnavailable)));
}
@Test
public void testDefaultPodDisruptionBudget() {
Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
image, healthDelay, healthTimeout, metricsCmJson, configurationJson, emptyMap()))
.build();
ZookeeperCluster zc = ZookeeperCluster.fromCrd(kafkaAssembly, VERSIONS);
PodDisruptionBudget pdb = zc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(1)));
}
@Test
public void testDefaultPodDisruptionBudget() {
KafkaMirrorMaker2 resource = new KafkaMirrorMaker2Builder(this.resource).build();
KafkaMirrorMaker2Cluster kmm2 = KafkaMirrorMaker2Cluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = kmm2.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(1)));
}
@Test
public void testDefaultPodDisruptionBudget() {
KafkaConnectS2I resource = new KafkaConnectS2IBuilder(this.resource).build();
KafkaConnectS2ICluster kc = KafkaConnectS2ICluster.fromCrd(resource, VERSIONS);
PodDisruptionBudget pdb = kc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(1)));
}
@Test
public void testDefaultPodDisruptionBudget() {
Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
.build();
KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);
PodDisruptionBudget pdb = kc.generatePodDisruptionBudget();
assertThat(pdb.getSpec().getMaxUnavailable(), is(new IntOrString(1)));
}
@Override
protected PodDisruptionBudget resource() {
return new PodDisruptionBudgetBuilder()
.withNewMetadata()
.withName(RESOURCE_NAME)
.withNamespace(NAMESPACE)
.withLabels(singletonMap("foo", "bar"))
.endMetadata()
.withNewSpec()
.withNewMaxUnavailable(1)
.endSpec()
.build();
}
@Override
public void createWhenExistsIsAPatch(VertxTestContext context, boolean cascade) {
PodDisruptionBudget resource = resource();
Resource mockResource = mock(resourceType());
when(mockResource.get()).thenReturn(resource);
when(mockResource.create(any())).thenReturn(resource);
Deletable mockDeletable = mock(Deletable.class);
EditReplacePatchDeletable mockERPD = mock(EditReplacePatchDeletable.class);
when(mockERPD.withGracePeriod(anyLong())).thenReturn(mockDeletable);
when(mockResource.cascading(cascade)).thenReturn(mockERPD);
NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
when(mockNameable.withName(matches(resource.getMetadata().getName()))).thenReturn(mockResource);
MixedOperation mockCms = mock(MixedOperation.class);
when(mockCms.inNamespace(matches(resource.getMetadata().getNamespace()))).thenReturn(mockNameable);
KubernetesClient mockClient = mock(clientType());
mocker(mockClient, mockCms);
AbstractResourceOperator<KubernetesClient, PodDisruptionBudget, PodDisruptionBudgetList, DoneablePodDisruptionBudget, Resource<PodDisruptionBudget, DoneablePodDisruptionBudget>> op = createResourceOperations(vertx, mockClient);
Checkpoint async = context.checkpoint();
Future<ReconcileResult<PodDisruptionBudget>> fut = op.createOrUpdate(resource());
fut.onComplete(ar -> {
if (!ar.succeeded()) {
ar.cause().printStackTrace();
}
assertThat(ar.succeeded(), is(true));
verify(mockResource).get();
verify(mockDeletable).delete();
verify(mockResource).create(any());
verify(mockResource, never()).patch(any());
verify(mockResource, never()).createNew();
verify(mockResource, never()).createOrReplace(any());
async.flag();
});
}
@Test
public void testDeleteWithNamespaceMismatch() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
PodDisruptionBudget podDisruptionBudget1 = new PodDisruptionBudgetBuilder().withNewMetadata().withName("podDisruptionBudget1").withNamespace("test").and().build();
KubernetesClient client = server.getClient();
Boolean deleted = client.policy().podDisruptionBudget().inNamespace("test1").delete(podDisruptionBudget1);
assertFalse(deleted);
});
}
@Test
public void testCreateWithNameMismatch() {
Assertions.assertThrows(KubernetesClientException.class, () -> {
PodDisruptionBudget podDisruptionBudget1 = new PodDisruptionBudgetBuilder().withNewMetadata().withName("podDisruptionBudget1").withNamespace("test").and().build();
KubernetesClient client = server.getClient();
client.policy().podDisruptionBudget().inNamespace("test1").withName("mypodDisruptionBudget1").create(podDisruptionBudget1);
});
}
public static void main(String args[]) throws InterruptedException {
String master = "https://192.168.99.100:8443/";
if (args.length == 1) {
master = args[0];
}
log("Using master with url ", master);
Config config = new ConfigBuilder().withMasterUrl(master).build();
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
final String namespace = "default";
PodDisruptionBudget podDisruptionBudget = new PodDisruptionBudgetBuilder()
.withNewMetadata().withName("zk-pkb").endMetadata()
.withNewSpec()
.withMaxUnavailable(new IntOrString("1%"))
.withNewSelector()
.withMatchLabels(Collections.singletonMap("app", "zookeeper"))
.endSelector()
.endSpec()
.build();
log("Current namespace is", namespace);
client.policy().podDisruptionBudget().inNamespace(namespace).create(podDisruptionBudget);
} catch (KubernetesClientException e) {
log("Could not create resource", e.getMessage());
}
}
public RabbitMQCluster fromCustomResource(final RabbitMQCustomResource resource) throws RabbitClusterConfigurationException {
final String clusterName = resource.getName();
final String namespace = resource.getMetadata().getNamespace();
final RabbitMQCustomResourceSpec spec = resource.getSpec();
final List<String> errors = clusterValidators.stream()
.map(validator -> validator.validate(spec.getClusterSpec()))
.flatMap(List::stream)
.collect(Collectors.toList());
if (!errors.isEmpty()) {
throw new RabbitClusterConfigurationException(errors);
}
final Secret adminSecret = getOrGenerateAdminSecret(resource);
final Secret erlangCookieSecret = getOrGenerateErlangSecret(resource);
final Service mainService = rabbitMQServices.buildService(namespace, resource);
final Service discoveryService = rabbitMQServices.buildDiscoveryService(namespace, resource);
final Optional<Service> loadBalancerService;
if (spec.isCreateLoadBalancer()) {
loadBalancerService = Optional.of(rabbitMQServices.buildLoadBalancerService(namespace, resource));
} else {
loadBalancerService = Optional.empty();
}
final Optional<Service> nodePortService;
if (spec.isCreateNodePort()) {
nodePortService = Optional.of(rabbitMQServices.buildNodePortService(namespace, resource));
} else {
nodePortService = Optional.empty();
}
final Container container = rabbitMQContainers.buildContainer(
namespace,
clusterName,
spec.getRabbitMQImage(),
spec.getComputeResources(),
spec.getClusterSpec().getHighWatermarkFraction());
final StatefulSet statefulSet = buildStatefulSet(resource, container);
final PodDisruptionBudget podDisruptionBudget = buildPodDisruptionBudget(resource);
final List<RabbitMQUser> users = buildUsers(resource);
return RabbitMQCluster.newBuilder()
.withName(clusterName)
.withNamespace(namespace)
.withAdminSecret(adminSecret)
.withErlangCookieSecret(erlangCookieSecret)
.withMainService(mainService)
.withDiscoveryService(discoveryService)
.withLoadBalancerService(loadBalancerService)
.withNodePortService(nodePortService)
.withStatefulSet(statefulSet)
.withPodDisruptionBudget(podDisruptionBudget)
.withShovels(resource.getSpec().getClusterSpec().getShovels())
.withUsers(users)
.withPolicies(spec.getClusterSpec().getPolicies())
.withOperatorPolicies(spec.getClusterSpec().getOperatorPolicies())
.build();
}
public PodDisruptionBudgetController(
final KubernetesClient client,
final Map<String, String> labelsToWatch
) {
super(client, labelsToWatch, PodDisruptionBudget.class);
}
@Override
protected MixedOperation<PodDisruptionBudget, PodDisruptionBudgetList, DoneablePodDisruptionBudget, Resource<PodDisruptionBudget, DoneablePodDisruptionBudget>> operation() {
return getClient().policy().podDisruptionBudget();
}
public PodDisruptionBudget getPodDisruptionBudget() {
return podDisruptionBudget;
}
public Builder withPodDisruptionBudget(final PodDisruptionBudget podDisruptionBudget) {
this.podDisruptionBudget = podDisruptionBudget;
return this;
}