1
1
package io .javaoperatorsdk .operator .api .reconciler ;
2
2
3
3
import java .util .function .BiFunction ;
4
+ import java .util .function .UnaryOperator ;
4
5
5
6
import org .slf4j .Logger ;
6
7
import org .slf4j .LoggerFactory ;
7
8
8
9
import io .fabric8 .kubernetes .api .model .HasMetadata ;
9
10
import io .fabric8 .kubernetes .client .KubernetesClient ;
11
+ import io .fabric8 .kubernetes .client .dsl .base .PatchContext ;
12
+ import io .fabric8 .kubernetes .client .dsl .base .PatchType ;
10
13
import io .javaoperatorsdk .operator .api .reconciler .support .UserPrimaryResourceCache ;
11
14
import io .javaoperatorsdk .operator .processing .event .ResourceID ;
12
15
13
16
public class PrimaryUpdateAndCacheUtils {
14
17
18
+ private PrimaryUpdateAndCacheUtils () {}
19
+
15
20
private static final Logger log = LoggerFactory .getLogger (PrimaryUpdateAndCacheUtils .class );
16
21
17
22
public static <P extends HasMetadata > P updateAndCacheStatus (P primary , Context <P > context ) {
18
- if (primary .getMetadata ().getResourceVersion () == null ) {
19
- throw new IllegalStateException (
20
- "Primary resource version is null, it is expected to set resource version for updates"
21
- + " with for cache" );
22
- }
23
- var updatedResource = context .getClient ().resource (primary ).updateStatus ();
23
+ return patchAndCacheStatusWithLock (
24
+ primary , context , (p , c ) -> c .resource (primary ).updateStatus ());
25
+ }
26
+
27
+ public static <P extends HasMetadata > P patchAndCacheStatusWithLock (
28
+ P primary , Context <P > context ) {
29
+ return patchAndCacheStatusWithLock (
30
+ primary , context , (p , c ) -> c .resource (primary ).patchStatus ());
31
+ }
32
+
33
+ public static <P extends HasMetadata > P editAndCacheStatusWithLock (
34
+ P primary , Context <P > context , UnaryOperator <P > operation ) {
35
+ return patchAndCacheStatusWithLock (
36
+ primary , context , (p , c ) -> c .resource (primary ).editStatus (operation ));
37
+ }
38
+
39
+ public static <P extends HasMetadata > P patchAndCacheStatusWithLock (
40
+ P primary , Context <P > context , BiFunction <P , KubernetesClient , P > patch ) {
41
+ checkResourceVersionPresent (primary );
42
+ var updatedResource = patch .apply (primary , context .getClient ());
24
43
context
25
44
.eventSourceRetriever ()
26
45
.getControllerEventSource ()
27
46
.handleRecentResourceUpdate (ResourceID .fromResource (primary ), updatedResource , primary );
28
- return updatedResource ;
47
+ return null ;
29
48
}
30
49
31
- public static <P extends HasMetadata > P patchAndCacheStatus (
32
- P primary , Context <P > context , UserPrimaryResourceCache <P > cache ) {
50
+ public static <P extends HasMetadata > P ssaPatchAndCacheStatusWithLock (
51
+ P primary , P freshResourceWithStatus , Context <P > context ) {
52
+ checkResourceVersionPresent (freshResourceWithStatus );
53
+ var res =
54
+ context
55
+ .getClient ()
56
+ .resource (freshResourceWithStatus )
57
+ .subresource ("status" )
58
+ .patch (
59
+ new PatchContext .Builder ()
60
+ .withForce (true )
61
+ .withFieldManager (context .getControllerConfiguration ().fieldManager ())
62
+ .withPatchType (PatchType .SERVER_SIDE_APPLY )
63
+ .build ());
33
64
65
+ context
66
+ .eventSourceRetriever ()
67
+ .getControllerEventSource ()
68
+ .handleRecentResourceUpdate (ResourceID .fromResource (primary ), res , primary );
69
+ return res ;
70
+ }
71
+
72
+ public static <P extends HasMetadata > P ssaPatchAndCacheStatus (
73
+ P primary , P freshResource , Context <P > context , UserPrimaryResourceCache <P > cache ) {
74
+ logWarnIfResourceVersionPresent (freshResource );
34
75
return patchAndCacheStatus (
35
76
primary ,
36
77
context .getClient (),
37
78
cache ,
38
- (P p , KubernetesClient c ) -> {
39
- if (context
40
- .getControllerConfiguration ()
41
- .getConfigurationService ()
42
- .useSSAToPatchPrimaryResource ()) {
43
- return c .resource (p ).serverSideApply ();
44
- } else {
45
- return c .resource (p ).patchStatus ();
46
- }
47
- });
79
+ (P p , KubernetesClient c ) ->
80
+ c .resource (freshResource )
81
+ .subresource ("status" )
82
+ .patch (
83
+ new PatchContext .Builder ()
84
+ .withForce (true )
85
+ .withFieldManager (context .getControllerConfiguration ().fieldManager ())
86
+ .withPatchType (PatchType .SERVER_SIDE_APPLY )
87
+ .build ()));
48
88
}
49
89
50
90
public static <P extends HasMetadata > P patchAndCacheStatus (
@@ -56,4 +96,22 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
56
96
cache .cacheResource (primary , updatedResource );
57
97
return updatedResource ;
58
98
}
99
+
100
+ private static <P extends HasMetadata > void checkResourceVersionPresent (P primary ) {
101
+ if (primary .getMetadata ().getResourceVersion () == null ) {
102
+ throw new IllegalStateException (
103
+ "Primary resource version is null, it is expected to set resource version for updates for caching. Name: %s namespace: %s"
104
+ .formatted (primary .getMetadata ().getName (), primary .getMetadata ().getNamespace ()));
105
+ }
106
+ }
107
+
108
+ private static <P extends HasMetadata > void logWarnIfResourceVersionPresent (P primary ) {
109
+ if (primary .getMetadata ().getResourceVersion () != null ) {
110
+ log .warn (
111
+ "Primary resource version is NOT null, for caching with optimistic locking use"
112
+ + " alternative methods. Name: {} namespace: {}" ,
113
+ primary .getMetadata ().getName (),
114
+ primary .getMetadata ().getNamespace ());
115
+ }
116
+ }
59
117
}
0 commit comments