@@ -64,6 +64,7 @@ const (
64
64
separator = "---\n "
65
65
reasonLabel = "projectsveltos.io/reason"
66
66
clusterSummaryAnnotation = "projectsveltos.io/clustersummary"
67
+ subresourcesAnnotation = "projectsveltos.io/subresources"
67
68
pathAnnotation = "path"
68
69
)
69
70
@@ -202,18 +203,34 @@ func readFiles(dir string) (map[string]string, error) {
202
203
// updateResource creates or updates a resource in a CAPI Cluster.
203
204
// No action in DryRun mode.
204
205
func updateResource (ctx context.Context , dr dynamic.ResourceInterface ,
205
- clusterSummary * configv1beta1.ClusterSummary , object * unstructured.Unstructured ,
206
+ clusterSummary * configv1beta1.ClusterSummary , object * unstructured.Unstructured , subresources [] string ,
206
207
logger logr.Logger ) error {
207
208
208
209
// No-op in DryRun mode
209
210
if clusterSummary .Spec .ClusterProfileSpec .SyncMode == configv1beta1 .SyncModeDryRun {
210
211
return nil
211
212
}
212
213
213
- l := logger .WithValues ("resourceNamespace" , object .GetNamespace (),
214
- "resourceName" , object . GetName (), " resourceGVK" , object .GetObjectKind ().GroupVersionKind ())
214
+ l := logger .WithValues ("resourceNamespace" , object .GetNamespace (), "resourceName" , object . GetName (),
215
+ "resourceGVK" , object .GetObjectKind ().GroupVersionKind (), "subresources" , subresources )
215
216
l .V (logs .LogDebug ).Info ("deploying policy" )
216
217
218
+ if len (subresources ) > 0 {
219
+ // Patch without subresources. This will make sure metadata and spec are eventually updated
220
+ err := patchRessource (ctx , dr , object , nil )
221
+ if err != nil {
222
+ return err
223
+ }
224
+ }
225
+ // Reset resource version in case of subresources a patch has already been made
226
+ // and that alters the resourceVersion
227
+ object .SetResourceVersion ("" )
228
+ return patchRessource (ctx , dr , object , subresources )
229
+ }
230
+
231
+ func patchRessource (ctx context.Context , dr dynamic.ResourceInterface ,
232
+ object * unstructured.Unstructured , subresources []string ) error {
233
+
217
234
data , err := runtime .Encode (unstructured .UnstructuredJSONScheme , object )
218
235
if err != nil {
219
236
return err
@@ -224,7 +241,7 @@ func updateResource(ctx context.Context, dr dynamic.ResourceInterface,
224
241
FieldManager : "application/apply-patch" ,
225
242
Force : & forceConflict ,
226
243
}
227
- _ , err = dr .Patch (ctx , object .GetName (), types .ApplyPatchType , data , options )
244
+ _ , err = dr .Patch (ctx , object .GetName (), types .ApplyPatchType , data , options , subresources ... )
228
245
return err
229
246
}
230
247
@@ -241,6 +258,18 @@ func instantiateTemplate(referencedObject client.Object, logger logr.Logger) boo
241
258
return false
242
259
}
243
260
261
+ func getSubresources (referencedObject client.Object ) []string {
262
+ annotations := referencedObject .GetAnnotations ()
263
+ if annotations != nil {
264
+ value , exists := annotations [subresourcesAnnotation ]
265
+ if exists {
266
+ subresources := strings .Split (value , "," )
267
+ return subresources
268
+ }
269
+ }
270
+ return nil
271
+ }
272
+
244
273
// deployContent deploys policies contained in a ConfigMap/Secret.
245
274
// data might have one or more keys. Each key might contain a single policy
246
275
// or multiple policies separated by '---'
@@ -252,6 +281,7 @@ func deployContent(ctx context.Context, deployingToMgmtCluster bool, destConfig
252
281
mgmtResources map [string ]* unstructured.Unstructured , logger logr.Logger ,
253
282
) (reports []configv1beta1.ResourceReport , err error ) {
254
283
284
+ subresources := getSubresources (referencedObject )
255
285
instantiateTemplate := instantiateTemplate (referencedObject , logger )
256
286
resources , err := collectContent (ctx , clusterSummary , mgmtResources , data , instantiateTemplate , logger )
257
287
if err != nil {
@@ -265,7 +295,7 @@ func deployContent(ctx context.Context, deployingToMgmtCluster bool, destConfig
265
295
}
266
296
267
297
return deployUnstructured (ctx , deployingToMgmtCluster , destConfig , destClient , resources , ref ,
268
- configv1beta1 .FeatureResources , clusterSummary , mgmtResources , logger )
298
+ configv1beta1 .FeatureResources , clusterSummary , mgmtResources , subresources , logger )
269
299
}
270
300
271
301
// adjustNamespace fixes namespace.
@@ -298,8 +328,8 @@ func adjustNamespace(policy *unstructured.Unstructured, destConfig *rest.Config)
298
328
//nolint:funlen // requires a lot of arguments because kustomize and plain resources are using this function
299
329
func deployUnstructured (ctx context.Context , deployingToMgmtCluster bool , destConfig * rest.Config ,
300
330
destClient client.Client , referencedUnstructured []* unstructured.Unstructured , referencedObject * corev1.ObjectReference ,
301
- featureID configv1beta1.FeatureID , clusterSummary * configv1beta1.ClusterSummary , mgmtResources map [string ]* unstructured.Unstructured , logger logr. Logger ,
302
- ) (reports []configv1beta1.ResourceReport , err error ) {
331
+ featureID configv1beta1.FeatureID , clusterSummary * configv1beta1.ClusterSummary , mgmtResources map [string ]* unstructured.Unstructured ,
332
+ subresources [] string , logger logr. Logger ) (reports []configv1beta1.ResourceReport , err error ) {
303
333
304
334
profile , profileTier , err := configv1beta1 .GetProfileOwnerAndTier (ctx , getManagementClusterClient (), clusterSummary )
305
335
if err != nil {
@@ -343,9 +373,6 @@ func deployUnstructured(ctx context.Context, deployingToMgmtCluster bool, destCo
343
373
return nil , err
344
374
}
345
375
346
- // If policy already exists, just get current version and update it by overridding
347
- // all metadata and spec.
348
- // If policy does not exist already, create it
349
376
dr , err := utils .GetDynamicResourceInterface (destConfig , policy .GroupVersionKind (), policy .GetNamespace ())
350
377
if err != nil {
351
378
return nil , err
@@ -393,7 +420,7 @@ func deployUnstructured(ctx context.Context, deployingToMgmtCluster bool, destCo
393
420
}
394
421
}
395
422
396
- err = updateResource (ctx , dr , clusterSummary , policy , logger )
423
+ err = updateResource (ctx , dr , clusterSummary , policy , subresources , logger )
397
424
if err != nil {
398
425
return reports , err
399
426
}
0 commit comments