@@ -113,8 +113,9 @@ private static Set<PolicyRule> getClusterRolePolicyRulesFromDependentResources(Q
113
113
114
114
// only process Kubernetes dependents
115
115
if (HasMetadata .class .isAssignableFrom (associatedResourceClass )) {
116
- var resourceGroup = HasMetadata .getGroup (associatedResourceClass );
117
- var resourcePlural = HasMetadata .getPlural (associatedResourceClass );
116
+ final var asHasMetadataClass = (Class <? extends HasMetadata >) associatedResourceClass ;
117
+ var resourceGroup = HasMetadata .getGroup (asHasMetadataClass );
118
+ var resourcePlural = HasMetadata .getPlural (asHasMetadataClass );
118
119
119
120
final var verbs = new TreeSet <>(List .of (RBACVerbs .READ_VERBS ));
120
121
if (Updater .class .isAssignableFrom (dependentResourceClass )) {
@@ -124,34 +125,24 @@ private static Set<PolicyRule> getClusterRolePolicyRulesFromDependentResources(Q
124
125
verbs .add (RBACVerbs .DELETE );
125
126
}
126
127
final var isCreator = Creator .class .isAssignableFrom (dependentResourceClass );
127
- boolean shouldDoubleCheckPatch = false ;
128
128
if (isCreator ) {
129
129
verbs .add (RBACVerbs .CREATE );
130
-
131
- // PATCH verb is also needed when using SSA to be able to add the finalizer when creating the resource
132
- // Here, we optimistically add PATCH method if the resource configuration states that SSA should be
133
- // used, despite this not being a correct/complete determination of whether the resource actually
134
- // uses SSA. This can only be determined by instantiating the dependent, which is why, if we can
135
- // instantiate it, we double-check the SSA status later on and remove the PATCH method if we can
136
- // actually determine that it's not needed
137
- final Object dependentResourceConfig = spec .getDependentResourceConfig ();
138
- if (dependentResourceConfig instanceof KubernetesDependentResourceConfig <?> kubernetesDependentResourceConfig ) {
139
- if (kubernetesDependentResourceConfig .useSSA ().orElse (false )) {
140
- verbs .add (RBACVerbs .PATCH );
141
- shouldDoubleCheckPatch = true ;
142
- }
143
- }
144
130
}
145
131
146
132
// Check if we're dealing with typeless Kubernetes resource or if we need to deal with SSA
147
133
boolean ignore = false ;
148
- KubernetesDependentResource <?, ?> kubeResource = null ;
149
134
if (KubernetesDependentResource .class .isAssignableFrom (dependentResourceClass )) {
135
+ final var asKubeDRClass = (Class <? extends KubernetesDependentResource <?, ?>>) dependentResourceClass ;
136
+
137
+ // PATCH is also required when creating resources to add finalizers when using SSA
138
+ if (isCreator && cri .getConfigurationService ().shouldUseSSA (asKubeDRClass , asHasMetadataClass ,
139
+ (KubernetesDependentResourceConfig <? extends HasMetadata >) spec .getDependentResourceConfig ())) {
140
+ verbs .add (RBACVerbs .PATCH );
141
+ }
142
+
150
143
try {
151
- //noinspection rawtypes
152
- kubeResource = Utils .instantiate (
153
- (Class <? extends KubernetesDependentResource >) dependentResourceClass ,
154
- KubernetesDependentResource .class , ADD_CLUSTER_ROLES_DECORATOR );
144
+ final var kubeResource = Utils .instantiate (asKubeDRClass , KubernetesDependentResource .class ,
145
+ ADD_CLUSTER_ROLES_DECORATOR );
155
146
156
147
if (kubeResource instanceof GenericKubernetesDependentResource <? extends HasMetadata > genericKubeRes ) {
157
148
final var gvk = genericKubeRes .getGroupVersionKind ();
@@ -166,21 +157,6 @@ private static Set<PolicyRule> getClusterRolePolicyRulesFromDependentResources(Q
166
157
}
167
158
168
159
if (!ignore ) {
169
- // if we need to double check if we really should use SSA
170
- if (shouldDoubleCheckPatch ) {
171
- // we can only check if we managed to instantiate the dependent, though
172
- if (kubeResource != null ) {
173
- if (!cri .getConfigurationService ().shouldUseSSA (kubeResource )) {
174
- verbs .remove (RBACVerbs .PATCH );
175
- }
176
- } else {
177
- // if we couldn't double check, warn the user
178
- log .warn ("Couldn't verify that dependent " + dependentResourceClass .getName ()
179
- + " really needs PATCH permission for SSA because it couldn't be instantiated. This means that a PATCH verb might have been added to the rule (group: "
180
- + resourceGroup + " / plural: " + resourcePlural + ") when not needed." );
181
- }
182
- }
183
-
184
160
final var dependentRule = new PolicyRuleBuilder ()
185
161
.addToApiGroups (resourceGroup )
186
162
.addToResources (resourcePlural );
0 commit comments