diff --git a/CHANGELOG.md b/CHANGELOG.md index af3b8f47125..581082fd2a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,9 @@ And Store.getKey can be used rather than directly referencing static Cache funct * Fix #3407: Added Itemable.withItem to directly associate a resource with the DSL. It can be used as an alternative to Loadable.load when you already have the item. There is also client.resourceList(...).getResources() - that will provide the resource list as Resources. This allows you to implement composite operations easily with lambda: client.resourceList(...).getResources().forEach(r -> r.delete()); * Fix #3922: added Client.supports and Client.hasApiGroup methods * KubernetesMockServer has new methods - unsupported and reset - to control what apis are unsupported and to reset its state. +* Fix #3407 #3973: Added Resourceable.resource to directly associate a resource with the DSL. It can be used as an alternative to Loadable.load when you already have the item. +There is also client.resourceList(...).resources() and client.configMaps().resources() - that will provide a Resource stream. +This allows you to implement composite operations easily with lambda: client.secrets().resources().forEach(r -> r.delete()); #### _**Note**_: Breaking changes in the API Please see the [migration guide](doc/MIGRATION-v6.md) diff --git a/doc/MIGRATION-v6.md b/doc/MIGRATION-v6.md index 46a9bc57c26..ebb1752d938 100644 --- a/doc/MIGRATION-v6.md +++ b/doc/MIGRATION-v6.md @@ -13,7 +13,7 @@ - [Adapt Changes](#adapt-changes) - [Deprecations](#deprecations) - [Object sorting](#object-sorting) -- [Boolean Changes](#boolean-changes) +- [DSL Interface Changes](#dsl-interface-changes) - [evict Changes](#evict-changes) ## Namespace Changes @@ -192,10 +192,15 @@ Client.adapt will no longer perform the isAdaptable check - that is you may free KubernetesList and Template will no longer automatically sort their objects by default. You may use the HasMetadataComparator to sort the items as needed. -## Boolean Changes +## DSL Interface Changes -The usage of Boolean in the api was removed where it was not a nullable value. Please expect a boolean primitive from methods such as delete, copy, or as an argument in Loggable.getLog +- The usage of Boolean in the api was removed where it was not a nullable value. Please expect a boolean primitive from methods such as delete, copy, or as an argument in Loggable.getLog + +- WatchListDeletable now takes three type parameters to include the Resource type. + +- PodResource is no longer generic. ## Evict Changes Evictable.evict will throw an exception rather than returning false if the pod is not found. This ensures that false strictly means that the evict failed. + diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceBrokerOperationsImpl.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceBrokerOperationsImpl.java index a057d2e1050..13ffc098b83 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceBrokerOperationsImpl.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceBrokerOperationsImpl.java @@ -69,7 +69,7 @@ public ClusterServiceClassResource useServiceClass(String externalName) { + "found for ClusterServiceBroker: " + item.getMetadata().getName()); } ClusterServiceClass c = list.get(0); - return client.adapt(ServiceCatalogClient.class).clusterServiceClasses().withItem(c); + return client.adapt(ServiceCatalogClient.class).clusterServiceClasses().resource(c); } } diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceClassOperationsImpl.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceClassOperationsImpl.java index 6982157999f..b5297ed3a92 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceClassOperationsImpl.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServiceClassOperationsImpl.java @@ -65,7 +65,7 @@ public ClusterServicePlanResource usePlan(String externalName) { + " and ClusterServiceClass: " + item.getSpec().getExternalName() + "."); } ClusterServicePlan plan = list.get(0); - return client.adapt(ServiceCatalogClient.class).clusterServicePlans().withItem(plan); + return client.adapt(ServiceCatalogClient.class).clusterServicePlans().resource(plan); } @Override diff --git a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServicePlanOperationsImpl.java b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServicePlanOperationsImpl.java index 5f948e5aa7d..48481bcb171 100644 --- a/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServicePlanOperationsImpl.java +++ b/extensions/service-catalog/client/src/main/java/io/fabric8/servicecatalog/client/internal/ClusterServicePlanOperationsImpl.java @@ -65,7 +65,7 @@ public ServiceInstance instantiate(String... args) { @Override public ServiceInstanceResource instantiateAnd(String... args) { ServiceInstance item = instantiate(args); - return client.adapt(ServiceCatalogClient.class).serviceInstances().withItem(item); + return client.adapt(ServiceCatalogClient.class).serviceInstances().resource(item); } } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java index 791fb4d3c59..57e0e5869db 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java @@ -130,13 +130,13 @@ public interface KubernetesClient extends Client { * specific to CustomResource. * *

- * Note: your CustomResource POJO (T in this context) must implement - * {@link io.fabric8.kubernetes.api.model.Namespaced} if it is a namespace-scoped resource. + * Note: your CustomResource POJO (T in this context) must implement + * {@link io.fabric8.kubernetes.api.model.Namespaced} if it is a namespace-scoped resource. *

* * @param resourceType Class for CustomResource * @param T type represents CustomResource type. If it's a namespaced resource, it must implement - * {@link io.fabric8.kubernetes.api.model.Namespaced} + * {@link io.fabric8.kubernetes.api.model.Namespaced} * @return returns a MixedOperation object with which you can do basic CustomResource operations * @deprecated use {@link #resources(Class)} instead */ @@ -147,14 +147,15 @@ public interface KubernetesClient extends Client { * Typed API for managing resources. Any properly annotated POJO can be utilized as a resource. * *

- * Note: your resource POJO (T in this context) must implement - * {@link io.fabric8.kubernetes.api.model.Namespaced} if it is a namespace-scoped resource. + * Note: your resource POJO (T in this context) must implement + * {@link io.fabric8.kubernetes.api.model.Namespaced} if it is a namespace-scoped resource. *

* * @param resourceType Class for resource * @param T type represents resource type. If it's a namespaced resource, it must implement - * {@link io.fabric8.kubernetes.api.model.Namespaced} - * @return returns a MixedOperation object with which you can do basic resource operations. If the class is a known type the dsl operation logic will be used. + * {@link io.fabric8.kubernetes.api.model.Namespaced} + * @return returns a MixedOperation object with which you can do basic resource operations. If the class is a known type the + * dsl operation logic will be used. */ default MixedOperation, Resource> resources(Class resourceType) { return resources(resourceType, null); @@ -166,20 +167,21 @@ default MixedOperation, Res * specific to CustomResource. * *

- * Note: your CustomResource POJO (T in this context) must implement - * {@link io.fabric8.kubernetes.api.model.Namespaced} if it is a namespace-scoped resource. + * Note: your CustomResource POJO (T in this context) must implement + * {@link io.fabric8.kubernetes.api.model.Namespaced} if it is a namespace-scoped resource. *

* * @param resourceType Class for CustomResource * @param listClass Class for list object for CustomResource * @param T type represents CustomResource type. If it's a namespace-scoped resource, it must implement - * {@link io.fabric8.kubernetes.api.model.Namespaced} + * {@link io.fabric8.kubernetes.api.model.Namespaced} * @param L type represents CustomResourceList type * @return returns a MixedOperation object with which you can do basic CustomResource operations * @deprecated use {@link #resources(Class, Class)} instead */ @Deprecated - > MixedOperation> customResources(Class resourceType, Class listClass); + > MixedOperation> customResources( + Class resourceType, Class listClass); /** * Typed API for managing CustomResources. You would need to provide POJOs for @@ -187,10 +189,11 @@ default MixedOperation, Res * specific to CustomResource. * *

- * Note: your CustomResource POJO (T in this context) must implement - * - * io.fabric8.kubernetes.api.model.Namespaced - * if it is a Namespaced scoped resource. + * Note: your CustomResource POJO (T in this context) must implement + * + * io.fabric8.kubernetes.api.model.Namespaced + * if it is a Namespaced scoped resource. *

* * @deprecated Since 5.x versions of client {@link CustomResourceDefinitionContext} is now configured via annotations @@ -199,12 +202,13 @@ default MixedOperation, Res * @param resourceType Class for CustomResource * @param listClass Class for list object for CustomResource * @param T type represents CustomResource type. If it's Namespaced resource, it must implement - * io.fabric8.kubernetes.api.model.Namespaced + * io.fabric8.kubernetes.api.model.Namespaced * @param L type represents CustomResourceList type * @return returns a MixedOperation object with which you can do basic CustomResource operations */ @Deprecated - > MixedOperation> customResources(ResourceDefinitionContext context, Class resourceType, Class listClass); + > MixedOperation> customResources( + ResourceDefinitionContext context, Class resourceType, Class listClass); /** * Semi-Typed API for managing {@link GenericKubernetesResource}s which can represent any resource. @@ -226,7 +230,8 @@ default MixedOperation> genericKubernetesResources(String apiVersion, String kind); + MixedOperation> genericKubernetesResources( + String apiVersion, String kind); /** * Discovery API entrypoint for APIGroup discovery.k8s.io @@ -389,7 +394,8 @@ default MixedOperation resourceList(Collection items); + NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable resourceList( + Collection items); /** * KubernetesResource operations. You can pass any Kubernetes resource as a HasMetadata object and do @@ -429,7 +435,7 @@ default MixedOperation> namespaces(); + NonNamespaceOperation> namespaces(); /** * API entrypoint for node related operations in Kubernetes. Node (core/v1) @@ -457,7 +463,7 @@ default MixedOperation> pods(); + MixedOperation pods(); /** * API entrypoint for ReplicationController related operations. ReplicationController (core/v1) diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListDeletable.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListDeletable.java index 5a94ac28897..94276db2046 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListDeletable.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListDeletable.java @@ -15,12 +15,14 @@ */ package io.fabric8.kubernetes.client.dsl; -public interface FilterWatchListDeletable extends Filterable>, WatchListDeletable { - +public interface FilterWatchListDeletable + extends Filterable>, WatchListDeletable { + /** * Accumulate a filter on the context, when done {@link FilterNested#endFilter()} or and must be called + * * @return a {@link FilterNested} */ - FilterNested> withNewFilter(); + FilterNested> withNewFilter(); } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListMultiDeletable.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListMultiDeletable.java index 4d29967d8cb..4a9012c249d 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListMultiDeletable.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListMultiDeletable.java @@ -15,6 +15,6 @@ */ package io.fabric8.kubernetes.client.dsl; -public interface FilterWatchListMultiDeletable extends FilterWatchListDeletable, MultiDeleteable { +public interface FilterWatchListMultiDeletable extends FilterWatchListDeletable, MultiDeleteable { } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ListVisitFromServerGetDeleteRecreateWaitApplicable.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ListVisitFromServerGetDeleteRecreateWaitApplicable.java index bcade2b8391..25527416b37 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ListVisitFromServerGetDeleteRecreateWaitApplicable.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/ListVisitFromServerGetDeleteRecreateWaitApplicable.java @@ -20,18 +20,20 @@ import io.fabric8.kubernetes.client.FromServerGettable; import java.util.List; +import java.util.stream.Stream; -public interface ListVisitFromServerGetDeleteRecreateWaitApplicable extends Visitable>, - FromServerGettable>, - Waitable, T>, - ListVisitFromServerWritable, - DryRunable> { - +public interface ListVisitFromServerGetDeleteRecreateWaitApplicable + extends Visitable>, + FromServerGettable>, + Waitable, T>, + ListVisitFromServerWritable, + DryRunable> { /** * Get each item as as a {@link Resource} - * @return the resources + * + * @return the {@link Resource} steam */ - List> getResources(); + Stream> resources(); } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java index 62b54c57545..a88f4e42c9d 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java @@ -16,14 +16,13 @@ package io.fabric8.kubernetes.client.dsl; -import java.util.List; +import java.util.stream.Stream; public interface NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable extends - ListVisitFromServerGetDeleteRecreateWaitApplicable, - Namespaceable> -{ + ListVisitFromServerGetDeleteRecreateWaitApplicable, + Namespaceable> { @Override - List> getResources(); + Stream> resources(); } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NonNamespaceOperation.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NonNamespaceOperation.java index 7222852745a..eb4cdb66bde 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NonNamespaceOperation.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NonNamespaceOperation.java @@ -18,19 +18,19 @@ /** * The entry point to client operations that are either "cross namespace resources", or are available in the invocation chain * after a namespace has already been specified. - * + * * @param The Kubernetes resource type. * @param The list variant of the Kubernetes resource type. * @param The resource operations. */ public interface NonNamespaceOperation extends Nameable, - FilterWatchListMultiDeletable, + FilterWatchListMultiDeletable, Createable, CreateOrReplaceable, DryRunable>, Replaceable, StatusReplaceable, Loadable, - Itemable { + Resourceable { } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Operation.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Operation.java index 69e80257076..ceb4b9823d6 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Operation.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Operation.java @@ -17,16 +17,16 @@ /** * The entry point to client operations. - * + * * @param The Kubernetes resource type. * @param The list variant of the Kubernetes resource type. * @param The resource operations. */ public interface Operation extends - AnyNamespaceable>, + AnyNamespaceable>, Namespaceable>, - FilterWatchListMultiDeletable, + FilterWatchListMultiDeletable, Loadable, - Itemable { + Resourceable { } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/PodResource.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/PodResource.java index 42b01e374bd..c10a9233863 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/PodResource.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/PodResource.java @@ -15,6 +15,7 @@ */ package io.fabric8.kubernetes.client.dsl; +import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.client.LocalPortForward; import io.fabric8.kubernetes.client.PortForward; @@ -25,10 +26,10 @@ import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; -public interface PodResource extends Resource, - Loggable, - Containerable>, - ContainerResource, - PortForwardable, - Evictable{ +public interface PodResource extends Resource, + Loggable, + Containerable>, + ContainerResource, + PortForwardable, + Evictable { } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Itemable.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Resourceable.java similarity index 91% rename from kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Itemable.java rename to kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Resourceable.java index e52e30eb675..4e7f77c2e5f 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Itemable.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Resourceable.java @@ -15,8 +15,8 @@ */ package io.fabric8.kubernetes.client.dsl; -public interface Itemable { +public interface Resourceable { - R withItem(T item); + R resource(T item); } diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/WatchListDeletable.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/WatchListDeletable.java index aef6b9181d5..d4c852c03fe 100644 --- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/WatchListDeletable.java +++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/WatchListDeletable.java @@ -19,10 +19,20 @@ import io.fabric8.kubernetes.client.PropagationPolicyConfigurable; import io.fabric8.kubernetes.client.Watcher; -public interface WatchListDeletable extends Watchable>, Versionable>, Listable, Deletable, - GracePeriodConfigurable, - PropagationPolicyConfigurable>, - StatusUpdatable, - Informable -{ +import java.util.stream.Stream; + +public interface WatchListDeletable + extends Watchable>, Versionable>, Listable, Deletable, + GracePeriodConfigurable, + PropagationPolicyConfigurable>, + StatusUpdatable, + Informable { + + /** + * Perform a list operation and return the items as a stream of {@link Resource}s + * + * @return the {@link Resource} steam + */ + Stream resources(); + } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/DefaultKubernetesClient.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/DefaultKubernetesClient.java index bc0b16dfe64..7a2f8cecb19 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/DefaultKubernetesClient.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/DefaultKubernetesClient.java @@ -135,7 +135,7 @@ /** * Class for Default Kubernetes Client implementing KubernetesClient interface. * It is thread safe. - * + * * @deprecated direct usage should no longer be needed. Please use the {@link KubernetesClientBuilder} instead. */ @Deprecated @@ -381,7 +381,7 @@ public MixedOperation> pods() { + public MixedOperation pods() { return new PodOperationsImpl(this); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/NamespaceableResourceAdapter.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/NamespaceableResourceAdapter.java index 07ecbf7b89b..d6bb2bc3518 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/NamespaceableResourceAdapter.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/NamespaceableResourceAdapter.java @@ -29,27 +29,30 @@ * {@link NamespaceableResource} is not {@link Namespaceable} to allow for future versions of the logic * to be consolidated. *

