Skip to content
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

Allow Starlark source to be defined in Kptfile #2575

Closed
FrenchBen opened this issue Nov 6, 2021 · 2 comments
Closed

Allow Starlark source to be defined in Kptfile #2575

FrenchBen opened this issue Nov 6, 2021 · 2 comments
Assignees
Labels
area/fn-catalog Functions Catalog enhancement New feature or request triaged Issue has been triaged by adding an `area/` label

Comments

@FrenchBen
Copy link

Describe your problem

functionConfig currently supports 2 approaches:

  • Run via Kptfile
  • Run via CLI arguments

While these 2 approaches provide some flexibility in testing, it also creates inequality for certain functions, more specifically Starlark.

Example

Taking the core example of Starlark Configmap

# fn-set-replicas.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: set-replicas
data:
  replicas: "5"
  source: |
    def setReplicas(resources, replicas):
      for r in resources:
        if r["apiVersion"] == "apps/v1" and r["kind"] == "Deployment":
          r["spec"]["replicas"] = replicas
    replicas = ctx.resource_list["functionConfig"]["data"]["replicas"]
    setReplicas(ctx.resource_list["items"], replicas)

When running this, a few options are available:

  1. Call it via config map: kpt fn eval --image gcr.io/kpt-fn/starlark:v0.3 --fn-config fn-set-replicas.yaml
  2. Add it to your Kptfile, and call kpt fn render
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
  name: sample-pkg
info:
  description: sample description
pipeline:
  mutators:
    - image: gcr.io/kpt-fn/starlark:v0.3
      configPath: fn-set-replicas.yaml

But a 3rd option exists, which is to extract the Starlark code into a much more user-friendly (and editor parsable) file:

#set-replicas.star
    def setReplicas(resources, replicas):
      for r in resources:
        if r["apiVersion"] == "apps/v1" and r["kind"] == "Deployment":
          r["spec"]["replicas"] = replicas
    replicas = ctx.resource_list["functionConfig"]["data"]["replicas"]
    setReplicas(ctx.resource_list["items"], replicas)

Then run it via:
kpt fn eval --image gcr.io/kpt-fn/starlark:v0.3 -- source="$(cat set-replicas.star)" replicas=5

The problem is that you cannot then bring this back into your Kptfile, forcing you to copy this back into your awkward yaml file.

Solution

Add support for Starlark file parsing, from a Kptfile. Similar to the configPath or configMap option, add a sourcePath option where a .star file path could be declared.

apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
  name: sample-pkg
info:
  description: sample description
pipeline:
  mutators:
    - image: gcr.io/kpt-fn/starlark:v0.3
      sourcePath: set-replicas.star
      configMap:
          replicas: 5

Which would result in something like:

# set-replicas.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: set-replicas
data:
  replicas: "5"
  source: | <insert-content-from-sourcePath>
@FrenchBen FrenchBen added the enhancement New feature or request label Nov 6, 2021
@droot droot added triaged Issue has been triaged by adding an `area/` label area/fn-sdk Typescript SDK labels Nov 8, 2021
@mengqiy mengqiy added area/fn-catalog Functions Catalog and removed area/fn-sdk Typescript SDK triaged Issue has been triaged by adding an `area/` label labels Nov 8, 2021
@droot
Copy link
Contributor

droot commented Nov 10, 2021

Another solution that has been discussed: include-file support

@droot droot added the triaged Issue has been triaged by adding an `area/` label label Nov 10, 2021
@FrenchBen
Copy link
Author

Closing as duplicate of #2350

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/fn-catalog Functions Catalog enhancement New feature or request triaged Issue has been triaged by adding an `area/` label
Projects
None yet
Development

No branches or pull requests

3 participants