kptdev / kpt

Automate Kubernetes Configuration Editing
https://kpt.dev
Apache License 2.0
1.69k stars 227 forks source link

Design and implementation of "built-in" functions #1905

Open frankfarzan opened 3 years ago

frankfarzan commented 3 years ago

Currently, there is no notion of built-in functions. The first-party function images in gcr.io/kpt-fn have no special meaning or treatment and are released and versioned independently from the kpt CLI. This is by design and has many benefits:

One disadvantage of this approach (See https://github.com/GoogleContainerTools/kpt/issues/1838#issuecomment-831907230) is that the user is exposed to, and needs to think about, version management for even basic functions. To address this, we can provide a porcelain on top of the existing functionality without creating fundamentally different mechanisms under the hood.

Strawman:

In addition to supporting fully specified function images e.g.:

gcr.io/kpt-fn/set-labels:v1

We can support a notation for functions in the gcr.io/kpt-fn registry, e.g.:

set-labels

This will resolve to a container image with a fully specified semver, e.g.:

gcr.io/kpt-fn/set-labels:v1.2.3

This resolution is done by lookin up manifest of functions baked into the CLI, e.g.:

"set-labels": "v1.2.3"
"apply-setters": "v2.3.4"

This manifest is is cut with the release of the CLI and contains the latest stable version of each function in kpt-fn registry at the time.

Caveats:

bgrant0607 commented 3 years ago

Related idea: Multiple functions (e.g., set namespace, name, labels, annotations) could be bundled into the same container, though I'm sure it would affect the UX some.

bgrant0607 commented 3 years ago

In addition to setters, common kustomize-style transforms (names, namespaces, labels, annotations), and common validation functions, the Starlark runtime is a prime candidate as a built-in function also.

karlkfi commented 3 years ago

To address this, we can provide a porcelain on top of the existing functionality without creating fundamentally different mechanisms under the hood.

I actually think that having a special in-process mechanism for running Starlark functions would be advantageous.

I know it used to be in-process and was moved to a container like all the other functions, but the current strategy of having all functions be in a container has some negative consequences:

If we make Starlark functions a special type of function that run in-process it would have the following benefits:

We could even integrate with Cloud Code so that syntax highlighting and IDE features work when writing Starlark in KRM.

In this world, functions would be pretty trivial to write, could be used both client-side and server-side, and would even be usable for validating and mutating webhooks. You could use the same validation functions in kpt, Config Sync, and Policy Controller. This would really unify the Anthos Config Management story around a single ecosystem of functions for both config and policy.

mikebz commented 2 years ago

I think after being in the market for a year and getting feedback and comparison to existing tools like kustomize I would advise the team to build the basic functions in. Here are some thoughts for why building them in makes sense: 1) Docker licensing has changed 2) Docker is much slower than in-binary execution. 3) The churn in basic transformers has probably slowed down a bunch.

Here are my candidates for built in (this is a subset of kustomize):

  1. Annotations
  2. Labels
  3. Namespace
  4. Prefix
  5. Suffix
  6. Imagetag
  7. ReplicaCount

I think these are generally useful and the behavior is well understood.

yuwenma commented 2 years ago

@mikebz Yes, it's a good idea to have some common functions built into Kpt and totally agree with those 3 reasons you listed. I saw you've called it out several times and here's more detailed reasons why it's not planned:

  1. We are more focusing on the WYSIWYG, which prioritizes the Porch UI experience over kpt CLI. On the porch UI side, we already have some functions builtin (starlark, set-namespace and replacement) and get the benefit.
  2. Most of the functions you listed are not quite mature yet, especially prefix/suffix. Other functions needs to add more features (e.g. https://github.com/GoogleContainerTools/kpt/issues/3444) and need better first-class identifier transformer support.
  3. I prioritize the function and SDK work according to our WYSIWYG UI critical paths. And the function execution time hasn't been a big blocker yet. While maintaining a up-to-date builtin function list is a big overhead. And we've spend many effort on it in May. It seems not to be a good timing to add the support in Kpt CLI.