- * With generic typed interface design Namespacable<X> and Namespaceable<Y> are considered incompatible no matter the types for X and Y, - * but NamespaceableX and NamespaceableY can be polymorphic. As long as there is no usage expectation + * With generic typed interface design Namespacable<X> and Namespaceable<Y> are considered incompatible no matter + * the types for X and Y, + * but NamespaceableX and NamespaceableY can be polymorphic. As long as there is no usage expectation * like instanceof Namespaceable - then it would be fine to have the non-generic versions. *

- * The constructor and the inNamespace method determine the namespacing rules: - * - if the item is namespaced, the resource context will use that namespace as that is not currently the default behavior of withItem + * The constructor and the inNamespace method determine the namespacing rules: + * - if the item is namespaced, the resource context will use that namespace as that is not currently the default behavior of + * withItem */ -public class NamespaceableResourceAdapter extends ResourceAdapter implements NamespaceableResource { - +public class NamespaceableResourceAdapter extends ResourceAdapter + implements NamespaceableResource { + protected final T item; protected final HasMetadataOperation operation; - + public NamespaceableResourceAdapter(T item, HasMetadataOperation op) { - super(op.withItem(item)); + super(op.resource(item)); this.operation = op; this.item = item; } @Override public Resource inNamespace(String name) { - return operation.inNamespace(name).withItem(item); + return operation.inNamespace(name).resource(item); } - + } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperation.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperation.java index 117d53d1b5a..cca0ddfbf52 100755 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperation.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperation.java @@ -73,6 +73,7 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.UnaryOperator; +import java.util.stream.Stream; public class BaseOperation, R extends Resource> extends CreateOnlyResourceOperation @@ -257,7 +258,7 @@ public ExtensibleResource cascading(boolean cascading) { @Override public R load(InputStream is) { - return withItem(unmarshal(is, type)); + return resource(unmarshal(is, type)); } @Override @@ -297,7 +298,7 @@ public final T createOrReplace(T... items) { throw new IllegalArgumentException("Too many items to create."); } else if (items.length == 1) { itemToCreateOrReplace = items[0]; - resource = withItem(itemToCreateOrReplace); + resource = resource(itemToCreateOrReplace); } else { itemToCreateOrReplace = getItem(); resource = this; @@ -317,57 +318,57 @@ public final T createOrReplace(T... items) { } @Override - public FilterWatchListDeletable withLabels(Map labels) { + public FilterWatchListDeletable withLabels(Map labels) { return withNewFilter().withLabels(labels).endFilter(); } @Override - public FilterWatchListDeletable withLabelSelector(LabelSelector selector) { + public FilterWatchListDeletable withLabelSelector(LabelSelector selector) { return withNewFilter().withLabelSelector(selector).endFilter(); } @Override - public FilterWatchListDeletable withoutLabels(Map labels) { + public FilterWatchListDeletable withoutLabels(Map labels) { return withNewFilter().withoutLabels(labels).endFilter(); } @Override - public FilterWatchListDeletable withLabelIn(String key, String... values) { + public FilterWatchListDeletable withLabelIn(String key, String... values) { return withNewFilter().withLabelIn(key, values).endFilter(); } @Override - public FilterWatchListDeletable withLabelNotIn(String key, String... values) { + public FilterWatchListDeletable withLabelNotIn(String key, String... values) { return withNewFilter().withLabelNotIn(key, values).endFilter(); } @Override - public FilterWatchListDeletable withLabel(String key, String value) { + public FilterWatchListDeletable withLabel(String key, String value) { return withNewFilter().withLabel(key, value).endFilter(); } @Override - public FilterWatchListDeletable withoutLabel(String key, String value) { + public FilterWatchListDeletable withoutLabel(String key, String value) { return withNewFilter().withoutLabel(key, value).endFilter(); } @Override - public FilterWatchListDeletable withLabelSelector(String selectorAsString) { + public FilterWatchListDeletable withLabelSelector(String selectorAsString) { return withNewFilter().withLabelSelector(selectorAsString).endFilter(); } @Override - public FilterWatchListDeletable withFields(Map fields) { + public FilterWatchListDeletable withFields(Map fields) { return withNewFilter().withFields(fields).endFilter(); } @Override - public FilterWatchListDeletable withField(String key, String value) { + public FilterWatchListDeletable withField(String key, String value) { return withNewFilter().withField(key, value).endFilter(); } @Override - public FilterWatchListDeletable withInvolvedObject(ObjectReference objectReference) { + public FilterWatchListDeletable withInvolvedObject(ObjectReference objectReference) { if (objectReference != null) { return withNewFilter().withInvolvedObject(objectReference).endFilter(); } @@ -375,17 +376,17 @@ public FilterWatchListDeletable withInvolvedObject(ObjectReference objectR } @Override - public FilterNested> withNewFilter() { + public FilterNested> withNewFilter() { return new FilterNestedImpl<>(this); } @Override - public FilterWatchListDeletable withoutFields(Map fields) { + public FilterWatchListDeletable withoutFields(Map fields) { return withNewFilter().withoutFields(fields).endFilter(); } @Override - public FilterWatchListDeletable withoutField(String key, String value) { + public FilterWatchListDeletable withoutField(String key, String value) { return withNewFilter().withoutField(key, value).endFilter(); } @@ -478,7 +479,7 @@ public boolean delete(List items) { updateApiVersion(toDelete); try { - deleted &= withItem(toDelete).delete(); + deleted &= resource(toDelete).delete(); } catch (KubernetesClientException e) { if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) { throw e; @@ -509,7 +510,7 @@ public T patchStatus(T item) { } @Override - public R withItem(T item) { + public R resource(T item) { // set the name, namespace, and item - not all operations are looking at the item for the name // things like configMaps().load(...).watch(...) for example item = correctNamespace(item); @@ -1011,4 +1012,9 @@ public static URL appendListOptionParams(URL base, ListOptions listOptions) { return urlBuilder.build(); } + @Override + public Stream resources() { + return list().getItems().stream().map(this::resource); + } + } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/CreateOnlyResourceOperation.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/CreateOnlyResourceOperation.java index d8a5c834ee7..746b53227aa 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/CreateOnlyResourceOperation.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/CreateOnlyResourceOperation.java @@ -23,7 +23,7 @@ public class CreateOnlyResourceOperation extends OperationSupport implements InOutCreateable { protected Class type; - + protected CreateOnlyResourceOperation(OperationContext ctx) { super(ctx); } @@ -31,7 +31,7 @@ protected CreateOnlyResourceOperation(OperationContext ctx) { public Class getType() { return type; } - + protected O handleCreate(I resource) throws ExecutionException, InterruptedException, IOException { return handleCreate(resource, getType()); } @@ -39,19 +39,12 @@ protected O handleCreate(I resource) throws ExecutionException, InterruptedExcep @SafeVarargs @Override public final O create(I... resources) { - try { - if (resources.length > 1) { - throw new IllegalArgumentException("Too many items to create."); - } else if (resources.length == 1) { - return handleCreate(resources[0]); - } else { - return handleCreate(getItem()); - } - } catch (ExecutionException | IOException e) { - throw KubernetesClientException.launderThrowable(e); - } catch (InterruptedException ie) { - Thread.currentThread().interrupt(); - throw KubernetesClientException.launderThrowable(ie); + if (resources.length > 1) { + throw new IllegalArgumentException("Too many items to create."); + } else if (resources.length == 1) { + return create(resources[0]); + } else { + return create(getItem()); } } @@ -70,5 +63,5 @@ public O create(I item) { public I getItem() { return (I) context.getItem(); } - + } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/FilterNestedImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/FilterNestedImpl.java index 7f9e1c8376e..61ca7c9bbf6 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/FilterNestedImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/FilterNestedImpl.java @@ -31,8 +31,9 @@ import java.util.List; import java.util.Map; -final class FilterNestedImpl, R extends Resource> implements FilterNested> { - +final class FilterNestedImpl, R extends Resource> + implements FilterNested> { + private static final String INVOLVED_OBJECT_NAME = "involvedObject.name"; private static final String INVOLVED_OBJECT_NAMESPACE = "involvedObject.namespace"; private static final String INVOLVED_OBJECT_KIND = "involvedObject.kind"; @@ -40,7 +41,7 @@ final class FilterNestedImpl baseOperation; private OperationContext context; @@ -61,39 +62,39 @@ final class FilterNestedImpl> withLabels(Map labels) { + public FilterNested> withLabels(Map labels) { this.context.labels.putAll(labels); return this; } @Override - public FilterNested> withoutLabels(Map labels) { + public FilterNested> withoutLabels(Map labels) { // Re-use "withoutLabel" to convert values from String to String[] labels.forEach(this::withoutLabel); return this; } @Override - public FilterNested> withLabelIn(String key, String... values) { + public FilterNested> withLabelIn(String key, String... values) { context.labelsIn.put(key, values); return this; } @Override - public FilterNested> withLabelNotIn(String key, String... values) { + public FilterNested> withLabelNotIn(String key, String... values) { context.labelsNotIn.put(key, values); return this; } @Override - public FilterNested> withLabel(String key, String value) { + public FilterNested> withLabel(String key, String value) { context.labels.put(key, value); return this; } @Override - public FilterNested> withoutLabel(String key, String value) { - context.labelsNot.merge(key, new String[]{value}, (oldList, newList) -> { + public FilterNested> withoutLabel(String key, String value) { + context.labelsNot.merge(key, new String[] { value }, (oldList, newList) -> { final String[] concatList = (String[]) Array.newInstance(String.class, oldList.length + newList.length); System.arraycopy(oldList, 0, concatList, 0, oldList.length); System.arraycopy(newList, 0, concatList, oldList.length, newList.length); @@ -103,27 +104,27 @@ public FilterNested> withoutLabel(String key, Str } @Override - public FilterNested> withFields(Map fields) { + public FilterNested> withFields(Map fields) { this.context.fields.putAll(fields); return this; } @Override - public FilterNested> withField(String key, String value) { + public FilterNested> withField(String key, String value) { this.context.fields.put(key, value); return this; } @Override - public FilterNested> withoutFields(Map fields) { + public FilterNested> withoutFields(Map fields) { // Re-use "withoutField" to convert values from String to String[] fields.forEach(this::withoutField); return this; } @Override - public FilterNested> withoutField(String key, String value) { - context.fieldsNot.merge(key, new String[]{value}, (oldList, newList) -> { + public FilterNested> withoutField(String key, String value) { + context.fieldsNot.merge(key, new String[] { value }, (oldList, newList) -> { if (Utils.isNotNullOrEmpty(newList[0])) { // Only add new values when not null final String[] concatList = (String[]) Array.newInstance(String.class, oldList.length + newList.length); System.arraycopy(oldList, 0, concatList, 0, oldList.length); @@ -137,7 +138,7 @@ public FilterNested> withoutField(String key, Str } @Override - public FilterNested> withLabelSelector(LabelSelector selector) { + public FilterNested> withLabelSelector(LabelSelector selector) { Map matchLabels = selector.getMatchLabels(); if (matchLabels != null) { withLabels(matchLabels); @@ -149,10 +150,10 @@ public FilterNested> withLabelSelector(LabelSelec String key = req.getKey(); switch (operator) { case "In": - withLabelIn(key, req.getValues().toArray(new String[]{})); + withLabelIn(key, req.getValues().toArray(new String[] {})); break; case "NotIn": - withLabelNotIn(key, req.getValues().toArray(new String[]{})); + withLabelNotIn(key, req.getValues().toArray(new String[] {})); break; case "DoesNotExist": withoutLabel(key); @@ -169,7 +170,7 @@ public FilterNested> withLabelSelector(LabelSelec } @Override - public FilterNested> withInvolvedObject(ObjectReference objectReference) { + public FilterNested> withInvolvedObject(ObjectReference objectReference) { if (objectReference.getName() != null) { context.fields.put(INVOLVED_OBJECT_NAME, objectReference.getName()); } @@ -195,13 +196,12 @@ public FilterNested> withInvolvedObject(ObjectRef } @Override - public FilterWatchListDeletable and() { + public FilterWatchListDeletable and() { return this.baseOperation.newInstance(context); } - @Override - public FilterNested> withLabelSelector(String selectorAsString) { + public FilterNested> withLabelSelector(String selectorAsString) { this.context.selectorAsString = selectorAsString; return this; } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java index 8be8bf1f81a..3df16f551d0 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl.java @@ -63,6 +63,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import java.util.stream.Collectors; +import java.util.stream.Stream; public class NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl implements ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable, @@ -120,10 +121,13 @@ List getItems() { } @Override - public List> getResources() { + public Stream> resources() { return getItems().stream() - .map(this::getResource) - .collect(Collectors.toList()); + .map(this::getResource); + } + + public List> getResources() { + return resources().collect(Collectors.toList()); } /** diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/DeploymentRollingUpdater.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/DeploymentRollingUpdater.java index 235c56cab68..17cc9b33d9a 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/DeploymentRollingUpdater.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/DeploymentRollingUpdater.java @@ -22,6 +22,7 @@ import io.fabric8.kubernetes.api.model.apps.DeploymentList; import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.dsl.Operation; +import io.fabric8.kubernetes.client.dsl.PodResource; import io.fabric8.kubernetes.client.dsl.RollableScalableResource; import io.fabric8.kubernetes.client.dsl.WatchListDeletable; @@ -51,7 +52,7 @@ protected Deployment createClone(Deployment obj, String newName, String newDeplo } @Override - protected WatchListDeletable selectedPodLister(Deployment obj) { + protected WatchListDeletable selectedPodLister(Deployment obj) { return selectedPodLister(obj.getSpec().getSelector()); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/ReplicaSetOperationsImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/ReplicaSetOperationsImpl.java index 8746c5acc27..62295b23a3d 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/ReplicaSetOperationsImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/ReplicaSetOperationsImpl.java @@ -159,7 +159,7 @@ public String getLog(boolean isPretty) { return PodOperationUtil.getLog(doGetLog(isPretty), isPretty); } - private List> doGetLog(boolean isPretty) { + private List doGetLog(boolean isPretty) { ReplicaSet replicaSet = requireFromServer(); return PodOperationUtil.getPodOperationsForController(context, replicaSet.getMetadata().getUid(), getReplicaSetSelectorLabels(replicaSet), isPretty, rollingOperationContext.getLogWaitTimeout(), diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/ReplicaSetRollingUpdater.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/ReplicaSetRollingUpdater.java index d81d4039d9e..18f6a6df8d4 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/ReplicaSetRollingUpdater.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/ReplicaSetRollingUpdater.java @@ -22,6 +22,7 @@ import io.fabric8.kubernetes.api.model.apps.ReplicaSetList; import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.dsl.Operation; +import io.fabric8.kubernetes.client.dsl.PodResource; import io.fabric8.kubernetes.client.dsl.RollableScalableResource; import io.fabric8.kubernetes.client.dsl.WatchListDeletable; @@ -51,7 +52,7 @@ protected ReplicaSet createClone(ReplicaSet obj, String newName, String newDeplo } @Override - protected WatchListDeletable selectedPodLister(ReplicaSet obj) { + protected WatchListDeletable selectedPodLister(ReplicaSet obj) { return selectedPodLister(obj.getSpec().getSelector()); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/RollingUpdater.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/RollingUpdater.java index 6401d368ed2..e1998ea006d 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/RollingUpdater.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/RollingUpdater.java @@ -79,7 +79,7 @@ protected RollingUpdater(Client client, String namespace, long rollingTimeoutMil protected abstract T createClone(T obj, String newName, String newDeploymentHash); - protected abstract WatchListDeletable selectedPodLister(T obj); + protected abstract WatchListDeletable selectedPodLister(T obj); protected abstract T updateDeploymentKey(String name, String hash); @@ -264,11 +264,11 @@ private String md5sum(HasMetadata obj) throws NoSuchAlgorithmException, JsonProc protected abstract Operation> resources(); - protected Operation> pods() { + protected Operation pods() { return new PodOperationsImpl(client); } - protected FilterWatchListDeletable selectedPodLister(LabelSelector selector) { + protected FilterWatchListDeletable selectedPodLister(LabelSelector selector) { return pods().inNamespace(namespace).withLabelSelector(selector); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/StatefulSetOperationsImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/StatefulSetOperationsImpl.java index 1a9188f8a3f..c68821aa350 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/StatefulSetOperationsImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/StatefulSetOperationsImpl.java @@ -143,7 +143,7 @@ public String getLog(boolean isPretty) { return PodOperationUtil.getLog(doGetLog(isPretty), isPretty); } - private List> doGetLog(boolean isPretty) { + private List doGetLog(boolean isPretty) { StatefulSet statefulSet = requireFromServer(); return PodOperationUtil.getPodOperationsForController(context, statefulSet.getMetadata().getUid(), diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/StatefulSetRollingUpdater.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/StatefulSetRollingUpdater.java index e647e099f16..88a9413de24 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/StatefulSetRollingUpdater.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/apps/v1/StatefulSetRollingUpdater.java @@ -22,6 +22,7 @@ import io.fabric8.kubernetes.api.model.apps.StatefulSetList; import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.dsl.Operation; +import io.fabric8.kubernetes.client.dsl.PodResource; import io.fabric8.kubernetes.client.dsl.RollableScalableResource; import io.fabric8.kubernetes.client.dsl.WatchListDeletable; @@ -51,7 +52,7 @@ protected StatefulSet createClone(StatefulSet obj, String newName, String newDep } @Override - protected WatchListDeletable selectedPodLister(StatefulSet obj) { + protected WatchListDeletable selectedPodLister(StatefulSet obj) { return selectedPodLister(obj.getSpec().getSelector()); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/batch/v1/JobOperationsImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/batch/v1/JobOperationsImpl.java index 8d1f5d0eb21..cf31dd09802 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/batch/v1/JobOperationsImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/batch/v1/JobOperationsImpl.java @@ -15,7 +15,6 @@ */ package io.fabric8.kubernetes.client.dsl.internal.batch.v1; -import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.api.model.autoscaling.v1.Scale; import io.fabric8.kubernetes.api.model.batch.v1.Job; import io.fabric8.kubernetes.api.model.batch.v1.JobList; @@ -118,14 +117,14 @@ public String getLog() { @Override public String getLog(boolean isPretty) { StringBuilder stringBuilder = new StringBuilder(); - List> podOperationList = doGetLog(false); - for (PodResource podOperation : podOperationList) { + List podOperationList = doGetLog(false); + for (PodResource podOperation : podOperationList) { stringBuilder.append(podOperation.getLog(isPretty)); } return stringBuilder.toString(); } - private List> doGetLog(boolean isPretty) { + private List doGetLog(boolean isPretty) { Job job = requireFromServer(); return PodOperationUtil.getPodOperationsForController(context, job.getMetadata().getUid(), diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java index 826ee9dea30..c6a3323ed51 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/PodOperationsImpl.java @@ -85,8 +85,8 @@ import static io.fabric8.kubernetes.client.utils.internal.OptionalDependencyWrapper.wrapRunWithOptionalDependency; -public class PodOperationsImpl extends HasMetadataOperation> - implements PodResource, CopyOrReadable { +public class PodOperationsImpl extends HasMetadataOperation + implements PodResource, CopyOrReadable { public static final int HTTP_TOO_MANY_REQUESTS = 429; private static final Integer DEFAULT_POD_LOG_WAIT_TIMEOUT = 5; diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/ReplicationControllerOperationsImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/ReplicationControllerOperationsImpl.java index 541de9e926b..725e4f07a8e 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/ReplicationControllerOperationsImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/ReplicationControllerOperationsImpl.java @@ -141,7 +141,7 @@ public String getLog(boolean isPretty) { return PodOperationUtil.getLog(doGetLog(isPretty), isPretty); } - private List> doGetLog(boolean isPretty) { + private List doGetLog(boolean isPretty) { ReplicationController rc = requireFromServer(); return PodOperationUtil.getPodOperationsForController(context, rc.getMetadata().getUid(), diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/ReplicationControllerRollingUpdater.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/ReplicationControllerRollingUpdater.java index e7f1892288c..3d421674181 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/ReplicationControllerRollingUpdater.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/core/v1/ReplicationControllerRollingUpdater.java @@ -22,6 +22,7 @@ import io.fabric8.kubernetes.api.model.ReplicationControllerList; import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.dsl.Operation; +import io.fabric8.kubernetes.client.dsl.PodResource; import io.fabric8.kubernetes.client.dsl.RollableScalableResource; import io.fabric8.kubernetes.client.dsl.WatchListDeletable; import io.fabric8.kubernetes.client.dsl.internal.apps.v1.RollingUpdater; @@ -51,7 +52,7 @@ protected ReplicationController createClone(ReplicationController obj, String ne } @Override - protected WatchListDeletable selectedPodLister(ReplicationController obj) { + protected WatchListDeletable selectedPodLister(ReplicationController obj) { return pods().inNamespace(namespace).withLabels(obj.getSpec().getSelector()); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/DeploymentRollingUpdater.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/DeploymentRollingUpdater.java index eb96f81f529..38650163226 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/DeploymentRollingUpdater.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/DeploymentRollingUpdater.java @@ -22,6 +22,7 @@ import io.fabric8.kubernetes.api.model.extensions.DeploymentList; import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.dsl.Operation; +import io.fabric8.kubernetes.client.dsl.PodResource; import io.fabric8.kubernetes.client.dsl.RollableScalableResource; import io.fabric8.kubernetes.client.dsl.WatchListDeletable; import io.fabric8.kubernetes.client.dsl.internal.apps.v1.RollingUpdater; @@ -52,7 +53,7 @@ protected Deployment createClone(Deployment obj, String newName, String newDeplo } @Override - protected WatchListDeletable selectedPodLister(Deployment obj) { + protected WatchListDeletable selectedPodLister(Deployment obj) { return selectedPodLister(obj.getSpec().getSelector()); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/ReplicaSetOperationsImpl.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/ReplicaSetOperationsImpl.java index ee1f6278b4f..afb66b8852d 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/ReplicaSetOperationsImpl.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/ReplicaSetOperationsImpl.java @@ -159,14 +159,14 @@ public Status rollback(DeploymentRollback deploymentRollback) { @Override public String getLog(boolean isPretty) { StringBuilder stringBuilder = new StringBuilder(); - List> podOperationList = doGetLog(isPretty); - for (PodResource podOperation : podOperationList) { + List podOperationList = doGetLog(isPretty); + for (PodResource podOperation : podOperationList) { stringBuilder.append(podOperation.getLog(isPretty)); } return stringBuilder.toString(); } - private List> doGetLog(boolean isPretty) { + private List doGetLog(boolean isPretty) { ReplicaSet replicaSet = requireFromServer(); return PodOperationUtil.getPodOperationsForController(context, replicaSet.getMetadata().getUid(), getReplicaSetSelectorLabels(replicaSet), isPretty, rollingOperationContext.getLogWaitTimeout(), diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/ReplicaSetRollingUpdater.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/ReplicaSetRollingUpdater.java index b2cd4dc67d9..581fec7535a 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/ReplicaSetRollingUpdater.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/extensions/v1beta1/ReplicaSetRollingUpdater.java @@ -22,6 +22,7 @@ import io.fabric8.kubernetes.api.model.extensions.ReplicaSetList; import io.fabric8.kubernetes.client.Client; import io.fabric8.kubernetes.client.dsl.Operation; +import io.fabric8.kubernetes.client.dsl.PodResource; import io.fabric8.kubernetes.client.dsl.RollableScalableResource; import io.fabric8.kubernetes.client.dsl.WatchListDeletable; import io.fabric8.kubernetes.client.dsl.internal.apps.v1.RollingUpdater; @@ -52,7 +53,7 @@ protected ReplicaSet createClone(ReplicaSet obj, String newName, String newDeplo } @Override - protected WatchListDeletable selectedPodLister(ReplicaSet obj) { + protected WatchListDeletable selectedPodLister(ReplicaSet obj) { return selectedPodLister(obj.getSpec().getSelector()); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/osgi/ManagedKubernetesClient.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/osgi/ManagedKubernetesClient.java index 7d90604ba71..0fd133828fd 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/osgi/ManagedKubernetesClient.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/osgi/ManagedKubernetesClient.java @@ -314,7 +314,7 @@ public MixedOperation, Resource> pods() { + public MixedOperation pods() { return delegate.pods(); } diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/internal/PodOperationUtil.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/internal/PodOperationUtil.java index 442c82460ad..a826cc2145c 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/internal/PodOperationUtil.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/utils/internal/PodOperationUtil.java @@ -36,7 +36,9 @@ public class PodOperationUtil { private static final Logger LOG = LoggerFactory.getLogger(PodOperationUtil.class); - private PodOperationUtil() { } + + private PodOperationUtil() { + } /** * Gets PodOperations for Pods specific to a controller @@ -46,8 +48,9 @@ private PodOperationUtil() { } * @param controllerUid UID of Controller * @return returns list of PodOperations with pods whose owner's UID is of the provided controller */ - public static List> getFilteredPodsForLogs(PodOperationsImpl podOperations, PodList controllerPodList, String controllerUid) { - List> pods = new ArrayList<>(); + public static List getFilteredPodsForLogs(PodOperationsImpl podOperations, PodList controllerPodList, + String controllerUid) { + List pods = new ArrayList<>(); for (Pod pod : controllerPodList.getItems()) { OwnerReference ownerReference = KubernetesResourceUtil.getControllerUid(pod); if (ownerReference != null && ownerReference.getUid().equals(controllerUid)) { @@ -57,20 +60,24 @@ public static List> getFilteredPodsForLogs(PodOperationsImpl po return pods; } - public static PodOperationsImpl getGenericPodOperations(OperationContext context, boolean isPretty, Integer podLogWaitTimeout, String containerId) { + public static PodOperationsImpl getGenericPodOperations(OperationContext context, boolean isPretty, Integer podLogWaitTimeout, + String containerId) { return new PodOperationsImpl(new PodOperationContext(containerId, - null, null, null, null, null, - null, null, null, false, false, - false, null, null, null, isPretty, - null, null, null, - null, null, podLogWaitTimeout), context.withName(null).withApiGroupName(null).withApiGroupVersion("v1")); + null, null, null, null, null, + null, null, null, false, false, + false, null, null, null, isPretty, + null, null, null, + null, null, podLogWaitTimeout), context.withName(null).withApiGroupName(null).withApiGroupVersion("v1")); } - public static List> getPodOperationsForController(OperationContext context, String controllerUid, Map selectorLabels, boolean isPretty, Integer podLogWaitTimeout, String containerId) { - return getPodOperationsForController(PodOperationUtil.getGenericPodOperations(context, isPretty, podLogWaitTimeout, containerId), controllerUid, selectorLabels); + public static List getPodOperationsForController(OperationContext context, String controllerUid, + Map selectorLabels, boolean isPretty, Integer podLogWaitTimeout, String containerId) { + return getPodOperationsForController( + PodOperationUtil.getGenericPodOperations(context, isPretty, podLogWaitTimeout, containerId), controllerUid, + selectorLabels); } - public static LogWatch watchLog(List> podResources, OutputStream out) { + public static LogWatch watchLog(List podResources, OutputStream out) { if (!podResources.isEmpty()) { if (podResources.size() > 1) { LOG.debug("Found {} pods, Using first one to watch logs", podResources.size()); @@ -80,7 +87,7 @@ public static LogWatch watchLog(List> podResources, OutputStrea return null; } - public static Reader getLogReader(List> podResources) { + public static Reader getLogReader(List podResources) { if (!podResources.isEmpty()) { if (podResources.size() > 1) { LOG.debug("Found {} pods, Using first one to get log reader", podResources.size()); @@ -90,21 +97,22 @@ public static Reader getLogReader(List> podResources) { return null; } - public static String getLog(List> podOperationList, Boolean isPretty) { + public static String getLog(List podOperationList, Boolean isPretty) { StringBuilder stringBuilder = new StringBuilder(); - for (PodResource podOperation : podOperationList) { + for (PodResource podOperation : podOperationList) { stringBuilder.append(podOperation.getLog(isPretty)); } return stringBuilder.toString(); } - public static List> getPodOperationsForController(PodOperationsImpl podOperations, String controllerUid, Map selectorLabels) { + public static List getPodOperationsForController(PodOperationsImpl podOperations, String controllerUid, + Map selectorLabels) { PodList controllerPodList = podOperations.withLabels(selectorLabels).list(); return PodOperationUtil.getFilteredPodsForLogs(podOperations, controllerPodList, controllerUid); } - public static void waitUntilReadyBeforeFetchingLogs(PodResource podOperation, Integer logWaitTimeout) { + public static void waitUntilReadyBeforeFetchingLogs(PodResource podOperation, Integer logWaitTimeout) { try { // Wait for Pod to become ready Pod pod = podOperation.fromServer().get(); diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/PatchTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/PatchTest.java index cbddde755c8..d949d76776c 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/PatchTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/PatchTest.java @@ -75,7 +75,7 @@ void testJsonPatch() throws IOException { // When kubernetesClient.pods().inNamespace("ns1").withName("foo") - .patch("{\"metadata\":{\"annotations\":{\"bob\":\"martin\"}}}"); + .patch("{\"metadata\":{\"annotations\":{\"bob\":\"martin\"}}}"); // Then verify(mockClient, times(2)).send(any(), any()); @@ -87,12 +87,12 @@ void testJsonPatch() throws IOException { void testJsonMergePatch() throws IOException { // Given PatchContext patchContext = new PatchContext.Builder() - .withPatchType(PatchType.JSON_MERGE) - .build(); + .withPatchType(PatchType.JSON_MERGE) + .build(); // When kubernetesClient.pods().inNamespace("ns1").withName("foo") - .patch(patchContext, "{\"metadata\":{\"annotations\":{\"bob\":\"martin\"}}}"); + .patch(patchContext, "{\"metadata\":{\"annotations\":{\"bob\":\"martin\"}}}"); // Then verify(mockClient, times(2)).send(any(), any()); @@ -120,7 +120,7 @@ void testPatchThrowExceptionWhenResourceNotFound() throws IOException { when(mockClient.send(any(), Mockito.eq(InputStream.class))).thenReturn(mockResponse); // When - PodResource podResource = kubernetesClient.pods() + PodResource podResource = kubernetesClient.pods() .inNamespace("ns1") .withName("foo"); KubernetesClientException e = assertThrows(KubernetesClientException.class, @@ -139,7 +139,8 @@ void testJsonPatchWithPositionalArrays() throws IOException { // When kubernetesClient.pods().inNamespace("ns1").withName("foo") - .patch(patchContext, "[{\"op\": \"replace\", \"path\":\"/spec/containers/0/image\", \"value\":\"foo/gb-frontend:v4\"}]"); + .patch(patchContext, + "[{\"op\": \"replace\", \"path\":\"/spec/containers/0/image\", \"value\":\"foo/gb-frontend:v4\"}]"); // Then verify(mockClient, times(2)).send(any(), any()); @@ -153,21 +154,22 @@ void testPatchWithPatchOptions() throws IOException { // When kubernetesClient.pods().inNamespace("ns1").withName("foo") - .patch(new PatchContext.Builder() - .withFieldManager("fabric8") - .withDryRun(Collections.singletonList("All")) - .build(), "{\"metadata\":{\"annotations\":{\"bob\":\"martin\"}}}"); + .patch(new PatchContext.Builder() + .withFieldManager("fabric8") + .withDryRun(Collections.singletonList("All")) + .build(), "{\"metadata\":{\"annotations\":{\"bob\":\"martin\"}}}"); // Then verify(mockClient, times(2)).send(any(), any()); assertRequest("GET", "/api/v1/namespaces/ns1/pods/foo", null); - assertRequest(1, "PATCH", "/api/v1/namespaces/ns1/pods/foo", "fieldManager=fabric8&dryRun=All", OperationSupport.STRATEGIC_MERGE_JSON_PATCH); + assertRequest(1, "PATCH", "/api/v1/namespaces/ns1/pods/foo", "fieldManager=fabric8&dryRun=All", + OperationSupport.STRATEGIC_MERGE_JSON_PATCH); } private void assertRequest(String method, String url, String queryParam) { assertRequest(0, method, url, queryParam, null); } - + private void assertRequest(int index, String method, String url, String queryParam, String contentType) { ArgumentCaptor urlCaptor = ArgumentCaptor.forClass(URL.class); Builder mock = builders.get(index); @@ -176,33 +178,32 @@ private void assertRequest(int index, String method, String url, String queryPar assertEquals(url, capturedURL.getPath()); validateMethod(method, contentType, mock); - + assertEquals(queryParam, capturedURL.getQuery()); } static void validateMethod(String method, String contentType, Builder mock) { ArgumentCaptor contentTypeCaptor = ArgumentCaptor.forClass(String.class); switch (method) { - case "DELETE": - Mockito.verify(mock).delete(contentTypeCaptor.capture(), any()); - break; - case "POST": - Mockito.verify(mock).post(contentTypeCaptor.capture(), any(String.class)); - break; - case "PUT": - Mockito.verify(mock).put(contentTypeCaptor.capture(), any()); - break; - case "PATCH": - Mockito.verify(mock).patch(contentTypeCaptor.capture(), any()); - break; - default: - break; //TODO: validate GET, but that explicit call was removed + case "DELETE": + Mockito.verify(mock).delete(contentTypeCaptor.capture(), any()); + break; + case "POST": + Mockito.verify(mock).post(contentTypeCaptor.capture(), any(String.class)); + break; + case "PUT": + Mockito.verify(mock).put(contentTypeCaptor.capture(), any()); + break; + case "PATCH": + Mockito.verify(mock).patch(contentTypeCaptor.capture(), any()); + break; + default: + break; //TODO: validate GET, but that explicit call was removed } - + if (contentType != null) { assertEquals(contentType, contentTypeCaptor.getValue()); } } - - + } diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperationWatchTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperationWatchTest.java index 11c25d82ab5..07d94f5b8de 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperationWatchTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperationWatchTest.java @@ -41,12 +41,12 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -@SuppressWarnings({"rawtypes", "FieldCanBeLocal"}) +@SuppressWarnings({ "rawtypes", "FieldCanBeLocal" }) class BaseOperationWatchTest { private Watcher watcher; private OperationContext operationContext; - private BaseOperation> baseOperation; + private BaseOperation baseOperation; @SuppressWarnings("unchecked") @BeforeEach @@ -59,25 +59,26 @@ void setUp() { @Test @DisplayName("watch, with exception on connection open, should throw Exception and close WatchConnectionManager") void watchWithExceptionOnOpen() { - try (final MockedConstruction m = mockConstruction(WatchConnectionManager.class, (mock, context) -> { - // Given - doThrow(new KubernetesClientException("Mocked Connection Error")).when(mock).waitUntilReady(); - })) { + try ( + final MockedConstruction m = mockConstruction(WatchConnectionManager.class, (mock, context) -> { + // Given + doThrow(new KubernetesClientException("Mocked Connection Error")).when(mock).waitUntilReady(); + })) { // When final KubernetesClientException result = assertThrows(KubernetesClientException.class, - () -> { - baseOperation.watch(watcher); - fail(); - }); + () -> { + baseOperation.watch(watcher); + fail(); + }); // Then assertThat(result).hasMessage("Mocked Connection Error"); assertThat(m.constructed()) - .hasSize(1) - .element(0) - .matches(wcm -> { - verify(wcm, times(1)).close(); - return true; - }); + .hasSize(1) + .element(0) + .matches(wcm -> { + verify(wcm, times(1)).close(); + return true; + }); } } @@ -85,23 +86,22 @@ void watchWithExceptionOnOpen() { @DisplayName("watch, with retryable exception on connection open, should close initial WatchConnectionManager and retry") void watchWithRetryableExceptionOnOpen() { try ( - final MockedConstruction m = mockConstruction(WatchConnectionManager.class, (mock, context) -> { - // Given - doThrow(new KubernetesClientException(new StatusBuilder().withCode(503).build())).when(mock).waitUntilReady(); - }); - final MockedConstruction mHttp = mockConstruction(WatchHTTPManager.class) - ) { + final MockedConstruction m = mockConstruction(WatchConnectionManager.class, (mock, context) -> { + // Given + doThrow(new KubernetesClientException(new StatusBuilder().withCode(503).build())).when(mock).waitUntilReady(); + }); + final MockedConstruction mHttp = mockConstruction(WatchHTTPManager.class)) { // When final Watch result = baseOperation.watch(watcher); // Then assertThat(result).isInstanceOf(WatchHTTPManager.class).isSameAs(mHttp.constructed().get(0)); assertThat(m.constructed()) - .hasSize(1) - .element(0) - .matches(wcm -> { - verify(wcm, times(1)).close(); - return true; - }); + .hasSize(1) + .element(0) + .matches(wcm -> { + verify(wcm, times(1)).close(); + return true; + }); } } } diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/utils/internal/PodOperationUtilTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/utils/internal/PodOperationUtilTest.java index 5823fed45d7..4435e353cf0 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/utils/internal/PodOperationUtilTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/utils/internal/PodOperationUtilTest.java @@ -65,7 +65,7 @@ void testGetFilteredPodsForLogs() { PodList podList = getMockPodList(controllerUid); // When - List> podResourceList = PodOperationUtil.getFilteredPodsForLogs(podOperations, podList, controllerUid); + List podResourceList = PodOperationUtil.getFilteredPodsForLogs(podOperations, podList, controllerUid); // Then assertNotNull(podResourceList); @@ -88,9 +88,9 @@ void testGetGenericPodOperations() { void testWaitUntilReadyBeforeFetchingLogs() throws InterruptedException { // Given Pod pod = new PodBuilder() - .withNewMetadata().withName("foo").endMetadata() - .withNewStatus().withPhase("Pending").endStatus() - .build(); + .withNewMetadata().withName("foo").endMetadata() + .withNewStatus().withPhase("Pending").endStatus() + .build(); ExtensibleResource gettablePod = Mockito.mock(ExtensibleResource.class); when(gettablePod.get()).thenReturn(pod); when(podOperations.fromServer()).thenReturn(gettablePod); @@ -108,11 +108,12 @@ void testGetPodOperationsForController() { // Given String controllerUid = "some-uid"; Map selectorLabels = Collections.singletonMap("foo", "bar"); - FilterWatchListDeletable filterWatchListDeletable = getMockPodFilterOperation(controllerUid); + FilterWatchListDeletable filterWatchListDeletable = getMockPodFilterOperation(controllerUid); when(podOperations.withLabels(any())).thenReturn(filterWatchListDeletable); // When - List> podResources = PodOperationUtil.getPodOperationsForController(podOperations, controllerUid, selectorLabels); + List podResources = PodOperationUtil.getPodOperationsForController(podOperations, controllerUid, + selectorLabels); // Then assertNotNull(podResources); @@ -122,7 +123,7 @@ void testGetPodOperationsForController() { @Test void testWatchLogSinglePod() { // Given - PodResource podResource = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); + PodResource podResource = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); ByteArrayOutputStream byteArrayOutputStream = Mockito.mock(ByteArrayOutputStream.class, Mockito.RETURNS_DEEP_STUBS); // When @@ -136,8 +137,8 @@ void testWatchLogSinglePod() { @Test void testWatchLogMultiplePodReplicasPicksFirstPod() { // Given - PodResource p1 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); - PodResource p2 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); + PodResource p1 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); + PodResource p2 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); ByteArrayOutputStream byteArrayOutputStream = Mockito.mock(ByteArrayOutputStream.class, Mockito.RETURNS_DEEP_STUBS); // When @@ -162,8 +163,8 @@ void testGetLogReaderEmptyPodResourceList() { @Test void testGetLogReaderMultiplePodReplicasPicksFirstPod() { // Given - PodResource p1 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); - PodResource p2 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); + PodResource p1 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); + PodResource p2 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); // When Reader reader = PodOperationUtil.getLogReader(createMockPodResourceList(p1, p2)); @@ -177,8 +178,8 @@ void testGetLogReaderMultiplePodReplicasPicksFirstPod() { @Test void testGetLog() { // Given - PodResource p1 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); - PodResource p2 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); + PodResource p1 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); + PodResource p2 = Mockito.mock(PodResource.class, Mockito.RETURNS_DEEP_STUBS); when(p1.getLog(anyBoolean())).thenReturn("p1-log"); when(p2.getLog(anyBoolean())).thenReturn("p2-log"); @@ -193,22 +194,22 @@ void testGetLog() { private PodList getMockPodList(String controllerUid) { return new PodListBuilder() - .addToItems( - new PodBuilder() - .withNewMetadata() - .withName("pod1") - .addNewOwnerReference() - .withController(true) - .withUid(controllerUid) - .endOwnerReference() - .endMetadata() - .build()) - .addToItems(new PodBuilder().withNewMetadata().withName("pod2").endMetadata().build()) - .build(); + .addToItems( + new PodBuilder() + .withNewMetadata() + .withName("pod1") + .addNewOwnerReference() + .withController(true) + .withUid(controllerUid) + .endOwnerReference() + .endMetadata() + .build()) + .addToItems(new PodBuilder().withNewMetadata().withName("pod2").endMetadata().build()) + .build(); } - private FilterWatchListDeletable getMockPodFilterOperation(String controllerUid) { - FilterWatchListDeletable result = Mockito.mock(FilterWatchListDeletable.class); + private FilterWatchListDeletable getMockPodFilterOperation(String controllerUid) { + FilterWatchListDeletable result = Mockito.mock(FilterWatchListDeletable.class); Mockito.when(result.list()).then(new Answer() { @Override public PodList answer(InvocationOnMock invocation) throws Throwable { @@ -219,8 +220,8 @@ public PodList answer(InvocationOnMock invocation) throws Throwable { } @SafeVarargs - private final List> createMockPodResourceList(PodResource... podResources) { - List> podResourceList = new ArrayList<>(); + private final List createMockPodResourceList(PodResource... podResources) { + List podResourceList = new ArrayList<>(); Collections.addAll(podResourceList, podResources); return podResourceList; } diff --git a/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/CreatePod.java b/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/CreatePod.java index 9dfdbeba4b5..6b6fabe1257 100644 --- a/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/CreatePod.java +++ b/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples/CreatePod.java @@ -68,10 +68,10 @@ public static void main(String[] args) { return; } HasMetadata resource = resources.get(0); - if (resource instanceof Pod){ + if (resource instanceof Pod) { Pod pod = (Pod) resource; logger.info("Creating pod in namespace {}", namespace); - NonNamespaceOperation> pods = client.pods().inNamespace(namespace); + NonNamespaceOperation pods = client.pods().inNamespace(namespace); Pod result = pods.create(pod); logger.info("Created pod {}", result.getMetadata().getName()); } else { diff --git a/kubernetes-itests/src/test/java/io/fabric8/kubernetes/PodIT.java b/kubernetes-itests/src/test/java/io/fabric8/kubernetes/PodIT.java index 540aa5ad697..e64886b9afd 100644 --- a/kubernetes-itests/src/test/java/io/fabric8/kubernetes/PodIT.java +++ b/kubernetes-itests/src/test/java/io/fabric8/kubernetes/PodIT.java @@ -274,7 +274,7 @@ public void uploadFile() throws IOException { } private void assertUploaded(Pod pod1, final Path tmpFile, String filename) throws IOException { - PodResource podResource = client.pods().inNamespace(session.getNamespace()) + PodResource podResource = client.pods().inNamespace(session.getNamespace()) .withName(pod1.getMetadata().getName()); podResource.file(filename).upload(tmpFile); @@ -301,7 +301,7 @@ public void uploadDir() throws IOException { Files.write(Files.createFile(file), Arrays.asList("I'm uploaded", fileName)); } - PodResource podResource = client.pods().inNamespace(session.getNamespace()) + PodResource podResource = client.pods().inNamespace(session.getNamespace()) .withName(pod1.getMetadata().getName()); podResource.dir("/tmp/uploadDir").upload(tmpDir); @@ -325,7 +325,7 @@ public void copyFile() throws IOException { final Path tmpDir = Files.createTempDirectory("copyFile"); - PodResource podResource = client.pods().inNamespace(session.getNamespace()) + PodResource podResource = client.pods().inNamespace(session.getNamespace()) .withName(pod1.getMetadata().getName()); podResource.writingOutput(System.out).exec("sh", "-c", "echo 'hello' > /msg.txt"); podResource.file("/msg.txt").copy(tmpDir); diff --git a/kubernetes-itests/src/test/java/io/fabric8/kubernetes/ServerSideApplyIT.java b/kubernetes-itests/src/test/java/io/fabric8/kubernetes/ServerSideApplyIT.java index 31d74341c85..6c865a57db3 100644 --- a/kubernetes-itests/src/test/java/io/fabric8/kubernetes/ServerSideApplyIT.java +++ b/kubernetes-itests/src/test/java/io/fabric8/kubernetes/ServerSideApplyIT.java @@ -64,7 +64,7 @@ public void testServerSideApply() { .endSpec() .build(); - Resource resource = client.services().inNamespace(session.getNamespace()).withItem(service); + Resource resource = client.services().inNamespace(session.getNamespace()).resource(service); resource.delete(); // 1st apply - create must be a server side apply - otherwise the later operations will need to force diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CreateOrReplaceResourceTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CreateOrReplaceResourceTest.java index 7769acf6abb..e86812a82d8 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CreateOrReplaceResourceTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/CreateOrReplaceResourceTest.java @@ -55,15 +55,16 @@ class CreateOrReplaceResourceTest { @DisplayName("Should replace an existing resource in Kubernetes Cluster") void testResourceReplace() { server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).always(); + .withNewMetadata().withResourceVersion("12345").and().build()).always(); server.expect().get().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).times(2); + .withNewMetadata().withResourceVersion("12345").and().build()).times(2); server.expect().put().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + .withNewMetadata().withResourceVersion("12345").and().build()).once(); - Pod pod = client.resource(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()).createOrReplace(); + Pod pod = client.resource(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()) + .createOrReplace(); assertNotNull(pod); assertEquals("12345", pod.getMetadata().getResourceVersion()); } @@ -72,9 +73,10 @@ void testResourceReplace() { @DisplayName("Should create a new resource in Kubernetes Cluster") void testResourceCreate() { server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + .withNewMetadata().withResourceVersion("12345").and().build()).once(); - HasMetadata result = client.resource(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()).createOrReplace(); + HasMetadata result = client + .resource(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()).createOrReplace(); assertNotNull(result); assertEquals("12345", result.getMetadata().getResourceVersion()); } @@ -83,9 +85,12 @@ void testResourceCreate() { @DisplayName("Should throw Exception on failed create") void testResourceCreateFailure() { // Given - server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_BAD_REQUEST, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").endMetadata().build()).once(); - NamespaceableResource podOperation = client.resource(new PodBuilder().withNewMetadata().withName("pod123").endMetadata().build()); + server.expect().post().withPath("/api/v1/namespaces/test/pods") + .andReturn(HttpURLConnection.HTTP_BAD_REQUEST, new PodBuilder() + .withNewMetadata().withResourceVersion("12345").endMetadata().build()) + .once(); + NamespaceableResource podOperation = client + .resource(new PodBuilder().withNewMetadata().withName("pod123").endMetadata().build()); // When assertThrows(KubernetesClientException.class, podOperation::createOrReplace); @@ -95,9 +100,10 @@ void testResourceCreateFailure() { @DisplayName("Should create a new resource in Kubernetes Cluster") void testCreate() { server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + .withNewMetadata().withResourceVersion("12345").and().build()).once(); - Pod pod = client.pods().createOrReplace(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()); + Pod pod = client.pods() + .createOrReplace(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()); assertNotNull(pod); assertEquals("12345", pod.getMetadata().getResourceVersion()); } @@ -105,13 +111,17 @@ void testCreate() { @Test @DisplayName("Shoulc replace an existing resource in Kubernetes Cluster") void testReplace() { - server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always(); + server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, + new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always(); - server.expect().get().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).times(2); + server.expect().get().withPath("/api/v1/namespaces/test/pods/pod123") + .andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()) + .times(2); server.expect().put().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + .withNewMetadata().withResourceVersion("12345").and().build()).once(); - Pod pod = client.pods().createOrReplace(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()); + Pod pod = client.pods() + .createOrReplace(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()); assertNotNull(pod); assertEquals("12345", pod.getMetadata().getResourceVersion()); } @@ -120,12 +130,17 @@ void testReplace() { @DisplayName("Should throw exception on failed replace") void testFailedReplace() { // Given - server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always(); - server.expect().get().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).times(2); - server.expect().put().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_BAD_REQUEST, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, + new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always(); + server.expect().get().withPath("/api/v1/namespaces/test/pods/pod123") + .andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()) + .times(2); + server.expect().put().withPath("/api/v1/namespaces/test/pods/pod123") + .andReturn(HttpURLConnection.HTTP_BAD_REQUEST, new PodBuilder() + .withNewMetadata().withResourceVersion("12345").and().build()) + .once(); final Pod toCreate = new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build(); - final MixedOperation> pods = client.pods(); + final MixedOperation pods = client.pods(); // When assertThrows(KubernetesClientException.class, () -> pods.create(toCreate)); } @@ -134,7 +149,7 @@ void testFailedReplace() { @DisplayName("Should create a new resource in Kubernetes Cluster") void testResourceCreateFromLoad() throws Exception { server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + .withNewMetadata().withResourceVersion("12345").and().build()).once(); List result = client.load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")).createOrReplace(); assertNotNull(result); @@ -151,11 +166,14 @@ void testResourceCreateFromLoad() throws Exception { @Test @DisplayName("Should replace an existing resource in Kubernetes Cluster") void testResourceReplaceFromLoad() throws Exception { - server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always(); + server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, + new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always(); - server.expect().get().withPath("/api/v1/namespaces/test/pods/nginx").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).times(2); + server.expect().get().withPath("/api/v1/namespaces/test/pods/nginx") + .andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()) + .times(2); server.expect().put().withPath("/api/v1/namespaces/test/pods/nginx").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + .withNewMetadata().withResourceVersion("12345").and().build()).once(); List result = client.load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")).createOrReplace(); assertNotNull(result); @@ -172,7 +190,7 @@ void testResourceReplaceFromLoad() throws Exception { @DisplayName("Should create a new resource from yaml") void testCreateFromLoad() throws Exception { server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + .withNewMetadata().withResourceVersion("12345").and().build()).once(); Pod pod = client.pods().load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")).createOrReplace(); assertNotNull(pod); @@ -185,17 +203,18 @@ void testCreateFromLoad() throws Exception { @Test @DisplayName("Should update existing resource") void testReplaceFromLoad() throws Exception { - server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().build()).always(); + server.expect().post().withPath("/api/v1/namespaces/test/pods") + .andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().build()).always(); - server.expect().get().withPath("/api/v1/namespaces/test/pods/nginx").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().build()).times(2); + server.expect().get().withPath("/api/v1/namespaces/test/pods/nginx") + .andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().build()).times(2); server.expect().put().withPath("/api/v1/namespaces/test/pods/nginx").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder() - .withNewMetadata().withResourceVersion("12345").and().build()).once(); + .withNewMetadata().withResourceVersion("12345").and().build()).once(); Pod pod = client.pods().load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")).createOrReplace(); assertNotNull(pod); assertEquals("12345", pod.getMetadata().getResourceVersion()); - RecordedRequest request = server.getLastRequest(); assertEquals("/api/v1/namespaces/test/pods/nginx", request.getPath()); Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(request.getBody().inputStream()); @@ -205,74 +224,89 @@ void testReplaceFromLoad() throws Exception { @Test void testReplaceWithItemResourceVersion() throws Exception { server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1") - .andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() - .withNewMetadata().withResourceVersion("1001").and().build()).once(); + .andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() + .withNewMetadata().withResourceVersion("1001").and().build()) + .once(); // when you try to replace with a resource version specified, it will try the existing one first ConfigMap map = client.configMaps().withName("map1").replace(new ConfigMapBuilder() - .withNewMetadata().withName("map1").withResourceVersion("1000").and().build()); + .withNewMetadata().withName("map1").withResourceVersion("1000").and().build()); assertNotNull(map); assertEquals("1001", map.getMetadata().getResourceVersion()); - ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(server.getLastRequest().getBody().inputStream()); + ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class) + .readValue(server.getLastRequest().getBody().inputStream()); assertEquals("1000", replacedMap.getMetadata().getResourceVersion()); } @Test void testReplaceWithItemResourceVersionRetry() throws Exception { - server.expect().get().withPath("/api/v1/namespaces/test/configmaps/map1").andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() - .withNewMetadata().withResourceVersion("1000").and().build()).always(); + server.expect().get().withPath("/api/v1/namespaces/test/configmaps/map1") + .andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() + .withNewMetadata().withResourceVersion("1000").and().build()) + .always(); server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1") - .andReturn(HttpURLConnection.HTTP_CONFLICT, new StatusBuilder().withCode(HttpURLConnection.HTTP_CONFLICT).build()).once(); + .andReturn(HttpURLConnection.HTTP_CONFLICT, new StatusBuilder().withCode(HttpURLConnection.HTTP_CONFLICT).build()) + .once(); server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1") - .andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() - .withNewMetadata().withResourceVersion("1001").and().build()).once(); + .andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() + .withNewMetadata().withResourceVersion("1001").and().build()) + .once(); // when you try to replace with a resource version specified, it will try the existing one first ConfigMap map = client.configMaps().withName("map1").replace(new ConfigMapBuilder() - .withNewMetadata().withName("map1").withResourceVersion("999").and().build()); + .withNewMetadata().withName("map1").withResourceVersion("999").and().build()); assertNotNull(map); assertEquals("1001", map.getMetadata().getResourceVersion()); - ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(server.getLastRequest().getBody().inputStream()); + ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class) + .readValue(server.getLastRequest().getBody().inputStream()); assertEquals("1000", replacedMap.getMetadata().getResourceVersion()); } @Test @DisplayName("Should replace an existing ConfigMap without lock") void testReplaceWithoutLock() throws Exception { - server.expect().get().withPath("/api/v1/namespaces/test/configmaps/map1").andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() - .withNewMetadata().withResourceVersion("1000").and().build()).always(); + server.expect().get().withPath("/api/v1/namespaces/test/configmaps/map1") + .andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() + .withNewMetadata().withResourceVersion("1000").and().build()) + .always(); - server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1").andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() - .withNewMetadata().withResourceVersion("1001").and().build()).once(); + server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1") + .andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() + .withNewMetadata().withResourceVersion("1001").and().build()) + .once(); ConfigMap original = new ConfigMapBuilder() - .withNewMetadata().withName("map1").and().build(); + .withNewMetadata().withName("map1").and().build(); ConfigMap map = client.configMaps().withName("map1").replace(original); assertNotNull(map); assertEquals("1001", map.getMetadata().getResourceVersion()); assertNull(original.getMetadata().getResourceVersion()); - ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(server.getLastRequest().getBody().inputStream()); + ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class) + .readValue(server.getLastRequest().getBody().inputStream()); assertEquals("1000", replacedMap.getMetadata().getResourceVersion()); } @Test @DisplayName("Should replace an existing resource with lock") void testReplaceWithLock() throws Exception { - server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1").andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() - .withNewMetadata().withResourceVersion("1001").and().build()).once(); + server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1") + .andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder() + .withNewMetadata().withResourceVersion("1001").and().build()) + .once(); ConfigMap map = client.configMaps().withName("map1") - .lockResourceVersion("900") - .replace(new ConfigMapBuilder().withNewMetadata().withName("map1").and().build()); + .lockResourceVersion("900") + .replace(new ConfigMapBuilder().withNewMetadata().withName("map1").and().build()); assertNotNull(map); assertEquals("1001", map.getMetadata().getResourceVersion()); - ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(server.getLastRequest().getBody().inputStream()); + ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class) + .readValue(server.getLastRequest().getBody().inputStream()); assertEquals("900", replacedMap.getMetadata().getResourceVersion()); } @@ -280,16 +314,17 @@ void testReplaceWithLock() throws Exception { @DisplayName("Should delete an existing resource with lock") void testDeleteWithLock() throws Exception { server.expect().delete() - .withPath("/api/v1/namespaces/test/configmaps/map1") - .andReturn(HttpURLConnection.HTTP_OK, null) - .once(); + .withPath("/api/v1/namespaces/test/configmaps/map1") + .andReturn(HttpURLConnection.HTTP_OK, null) + .once(); Boolean deleted = client.configMaps().withName("map1") - .lockResourceVersion("800") - .delete(); + .lockResourceVersion("800") + .delete(); assertTrue(deleted); - DeleteOptions options = new ObjectMapper().readerFor(DeleteOptions.class).readValue(server.getLastRequest().getBody().inputStream()); + DeleteOptions options = new ObjectMapper().readerFor(DeleteOptions.class) + .readValue(server.getLastRequest().getBody().inputStream()); assertEquals("800", options.getPreconditions().getResourceVersion()); } diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentCrudTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentCrudTest.java index cfc942add60..879ab450cb2 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentCrudTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/DeploymentCrudTest.java @@ -20,6 +20,8 @@ import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; import io.fabric8.kubernetes.api.model.apps.DeploymentList; import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable; +import io.fabric8.kubernetes.client.dsl.RollableScalableResource; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -40,31 +42,31 @@ class DeploymentCrudTest { void testCrud() { Deployment deployment1 = new DeploymentBuilder().withNewMetadata() - .withName("deployment1") - .withNamespace("ns1") - .addToLabels("testKey", "testValue") - .endMetadata() - .withNewSpec() - .endSpec() - .build(); + .withName("deployment1") + .withNamespace("ns1") + .addToLabels("testKey", "testValue") + .endMetadata() + .withNewSpec() + .endSpec() + .build(); Deployment deployment2 = new DeploymentBuilder().withNewMetadata() - .withName("deployment2") - .withNamespace("ns1") - .addToLabels("testKey", "testValue") - .endMetadata() - .withNewSpec() - .endSpec() - .build(); + .withName("deployment2") + .withNamespace("ns1") + .addToLabels("testKey", "testValue") + .endMetadata() + .withNewSpec() + .endSpec() + .build(); Deployment deployment3 = new DeploymentBuilder().withNewMetadata() - .withName("deployment3") - .addToLabels("testKey", "testValue") - .withNamespace("ns2") - .endMetadata() - .withNewSpec() - .endSpec() - .build(); + .withName("deployment3") + .addToLabels("testKey", "testValue") + .withNamespace("ns2") + .endMetadata() + .withNewSpec() + .endSpec() + .build(); client.apps().deployments().inNamespace("ns1").create(deployment1); client.apps().deployments().inNamespace("ns1").create(deployment2); @@ -78,23 +80,23 @@ void testCrud() { assertNotNull(aDeploymentList); assertEquals(2, aDeploymentList.getItems().size()); - aDeploymentList = client.apps() - .deployments() - .inAnyNamespace() - .withLabels(Collections.singletonMap("testKey", "testValue")) - .list(); + FilterWatchListDeletable> withLabels = client.apps() + .deployments() + .inAnyNamespace() + .withLabels(Collections.singletonMap("testKey", "testValue")); + aDeploymentList = withLabels.list(); assertNotNull(aDeploymentList); assertEquals(3, aDeploymentList.getItems().size()); + assertEquals(3, withLabels.resources().count()); boolean bDeleted = client.apps().deployments().inNamespace("ns2").withName("deployment3").delete(); assertTrue(bDeleted); - deployment2 = client.apps().deployments() - .inNamespace("ns1").withName("deployment2").edit(d -> new DeploymentBuilder(d) - .editMetadata().addToLabels("key1", "value1").endMetadata() - .build()); + .inNamespace("ns1").withName("deployment2").edit(d -> new DeploymentBuilder(d) + .editMetadata().addToLabels("key1", "value1").endMetadata() + .build()); assertNotNull(deployment2); assertEquals("value1", deployment2.getMetadata().getLabels().get("key1")); @@ -105,13 +107,13 @@ void testCrud() { void testReplace() { // Given Deployment deployment1 = new DeploymentBuilder().withNewMetadata() - .withName("d1") - .withNamespace("ns1") - .addToLabels("testKey", "testValue") - .endMetadata() - .withNewSpec() - .endSpec() - .build(); + .withName("d1") + .withNamespace("ns1") + .addToLabels("testKey", "testValue") + .endMetadata() + .withNewSpec() + .endSpec() + .build(); deployment1 = client.apps().deployments().inNamespace("ns1").create(deployment1); // When @@ -125,4 +127,3 @@ void testReplace() { assertEquals("one", replacedDeployment.getMetadata().getLabels().get("testKey")); } } - diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/MetadataCrudTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/MetadataCrudTest.java index e64ff909792..41ab4ba2229 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/MetadataCrudTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/MetadataCrudTest.java @@ -40,7 +40,7 @@ void testResourceVersion() { Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").addToLabels("testKey", "testValue").endMetadata().build(); Pod pod2 = new PodBuilder().withNewMetadata().withName("pod2").addToLabels("testKey", "testValue").endMetadata().build(); - NonNamespaceOperation> podClient = client.pods().inNamespace("ns1"); + NonNamespaceOperation podClient = client.pods().inNamespace("ns1"); pod1 = podClient.create(pod1); pod2 = podClient.create(pod2); @@ -55,7 +55,7 @@ void testResourceVersion() { assertEquals("1", pod1.getMetadata().getResourceVersion()); // should increment - pod1 = podClient.withName("pod1").editStatus(p->new PodBuilder(p).withStatus(new PodStatus()).build()); + pod1 = podClient.withName("pod1").editStatus(p -> new PodBuilder(p).withStatus(new PodStatus()).build()); assertEquals("3", pod1.getMetadata().getResourceVersion()); } diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodTest.java index 2ce6d6fb459..4eecea91b5a 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/PodTest.java @@ -88,16 +88,16 @@ void setUp() { @Test void testList() { - server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().build()).once(); - server.expect().withPath("/api/v1/namespaces/ns1/pods").andReturn(200, new PodListBuilder() - .addNewItem().and() - .addNewItem().and().build()).once(); + server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().build()).once(); + server.expect().withPath("/api/v1/namespaces/ns1/pods").andReturn(200, new PodListBuilder() + .addNewItem().and() + .addNewItem().and().build()).once(); - server.expect().withPath("/api/v1/pods").andReturn(200, new PodListBuilder() - .addNewItem().and() - .addNewItem().and() - .addNewItem() - .and().build()).once(); + server.expect().withPath("/api/v1/pods").andReturn(200, new PodListBuilder() + .addNewItem().and() + .addNewItem().and() + .addNewItem() + .and().build()).once(); PodList podList = client.pods().list(); assertNotNull(podList); @@ -114,27 +114,30 @@ void testList() { @Test void testListWithLabels() { - server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3=value3")).andReturn(200, new PodListBuilder().build()).always(); - server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2")).andReturn(200, new PodListBuilder() - .addNewItem().and() - .addNewItem().and() - .addNewItem().and() - .build()).once(); + server.expect() + .withPath("/api/v1/namespaces/test/pods?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3=value3")) + .andReturn(200, new PodListBuilder().build()).always(); + server.expect().withPath("/api/v1/namespaces/test/pods?labelSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2")) + .andReturn(200, new PodListBuilder() + .addNewItem().and() + .addNewItem().and() + .addNewItem().and() + .build()) + .once(); PodList podList = client.pods() - .withLabel("key1", "value1") - .withLabel("key2","value2") - .withLabel("key3","value3") - .list(); - + .withLabel("key1", "value1") + .withLabel("key2", "value2") + .withLabel("key3", "value3") + .list(); assertNotNull(podList); assertEquals(0, podList.getItems().size()); podList = client.pods() - .withLabel("key1", "value1") - .withLabel("key2","value2") - .list(); + .withLabel("key1", "value1") + .withLabel("key2", "value2") + .list(); assertNotNull(podList); assertEquals(3, podList.getItems().size()); @@ -142,30 +145,32 @@ void testListWithLabels() { @Test void testListWithFields() { - server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3!=value3,key3!=value4")).andReturn(200, new PodListBuilder() - .addNewItem().and() - .addNewItem().and() - .build()).once(); + server.expect().withPath( + "/api/v1/namespaces/test/pods?fieldSelector=" + Utils.toUrlEncoded("key1=value1,key2=value2,key3!=value3,key3!=value4")) + .andReturn(200, new PodListBuilder() + .addNewItem().and() + .addNewItem().and() + .build()) + .once(); PodList podList = client.pods() - .withField("key1", "value1") - .withField("key2","value2") - .withoutField("key3","value3") - .withoutField("key3", "value4") - .list(); + .withField("key1", "value1") + .withField("key2", "value2") + .withoutField("key3", "value3") + .withoutField("key3", "value4") + .list(); assertNotNull(podList); assertEquals(2, podList.getItems().size()); } - @Test void testEditMissing() { // Given server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(404, "error message from kubernetes").always(); // When - PodResource podOp = client.pods().withName("pod1"); + PodResource podOp = client.pods().withName("pod1"); // Then Assertions.assertThrows(KubernetesClientException.class, () -> podOp.edit(p -> p)); @@ -173,9 +178,8 @@ void testEditMissing() { @Test void testDelete() { - server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, new PodBuilder().build()).once(); - server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2").andReturn(200, new PodBuilder().build()).once(); - + server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, new PodBuilder().build()).once(); + server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2").andReturn(200, new PodBuilder().build()).once(); Boolean deleted = client.pods().withName("pod1").delete(); assertTrue(deleted); @@ -193,8 +197,8 @@ void testDeleteMulti() { Pod pod2 = new PodBuilder().withNewMetadata().withName("pod2").withNamespace("ns1").and().build(); Pod pod3 = new PodBuilder().withNewMetadata().withName("pod3").withNamespace("any").and().build(); - server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, pod1).once(); - server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2").andReturn(200, pod2).once(); + server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, pod1).once(); + server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2").andReturn(200, pod2).once(); Boolean deleted = client.pods().inAnyNamespace().delete(pod1, pod2); assertTrue(deleted); @@ -209,7 +213,7 @@ void testDeleteWithNamespaceMismatch() { Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); // When + Then - NonNamespaceOperation> podOp = client.pods().inNamespace("test1"); + NonNamespaceOperation podOp = client.pods().inNamespace("test1"); assertFalse(podOp.delete(pod1)); } @@ -218,7 +222,8 @@ void testDeleteWithPropagationPolicy() { Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); server.expect().withPath("/api/v1/namespaces/test/pods/pod1").andReturn(200, pod1).once(); - Boolean deleted = client.pods().inNamespace("test").withName("pod1").withPropagationPolicy(DeletionPropagation.FOREGROUND).delete(); + Boolean deleted = client.pods().inNamespace("test").withName("pod1").withPropagationPolicy(DeletionPropagation.FOREGROUND) + .delete(); assertTrue(deleted); } @@ -226,7 +231,8 @@ void testDeleteWithPropagationPolicy() { void testEvict() { server.expect().withPath("/api/v1/namespaces/test/pods/pod1/eviction").andReturn(200, new PodBuilder().build()).once(); server.expect().withPath("/api/v1/namespaces/ns1/pods/pod2/eviction").andReturn(200, new PodBuilder().build()).once(); - server.expect().withPath("/api/v1/namespaces/ns1/pods/pod3/eviction").andReturn(PodOperationsImpl.HTTP_TOO_MANY_REQUESTS, new PodBuilder().build()).once(); + server.expect().withPath("/api/v1/namespaces/ns1/pods/pod3/eviction") + .andReturn(PodOperationsImpl.HTTP_TOO_MANY_REQUESTS, new PodBuilder().build()).once(); server.expect().withPath("/api/v1/namespaces/ns1/pods/pod3/eviction").andReturn(200, new PodBuilder().build()).once(); server.expect().withPath("/api/v1/namespaces/ns1/pods/pod4/eviction").andReturn(500, new PodBuilder().build()).once(); @@ -234,7 +240,7 @@ void testEvict() { assertTrue(deleted); // not found - PodResource podResource = client.pods().withName("pod2"); + PodResource podResource = client.pods().withName("pod2"); assertThrows(KubernetesClientException.class, () -> podResource.evict()); deleted = client.pods().inNamespace("ns1").withName("pod2").evict(); @@ -248,7 +254,7 @@ void testEvict() { assertTrue(deleted); // unhandled error - PodResource resource = client.pods().inNamespace("ns1").withName("pod4"); + PodResource resource = client.pods().inNamespace("ns1").withName("pod4"); assertThrows(KubernetesClientException.class, resource::evict); } @@ -256,9 +262,9 @@ void testEvict() { void testEvictWithPolicyV1Eviction() { // Given server.expect().post() - .withPath("/api/v1/namespaces/ns1/pods/foo/eviction") - .andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().build()) - .once(); + .withPath("/api/v1/namespaces/ns1/pods/foo/eviction") + .andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().build()) + .once(); // When boolean evicted = client.pods().inNamespace("ns1").withName("foo").evict(new EvictionBuilder() @@ -267,7 +273,7 @@ void testEvictWithPolicyV1Eviction() { .withNamespace("ns1") .endMetadata() .withDeleteOptions(new DeleteOptionsBuilder().build()) - .build()); + .build()); // Then assertTrue(evicted); @@ -277,7 +283,7 @@ void testEvictWithPolicyV1Eviction() { void testCreateWithNameMismatch() { Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); - PodResource podOp = client.pods().inNamespace("test1").withName("mypod1"); + PodResource podOp = client.pods().inNamespace("test1").withName("mypod1"); Assertions.assertThrows(KubernetesClientException.class, () -> podOp.create(pod1)); } @@ -288,12 +294,16 @@ void testGetLog() { String pod3Log = "pod3Log"; String pod4Log = "pod4Log"; - server.expect().withPath("/api/v1/namespaces/test/pods/pod1/log?pretty=true").andReturn(200, pod1Log).once(); - server.expect().withPath("/api/v1/namespaces/test/pods/pod2/log?pretty=false").andReturn(200, pod2Log).once(); - server.expect().withPath("/api/v1/namespaces/test/pods/pod3/log?pretty=false&container=cnt3").andReturn(200, pod3Log).once(); - server.expect().withPath("/api/v1/namespaces/test4/pods/pod4/log?pretty=true&container=cnt4").andReturn(200, pod4Log).once(); - server.expect().withPath("/api/v1/namespaces/test4/pods/pod1/log?pretty=false&limitBytes=100").andReturn(200, pod1Log).once(); - server.expect().withPath("/api/v1/namespaces/test5/pods/pod1/log?pretty=false&tailLines=1×tamps=true").andReturn(200, pod1Log).once(); + server.expect().withPath("/api/v1/namespaces/test/pods/pod1/log?pretty=true").andReturn(200, pod1Log).once(); + server.expect().withPath("/api/v1/namespaces/test/pods/pod2/log?pretty=false").andReturn(200, pod2Log).once(); + server.expect().withPath("/api/v1/namespaces/test/pods/pod3/log?pretty=false&container=cnt3").andReturn(200, pod3Log) + .once(); + server.expect().withPath("/api/v1/namespaces/test4/pods/pod4/log?pretty=true&container=cnt4").andReturn(200, pod4Log) + .once(); + server.expect().withPath("/api/v1/namespaces/test4/pods/pod1/log?pretty=false&limitBytes=100").andReturn(200, pod1Log) + .once(); + server.expect().withPath("/api/v1/namespaces/test5/pods/pod1/log?pretty=false&tailLines=1×tamps=true") + .andReturn(200, pod1Log).once(); String log = client.pods().withName("pod1").getLog(true); assertEquals(pod1Log, log); @@ -318,10 +328,10 @@ void testGetLog() { void testExec() throws InterruptedException { String expectedOutput = "file1 file2"; server.expect().withPath("/api/v1/namespaces/test/pods/pod1/exec?command=ls&stdout=true") - .andUpgradeToWebSocket() - .open(new OutputStreamMessage(expectedOutput)) - .done() - .always(); + .andUpgradeToWebSocket() + .open(new OutputStreamMessage(expectedOutput)) + .done() + .always(); final CountDownLatch execLatch = new CountDownLatch(1); ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -344,30 +354,29 @@ public void onClose(int code, String reason) { watch.close(); } - @Test void testWatch() throws InterruptedException { // Given //We start with a list Pod pod1 = new PodBuilder() - .withNewMetadata() - .withName("pod1") - .withResourceVersion("1") - .endMetadata() - .build(); + .withNewMetadata() + .withName("pod1") + .withResourceVersion("1") + .endMetadata() + .build(); server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder() - .withNewMetadata() - .withResourceVersion("1") - .endMetadata() - .addToItems(pod1) - .build() - ).once(); - server.expect().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&allowWatchBookmarks=true&watch=true") - .andUpgradeToWebSocket() - .open() - .waitFor(50).andEmit(new WatchEvent(pod1, "DELETED")) - .done() - .always(); + .withNewMetadata() + .withResourceVersion("1") + .endMetadata() + .addToItems(pod1) + .build()).once(); + server.expect() + .withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&allowWatchBookmarks=true&watch=true") + .andUpgradeToWebSocket() + .open() + .waitFor(50).andEmit(new WatchEvent(pod1, "DELETED")) + .done() + .always(); final CountDownLatch deleteLatch = new CountDownLatch(1); final Watcher watcher = new Watcher() { @Override @@ -389,11 +398,10 @@ public void onClose(WatcherException cause) { watch.close(); } - @Test void testGetLogNotFound() { // Given - PodResource podOp = client.pods().withName("pod5"); + PodResource podOp = client.pods().withName("pod5"); // When + Then Assertions.assertThrows(KubernetesClientException.class, () -> podOp.getLog(true)); @@ -408,39 +416,41 @@ void testLoad() { @Test void testWait() throws InterruptedException { Pod notReady = new PodBuilder() - .withNewMetadata() - .withName("pod1") - .withResourceVersion("1") - .withNamespace("test") - .endMetadata() - .withNewStatus() - .addNewCondition() - .withType("Ready") - .withStatus("False") - .endCondition() - .endStatus() - .build(); - + .withNewMetadata() + .withName("pod1") + .withResourceVersion("1") + .withNamespace("test") + .endMetadata() + .withNewStatus() + .addNewCondition() + .withType("Ready") + .withStatus("False") + .endCondition() + .endStatus() + .build(); Pod ready = new PodBuilder(notReady) - .editMetadata() - .withResourceVersion("2") - .endMetadata() - .withNewStatus() - .addNewCondition() - .withType("Ready") - .withStatus("True") - .endCondition() - .endStatus() - .build(); - - server.expect().get().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1").andReturn(200, notReady).once(); - - server.expect().get().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket() - .open() - .waitFor(50).andEmit(new WatchEvent(ready, "MODIFIED")) - .done() - .always(); + .editMetadata() + .withResourceVersion("2") + .endMetadata() + .withNewStatus() + .addNewCondition() + .withType("Ready") + .withStatus("True") + .endCondition() + .endStatus() + .build(); + + server.expect().get().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1").andReturn(200, notReady) + .once(); + + server.expect().get().withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + .andUpgradeToWebSocket() + .open() + .waitFor(50).andEmit(new WatchEvent(ready, "MODIFIED")) + .done() + .always(); Pod result = client.pods().withName("pod1").waitUntilReady(5, TimeUnit.SECONDS); Assert.assertEquals("2", result.getMetadata().getResourceVersion()); @@ -449,19 +459,18 @@ void testWait() throws InterruptedException { @Test void testPortForward() throws IOException { server.expect().withPath("/api/v1/namespaces/test/pods/pod1/portforward?ports=123") - .andUpgradeToWebSocket() - .open() - .waitFor(10).andEmit(portForwardEncode(true, "12")) // data channel info - .waitFor(10).andEmit(portForwardEncode(false, "12")) // error channel info - .waitFor(10).andEmit(portForwardEncode(true, "Hell")) - .waitFor(10).andEmit(portForwardEncode(true, "o World")) - .done() - .once(); - - try( - LocalPortForward portForward = client.pods().withName("pod1").portForward(123); - SocketChannel channel = SocketChannel.open() - ) { + .andUpgradeToWebSocket() + .open() + .waitFor(10).andEmit(portForwardEncode(true, "12")) // data channel info + .waitFor(10).andEmit(portForwardEncode(false, "12")) // error channel info + .waitFor(10).andEmit(portForwardEncode(true, "Hell")) + .waitFor(10).andEmit(portForwardEncode(true, "o World")) + .done() + .once(); + + try ( + LocalPortForward portForward = client.pods().withName("pod1").portForward(123); + SocketChannel channel = SocketChannel.open()) { int localPort = portForward.getLocalPort(); assertTrue(channel.connect(new InetSocketAddress("localhost", localPort))); @@ -479,7 +488,7 @@ void testPortForward() throws IOException { } read = -1; } - } while(read >= 0); + } while (read >= 0); buffer.flip(); channel.socket().close(); assertEquals("Hello World", ByteString.of(buffer).utf8()); @@ -493,14 +502,14 @@ void testPortForward() throws IOException { void testPortForwardWithChannel() throws InterruptedException, IOException { server.expect().withPath("/api/v1/namespaces/test/pods/pod1/portforward?ports=123") - .andUpgradeToWebSocket() - .open() - .waitFor(10).andEmit(portForwardEncode(true, "12")) // data channel info - .waitFor(10).andEmit(portForwardEncode(false, "12")) // error channel info - .waitFor(10).andEmit(portForwardEncode(true, "Hell")) - .waitFor(10).andEmit(portForwardEncode(true, "o World!")) - .done() - .once(); + .andUpgradeToWebSocket() + .open() + .waitFor(10).andEmit(portForwardEncode(true, "12")) // data channel info + .waitFor(10).andEmit(portForwardEncode(false, "12")) // error channel info + .waitFor(10).andEmit(portForwardEncode(true, "Hell")) + .waitFor(10).andEmit(portForwardEncode(true, "o World!")) + .done() + .once(); ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]); ReadableByteChannel inChannel = Channels.newChannel(in); @@ -508,8 +517,8 @@ void testPortForwardWithChannel() throws InterruptedException, IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); WritableByteChannel outChannel = Channels.newChannel(out); - try(PortForward portForward = client.pods().withName("pod1").portForward(123, inChannel, outChannel)) { - while(portForward.isAlive()) { + try (PortForward portForward = client.pods().withName("pod1").portForward(123, inChannel, outChannel)) { + while (portForward.isAlive()) { Thread.sleep(100); } } @@ -542,27 +551,27 @@ void testOptionalCopyDir() { @Test void testListFromServer() { PodBuilder podBuilder = new PodBuilder() - .withNewMetadata() + .withNewMetadata() .withNamespace("test") .withName("pod1") - .endMetadata(); + .endMetadata(); Pod clientPod = podBuilder.build(); Pod serverPod = podBuilder - .editMetadata() + .editMetadata() .withResourceVersion("1") - .endMetadata() - .withNewStatus() + .endMetadata() + .withNewStatus() .addNewCondition() - .withType("Ready") - .withStatus("True") + .withType("Ready") + .withStatus("True") .endCondition() - .endStatus() - .build(); + .endStatus() + .build(); server.expect().get() - .withPath("/api/v1/namespaces/test/pods/pod1") - .andReturn(200, serverPod).once(); + .withPath("/api/v1/namespaces/test/pods/pod1") + .andReturn(200, serverPod).once(); List resources = client.resourceList(clientPod).fromServer().get(); diff --git a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java index 0e0b2beb10a..1b6f02cd96c 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/kubernetes/client/mock/ResourceTest.java @@ -70,7 +70,11 @@ class ResourceTest { void testCreateOrReplace() { // Given Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); - server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, pod1).once(); + server.expect() + .post() + .withPath("/api/v1/namespaces/test/pods") + .andReturn(HttpURLConnection.HTTP_CREATED, pod1) + .once(); // When HasMetadata response = client.resource(pod1).createOrReplace(); @@ -83,7 +87,11 @@ void testCreateOrReplace() { void testCreateOrReplaceString() { // Given Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); - server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, pod1).once(); + server.expect() + .post() + .withPath("/api/v1/namespaces/test/pods") + .andReturn(HttpURLConnection.HTTP_CREATED, pod1) + .once(); // When HasMetadata response = client.resource(Serialization.asYaml(pod1)).createOrReplace(); @@ -94,17 +102,22 @@ void testCreateOrReplaceString() { @Test void testGenericResourceFails() { - assertThrows(KubernetesClientException.class, () -> client.resource(Serialization.unmarshal("apiVersion: example.io/v1\n" - + "kind: GenericThatFails\n" - + "metadata:\n" - + " name: failure\n", GenericKubernetesResource.class))); + assertThrows(KubernetesClientException.class, + () -> client.resource(Serialization.unmarshal("apiVersion: example.io/v1\n" + + "kind: GenericThatFails\n" + + "metadata:\n" + + " name: failure\n", GenericKubernetesResource.class))); } @Test void testCreateOrReplaceWhenCreateFails() { // Given Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); - server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_BAD_REQUEST, pod1).once(); + server.expect() + .post() + .withPath("/api/v1/namespaces/test/pods") + .andReturn(HttpURLConnection.HTTP_BAD_REQUEST, pod1) + .once(); NamespaceableResource podOperation = client.resource(pod1); @@ -116,7 +129,11 @@ void testCreateOrReplaceWhenCreateFails() { void testCreateWithExplicitNamespace() { Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); - server.expect().post().withPath("/api/v1/namespaces/ns1/pods").andReturn(HttpURLConnection.HTTP_CREATED, pod1).once(); + server.expect() + .post() + .withPath("/api/v1/namespaces/ns1/pods") + .andReturn(HttpURLConnection.HTTP_CREATED, pod1) + .once(); HasMetadata response = client.resource(pod1).inNamespace("ns1").createOrReplace(); assertEquals(pod1, response); @@ -126,8 +143,16 @@ void testCreateWithExplicitNamespace() { void testCreateOrReplaceWithDeleteExisting() throws Exception { Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); - server.expect().delete().withPath("/api/v1/namespaces/ns1/pods/pod1").andReturn(HttpURLConnection.HTTP_OK, pod1).once(); - server.expect().post().withPath("/api/v1/namespaces/ns1/pods").andReturn(HttpURLConnection.HTTP_CREATED, pod1).once(); + server.expect() + .delete() + .withPath("/api/v1/namespaces/ns1/pods/pod1") + .andReturn(HttpURLConnection.HTTP_OK, pod1) + .once(); + server.expect() + .post() + .withPath("/api/v1/namespaces/ns1/pods") + .andReturn(HttpURLConnection.HTTP_CREATED, pod1) + .once(); Resource resource = client.resource(pod1).inNamespace("ns1"); resource.delete(); @@ -144,8 +169,16 @@ void testCreateOrReplaceWithDeleteExisting() throws Exception { void itPassesPropagationPolicyWithDeleteExisting() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); - server.expect().delete().withPath("/api/v1/namespaces/ns1/pods/pod1").andReturn(HttpURLConnection.HTTP_OK, pod1).once(); - server.expect().post().withPath("/api/v1/namespaces/ns1/pods").andReturn(HttpURLConnection.HTTP_CREATED, pod1).once(); + server.expect() + .delete() + .withPath("/api/v1/namespaces/ns1/pods/pod1") + .andReturn(HttpURLConnection.HTTP_OK, pod1) + .once(); + server.expect() + .post() + .withPath("/api/v1/namespaces/ns1/pods") + .andReturn(HttpURLConnection.HTTP_CREATED, pod1) + .once(); Resource resource = client.resource(pod1).inNamespace("ns1"); resource.withPropagationPolicy(DeletionPropagation.FOREGROUND).delete(); @@ -169,9 +202,21 @@ void itPassesPropagationPolicyWithDeleteExisting() throws InterruptedException { void testCreateOrReplaceWithDeleteExistingWithCreateFailed() { // Given Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); - server.expect().post().withPath("/api/v1/namespaces/ns1/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, pod1).once(); - server.expect().delete().withPath("/api/v1/namespaces/ns1/pods/pod1").andReturn(HttpURLConnection.HTTP_OK, pod1).once(); - server.expect().post().withPath("/api/v1/namespaces/ns1/pods").andReturn(HttpURLConnection.HTTP_BAD_REQUEST, pod1).once(); + server.expect() + .post() + .withPath("/api/v1/namespaces/ns1/pods") + .andReturn(HttpURLConnection.HTTP_CONFLICT, pod1) + .once(); + server.expect() + .delete() + .withPath("/api/v1/namespaces/ns1/pods/pod1") + .andReturn(HttpURLConnection.HTTP_OK, pod1) + .once(); + server.expect() + .post() + .withPath("/api/v1/namespaces/ns1/pods") + .andReturn(HttpURLConnection.HTTP_BAD_REQUEST, pod1) + .once(); Resource podOperation = client.resource(pod1).inNamespace("ns1"); podOperation.delete(); // When @@ -180,8 +225,12 @@ void testCreateOrReplaceWithDeleteExistingWithCreateFailed() { @Test void testRequire() { - server.expect().get().withPath("/api/v1/namespaces/ns1/pods/pod1").andReturn(HttpURLConnection.HTTP_NOT_FOUND, "").once(); - PodResource podOp = client.pods().inNamespace("ns1").withName("pod1"); + server.expect() + .get() + .withPath("/api/v1/namespaces/ns1/pods/pod1") + .andReturn(HttpURLConnection.HTTP_NOT_FOUND, "") + .once(); + PodResource podOp = client.pods().inNamespace("ns1").withName("pod1"); Assertions.assertThrows(ResourceNotFoundException.class, podOp::require); } @@ -209,16 +258,20 @@ void testWatch() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); server.expect().get().withPath("/api/v1/namespaces/test/pods").andReturn(200, pod1).once(); server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(201, pod1).once(); - server.expect().get() + server.expect() + .get() .withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(1000).andEmit(new WatchEvent(pod1, "DELETED")) + .waitFor(1000) + .andEmit(new WatchEvent(pod1, "DELETED")) .done() .always(); @@ -244,18 +297,23 @@ void testWaitUntilReady() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); Pod noReady = createReadyFrom(pod1, "False", "2"); Pod ready = createReadyFrom(pod1, "True", "3"); list(noReady); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(500).andEmit(new WatchEvent(ready, "MODIFIED")) + .waitFor(500) + .andEmit(new WatchEvent(ready, "MODIFIED")) .done() .always(); @@ -282,20 +340,28 @@ void testWaitUntilExistsThenReady() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); Pod noReady = createReadyFrom(pod1, "False", "1"); Pod ready = createReadyFrom(pod1, "True", "2"); // and again so that "periodicWatchUntilReady" successfully begins - server.expect().get().withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1").andReturn(200, noReady) + server.expect() + .get() + .withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1") + .andReturn(200, noReady) .times(2); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(100).andEmit(new WatchEvent(ready, "MODIFIED")) + .waitFor(100) + .andEmit(new WatchEvent(ready, "MODIFIED")) .done() .always(); @@ -308,12 +374,16 @@ void testWaitUntilCondition() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); Pod noReady = createReadyFrom(pod1, "False", "1"); Pod ready = createReadyFrom(pod1, "True", "3"); - Pod withConditionBeingFalse = new PodBuilder(pod1).editMetadata().withResourceVersion("2").endMetadata() + Pod withConditionBeingFalse = new PodBuilder(pod1).editMetadata() + .withResourceVersion("2") + .endMetadata() .withNewStatus() .addNewCondition() .withType("Ready") @@ -326,7 +396,9 @@ void testWaitUntilCondition() throws InterruptedException { .endStatus() .build(); - Pod withConditionBeingTrue = new PodBuilder(pod1).editMetadata().withResourceVersion("4").endMetadata() + Pod withConditionBeingTrue = new PodBuilder(pod1).editMetadata() + .withResourceVersion("4") + .endMetadata() .withNewStatus() .addNewCondition() .withType("Ready") @@ -342,21 +414,29 @@ void testWaitUntilCondition() throws InterruptedException { // at first the pod is non-ready list(noReady); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(1000).andEmit(new WatchEvent(ready, "MODIFIED")) - .waitFor(2000).andEmit(new WatchEvent(withConditionBeingFalse, "MODIFIED")) - .waitFor(2500).andEmit(new WatchEvent(withConditionBeingTrue, "MODIFIED")) + .waitFor(1000) + .andEmit(new WatchEvent(ready, "MODIFIED")) + .waitFor(2000) + .andEmit(new WatchEvent(withConditionBeingFalse, "MODIFIED")) + .waitFor(2500) + .andEmit(new WatchEvent(withConditionBeingTrue, "MODIFIED")) .done() .always(); - Pod p = client.pods().withName("pod1").waitUntilCondition( - r -> r.getStatus().getConditions() - .stream() - .anyMatch(c -> "Dummy".equals(c.getType()) && "True".equals(c.getStatus())), - 8, SECONDS); + Pod p = client.pods() + .withName("pod1") + .waitUntilCondition( + r -> r.getStatus() + .getConditions() + .stream() + .anyMatch(c -> "Dummy".equals(c.getType()) && "True".equals(c.getStatus())), + 8, SECONDS); assertThat(p.getStatus().getConditions()) .extracting("type", "status") @@ -368,7 +448,9 @@ void tesErrorEventDuringWaitReturnFromAPIIfMatch() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); Pod noReady = createReadyFrom(pod1, "False", "2"); Pod ready = createReadyFrom(pod1, "True", "3"); @@ -380,12 +462,16 @@ void tesErrorEventDuringWaitReturnFromAPIIfMatch() throws InterruptedException { // once not ready, to begin watch list(noReady); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(500).andEmit(new WatchEvent(status, "ERROR")) - .waitFor(500).andEmit(new WatchEvent(ready, "MODIFIED")) + .waitFor(500) + .andEmit(new WatchEvent(status, "ERROR")) + .waitFor(500) + .andEmit(new WatchEvent(ready, "MODIFIED")) .done() .once(); @@ -398,7 +484,9 @@ void testRetryOnErrorEventDuringWait() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); Pod noReady = createReadyFrom(pod1, "False", "2"); Pod ready = createReadyFrom(pod1, "True", "3"); @@ -410,19 +498,25 @@ void testRetryOnErrorEventDuringWait() throws InterruptedException { // once not ready, to begin watch list(noReady); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(500).andEmit(new WatchEvent(status, "ERROR")) + .waitFor(500) + .andEmit(new WatchEvent(status, "ERROR")) .done() .once(); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(500).andEmit(new WatchEvent(ready, "MODIFIED")) + .waitFor(500) + .andEmit(new WatchEvent(ready, "MODIFIED")) .done() .once(); @@ -435,7 +529,9 @@ void testSkipWatchIfAlreadyMatchingCondition() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); Pod noReady = createReadyFrom(pod1, "False", "2"); Pod ready = createReadyFrom(pod1, "True", "3"); @@ -452,7 +548,9 @@ void testRetryWatchOnHttpGone() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); Pod noReady = createReadyFrom(pod1, "False", "2"); Pod ready = createReadyFrom(pod1, "True", "3"); @@ -464,11 +562,14 @@ void testRetryWatchOnHttpGone() throws InterruptedException { // once not ready, to begin watch list(noReady); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(500).andEmit(new WatchEvent(status, "ERROR")) + .waitFor(500) + .andEmit(new WatchEvent(status, "ERROR")) .done() .once(); @@ -482,7 +583,9 @@ void testWaitOnConditionDeleted() throws InterruptedException { Pod ready = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().withNewStatus() + .withNamespace("test") + .and() + .withNewStatus() .addNewCondition() .withType("Ready") .withStatus("True") @@ -492,11 +595,14 @@ void testWaitOnConditionDeleted() throws InterruptedException { list(ready); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(1000).andEmit(new WatchEvent(ready, "DELETED")) + .waitFor(1000) + .andEmit(new WatchEvent(ready, "DELETED")) .done() .once(); @@ -509,7 +615,9 @@ void testCreateAndWaitUntilReady() throws InterruptedException { Pod pod1 = new PodBuilder().withNewMetadata() .withName("pod1") .withResourceVersion("1") - .withNamespace("test").and().build(); + .withNamespace("test") + .and() + .build(); Pod noReady = createReadyFrom(pod1, "False", "2"); Pod ready = createReadyFrom(pod1, "True", "3"); @@ -517,11 +625,14 @@ void testCreateAndWaitUntilReady() throws InterruptedException { list(noReady); server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(201, noReady).once(); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&allowWatchBookmarks=true&watch=true") .andUpgradeToWebSocket() .open() - .waitFor(1000).andEmit(new WatchEvent(ready, "MODIFIED")) + .waitFor(1000) + .andEmit(new WatchEvent(ready, "MODIFIED")) .done() .always(); @@ -555,7 +666,8 @@ void testFromServerWaitUntilConditionAlwaysGetsResourceFromServer() throws Excep .withNamespace("test") .withResourceVersion("1") .addToLabels("CONDITION", "NOT_MET") - .endMetadata().build(); + .endMetadata() + .build(); final Pod conditionMetPod = new PodBuilder().withNewMetadata() .withName("pod") .withNamespace("test") @@ -564,11 +676,16 @@ void testFromServerWaitUntilConditionAlwaysGetsResourceFromServer() throws Excep .endMetadata() .build(); list(conditionNotMetPod); - server.expect().get().withPath( - "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod&resourceVersion=1&allowWatchBookmarks=true&watch=true") - .andUpgradeToWebSocket().open() - .immediately().andEmit(new WatchEvent(conditionNotMetPod, "MODIFIED")) - .waitFor(10).andEmit(new WatchEvent(conditionMetPod, "MODIFIED")) + server.expect() + .get() + .withPath( + "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod&resourceVersion=1&allowWatchBookmarks=true&watch=true") + .andUpgradeToWebSocket() + .open() + .immediately() + .andEmit(new WatchEvent(conditionNotMetPod, "MODIFIED")) + .waitFor(10) + .andEmit(new WatchEvent(conditionMetPod, "MODIFIED")) .done() .once(); // When diff --git a/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/NamespacedItemTest.java b/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/NamespacedItemTest.java index 7a58d1a888f..ef775410ccd 100644 --- a/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/NamespacedItemTest.java +++ b/kubernetes-tests/src/test/java/io/fabric8/openshift/client/server/mock/NamespacedItemTest.java @@ -23,6 +23,7 @@ import io.fabric8.kubernetes.client.NamespacedKubernetesClient; import io.fabric8.kubernetes.client.dsl.MixedOperation; import io.fabric8.kubernetes.client.dsl.Resource; +import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.openshift.client.OpenShiftClient; import org.junit.jupiter.api.Test; @@ -30,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -@EnableOpenShiftMockClient(crud = true) +@EnableKubernetesMockClient(crud = true) class NamespacedItemTest { private OpenShiftClient client; @@ -40,7 +41,7 @@ class NamespacedItemTest { void testDefaultNamespace() { ConfigMap created = this.client .configMaps() - .withItem(configmap) + .resource(configmap) .create(); // should create in the item namespace rather than the default - no exception @@ -53,7 +54,7 @@ void testClientExplicitNamespace() { .inNamespace("explicit"); ConfigMap created = inNamespace .configMaps() - .withItem(configmap) + .resource(configmap) .create(); assertEquals("explicit", inNamespace.getNamespace()); @@ -67,7 +68,7 @@ void testOperationExplicitNamespace() { ConfigMap created = this.client .configMaps() .inNamespace("explicit") - .withItem(configmap) + .resource(configmap) .create(); // should create in the explicit, rather than the item - no exception @@ -76,7 +77,7 @@ void testOperationExplicitNamespace() { ConfigMap modified = this.client .configMaps() .inNamespace("explicit") - .withItem(configmap) + .resource(configmap) .get(); // should create in the explicit, rather than the item - no exception diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/apps/DeploymentConfigOperationsImpl.java b/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/apps/DeploymentConfigOperationsImpl.java index 2d9b93581f7..90bfb583268 100644 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/apps/DeploymentConfigOperationsImpl.java +++ b/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/apps/DeploymentConfigOperationsImpl.java @@ -259,15 +259,15 @@ public Loggable withLogWaitTimeout(Integer logWaitTimeout) { private void waitUntilDeploymentConfigPodBecomesReady(DeploymentConfig deploymentConfig) { Integer podLogWaitTimeout = rollingOperationContext.getLogWaitTimeout(); - List> podOps = PodOperationUtil.getPodOperationsForController(context, + List podOps = PodOperationUtil.getPodOperationsForController(context, deploymentConfig.getMetadata().getUid(), getDeploymentConfigPodLabels(deploymentConfig), false, podLogWaitTimeout, rollingOperationContext.getContainerId()); waitForBuildPodToBecomeReady(podOps, podLogWaitTimeout != null ? podLogWaitTimeout : DEFAULT_POD_LOG_WAIT_TIMEOUT); } - private static void waitForBuildPodToBecomeReady(List> podOps, Integer podLogWaitTimeout) { - for (PodResource podOp : podOps) { + private static void waitForBuildPodToBecomeReady(List podOps, Integer podLogWaitTimeout) { + for (PodResource podOp : podOps) { PodOperationUtil.waitUntilReadyBeforeFetchingLogs(podOp, podLogWaitTimeout); } } diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/build/BuildOperationsImpl.java b/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/build/BuildOperationsImpl.java index ceb4754690b..8b136fb9475 100644 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/build/BuildOperationsImpl.java +++ b/openshift-client/src/main/java/io/fabric8/openshift/client/dsl/internal/build/BuildOperationsImpl.java @@ -136,7 +136,7 @@ public String getLog(boolean isPretty) { /** * Returns an unclosed Reader. It's the caller responsibility to close it. - * + * * @return Reader */ @Override @@ -205,14 +205,14 @@ public BytesLimitTerminateTimeTailPrettyLoggable usingTimestamps() { } private void waitUntilBuildPodBecomesReady(Build build) { - List> podOps = PodOperationUtil.getPodOperationsForController(context, build.getMetadata().getUid(), + List podOps = PodOperationUtil.getPodOperationsForController(context, build.getMetadata().getUid(), getBuildPodLabels(build), withPrettyOutput, podLogWaitTimeout, null); waitForBuildPodToBecomeReady(podOps, podLogWaitTimeout != null ? podLogWaitTimeout : DEFAULT_POD_LOG_WAIT_TIMEOUT); } - private static void waitForBuildPodToBecomeReady(List> podOps, Integer podLogWaitTimeout) { - for (PodResource podOp : podOps) { + private static void waitForBuildPodToBecomeReady(List podOps, Integer podLogWaitTimeout) { + for (PodResource podOp : podOps) { PodOperationUtil.waitUntilReadyBeforeFetchingLogs(podOp, podLogWaitTimeout); } } diff --git a/openshift-client/src/main/java/io/fabric8/openshift/client/osgi/ManagedOpenShiftClient.java b/openshift-client/src/main/java/io/fabric8/openshift/client/osgi/ManagedOpenShiftClient.java index 6e515a70875..2d7008141a2 100644 --- a/openshift-client/src/main/java/io/fabric8/openshift/client/osgi/ManagedOpenShiftClient.java +++ b/openshift-client/src/main/java/io/fabric8/openshift/client/osgi/ManagedOpenShiftClient.java @@ -658,7 +658,7 @@ public MixedOperation> pods() { + public MixedOperation pods() { return delegate.pods(); }