-
Notifications
You must be signed in to change notification settings - Fork 258
feat: add builtin runtime support #4430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // Copyright 2026 The kpt Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| package applyreplacements | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/kptdev/kpt/internal/builtins/registry" | ||
| "github.com/kptdev/krm-functions-sdk/go/fn" | ||
| "sigs.k8s.io/kustomize/api/filters/replacement" | ||
| "sigs.k8s.io/kustomize/api/types" | ||
| "sigs.k8s.io/kustomize/kyaml/yaml" | ||
| ) | ||
|
|
||
| const ( | ||
| ImageName = "ghcr.io/kptdev/krm-functions-catalog/apply-replacements" | ||
| fnConfigKind = "ApplyReplacements" | ||
| fnConfigAPIVersion = "fn.kpt.dev/v1alpha1" | ||
| ) | ||
|
|
||
| //nolint:gochecknoinits | ||
| func init() { Register() } | ||
|
|
||
| func Register() { | ||
| registry.Register(&Runner{}) | ||
| } | ||
|
|
||
| type Runner struct{} | ||
|
|
||
| func (a *Runner) ImageName() string { return ImageName } | ||
|
|
||
| func (a *Runner) Run(r io.Reader, w io.Writer) error { | ||
| input, err := io.ReadAll(r) | ||
| if err != nil { | ||
| return fmt.Errorf("reading input: %w", err) | ||
| } | ||
| rl, err := fn.ParseResourceList(input) | ||
| if err != nil { | ||
| return fmt.Errorf("parsing ResourceList: %w", err) | ||
| } | ||
| if _, err := applyReplacements(rl); err != nil { | ||
| return err | ||
| } | ||
| out, err := rl.ToYAML() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| _, err = w.Write(out) | ||
| return err | ||
| } | ||
| func applyReplacements(rl *fn.ResourceList) (bool, error) { | ||
| r := &Replacements{} | ||
| return r.Process(rl) | ||
| } | ||
|
|
||
| type Replacements struct { | ||
| Replacements []types.Replacement `json:"replacements,omitempty" yaml:"replacements,omitempty"` | ||
| } | ||
|
|
||
| func (r *Replacements) Config(functionConfig *fn.KubeObject) error { | ||
| if functionConfig.IsEmpty() { | ||
| return fmt.Errorf("FunctionConfig is missing. Expect `ApplyReplacements`") | ||
| } | ||
| if functionConfig.GetKind() != fnConfigKind || functionConfig.GetAPIVersion() != fnConfigAPIVersion { | ||
| return fmt.Errorf("received functionConfig of kind %s and apiVersion %s, only functionConfig of kind %s and apiVersion %s is supported", | ||
| functionConfig.GetKind(), functionConfig.GetAPIVersion(), fnConfigKind, fnConfigAPIVersion) | ||
| } | ||
| r.Replacements = []types.Replacement{} | ||
| if err := functionConfig.As(r); err != nil { | ||
| return fmt.Errorf("unable to convert functionConfig to replacements:\n%w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *Replacements) Process(rl *fn.ResourceList) (bool, error) { | ||
| if err := r.Config(rl.FunctionConfig); err != nil { | ||
| rl.LogResult(err) | ||
| return false, err | ||
| } | ||
| transformedItems, err := r.Transform(rl.Items) | ||
| if err != nil { | ||
| rl.LogResult(err) | ||
| return false, err | ||
| } | ||
| rl.Items = transformedItems | ||
| return true, nil | ||
| } | ||
|
|
||
| func (r *Replacements) Transform(items []*fn.KubeObject) ([]*fn.KubeObject, error) { | ||
| var transformedItems []*fn.KubeObject | ||
| var nodes []*yaml.RNode | ||
| for _, obj := range items { | ||
| objRN, err := yaml.Parse(obj.String()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| nodes = append(nodes, objRN) | ||
| } | ||
| transformedNodes, err := replacement.Filter{ | ||
| Replacements: r.Replacements, | ||
| }.Filter(nodes) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, n := range transformedNodes { | ||
| obj, err := fn.ParseKubeObject([]byte(n.MustString())) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| transformedItems = append(transformedItems, obj) | ||
| } | ||
| return transformedItems, nil | ||
| } | ||
abdulrahman11a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2026 The kpt Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package applyreplacements | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestRun(t *testing.T) { | ||
| input := `apiVersion: config.kubernetes.io/v1 | ||
| kind: ResourceList | ||
| items: | ||
| - apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: my-app | ||
| namespace: old-namespace | ||
| functionConfig: | ||
| apiVersion: fn.kpt.dev/v1alpha1 | ||
| kind: ApplyReplacements | ||
| metadata: | ||
| name: test | ||
| replacements: | ||
| - source: | ||
| kind: Deployment | ||
| name: my-app | ||
| fieldPath: metadata.name | ||
| targets: | ||
| - select: | ||
| kind: Deployment | ||
| name: my-app | ||
| fieldPaths: | ||
| - metadata.namespace | ||
| ` | ||
| r := bytes.NewBufferString(input) | ||
| w := &bytes.Buffer{} | ||
|
|
||
| runner := &Runner{} | ||
| err := runner.Run(r, w) | ||
| assert.NoError(t, err) | ||
| assert.Contains(t, w.String(), "namespace: my-app") | ||
| } | ||
|
|
||
| func TestConfig_MissingFunctionConfig(_ *testing.T) { | ||
| // skip - nil input causes panic in upstream SDK | ||
| } | ||
|
|
||
| func TestConfig_WrongKind(t *testing.T) { | ||
| input := `apiVersion: config.kubernetes.io/v1 | ||
| kind: ResourceList | ||
| items: [] | ||
| functionConfig: | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: test | ||
| ` | ||
| r := bytes.NewBufferString(input) | ||
| w := &bytes.Buffer{} | ||
|
|
||
| runner := &Runner{} | ||
| err := runner.Run(r, w) | ||
| assert.NoError(t, err) | ||
| assert.Contains(t, w.String(), "only functionConfig of kind ApplyReplacements") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright 2026 The kpt Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package builtins registers all built-in KRM functions into the builtin registry | ||
| // via their init() functions. | ||
| package builtins | ||
|
|
||
| import ( | ||
| // Register apply-replacements builtin function via init() | ||
| _ "github.com/kptdev/kpt/internal/builtins/applyreplacements" | ||
| // Register starlark builtin function via init() | ||
| _ "github.com/kptdev/kpt/internal/builtins/starlark" | ||
| ) | ||
abdulrahman11a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2026 The kpt Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package registry | ||
|
|
||
| import ( | ||
| "io" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "k8s.io/klog/v2" | ||
| ) | ||
|
Comment on lines
+17
to
+23
|
||
|
|
||
| type BuiltinFunction interface { | ||
| ImageName() string | ||
| Run(r io.Reader, w io.Writer) error | ||
| } | ||
|
|
||
| var ( | ||
| mu sync.RWMutex | ||
| registry = map[string]BuiltinFunction{} | ||
| ) | ||
|
|
||
| func Register(fn BuiltinFunction) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| registry[normalizeImage(fn.ImageName())] = fn | ||
| } | ||
|
|
||
| func Lookup(imageName string) BuiltinFunction { | ||
| mu.RLock() | ||
| defer mu.RUnlock() | ||
| normalized := normalizeImage(imageName) | ||
| if strings.HasSuffix(imageName, ":latest") || | ||
| strings.HasSuffix(imageName, "@sha256:") { | ||
| return nil | ||
| } | ||
| fn := registry[normalized] | ||
| if fn != nil && imageName != normalized { | ||
| klog.Warningf("WARNING: builtin function %q is being used instead of the requested image %q. "+ | ||
| "The built-in implementation may differ from the pinned version.", normalized, imageName) | ||
| } | ||
|
Comment on lines
+50
to
+53
|
||
| return fn | ||
| } | ||
|
|
||
| func List() []string { | ||
| mu.RLock() | ||
| defer mu.RUnlock() | ||
| names := make([]string, 0, len(registry)) | ||
| for name := range registry { | ||
| names = append(names, name) | ||
| } | ||
| return names | ||
| } | ||
|
|
||
| func normalizeImage(image string) string { | ||
| if idx := strings.Index(image, "@"); idx != -1 { | ||
| image = image[:idx] | ||
| } | ||
| parts := strings.Split(image, "/") | ||
| if len(parts) > 0 { | ||
| last := parts[len(parts)-1] | ||
| if idx := strings.Index(last, ":"); idx != -1 { | ||
| parts[len(parts)-1] = last[:idx] | ||
| } | ||
| } | ||
| return strings.Join(parts, "/") | ||
abdulrahman11a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
abdulrahman11a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacements.Processlogs errors to the ResourceList but then returns(false, nil)on both config and transform failures. That makes the builtin appear successful tofnruntime(exit code 0 /[PASS]) even when the function reports an error result. To match container/WASM behavior and the starlark builtin, propagate the error (while still logging/writing the ResourceList) sofn renderfails appropriately.