|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package transformer |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/google/cel-go/cel" |
| 23 | + "github.com/google/cel-go/checker/decls" |
| 24 | + "github.com/tidwall/gjson" |
| 25 | + "github.com/tidwall/sjson" |
| 26 | + |
| 27 | + syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1" |
| 28 | + |
| 29 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 30 | +) |
| 31 | + |
| 32 | +type celTransformer struct { |
| 33 | + path string |
| 34 | + prg cel.Program |
| 35 | +} |
| 36 | + |
| 37 | +func NewCEL(mut *syncagentv1alpha1.ResourceCELMutation) (*celTransformer, error) { |
| 38 | + env, err := cel.NewEnv(cel.Declarations( |
| 39 | + decls.NewVar("self", decls.Dyn), |
| 40 | + decls.NewVar("other", decls.Dyn), |
| 41 | + decls.NewVar("value", decls.Dyn), |
| 42 | + )) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("failed to create CEL env: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + expr, issues := env.Compile(mut.Expression) |
| 48 | + if issues != nil && issues.Err() != nil { |
| 49 | + return nil, fmt.Errorf("failed to compile CEL expression: %w", issues.Err()) |
| 50 | + } |
| 51 | + |
| 52 | + prg, err := env.Program(expr) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("failed to create CEL program: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + return &celTransformer{ |
| 58 | + path: mut.Path, |
| 59 | + prg: prg, |
| 60 | + }, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (m *celTransformer) Apply(toMutate *unstructured.Unstructured, otherObj *unstructured.Unstructured) (*unstructured.Unstructured, error) { |
| 64 | + encoded, err := EncodeObject(toMutate) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to JSON encode object: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + // get the current value at the path |
| 70 | + current := gjson.Get(encoded, m.path) |
| 71 | + |
| 72 | + input := map[string]any{ |
| 73 | + "value": current.Value(), |
| 74 | + "self": toMutate.Object, |
| 75 | + "other": nil, |
| 76 | + } |
| 77 | + if otherObj != nil { |
| 78 | + input["other"] = otherObj.Object |
| 79 | + } |
| 80 | + |
| 81 | + // evaluate the expression |
| 82 | + out, _, err := m.prg.Eval(input) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("failed to evaluate CEL expression: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + // update the object |
| 88 | + updated, err := sjson.Set(encoded, m.path, out) |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("failed to set updated value: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + return DecodeObject(updated) |
| 94 | +} |
0 commit comments