Skip to content
This repository was archived by the owner on Feb 9, 2025. It is now read-only.

Commit 06b5b22

Browse files
authored
Merge pull request #7 from prosimcorp/feat/add-patches-patchtype
feat: Add object to link patch and patch-type
2 parents 6c09dd6 + 40c7149 commit 06b5b22

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

controllers/workloadaction_sync.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ const (
6363
ActionRestart = "restart"
6464
)
6565

66+
// PatchConstructorPatchT represents a 'patch' and (they way it is applied)
67+
// returned by a PatchConstructorFuncPointerT
68+
// TODO Move wherever it's better than here
69+
type PatchConstructorPatchT struct {
70+
// PatchType represents the type of patch to apply
71+
PatchType types.PatchType
72+
73+
// Patch represents the patch desired to be applied
74+
Patch []byte
75+
}
76+
6677
// PatchConstructorFuncPointerT represents a pointer to a function for crafting a patch
67-
type PatchConstructorFuncPointerT func(obj *unstructured.Unstructured) ([]byte, error)
78+
// TODO Move wherever it's better than here
79+
type PatchConstructorFuncPointerT func(obj *unstructured.Unstructured) (PatchConstructorPatchT, error)
6880

6981
// HttpRequestAuth represents authentication params provided to a request
7082
// TODO Move wherever it's better than here
@@ -296,18 +308,20 @@ func getPodTemplateAnnotations(obj *unstructured.Unstructured) (annotations []by
296308

297309
// defaultPatchConstructor return a patch valid for core workload resources (deployments, statefulsets, daemonsets)
298310
// adding previously existing annotations from podTemplate
299-
func defaultPatchConstructor(obj *unstructured.Unstructured) (patch []byte, err error) {
311+
func defaultPatchConstructor(obj *unstructured.Unstructured) (patch PatchConstructorPatchT, err error) {
300312
annotations, err := getPodTemplateAnnotations(obj)
301313
if err != nil {
302314
return patch, err
303315
}
304316

305-
patch = []byte(fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":%s}}}}`, annotations))
317+
patch.Patch = []byte(fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":%s}}}}`, annotations))
318+
patch.PatchType = types.StrategicMergePatchType
319+
306320
return patch, err
307321
}
308322

309323
// deploymentPatchConstructor return a patch for deployment resources to be used in SetWorkloadRestartAnnotation
310-
func deploymentPatchConstructor(obj *unstructured.Unstructured) (patch []byte, err error) {
324+
func deploymentPatchConstructor(obj *unstructured.Unstructured) (patch PatchConstructorPatchT, err error) {
311325
pausedValue, found, err := unstructured.NestedBool(obj.Object, "spec", "paused")
312326
if err != nil {
313327
return patch, err
@@ -345,13 +359,13 @@ func (r *WorkloadActionReconciler) SetWorkloadRestartAnnotation(ctx context.Cont
345359
}
346360

347361
// 2. Construct the patch with related function
348-
patchBytes, err := patchConstructorMap[kind](obj)
362+
returnedPatch, err := patchConstructorMap[kind](obj)
349363
if err != nil {
350364
return err
351365
}
352366

353367
// 3. Execute the patch
354-
err = r.Patch(ctx, obj, client.RawPatch(types.StrategicMergePatchType, patchBytes))
368+
err = r.Patch(ctx, obj, client.RawPatch(returnedPatch.PatchType, returnedPatch.Patch))
355369
if err != nil {
356370
err = fmt.Errorf(WorkloadActionAnnotationPatchErrorMessage, err)
357371
}

0 commit comments

Comments
 (0)