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

feat: simply krm-kcl usage and interface. #12

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 5 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,7 @@ diff \

## FunctionConfig

There are 2 kinds of `functionConfig` supported by this function:

+ ConfigMap
+ A custom resource of kind `KCLRun`

To use a ConfigMap as the functionConfig, the KCL script source must be specified in the data.source field. Additional parameters can be specified in the data field.

Here's an example:

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: set-replicas
data:
replicas: "5"
source: |
resources = option("resource_list")
setReplicas = lambda items, replicas {
[item | {
if item.kind == "Deployment":
spec.replicas = int(replicas)
} for item in items]
}
setReplicas(resources.items or [], resources.functionConfig.data.replicas)
```

In the example above, the script accesses the replicas parameters using `option("resource_list").functionConfig.data.replicas`.

To use a KCLRun as the functionConfig, the KCL source must be specified in the source field. Additional parameters can be specified in the params field. The params field supports any complex data structure as long as it can be represented in YAML.
To use a `KCLRun` as the functionConfig, the KCL source must be specified in the source field. Additional parameters can be specified in the params field. The params field supports any complex data structure as long as it can be represented in YAML.

```yaml
apiVersion: krm.kcl.dev/v1alpha1
Expand All @@ -83,21 +54,19 @@ spec:
toAdd:
configmanagement.gke.io/managed: disabled
source: |
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
params = option("params")
toMatch = params.toMatch
toAdd = params.toAdd
[item | {
items = [item | {
# If all annotations are matched, patch more annotations
if all key, value in toMatch {
item.metadata.annotations[key] == value
}:
metadata.annotations: toAdd
} for item in items]
} for item in option("items")]
```

In the example above, the script accesses the `toMatch` parameters using `option("resource_list").functionConfig.spec.params.toMatch`.
In the example above, the script accesses the `toMatch` parameters using `option("params").toMatch`.

Besides, the `source` ield supports different KCL sources, which can come from a local file, VCS such as github, OCI registry, http, etc. You can see the specific usage [here](./pkg/options/testdata/). Take an OCI source as the example.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.1
0.4.0
2 changes: 1 addition & 1 deletion examples/abstraction/web-service/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ spec:
[deployment, if a.service: service]
}

kubernetesRender(params)
items = kubernetesRender(params)
5 changes: 2 additions & 3 deletions examples/mutation/conditionally-add-annotations/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ spec:
configmanagement.gke.io/managed: disabled
source: |
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
toMatch = params.toMatch
toAdd = params.toAdd
[item | {
items = [item | {
# If all annotations are matched, patch more annotations
if all key, value in toMatch {
item.metadata.annotations[key] == value
}:
metadata.annotations: toAdd
} for item in items]
} for item in resource.items]
5 changes: 2 additions & 3 deletions examples/mutation/conditionally-add-labels/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ spec:
configmanagement.gke.io/managed: disabled
source: |
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
toMatch = params.toMatch
toAdd = params.toAdd
[item | {
items = [item | {
# If all labels are matched, patch more labels
if all key, value in toMatch {
item.metadata.labels[key] == value
}:
metadata.labels: toAdd
} for item in items]
} for item in resource.items]
5 changes: 2 additions & 3 deletions examples/mutation/set-annotations/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ spec:
config.kubernetes.io/local-config: "true"
source: |
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
# Use `k = v` to override existing annotations
annotations = {k = v for k, v in params.annotations}
[item | {
items = [item | {
metadata.annotations: annotations
} for item in items]
} for item in resource.items]
5 changes: 2 additions & 3 deletions examples/mutation/set-labels/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ spec:
config.kubernetes.io/local-config: "true"
source: |
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
# Use `k = v` to override existing labels
labels = {k = v for k, v in params.labels}
[item | {
items = [item | {
metadata.labels: labels
} for item in items]
} for item in resource.items]
5 changes: 2 additions & 3 deletions examples/mutation/set-replicas/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ spec:
replicas: 5
source: |
resource = option("resource_list")
items = resource.items
replicas = resource.functionConfig.spec.params.replicas
setReplicas = lambda items, replicas: int {
setReplicas = lambda items: [], replicas: int {
[item | {
if item.kind == "Deployment":
spec.replicas = replicas
} for item in items]
}
setReplicas(resources.items or [], replicas)
items = setReplicas(resources.items or [], replicas)
3 changes: 1 addition & 2 deletions examples/validation/external-ips/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ spec:
source: |
# Construct resource and params
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
# Define the validation function
validate_external_ips = lambda item, allowedIps: [str] {
Expand All @@ -26,4 +25,4 @@ spec:
item
}
# Validate All resource
[validate_external_ips(i, params.allowedIps or []) for i in items]
items = [validate_external_ips(i, params.allowedIps or []) for i in resource.items]
3 changes: 1 addition & 2 deletions examples/validation/https-only/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ spec:
source: |
# Construct resource and params
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
# Define the validation function
validate_https_only = lambda item {
Expand All @@ -25,4 +24,4 @@ spec:
item
}
# Validate All resource
[validate_https_only(i) for i in items]
items = [validate_https_only(i) for i in resource.items]
3 changes: 1 addition & 2 deletions examples/validation/replica-limits/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ spec:
source: |
# Construct resource and params
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
min_replicas: int = params.min_replicas or 0
max_replicas: int = params.max_replicas or 99999
Expand All @@ -27,4 +26,4 @@ spec:
item
}
# Validate All resource
[validate_replica_limit(i, min_replicas, max_replicas) for i in items]
items = [validate_replica_limit(i, min_replicas, max_replicas) for i in resource.items]
3 changes: 1 addition & 2 deletions examples/validation/required-annotations/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ spec:
source: |
# Construct resource and params
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
requires = params.requires or []
# Define the validation function
Expand All @@ -33,4 +32,4 @@ spec:
item
}
# Validate All resource
[validate_required_annotations(i, requires) for i in items]
items = [validate_required_annotations(i, requires) for i in resource.items]
3 changes: 1 addition & 2 deletions examples/validation/required-labels/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ spec:
source: |
# Construct resource and params
resource = option("resource_list")
items = resource.items
params = resource.functionConfig.spec.params
requires = params.requires or []
# Define the validation function
Expand All @@ -33,4 +32,4 @@ spec:
item
}
# Validate All resource
[validate_required_labels(i, requires) for i in items]
items = [validate_required_labels(i, requires) for i in resource.items]
31 changes: 22 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ go 1.19

require (
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20230427202446-3255accc518d
github.com/Masterminds/sprig/v3 v3.2.3
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/hashicorp/go-getter v1.7.1
github.com/stretchr/testify v1.8.4
k8s.io/api v0.27.3
k8s.io/apimachinery v0.27.3
kcl-lang.io/kcl-go v0.5.2
kcl-lang.io/kpm v0.3.2
kcl-lang.io/kpm v0.3.3-0.20230807021350-5a53906c5c3e
sigs.k8s.io/kustomize/kyaml v0.14.3
)

Expand All @@ -23,15 +22,20 @@ require (
cloud.google.com/go/storage v1.28.1 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/GoogleContainerTools/kpt-functions-sdk/go/api v0.0.0-20220720212527-133180134b93 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/alexflint/go-arg v1.4.3 // indirect
github.com/alexflint/go-scalar v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/jsonv v1.1.3 // indirect
github.com/chai2010/protorpc v1.1.4 // indirect
github.com/cloudflare/circl v1.1.0 // indirect
github.com/containerd/containerd v1.7.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -42,7 +46,11 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/go-git/go-git/v5 v5.6.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand All @@ -63,20 +71,19 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/moby/locker v1.0.1 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -85,34 +92,40 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect
github.com/otiai10/copy v1.9.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/powerman/rpc-codec v1.2.2 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.110.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
Expand Down
Loading
Loading