-
Notifications
You must be signed in to change notification settings - Fork 257
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 5 commits
4ee996a
e51e4c5
c4f465b
a1fce5e
390ab84
839d2ff
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,89 @@ | ||
| // 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" | ||
| "sigs.k8s.io/kustomize/api/filters/replacement" | ||
| "sigs.k8s.io/kustomize/api/types" | ||
| "sigs.k8s.io/kustomize/kyaml/fn/framework" | ||
| "sigs.k8s.io/kustomize/kyaml/kio" | ||
| "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, _ io.Writer) error { | ||
| return framework.Execute( | ||
| framework.ResourceListProcessorFunc(Process), | ||
| &kio.ByteReadWriter{ | ||
| Reader: r, | ||
| Writer: w, | ||
| KeepReaderAnnotations: true, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| func Process(rl *framework.ResourceList) error { | ||
| rep := &Replacements{} | ||
|
|
||
| if rl.FunctionConfig == nil { | ||
| return fmt.Errorf("FunctionConfig is missing. Expect `ApplyReplacements`") | ||
| } | ||
|
|
||
| meta, err := rl.FunctionConfig.GetMeta() | ||
| if err != nil { | ||
| return fmt.Errorf("reading functionConfig metadata: %w", err) | ||
| } | ||
| if meta.Kind != fnConfigKind || meta.APIVersion != fnConfigAPIVersion { | ||
| return fmt.Errorf("received functionConfig of kind %s and apiVersion %s, only functionConfig of kind %s and apiVersion %s is supported", | ||
| meta.Kind, meta.APIVersion, fnConfigKind, fnConfigAPIVersion) | ||
| } | ||
|
|
||
| if err := yaml.Unmarshal([]byte(rl.FunctionConfig.MustString()), rep); err != nil { | ||
| return fmt.Errorf("unable to convert functionConfig to replacements: %w", err) | ||
| } | ||
|
|
||
| transformed, err := replacement.Filter{ | ||
| Replacements: rep.Replacements, | ||
| }.Filter(rl.Items) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| rl.Items = transformed | ||
| return nil | ||
| } | ||
|
|
||
| type Replacements struct { | ||
| Replacements []types.Replacement `json:"replacements,omitempty" yaml:"replacements,omitempty"` | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // 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" | ||
| "io" | ||
| "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, io.Discard) | ||
| 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, io.Discard) | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "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" | ||
| ) | ||
| 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, stderr 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.Contains(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
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // 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" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| type fakeBuiltin struct { | ||
| name string | ||
| } | ||
|
|
||
| func (f *fakeBuiltin) ImageName() string { return f.name } | ||
| func (f *fakeBuiltin) Run(_ io.Reader, _ io.Writer, _ io.Writer) error { return nil } | ||
|
|
||
| func TestNormalizeImage(t *testing.T) { | ||
| tests := []struct { | ||
| input string | ||
| expected string | ||
| }{ | ||
| {"ghcr.io/kptdev/starlark:v0.5.0", "ghcr.io/kptdev/starlark"}, | ||
| {"ghcr.io/kptdev/starlark@sha256:abc123", "ghcr.io/kptdev/starlark"}, | ||
| {"ghcr.io/kptdev/starlark:v0.5.0@sha256:abc123", "ghcr.io/kptdev/starlark"}, | ||
| {"ghcr.io/kptdev/starlark", "ghcr.io/kptdev/starlark"}, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.input, func(t *testing.T) { | ||
| assert.Equal(t, tc.expected, normalizeImage(tc.input)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRegisterAndLookup(t *testing.T) { | ||
| registry = map[string]BuiltinFunction{} | ||
|
|
||
| fn := &fakeBuiltin{name: "ghcr.io/kptdev/test:v1.0"} | ||
| Register(fn) | ||
|
|
||
| result := Lookup("ghcr.io/kptdev/test:v2.0") | ||
| assert.NotNil(t, result) | ||
| assert.Equal(t, fn, result) | ||
|
|
||
| result = Lookup("ghcr.io/kptdev/unknown") | ||
| assert.Nil(t, result) | ||
| } | ||
|
|
||
| func TestList(t *testing.T) { | ||
| registry = map[string]BuiltinFunction{} | ||
|
|
||
| Register(&fakeBuiltin{name: "ghcr.io/kptdev/fn1:v1"}) | ||
| Register(&fakeBuiltin{name: "ghcr.io/kptdev/fn2:v1"}) | ||
|
|
||
| list := List() | ||
| assert.Len(t, list, 2) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.