Skip to content

Commit e29acd5

Browse files
committed
bring back support for mutations, remove Rudi support
On-behalf-of: @SAP [email protected]
1 parent 491adf3 commit e29acd5

File tree

5 files changed

+6
-126
lines changed

5 files changed

+6
-126
lines changed

Diff for: internal/controller/sync/controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Create(
9393
}
9494

9595
// create the syncer that holds the meat&potatoes of the synchronization logic
96-
mutator := mutation.NewMutator(nil) // pubRes.Spec.Mutation
96+
mutator := mutation.NewMutator(pubRes.Spec.Mutation)
9797
syncer, err := sync.NewResourceSyncer(log, localManager.GetClient(), virtualWorkspaceCluster.GetClient(), pubRes, localCRD, apiExportName, mutator, stateNamespace, agentName)
9898
if err != nil {
9999
return nil, fmt.Errorf("failed to create syncer: %w", err)

Diff for: internal/mutation/mutation.go

+1-30
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/Masterminds/sprig/v3"
2929
"github.com/tidwall/gjson"
3030
"github.com/tidwall/sjson"
31-
"go.xrstf.de/rudi"
3231

3332
syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1"
3433
)
@@ -46,11 +45,6 @@ func ApplyResourceMutations(value any, mutations []syncagentv1alpha1.ResourceMut
4645
}
4746

