diff --git a/README.md b/README.md index ca34838..802d338 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/fn.go b/fn.go index cef86a7..e92e3c6 100644 --- a/fn.go +++ b/fn.go @@ -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" @@ -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(), @@ -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) @@ -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) @@ -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{}) diff --git a/pkg/resource/res.go b/pkg/resource/res.go index 9e9cd09..261797c 100644 --- a/pkg/resource/res.go +++ b/pkg/resource/res.go @@ -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" ) @@ -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))