-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support loading starlib in starlark fn
- Loading branch information
Mengqi Yu
committed
Sep 8, 2021
1 parent
a608154
commit 5bf91e0
Showing
21 changed files
with
296 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
diff --git a/app.yaml b/app.yaml | ||
index b6ca925..d697d2f 100644 | ||
--- a/app.yaml | ||
+++ b/app.yaml | ||
@@ -5,7 +5,7 @@ metadata: | ||
annotations: | ||
last-applied: '{"spec":{"replicas":3}}' | ||
spec: | ||
- replicas: 1 | ||
+ replicas: 4 | ||
template: | ||
spec: | ||
containers: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.expected | ||
results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
apiVersion: kpt.dev/v1 | ||
kind: Kptfile | ||
metadata: | ||
name: example | ||
pipeline: | ||
mutators: | ||
- image: gcr.io/kpt-fn/starlark:unstable | ||
configPath: fn-config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# starlark: Load Library | ||
|
||
### Overview | ||
|
||
In this example, we are going to demonstrate how to load a library in the | ||
[`starlark`] function. | ||
|
||
### Fetch the example package | ||
|
||
Get the example package by running the following commands: | ||
|
||
```shell | ||
$ kpt pkg get https://github.com/GoogleContainerTools/kpt-functions-catalog.git/examples/starlark-load-library | ||
``` | ||
|
||
We are going to use the following `Kptfile` and `fn-config.yaml` to configure | ||
the function: | ||
|
||
```yaml | ||
apiVersion: kpt.dev/v1 | ||
kind: Kptfile | ||
metadata: | ||
name: example | ||
pipeline: | ||
mutators: | ||
- image: gcr.io/kpt-fn/starlark:unstable | ||
configPath: fn-config.yaml | ||
``` | ||
```yaml | ||
# fn-config.yaml | ||
apiVersion: fn.kpt.dev/v1alpha1 | ||
kind: StarlarkRun | ||
metadata: | ||
name: set-namespace-to-prod | ||
source: | | ||
load('encoding/json.star', 'json') | ||
def updateReplicas(resources): | ||
for resource in resources: | ||
if resource["kind"] == "Deployment": | ||
obj = json.decode(resource["metadata"]["annotations"]["last-applied"]) | ||
resource["spec"]["replicas"] = obj["spec"]["replicas"]+1 | ||
updateReplicas(ctx.resource_list["items"]) | ||
``` | ||
We load the json library by `load('encoding/json.star', 'json')`. Then we invoke | ||
the `json.decode` method to deserialize the content from an annotation. | ||
|
||
### Function invocation | ||
|
||
Invoke the function by running the following commands: | ||
|
||
```shell | ||
$ kpt fn render starlark-load-library | ||
``` | ||
|
||
### Expected result | ||
|
||
Check the `.spec.replicas` field should have been updated to 4. | ||
|
||
[`starlark`]: https://catalog.kpt.dev/starlark/v0.1/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: my-nginx | ||
annotations: | ||
last-applied: '{"spec":{"replicas":3}}' | ||
spec: | ||
replicas: 1 | ||
template: | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.1 | ||
ports: | ||
- containerPort: 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: fn.kpt.dev/v1alpha1 | ||
kind: StarlarkRun | ||
metadata: | ||
name: set-namespace-to-prod | ||
source: | | ||
load('encoding/json.star', 'json') | ||
def updateReplicas(resources): | ||
for resource in resources: | ||
if resource["kind"] == "Deployment": | ||
obj = json.decode(resource["metadata"]["annotations"]["last-applied"]) | ||
resource["spec"]["replicas"] = obj["spec"]["replicas"]+1 | ||
updateReplicas(ctx.resource_list["items"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
functions/go/starlark/third_party/sigs.k8s.io/kustomize/kyaml/fn/runtime/starlark/loadlib.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package starlark | ||
|
||
import ( | ||
"github.com/qri-io/starlib/bsoup" | ||
"github.com/qri-io/starlib/encoding/base64" | ||
"github.com/qri-io/starlib/encoding/csv" | ||
"github.com/qri-io/starlib/encoding/json" | ||
"github.com/qri-io/starlib/encoding/yaml" | ||
"github.com/qri-io/starlib/geo" | ||
"github.com/qri-io/starlib/hash" | ||
"github.com/qri-io/starlib/html" | ||
"github.com/qri-io/starlib/http" | ||
"github.com/qri-io/starlib/math" | ||
"github.com/qri-io/starlib/re" | ||
"github.com/qri-io/starlib/time" | ||
"github.com/qri-io/starlib/xlsx" | ||
"github.com/qri-io/starlib/zipfile" | ||
"go.starlark.net/starlark" | ||
) | ||
|
||
// load loads starlark libraries from https://github.com/qri-io/starlib#packages. | ||
func load(_ *starlark.Thread, module string) (starlark.StringDict, error) { | ||
switch module { | ||
case bsoup.ModuleName: | ||
return bsoup.LoadModule() | ||
case base64.ModuleName: | ||
return base64.LoadModule() | ||
case csv.ModuleName: | ||
return csv.LoadModule() | ||
case json.ModuleName: | ||
return starlark.StringDict{"json": json.Module}, nil | ||
case yaml.ModuleName: | ||
return yaml.LoadModule() | ||
case geo.ModuleName: | ||
return geo.LoadModule() | ||
case hash.ModuleName: | ||
return hash.LoadModule() | ||
case html.ModuleName: | ||
return html.LoadModule() | ||
case http.ModuleName: | ||
return http.LoadModule() | ||
case math.ModuleName: | ||
return starlark.StringDict{"math": math.Module}, nil | ||
case re.ModuleName: | ||
return re.LoadModule() | ||
case time.ModuleName: | ||
return starlark.StringDict{"time": time.Module}, nil | ||
case xlsx.ModuleName: | ||
return xlsx.LoadModule() | ||
case zipfile.ModuleName: | ||
return zipfile.LoadModule() | ||
} | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7fb55e20cd4f03912c7a77682ecaea1bd46a03a43b542908b917d21e3fe90c50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"registry-1.docker.io": { | ||
"Seed": "tORtWHykzW3E1tslm0aGVw==" | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exitCode: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.expected | ||
results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
apiVersion: kpt.dev/v1 | ||
kind: Kptfile | ||
metadata: | ||
name: example | ||
pipeline: | ||
mutators: | ||
- image: gcr.io/kpt-fn/starlark:unstable | ||
configPath: fn-config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: apps/v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: my-nginx | ||
data: | ||
foo: bar |
Oops, something went wrong.