Skip to content

Commit

Permalink
Set resource version for real
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Olshevski committed Dec 13, 2023
1 parent ca02f48 commit 2f3c505
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/controllers/reconciliation/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package reconciliation

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -143,6 +145,10 @@ func (c *Controller) reconcileResource(ctx context.Context, prev, resource *reco
logger.V(1).Info("skipping empty patch")
return nil
}
patch, err = addResourceVersion(patch, current.GetResourceVersion())
if err != nil {
return fmt.Errorf("adding resource version: %w", err)
}
logger.V(1).Info("TODO: REMOVE ME", "patchBody", string(patch))
err = c.upstreamClient.Patch(ctx, current, client.RawPatch(patchType, patch))
if err != nil {
Expand Down Expand Up @@ -181,3 +187,20 @@ func (c *Controller) buildPatch(ctx context.Context, prev, resource *reconstitut
patch, err := strategicpatch.CreateThreeWayMergePatch(prevManifest, []byte(resource.Manifest), currentJS, patchmeta, true)
return patch, types.StrategicMergePatchType, err
}

// TODO: Remove creationTimestamp=null from patches
func addResourceVersion(patch []byte, rv string) ([]byte, error) {
var patchMap map[string]interface{}
err := json.Unmarshal(patch, &patchMap)
if err != nil {
return nil, err
}
u := unstructured.Unstructured{Object: patchMap}
a, err := meta.Accessor(&u)
if err != nil {
return nil, err
}
a.SetResourceVersion(rv)

return json.Marshal(patchMap)
}

0 comments on commit 2f3c505

Please sign in to comment.