22
22
import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
23
23
import io .fabric8 .kubernetes .api .model .ObjectMeta ;
24
24
import io .fabric8 .kubernetes .client .KubernetesClientException ;
25
- import io .fabric8 .kubernetes .client .ResourceNotFoundException ;
26
25
import io .fabric8 .kubernetes .client .dsl .Resource ;
27
26
import io .fabric8 .kubernetes .client .dsl .base .PatchContext ;
28
27
import io .fabric8 .kubernetes .client .dsl .base .PatchType ;
29
28
import io .fabric8 .kubernetes .client .utils .KubernetesResourceUtil ;
30
29
import io .fabric8 .kubernetes .client .utils .Serialization ;
31
- import io .fabric8 .kubernetes .client .utils .Utils ;
32
30
33
31
import java .io .IOException ;
34
32
import java .util .concurrent .TimeUnit ;
@@ -53,7 +51,7 @@ public HasMetadataOperation(OperationContext ctx, Class<T> type, Class<L> listTy
53
51
54
52
@ Override
55
53
public T edit (UnaryOperator <T > function ) {
56
- T item = getMandatory ();
54
+ T item = getItemOrRequireFromServer ();
57
55
T clone = clone (item );
58
56
return patch (null , clone , function .apply (item ), false );
59
57
}
@@ -64,50 +62,26 @@ private T clone(T item) {
64
62
65
63
@ Override
66
64
public T editStatus (UnaryOperator <T > function ) {
67
- T item = getMandatory ();
65
+ T item = getItemOrRequireFromServer ();
68
66
T clone = clone (item );
69
67
return patch (null , clone , function .apply (item ), true );
70
68
}
71
69
72
70
@ Override
73
71
public T accept (Consumer <T > consumer ) {
74
- T item = getMandatory ();
72
+ T item = getItemOrRequireFromServer ();
75
73
T clone = clone (item );
76
74
consumer .accept (item );
77
75
return patch (null , clone , item , false );
78
76
}
79
77
80
78
@ Override
81
79
public T edit (Visitor ... visitors ) {
82
- T item = getMandatory ();
80
+ T item = getItemOrRequireFromServer ();
83
81
T clone = clone (item );
84
82
return patch (null , clone , context .getHandler (item ).edit (item ).accept (visitors ).build (), false );
85
83
}
86
84
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
-
111
85
@ Override
112
86
public T replace () {
113
87
return replace (getItem (), false );
@@ -192,9 +166,15 @@ protected T replace(T item, boolean status) {
192
166
throw KubernetesClientException .launderThrowable (forOperationType (REPLACE_OPERATION ), caught );
193
167
}
194
168
169
+ /**
170
+ * Perform a patch. If the base is not provided and one is required, it will
171
+ * be fetched from the server.
172
+ */
195
173
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
+ }
198
178
if (base .getMetadata () != null ) {
199
179
// prevent the resourceVersion from being modified in the patch
200
180
if (item .getMetadata () == null ) {
@@ -220,20 +200,35 @@ protected T patch(PatchContext context, T base, T item, boolean status) {
220
200
return visitor .apply (item );
221
201
}
222
202
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
+
223
218
@ Override
224
219
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 );
226
221
}
227
222
228
223
@ Override
229
224
public T patch (PatchContext patchContext , T item ) {
230
- return patch (patchContext , null , clone (item ), false );
225
+ return patch (patchContext , getItem () , clone (item ), false );
231
226
}
232
227
233
228
@ Override
234
229
public T patch (PatchContext patchContext , String patch ) {
235
230
try {
236
- final T got = getMandatory ();
231
+ final T got = getItemOrRequireFromServer ();
237
232
return handlePatch (patchContext , got , convertToJson (patch ), getType (), false );
238
233
} catch (InterruptedException interruptedException ) {
239
234
Thread .currentThread ().interrupt ();
0 commit comments