Skip to content

Commit c66eb7c

Browse files
authored
fix fabric8io#4574 changing the behavior of get (fabric8io#4601)
1 parent fdbffd0 commit c66eb7c

File tree

116 files changed

+271
-237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+271
-237
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#### New Features
1212

1313
#### _**Note**_: Breaking changes
14+
* Fix #4574: fromServer has been deprecated - it no longer needs to be called. All get() operations will fetch the resource(s) from the api server. If you need the context item that was passed in from a resource, load, or resourceList methods, use the item or items method.
1415

1516
### 6.3.1 (2022-12-15)
1617

extensions/camel-k/tests/src/test/java/io/fabric8/camelk/test/KameletBindingTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void load() {
3939
// Given + When
4040
KameletBinding kameletBinding = client.v1alpha1().kameletBindings()
4141
.load(getClass().getResourceAsStream("/test-kameletbinding.yml"))
42-
.get();
42+
.item();
4343

4444
// Then
4545
assertThat(kameletBinding)

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/FromServerGettable.java

+6
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,11 @@
2020

2121
public interface FromServerGettable<T> extends Gettable<T> {
2222

23+
/**
24+
* @deprecated {@link #get()} will always return the latest resource from the server, there is no
25+
* need to call fromServer.
26+
*/
27+
@Deprecated
2328
Gettable<T> fromServer();
29+
2430
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Gettable.java

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
public interface Gettable<T> {
1919

2020
/**
21+
* Get the current state from the api server.
22+
*
23+
* See also {@link Resource#item()}
24+
*
2125
* @return the item or null if the item doesn't exist.
2226
* @throws io.fabric8.kubernetes.client.KubernetesClientException if an error occurs
2327
*/

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable.java

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package io.fabric8.kubernetes.client.dsl;
1818

19+
import io.fabric8.kubernetes.api.model.HasMetadata;
20+
21+
import java.util.List;
1922
import java.util.stream.Stream;
2023

2124
public interface NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<T> extends
@@ -26,4 +29,9 @@ public interface NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<T>
2629
@Override
2730
Stream<NamespaceableResource<T>> resources();
2831

32+
/**
33+
* Return the items used to create this list context - these values are not from the server.
34+
*/
35+
List<HasMetadata> items();
36+
2937
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Resource.java

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ default GracePeriodConfigurable<? extends Deletable> cascading(boolean enabled)
5858
boolean isReady();
5959

6060
/**
61+
* Perform a {@link Gettable#get()}, but throws an exception if the server resource does not exist.
62+
*
6163
* @return the item or throws an exception if the item doesn't exist.
6264
* @throws io.fabric8.kubernetes.client.KubernetesClientException if an error occurs
6365
* @throws io.fabric8.kubernetes.client.ResourceNotFoundException if resource is absent
@@ -68,4 +70,12 @@ default GracePeriodConfigurable<? extends Deletable> cascading(boolean enabled)
6870

6971
ReplaceDeletable<T> lockResourceVersion(String resourceVersion);
7072

73+
/**
74+
* Get the item used to create the current operation context if available.
75+
*
76+
* @return the current item if provided via the load, resource, or resourceList method, or null if this resource was created
77+
* just from a class.
78+
*/
79+
T item();
80+
7181
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ResourceAdapter.java

+4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ public T serverSideApply() {
326326
}
327327

328328
@Override
329+
public T item() {
330+
return resource.item();
331+
}
332+
329333
public DeletableWithOptions withTimeout(long timeout, TimeUnit unit) {
330334
return resource.withTimeout(timeout, unit);
331335
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperation.java

+35-29
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public class BaseOperation<T extends HasMetadata, L extends KubernetesResourceLi
105105
private final T item;
106106

107107
private final String resourceVersion;
108-
private final boolean reloadingFromServer;
109108
private final long gracePeriodSeconds;
110109
private final DeletionPropagation propagationPolicy;
111110

@@ -119,7 +118,6 @@ public class BaseOperation<T extends HasMetadata, L extends KubernetesResourceLi
119118
protected BaseOperation(OperationContext ctx) {
120119
super(ctx);
121120
this.item = (T) ctx.getItem();
122-
this.reloadingFromServer = ctx.isReloadingFromServer();
123121
this.resourceVersion = ctx.getResourceVersion();
124122
this.gracePeriodSeconds = ctx.getGracePeriodSeconds();
125123
this.propagationPolicy = ctx.getPropagationPolicy();
@@ -141,9 +139,7 @@ protected URL fetchListUrl(URL url, ListOptions listOptions) {
141139
@Override
142140
public T get() {
143141
try {
144-
final T answer = getMandatory();
145-
updateApiVersion(answer);
146-
return answer;
142+
return requireFromServer();
147143
} catch (KubernetesClientException e) {
148144
if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
149145
throw e;
@@ -155,23 +151,36 @@ public T get() {
155151
@Override
156152
public T require() {
157153
try {
158-
T answer = getMandatory();
159-
if (answer == null) {
160-
throw new ResourceNotFoundException("The resource you request doesn't exist or couldn't be fetched.");
161-
}
162-
return answer;
154+
return requireFromServer();
163155
} catch (KubernetesClientException e) {
164-
if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
165-
throw e;
166-
}
167-
throw new ResourceNotFoundException("Resource not found : " + e.getMessage(), e);
156+
throw new ResourceNotFoundException("Resource couldn't be fetched : " + e.getMessage(), e);
168157
}
169158
}
170159

171-
public T getMandatory() {
172-
if (item != null && !reloadingFromServer) {
160+
/**
161+
* Return the context item or retrieves the remote item
162+
*
163+
* @return
164+
*/
165+
public T getItemOrRequireFromServer() {
166+
if (item != null) {
173167
return Serialization.clone(item);
174168
}
169+
return requireFromServer();
170+
}
171+
172+
/**
173+
* Get the current item from the server
174+
* <br>
175+
* Will always return non-null or throw an exception.
176+
* <br>
177+
* Differs from {@link #require()} in that it does not throw a {@link ResourceNotFoundException} exception
178+
* which for some reason is not a {@link KubernetesClientException}
179+
*/
180+
protected T requireFromServer() {
181+
if (Utils.isNullOrEmpty(getName())) {
182+
throw new KubernetesClientException("name not specified for an operation requiring one.");
183+
}
175184
try {
176185
URL requestUrl = getCompleteResourceUrl();
177186
return handleGet(requestUrl);
@@ -282,7 +291,7 @@ public R load(String path) {
282291

283292
@Override
284293
public BaseOperation<T, L, R> fromServer() {
285-
return newInstance(context.withReloadingFromServer(true));
294+
return this;
286295
}
287296

288297
@Override
@@ -551,19 +560,17 @@ public T patchStatus(T item) {
551560

552561
@Override
553562
public T patchStatus() {
554-
// fromServer shouldn't be necessary here as we're using a merge patch, but
555-
// just in case that changes we want consistency with the other patch methods
556-
return this.fromServer().patchStatus(getNonNullItem());
563+
throw new KubernetesClientException(READ_ONLY_UPDATE_EXCEPTION_MESSAGE);
557564
}
558565

559566
@Override
560567
public T patch() {
561-
return this.fromServer().patch(getNonNullItem());
568+
throw new KubernetesClientException(READ_ONLY_UPDATE_EXCEPTION_MESSAGE);
562569
}
563570

564571
@Override
565572
public T patch(PatchContext patchContext) {
566-
return this.fromServer().patch(patchContext, getNonNullItem());
573+
throw new KubernetesClientException(READ_ONLY_UPDATE_EXCEPTION_MESSAGE);
567574
}
568575

569576
protected T getNonNullItem() {
@@ -748,12 +755,15 @@ private URL getCompleteResourceUrl() throws MalformedURLException {
748755
URL requestUrl = getNamespacedUrl(checkNamespace(item));
749756
if (name != null) {
750757
requestUrl = new URL(URLUtils.join(requestUrl.toString(), name));
751-
} else if (item != null && reloadingFromServer) {
752-
requestUrl = new URL(URLUtils.join(requestUrl.toString(), checkName(item)));
753758
}
754759
return requestUrl;
755760
}
756761

762+
@Override
763+
public T item() {
764+
return getItem();
765+
}
766+
757767
@Override
758768
public final T getItem() {
759769
return item;
@@ -763,10 +773,6 @@ public String getResourceVersion() {
763773
return resourceVersion;
764774
}
765775

766-
public Boolean isReloadingFromServer() {
767-
return reloadingFromServer;
768-
}
769-
770776
public Long getGracePeriodSeconds() {
771777
return gracePeriodSeconds;
772778
}
@@ -860,7 +866,7 @@ public Readiness getReadiness() {
860866

861867
@Override
862868
public final boolean isReady() {
863-
T item = fromServer().get();
869+
T item = get();
864870
if (item == null) {
865871
return false;
866872
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/HasMetadataOperation.java

+30-35
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
2323
import io.fabric8.kubernetes.api.model.ObjectMeta;
2424
import io.fabric8.kubernetes.client.KubernetesClientException;
25-
import io.fabric8.kubernetes.client.ResourceNotFoundException;
2625
import io.fabric8.kubernetes.client.dsl.Resource;
2726
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
2827
import io.fabric8.kubernetes.client.dsl.base.PatchType;
2928
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;
3029
import io.fabric8.kubernetes.client.utils.Serialization;
31-
import io.fabric8.kubernetes.client.utils.Utils;
3230

3331
import java.io.IOException;
3432
import java.util.concurrent.TimeUnit;
@@ -53,7 +51,7 @@ public HasMetadataOperation(OperationContext ctx, Class<T> type, Class<L> listTy
5351

5452
@Override
5553
public T edit(UnaryOperator<T> function) {
56-
T item = getMandatory();
54+
T item = getItemOrRequireFromServer();
5755
T clone = clone(item);
5856
return patch(null, clone, function.apply(item), false);
5957
}
@@ -64,50 +62,26 @@ private T clone(T item) {
6462

6563
@Override
6664
public T editStatus(UnaryOperator<T> function) {
67-
T item = getMandatory();
65+
T item = getItemOrRequireFromServer();
6866
T clone = clone(item);
6967
return patch(null, clone, function.apply(item), true);
7068
}
7169

7270
@Override
7371
public T accept(Consumer<T> consumer) {
74-
T item = getMandatory();
72+
T item = getItemOrRequireFromServer();
7573
T clone = clone(item);
7674
consumer.accept(item);
7775
return patch(null, clone, item, false);
7876
}
7977

8078
@Override
8179
public T edit(Visitor... visitors) {
82-
T item = getMandatory();
80+
T item = getItemOrRequireFromServer();
8381
T clone = clone(item);
8482
return patch(null, clone, context.getHandler(item).edit(item).accept(visitors).build(), false);
8583
}
8684

87-
/**
88-
* Get the current item from the server
89-
* <br>
90-
* Will always return non-null or throw an exception.
91-
*/
92-
protected T requireFromServer() {
93-
try {
94-
if (Utils.isNotNullOrEmpty(getName())) {
95-
return newInstance(context.withItem(null)).require();
96-
}
97-
if (getItem() != null) {
98-
String name = KubernetesResourceUtil.getName(getItem());
99-
if (Utils.isNotNullOrEmpty(name)) {
100-
return newInstance(context.withItem(null)).withName(name).require();
101-
}
102-
}
103-
} catch (ResourceNotFoundException e) {
104-
if (e.getCause() instanceof KubernetesClientException) {
105-
throw (KubernetesClientException) e.getCause();
106-
}
107-
}
108-
throw new KubernetesClientException("name not specified for an operation requiring one.");
109-
}
110-
11185
@Override
11286
public T replace() {
11387
return replace(getItem(), false);
@@ -192,9 +166,15 @@ protected T replace(T item, boolean status) {
192166
throw KubernetesClientException.launderThrowable(forOperationType(REPLACE_OPERATION), caught);
193167
}
194168

169+
/**
170+
* Perform a patch. If the base is not provided and one is required, it will
171+
* be fetched from the server.
172+
*/
195173
protected T patch(PatchContext context, T base, T item, boolean status) {
196-
if (base == null && context != null && context.getPatchType() == PatchType.JSON) {
197-
base = getMandatory();
174+
if (context == null || context.getPatchType() == PatchType.JSON) {
175+
if (base == null) {
176+
base = requireFromServer();
177+
}
198178
if (base.getMetadata() != null) {
199179
// prevent the resourceVersion from being modified in the patch
200180
if (item.getMetadata() == null) {
@@ -220,20 +200,35 @@ protected T patch(PatchContext context, T base, T item, boolean status) {
220200
return visitor.apply(item);
221201
}
222202

203+
@Override
204+
public T patchStatus() {
205+
return patch(PatchContext.of(PatchType.JSON_MERGE), null, getNonNullItem(), true);
206+
}
207+
208+
@Override
209+
public T patch() {
210+
return patch(null, null, getNonNullItem(), false);
211+
}
212+
213+
@Override
214+
public T patch(PatchContext patchContext) {
215+
return patch(patchContext, null, getNonNullItem(), false);
216+
}
217+
223218
@Override
224219
public T patchStatus(T item) {
225-
return patch(PatchContext.of(PatchType.JSON_MERGE), null, clone(item), true);
220+
return patch(PatchContext.of(PatchType.JSON_MERGE), getItem(), clone(item), true);
226221
}
227222

228223
@Override
229224
public T patch(PatchContext patchContext, T item) {
230-
return patch(patchContext, null, clone(item), false);
225+
return patch(patchContext, getItem(), clone(item), false);
231226
}
232227

233228
@Override
234229
public T patch(PatchContext patchContext, String patch) {
235230
try {
236-
final T got = getMandatory();
231+
final T got = getItemOrRequireFromServer();
237232
return handlePatch(patchContext, got, convertToJson(patch), getType(), false);
238233
} catch (InterruptedException interruptedException) {
239234
Thread.currentThread().interrupt();

0 commit comments

Comments
 (0)