|
1 | 1 | package io.javaoperatorsdk.operator.api.reconciler;
|
2 | 2 |
|
| 3 | +import java.util.Optional; |
| 4 | + |
3 | 5 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
4 | 6 | import io.fabric8.kubernetes.client.CustomResource;
|
5 | 7 |
|
6 | 8 | public class UpdateControl<P extends HasMetadata> extends BaseControl<UpdateControl<P>> {
|
7 | 9 |
|
8 | 10 | private final P resource;
|
9 |
| - private final boolean updateStatus; |
10 |
| - private final boolean updateResource; |
| 11 | + private final boolean patchResource; |
11 | 12 | private final boolean patchStatus;
|
12 | 13 |
|
13 | 14 | private UpdateControl(
|
14 |
| - P resource, boolean updateStatus, boolean updateResource, boolean patchStatus) { |
15 |
| - if ((updateResource || updateStatus) && resource == null) { |
| 15 | + P resource, boolean patchResource, boolean patchStatus) { |
| 16 | + if ((patchResource || patchStatus) && resource == null) { |
16 | 17 | throw new IllegalArgumentException("CustomResource cannot be null in case of update");
|
17 | 18 | }
|
18 | 19 | this.resource = resource;
|
19 |
| - this.updateStatus = updateStatus; |
20 |
| - this.updateResource = updateResource; |
| 20 | + this.patchResource = patchResource; |
21 | 21 | this.patchStatus = patchStatus;
|
22 | 22 | }
|
23 | 23 |
|
24 |
| - /** |
25 |
| - * Creates an update control instance that instructs the framework to do an update on resource |
26 |
| - * itself, not on the status. Note that usually as a results of a reconciliation should be a |
27 |
| - * status update not an update to the resource itself. |
28 |
| - * |
29 |
| - * Using this update makes sure that the resource in the next reconciliation is the updated one - |
30 |
| - * this is not guaranteed by default if you do an update on a resource by the Kubernetes client. |
31 |
| - * |
32 |
| - * @param <T> custom resource type |
33 |
| - * @param customResource customResource to use for update |
34 |
| - * @return initialized update control |
35 |
| - */ |
36 |
| - public static <T extends HasMetadata> UpdateControl<T> updateResource(T customResource) { |
37 |
| - return new UpdateControl<>(customResource, false, true, false); |
38 |
| - } |
39 |
| - |
40 | 24 | /**
|
41 | 25 | * Preferred way to update the status. It does not do optimistic locking. Uses JSON Patch to patch
|
42 | 26 | * the resource.
|
43 | 27 | * <p>
|
44 |
| - * Note that this does not work, if the {@link CustomResource#initStatus() initStatus} is |
45 |
| - * implemented, since it breaks the diffing process. Don't implement it if using this method. |
| 28 | + * Note that this does not work, if the {@link CustomResource#initStatus()} is implemented, since |
| 29 | + * it breaks the diffing process. Don't implement it if using this method. |
46 | 30 | * </p>
|
47 |
| - * There is also an issue with setting value to null with older Kubernetes versions (1.19 and |
48 |
| - * below). See: <a href= |
| 31 | + * There is also an issue with setting value to {@code null} with older Kubernetes versions (1.19 |
| 32 | + * and below). See: <a href= |
49 | 33 | * "https://github.com/fabric8io/kubernetes-client/issues/4158">https://github.com/fabric8io/kubernetes-client/issues/4158</a>
|
50 | 34 | *
|
51 | 35 | * @param <T> resource type
|
52 | 36 | * @param customResource the custom resource with target status
|
53 | 37 | * @return UpdateControl instance
|
54 | 38 | */
|
55 | 39 | public static <T extends HasMetadata> UpdateControl<T> patchStatus(T customResource) {
|
56 |
| - return new UpdateControl<>(customResource, true, false, true); |
57 |
| - } |
58 |
| - |
59 |
| - /** |
60 |
| - * Note that usually "patchStatus" is advised to be used instead of this method. |
61 |
| - * <p> |
62 |
| - * Updates the status with optimistic locking regarding current resource version reconciled. Note |
63 |
| - * that this also ensures that on next reconciliation is the most up-to-date custom resource is |
64 |
| - * used. |
65 |
| - * </p> |
66 |
| - * |
67 |
| - * @param <T> resource type |
68 |
| - * @param customResource the custom resource with target status |
69 |
| - * @return UpdateControl instance |
70 |
| - */ |
71 |
| - public static <T extends HasMetadata> UpdateControl<T> updateStatus(T customResource) { |
72 |
| - return new UpdateControl<>(customResource, true, false, false); |
73 |
| - } |
74 |
| - |
75 |
| - /** |
76 |
| - * As a results of this there will be two call to K8S API. First the custom resource will be |
77 |
| - * updates then the status sub-resource. |
78 |
| - * |
79 |
| - * Using this update makes sure that the resource in the next reconciliation is the updated one - |
80 |
| - * this is not guaranteed by default if you do an update on a resource by the Kubernetes client. |
81 |
| - * |
82 |
| - * @param <T> resource type |
83 |
| - * @param customResource - custom resource to use in both API calls |
84 |
| - * @return UpdateControl instance |
85 |
| - */ |
86 |
| - public static <T extends HasMetadata> UpdateControl<T> updateResourceAndStatus( |
87 |
| - T customResource) { |
88 |
| - return new UpdateControl<>(customResource, true, true, false); |
| 40 | + return new UpdateControl<>(customResource, false, true); |
89 | 41 | }
|
90 | 42 |
|
91 |
| - /** |
92 |
| - * Updates the resource - with optimistic locking - and patches the status without optimistic |
93 |
| - * locking in place. |
94 |
| - * |
95 |
| - * Note that using this method, it is not guaranteed that the most recent updated resource will be |
96 |
| - * in case for next reconciliation. |
97 |
| - * |
98 |
| - * @param customResource to update |
99 |
| - * @return UpdateControl instance |
100 |
| - * @param <T> resource type |
101 |
| - */ |
102 |
| - public static <T extends HasMetadata> UpdateControl<T> updateResourceAndPatchStatus( |
103 |
| - T customResource) { |
104 |
| - return new UpdateControl<>(customResource, true, true, true); |
| 43 | + public static <T extends HasMetadata> UpdateControl<T> patchResource(T customResource) { |
| 44 | + return new UpdateControl<>(customResource, true, false); |
105 | 45 | }
|
106 | 46 |
|
107 | 47 | /**
|
108 |
| - * Marked for removal, because of confusing name. It does not patch the resource but rather |
109 |
| - * updates it. |
110 |
| - * |
111 |
| - * @deprecated use {@link UpdateControl#updateResourceAndPatchStatus(HasMetadata)} |
112 |
| - * |
113 | 48 | * @param customResource to update
|
114 | 49 | * @return UpdateControl instance
|
115 | 50 | * @param <T> resource type
|
116 | 51 | */
|
117 |
| - @Deprecated(forRemoval = true) |
118 | 52 | public static <T extends HasMetadata> UpdateControl<T> patchResourceAndStatus(T customResource) {
|
119 |
| - return updateResourceAndStatus(customResource); |
| 53 | + return new UpdateControl<>(customResource, true, true); |
120 | 54 | }
|
121 | 55 |
|
122 | 56 | public static <T extends HasMetadata> UpdateControl<T> noUpdate() {
|
123 |
| - return new UpdateControl<>(null, false, false, false); |
124 |
| - } |
125 |
| - |
126 |
| - public P getResource() { |
127 |
| - return resource; |
| 57 | + return new UpdateControl<>(null, false, false); |
128 | 58 | }
|
129 | 59 |
|
130 |
| - public boolean isUpdateStatus() { |
131 |
| - return updateStatus; |
| 60 | + public Optional<P> getResource() { |
| 61 | + return Optional.ofNullable(resource); |
132 | 62 | }
|
133 | 63 |
|
134 |
| - public boolean isUpdateResource() { |
135 |
| - return updateResource; |
| 64 | + public boolean isPatchResource() { |
| 65 | + return patchResource; |
136 | 66 | }
|
137 | 67 |
|
138 | 68 | public boolean isPatchStatus() {
|
139 | 69 | return patchStatus;
|
140 | 70 | }
|
141 | 71 |
|
142 | 72 | public boolean isNoUpdate() {
|
143 |
| - return !updateResource && !updateStatus; |
| 73 | + return !patchResource && !patchStatus; |
144 | 74 | }
|
145 | 75 |
|
146 |
| - public boolean isUpdateResourceAndStatus() { |
147 |
| - return updateResource && updateStatus; |
| 76 | + public boolean isPatchResourceAndStatus() { |
| 77 | + return patchResource && patchStatus; |
148 | 78 | }
|
149 | 79 |
|
150 | 80 | }
|
0 commit comments