Skip to content

Commit

Permalink
feat: impl xr params
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Dec 20, 2023
1 parent 15f7c1e commit d181817
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ A KRM YAML list which means that each document must have an `apiVersion`, `kind`
Here's what you can do in the KCL script:

+ Return an error using `assert {condition}, {error_message}`.
+ Read the `DesiredCompositeResources` from `option("dxr")` (**Not yet implemented**).
+ Read the `ObservedCompositeResources` from `option("oxr")` (**Not yet implemented**).
+ Read the `ObservedCompositeResource` from `option("params").oxr`.
+ Read the `ObservedComposedResources` from `option("params").ocds`.
+ Read the `DesiredCompositeResource` from `option("params").dxr`.
+ Read the `DesiredComposedResources` from `option("params").dcds`.
+ Read the environment variables. e.g. `option("PATH")` (**Not yet implemented**).

## Library
Expand Down
24 changes: 24 additions & 0 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"k8s.io/apimachinery/pkg/runtime"
"kcl-lang.io/krm-kcl/pkg/kio"

fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
Expand Down Expand Up @@ -48,6 +49,14 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
response.Fatal(rsp, errors.Wrap(err, "cannot get observed composite resource"))
return rsp, nil
}
if in.Spec.Params == nil {
in.Spec.Params = make(map[string]runtime.RawExtension)
}
in.Spec.Params["oxr"], err = pkgresource.UnstructuredToRawExtension(&oxr.Resource.Unstructured)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}
log = log.WithValues(
"xr-version", oxr.Resource.GetAPIVersion(),
"xr-kind", oxr.Resource.GetKind(),
Expand All @@ -63,6 +72,11 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
}
dxr.Resource.SetAPIVersion(oxr.Resource.GetAPIVersion())
dxr.Resource.SetKind(oxr.Resource.GetKind())
in.Spec.Params["dxr"], err = pkgresource.UnstructuredToRawExtension(&dxr.Resource.Unstructured)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}

// The composed resources desired by any previous Functions in the pipeline.
desired, err := request.GetDesiredComposedResources(req)
Expand All @@ -71,6 +85,11 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
return rsp, nil
}
log.Debug(fmt.Sprintf("DesiredComposed resources: %d", len(desired)))
in.Spec.Params["dcds"], err = pkgresource.ObjToRawExtension(desired)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}

// The composed resources desired by any previous Functions in the pipeline.
observed, err := request.GetObservedComposedResources(req)
Expand All @@ -79,6 +98,11 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ
return rsp, nil
}
log.Debug(fmt.Sprintf("ObservedComposed resources: %d", len(observed)))
in.Spec.Params["ocds"], err = pkgresource.ObjToRawExtension(desired)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}

// Input Example: https://github.com/kcl-lang/krm-kcl/blob/main/examples/mutation/set-annotations/suite/good.yaml
inputBytes, outputBytes := bytes.NewBuffer([]byte{}), bytes.NewBuffer([]byte{})
Expand Down
23 changes: 23 additions & 0 deletions pkg/resource/res.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
krmyaml "kcl-lang.io/krm-kcl/pkg/yaml"
)

Expand Down Expand Up @@ -46,6 +47,28 @@ func JsonByteToUnstructured(jsonByte []byte) (*unstructured.Unstructured, error)
return u, nil
}

func UnstructuredToRawExtension(obj *unstructured.Unstructured) (runtime.RawExtension, error) {
if obj == nil {
return runtime.RawExtension{}, nil
}
raw, err := obj.MarshalJSON()
if err != nil {
return runtime.RawExtension{}, err
}
return runtime.RawExtension{Raw: raw}, nil
}

func ObjToRawExtension(obj interface{}) (runtime.RawExtension, error) {
if obj == nil {
return runtime.RawExtension{}, nil
}
raw, err := json.Marshal(obj)
if err != nil {
return runtime.RawExtension{}, err
}
return runtime.RawExtension{Raw: raw}, nil
}

// DataResourcesFromYaml returns the manifests list from the YAML stream data.
func DataResourcesFromYaml(in []byte) (result []unstructured.Unstructured, err error) {
bytes, err := krmyaml.SplitDocuments(string(in))
Expand Down

0 comments on commit d181817

Please sign in to comment.