4847
func ApplyResourceMutation(value any, mut syncagentv1alpha1.ResourceMutation, ctx *TemplateMutationContext) (any, error) {
49-
// for Rudi scripts we can skip all the JSON encoding/decoding
50-
if mut.Rudi != nil {
51-
return applyResourceRudiMigration(value, *mut.Rudi, ctx)
52-
}
53-
5448
// encode current value as JSON
5549
encoded, err := json.Marshal(value)
5650
if err != nil {
@@ -82,33 +76,10 @@ func applyResourceMutationToJSON(jsonData string, mut syncagentv1alpha1.Resource
8276
case mut.Regex != nil:
8377
return applyResourceRegexMutation(jsonData, *mut.Regex)
8478
default:
85-
return "", errors.New("must use either Rudi, regex, template or delete mutation")
79+
return "", errors.New("must use either regex, template or delete mutation")
8680
}
8781
}
8882

89-
func applyResourceRudiMigration(value any, mut syncagentv1alpha1.ResourceRudiMutation, ctx *TemplateMutationContext) (any, error) {
90-
program, err := rudi.Parse("myscript", mut.Script)
91-
if err != nil {
92-
return nil, fmt.Errorf("invalid script: %w", err)
93-
}
94-
95-
funcs := rudi.NewBuiltInFunctions()
96-
vars := rudi.NewVariables()
97-
98-
if ctx != nil {
99-
vars.
100-
Set("localObj", ctx.LocalObject).
101-
Set("remoteObj", ctx.RemoteObject)
102-
}
103-
104-
processed, _, err := program.Run(value, vars, funcs)
105-
if err != nil {
106-
return nil, fmt.Errorf("script failed: %w", err)
107-
}
108-
109-
return processed, nil
110-
}
111-
11283
func applyResourceDeleteMutation(jsonData string, mut syncagentv1alpha1.ResourceDeleteMutation) (string, error) {
11384
jsonData, err := sjson.Delete(jsonData, mut.Path)
11485
if err != nil {

Diff for: internal/mutation/mutation_test.go

-83
Original file line numberDiff line numberDiff line change
@@ -187,89 +187,6 @@ func TestApplyResourceMutation(t *testing.T) {
187187
},
188188
expected: `{"spec":[1,3]}`,
189189
},
190-
191-
// Rudi
192-
193-
{
194-
name: "Rudi: empty script",
195-
inputData: `{"spec":{"secretName":"foo"}}`,
196-
mutation: syncagentv1alpha1.ResourceMutation{
197-
Rudi: &syncagentv1alpha1.ResourceRudiMutation{
198-
Script: `.`,
199-
},
200-
},
201-
expected: `{"spec":{"secretName":"foo"}}`,
202-
},
203-
{
204-
name: "Rudi: set one new key",
205-
inputData: `{"spec":{"secretName":"foo"}}`,
206-
mutation: syncagentv1alpha1.ResourceMutation{
207-
Rudi: &syncagentv1alpha1.ResourceRudiMutation{
208-
Script: `(set! .foo "bar")`,
209-
},
210-
},
211-
expected: `{"foo":"bar","spec":{"secretName":"foo"}}`,
212-
},
213-
{
214-
name: "Rudi: update existing key",
215-
inputData: `{"spec":{"secretName":"foo"}}`,
216-
mutation: syncagentv1alpha1.ResourceMutation{
217-
Rudi: &syncagentv1alpha1.ResourceRudiMutation{
218-
Script: `(set! .spec.secretName "bar")`,
219-
},
220-
},
221-
expected: `{"spec":{"secretName":"bar"}}`,
222-
},
223-
{
224-
name: "Rudi: remove a key",
225-
inputData: `{"spec":{"secretName":"foo"}}`,
226-
mutation: syncagentv1alpha1.ResourceMutation{
227-
Rudi: &syncagentv1alpha1.ResourceRudiMutation{
228-
Script: `(delete! .spec.secretName)`,
229-
},
230-
},
231-
expected: `{"spec":{}}`,
232-
},
233-
{
234-
name: "Rudi: result value is ignored, only document counts",
235-
inputData: `{"spec":{"secretName":"foo"}}`,
236-
mutation: syncagentv1alpha1.ResourceMutation{
237-
Rudi: &syncagentv1alpha1.ResourceRudiMutation{
238-
Script: `(delete! .spec.secretName) false`,
239-
},
240-
},
241-
expected: `{"spec":{}}`,
242-
},
243-
{
244-
name: "Rudi: local object becomes $localObj",
245-
inputData: `{"spec":{"secretName":"foo"}}`,
246-
mutation: syncagentv1alpha1.ResourceMutation{
247-
Rudi: &syncagentv1alpha1.ResourceRudiMutation{
248-
Script: `(set! .spec $localObj.local)`,
249-
},
250-
},
251-
ctx: &TemplateMutationContext{
252-
LocalObject: map[string]any{
253-
"local": "data",
254-
},
255-
},
256-
expected: `{"spec":"data"}`,
257-
},
258-
{
259-
name: "Rudi: remote object becomes $remoteObj",
260-
inputData: `{"spec":{"secretName":"foo"}}`,
261-
mutation: syncagentv1alpha1.ResourceMutation{
262-
Rudi: &syncagentv1alpha1.ResourceRudiMutation{
263-
Script: `(set! .spec $remoteObj.remote)`,
264-
},
265-
},
266-
ctx: &TemplateMutationContext{
267-
RemoteObject: map[string]any{
268-
"remote": "data",
269-
},
270-
},
271-
expected: `{"spec":"data"}`,
272-
},
273190
}
274191

275192
for _, testcase := range testcases {

Diff for: internal/sync/syncer_related.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (s *ResourceSyncer) processRelatedResource(log *zap.SugaredLogger, stateSto
150150
// sure we can clean up properly
151151
blockSourceDeletion: relRes.Origin == "kcp",
152152
// apply mutation rules configured for the related resource
153-
mutator: mutation.NewMutator(nil), // relRes.Mutation
153+
mutator: mutation.NewMutator(relRes.Mutation),
154154
}
155155

156156
requeue, err = syncer.Sync(log, sourceSide, destSide)

Diff for: sdk/apis/syncagent/v1alpha1/published_resource.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ type PublishedResourceSpec struct {
8585

8686
// Mutation allows to configure "rewrite rules" to modify the objects in both
8787
// directions during the synchronization.
88-
// This is currently disabled in order not to introduce too much freedom in the MVP
89-
// and instead rely on Crossplane or other solutions.
90-
// Mutation *ResourceMutationSpec `json:"mutation,omitempty"`
88+
Mutation *ResourceMutationSpec `json:"mutation,omitempty"`
9189

9290
Related []RelatedResourceSpec `json:"related,omitempty"`
9391
}
@@ -138,16 +136,11 @@ type ResourceMutation struct {
138136
// Must use exactly one of these options, never more, never fewer.
139137
// TODO: Add validation code for this somewhere.
140138

141-
Rudi *ResourceRudiMutation `json:"rudi,omitempty"`
142139
Delete *ResourceDeleteMutation `json:"delete,omitempty"`
143140
Regex *ResourceRegexMutation `json:"regex,omitempty"`
144141
Template *ResourceTemplateMutation `json:"template,omitempty"`
145142
}
146143

147-
type ResourceRudiMutation struct {
148-
Script string `json:"script"`
149-
}
150-
151144
type ResourceDeleteMutation struct {
152145
Path string `json:"path"`
153146
}
@@ -181,9 +174,8 @@ type RelatedResourceSpec struct {
181174
Reference RelatedResourceReference `json:"reference"`
182175

183176
// Mutation configures optional transformation rules for the related resource.
184-
// Status mutations are not supported and are ignored.
185-
// This is disabled for the same reason the mutations for the main resource are disabled.
186-
// Mutation *ResourceMutationSpec `json:"mutation,omitempty"`
177+
// Status mutations are only performed when the related resource originates in kcp.
178+
Mutation *ResourceMutationSpec `json:"mutation,omitempty"`
187179
}
188180

189181
type RelatedResourceReference struct {

0 commit comments

Comments
 (0)