4
4
import java .util .Map ;
5
5
import java .util .concurrent .atomic .AtomicInteger ;
6
6
7
+ import org .assertj .core .util .Lists ;
8
+
7
9
import io .fabric8 .kubernetes .api .model .ConfigMap ;
10
+ import io .fabric8 .kubernetes .api .model .FieldsV1Builder ;
11
+ import io .fabric8 .kubernetes .api .model .ManagedFieldsEntryBuilder ;
8
12
import io .fabric8 .kubernetes .api .model .ObjectMeta ;
9
13
import io .fabric8 .kubernetes .client .KubernetesClient ;
10
14
import io .fabric8 .kubernetes .client .dsl .base .PatchContext ;
11
- import io .fabric8 .kubernetes .client .dsl .base .PatchType ;
12
15
import io .javaoperatorsdk .operator .api .config .ConfigurationServiceProvider ;
13
16
import io .javaoperatorsdk .operator .api .config .informer .InformerConfiguration ;
14
17
import io .javaoperatorsdk .operator .api .reconciler .*;
15
18
import io .javaoperatorsdk .operator .junit .KubernetesClientAware ;
16
19
import io .javaoperatorsdk .operator .processing .event .source .EventSource ;
17
20
import io .javaoperatorsdk .operator .processing .event .source .informer .InformerEventSource ;
18
21
19
- @ ControllerConfiguration
20
- // @ControllerConfiguration(itemStore = LabelRemovingItemStore.class)
22
+ @ ControllerConfiguration (itemStore = LabelRemovingItemStore .class )
21
23
public class CachePruneReconciler
22
24
implements Reconciler <CachePruneCustomResource >,
23
25
EventSourceInitializer <CachePruneCustomResource >,
24
26
Cleaner <CachePruneCustomResource >, KubernetesClientAware {
25
27
26
28
public static final String DATA_KEY = "data" ;
29
+ public static final String FIELD_MANAGER = "controller" ;
30
+ public static final String SECONDARY_CREATE_FIELD_MANAGER = "creator" ;
27
31
private final AtomicInteger numberOfExecutions = new AtomicInteger (0 );
28
32
private KubernetesClient client ;
29
33
@@ -39,11 +43,15 @@ public UpdateControl<CachePruneCustomResource> reconcile(
39
43
.equals (resource .getSpec ().getData ())) {
40
44
var cloned = ConfigurationServiceProvider .instance ().getResourceCloner ().clone (cm );
41
45
cloned .getData ().put (DATA_KEY , resource .getSpec ().getData ());
42
- var res = client .configMaps ().resource (cm )
43
- .patch (PatchContext .of (PatchType .SERVER_SIDE_APPLY ), cloned );
44
- System .out .println (res );
46
+ var patchContext = patchContextWithFieldManager (FIELD_MANAGER );
47
+ // setting new field manager since we don't control label anymore
48
+ patchContext .setForce (true );
49
+ patchContext .setFieldManager (FIELD_MANAGER );
50
+ client .configMaps ().resource (cm )
51
+ .patch (patchContext , cloned );
45
52
}
46
- }, () -> client .configMaps ().resource (configMap (resource )).create ());
53
+ }, () -> client .configMaps ().resource (configMap (resource ))
54
+ .patch (patchContextWithFieldManager (SECONDARY_CREATE_FIELD_MANAGER )));
47
55
48
56
resource .setStatus (new CachePruneStatus ());
49
57
resource .getStatus ().setCreated (true );
@@ -55,12 +63,18 @@ public int getNumberOfExecutions() {
55
63
return numberOfExecutions .get ();
56
64
}
57
65
66
+ private PatchContext patchContextWithFieldManager (String fieldManager ) {
67
+ PatchContext patchContext = new PatchContext ();
68
+ patchContext .setFieldManager (fieldManager );
69
+ return patchContext ;
70
+ }
71
+
58
72
@ Override
59
73
public Map <String , EventSource > prepareEventSources (
60
74
EventSourceContext <CachePruneCustomResource > context ) {
61
75
InformerEventSource <ConfigMap , CachePruneCustomResource > configMapEventSource =
62
76
new InformerEventSource <>(InformerConfiguration .from (ConfigMap .class , context )
63
- // .withItemStore(new LabelRemovingItemStore<>())
77
+ .withItemStore (new LabelRemovingItemStore <>())
64
78
.build (),
65
79
context );
66
80
return EventSourceInitializer .nameEventSources (configMapEventSource );
@@ -71,6 +85,16 @@ ConfigMap configMap(CachePruneCustomResource resource) {
71
85
configMap .setMetadata (new ObjectMeta ());
72
86
configMap .getMetadata ().setName (resource .getMetadata ().getName ());
73
87
configMap .getMetadata ().setNamespace (resource .getMetadata ().getNamespace ());
88
+ configMap .getMetadata ().setManagedFields (Lists .list (new ManagedFieldsEntryBuilder ()
89
+ .withApiVersion ("v1" )
90
+ .withFieldsType ("FieldsV1" )
91
+ .withManager ("fabric8" )
92
+ .withOperation ("Apply" )
93
+ .withTime ("2022-11-30T09:27:00.000Z" )
94
+ .withFieldsV1 (new FieldsV1Builder ()
95
+ .withAdditionalProperties (Map .of ("f:data" , Map .of ("f:data" , Map .of ())))
96
+ .build ())
97
+ .build ()));
74
98
configMap .setData (Map .of (DATA_KEY , resource .getSpec ().getData ()));
75
99
HashMap <String , String > labels = new HashMap <>();
76
100
labels .put ("mylabel" , "val" );
0 commit comments