forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use StrategicMergePatch when updating existing cluster resources (fly…
- Loading branch information
Showing
2 changed files
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package clusterresource | ||
|
||
import ( | ||
jsonpatch "github.com/evanphx/json-patch" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type strategicMergeFromPatch struct { | ||
from runtime.Object | ||
} | ||
|
||
// Type implements patch. | ||
func (s *strategicMergeFromPatch) Type() types.PatchType { | ||
return types.StrategicMergePatchType | ||
} | ||
|
||
// Data implements Patch. | ||
func (s *strategicMergeFromPatch) Data(obj runtime.Object) ([]byte, error) { | ||
originalJSON, err := json.Marshal(s.from) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
modifiedJSON, err := json.Marshal(obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return jsonpatch.CreateMergePatch(originalJSON, modifiedJSON) | ||
} | ||
|
||
// StrategicMergeFrom creates a Patch using the strategic-merge-patch strategy with the given object as base. | ||
func StrategicMergeFrom(obj runtime.Object) client.Patch { | ||
return &strategicMergeFromPatch{obj} | ||
} |