Skip to content
Open
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
8 changes: 4 additions & 4 deletions claimconditions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xpv1 "github.com/crossplane/crossplane-runtime/v2/apis/common/v1"
"github.com/crossplane/function-sdk-go/errors"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/response"
Expand Down Expand Up @@ -43,12 +43,12 @@ func UpdateClaimConditions(rsp *fnv1.RunFunctionResponse, conditions ...Targeted
// transformCondition converts a TargetedCondition to be compatible with the Protobuf SDK
func transformCondition(tc TargetedCondition) *fnv1.Condition {
c := &fnv1.Condition{
Type: string(tc.Condition.Type),
Reason: string(tc.Condition.Reason),
Type: string(tc.Type),
Reason: string(tc.Reason),
Target: transformTarget(tc.Target),
}

switch tc.Condition.Status {
switch tc.Status {
case corev1.ConditionTrue:
c.Status = fnv1.Status_STATUS_CONDITION_TRUE
case corev1.ConditionFalse:
Expand Down
4 changes: 2 additions & 2 deletions claimconditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"reflect"
"testing"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/test"
xpv1 "github.com/crossplane/crossplane-runtime/v2/apis/common/v1"
"github.com/crossplane/crossplane-runtime/v2/pkg/test"
"github.com/crossplane/function-sdk-go/errors"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/google/go-cmp/cmp"
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"testing"

"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/resource"
"github.com/google/go-cmp/cmp"
Expand Down
35 changes: 31 additions & 4 deletions extraresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"maps"

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/response"
Expand All @@ -29,6 +29,8 @@ type ExtraResourcesRequirement struct {
// MatchName defines the name to match the resource, if MatchLabels is
// empty.
MatchName string `json:"matchName,omitempty"`
// Namespace defines the namespace of the resource to match, leave empty for cluster-scoped.
Namespace string `json:"namespace,omitempty"`
}

const (
Expand All @@ -51,18 +53,43 @@ func (e *ExtraResourcesRequirement) ToResourceSelector() *fnv1.ResourceSelector
out.Match = &fnv1.ResourceSelector_MatchName{
MatchName: e.MatchName,
}

if e.Namespace != "" {
*out.Namespace = e.Namespace
}
return out
}

func mergeExtraResourcesToContext(req *fnv1.RunFunctionRequest, rsp *fnv1.RunFunctionResponse) error {
b, err := json.Marshal(req.ExtraResources)
b, err := json.Marshal(req.ExtraResources) //nolint:staticcheck
if err != nil {
return errors.Errorf("cannot marshal %T: %w", req.ExtraResources, err) //nolint:staticcheck
}

s := &structpb.Struct{}
if err := protojson.Unmarshal(b, s); err != nil {
return errors.Errorf("cannot unmarshal %T into %T: %w", req.ExtraResources, s, err) //nolint:staticcheck
}

extraResourcesFromContext, exists := request.GetContextKey(req, extraResourcesContextKey)
if exists {
merged := mergeStructs(extraResourcesFromContext.GetStructValue(), s)
s = merged
}

response.SetContextKey(rsp, extraResourcesContextKey, structpb.NewStructValue(s))
return nil
}

func mergeRequiredResourcesToContext(req *fnv1.RunFunctionRequest, rsp *fnv1.RunFunctionResponse) error {
b, err := json.Marshal(req.RequiredResources)
if err != nil {
return errors.Errorf("cannot marshal %T: %w", req.ExtraResources, err)
return errors.Errorf("cannot marshal %T: %w", req.RequiredResources, err)
}

s := &structpb.Struct{}
if err := protojson.Unmarshal(b, s); err != nil {
return errors.Errorf("cannot unmarshal %T into %T: %w", req.ExtraResources, s, err)
return errors.Errorf("cannot unmarshal %T into %T: %w", req.RequiredResources, s, err)
}

extraResourcesFromContext, exists := request.GetContextKey(req, extraResourcesContextKey)
Expand Down
35 changes: 25 additions & 10 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/yaml"

"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/v2/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/v2/pkg/meta"

"github.com/crossplane/function-sdk-go/errors"
"github.com/crossplane/function-sdk-go/logging"
Expand Down Expand Up @@ -134,12 +134,12 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
if yamlErr == (YamlErrorContext{}) {
newErr = err
} else {
context := strings.TrimSpace(yamlErr.Context)
if len(context) > 80 {
context = context[:80] + "..."
ctx := strings.TrimSpace(yamlErr.Context)
if len(ctx) > 80 {
ctx = ctx[:80] + "..."
}

newErr = fmt.Errorf("error converting YAML to JSON: yaml: line %d (document %d, line %d) near: '%s': %s", yamlErr.AbsLine, docIndex+1, yamlErr.RelLine, context, yamlErr.Message)
newErr = fmt.Errorf("error converting YAML to JSON: yaml: line %d (document %d, line %d) near: '%s': %s", yamlErr.AbsLine, docIndex+1, yamlErr.RelLine, ctx, yamlErr.Message)
}

response.Fatal(rsp, errors.Wrap(newErr, "cannot decode manifest"))
Expand Down Expand Up @@ -274,11 +274,11 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
return rsp, nil
}
for k, v := range ers {
if _, found := requirements.ExtraResources[k]; found {
if _, found := requirements.ExtraResources[k]; found { // nolint:staticcheck // need to support Crossplane v1
response.Fatal(rsp, errors.Errorf("duplicate extra resource key %q", k))
return rsp, nil
}
requirements.ExtraResources[k] = v.ToResourceSelector()
requirements.ExtraResources[k] = v.ToResourceSelector() // nolint:staticcheck // need to support Crossplane v1
}
default:
response.Fatal(rsp, errors.Errorf("invalid kind %q for apiVersion %q - must be one of CompositeConnectionDetails, Context or ExtraResources", obj.GetKind(), metaApiVersion))
Expand Down Expand Up @@ -327,17 +327,24 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
return rsp, nil
}

if len(requirements.ExtraResources) > 0 {
if len(requirements.ExtraResources) > 0 { // nolint:staticcheck // need to support Crossplane v1
rsp.Requirements = requirements
}

if len(req.ExtraResources) > 0 {
if len(req.ExtraResources) > 0 { // nolint:staticcheck // need to support Crossplane v1
err = mergeExtraResourcesToContext(req, rsp)
if err != nil {
return rsp, nil
}
}

if len(req.RequiredResources) > 0 {
err = mergeRequiredResourcesToContext(req, rsp)
if err != nil {
return rsp, nil
}
}

f.log.Debug("Successfully composed desired resources", "source", in.Source, "count", len(objs))

return rsp, nil
Expand All @@ -354,6 +361,14 @@ func convertToMap(req *fnv1.RunFunctionRequest) (map[string]any, error) {
return nil, errors.Wrap(err, "cannot unmarshal json to map[string]any")
}

_, ok := mReq["extraResources"]
if !ok {
r, ok := mReq["requiredResources"]
if ok {
mReq["extraResources"] = r
}
}

return mReq, nil
}

Expand Down
4 changes: 2 additions & 2 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"google.golang.org/protobuf/types/known/durationpb"
"k8s.io/utils/ptr"

"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"

fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/resource"
Expand Down Expand Up @@ -1249,7 +1249,7 @@ func TestRunFunction(t *testing.T) {
Source: v1beta1.InlineSource,
Inline: &v1beta1.TemplateSourceInline{Template: extraResource},
}),
ExtraResources: map[string]*fnv1.Resources{
RequiredResources: map[string]*fnv1.Resources{
"cool-extra-resource": {
Items: []*fnv1.Resource{
{
Expand Down
6 changes: 3 additions & 3 deletions function_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

sprig "github.com/Masterminds/sprig/v3"
"github.com/crossplane-contrib/function-go-templating/input/v1beta1"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
xpv1 "github.com/crossplane/crossplane-runtime/v2/apis/common/v1"
"github.com/crossplane/crossplane-runtime/v2/pkg/fieldpath"
"github.com/crossplane/function-sdk-go/errors"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -141,7 +141,7 @@ func getCompositeResource(req map[string]any) map[string]any {

func getExtraResources(req map[string]any, name string) []any {
var ers []any
path := fmt.Sprintf("extraResources[%s].items", name)
path := fmt.Sprintf("requiredResources[%s].items", name)
if err := fieldpath.Pave(req).GetValueInto(path, &ers); err != nil {
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions function_maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/testing/protocmp"

v1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
v1 "github.com/crossplane/crossplane-runtime/v2/apis/common/v1"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
)

Expand Down Expand Up @@ -533,7 +533,7 @@ func Test_getExtraResources(t *testing.T) {
reason: "Should successfully retrieve the complete resource",
args: args{
req: map[string]any{
"extraResources": map[string]any{
"requiredResources": map[string]any{
"flexserver": map[string]any{
"items": []any{
completeResource,
Expand All @@ -553,7 +553,7 @@ func Test_getExtraResources(t *testing.T) {
reason: "Should return empty list if no extra resources are found",
args: args{
req: map[string]any{
"extraResources": map[string]any{
"requiredResources": map[string]any{
"flexserver": map[string]any{
"items": []any{},
},
Expand Down
78 changes: 44 additions & 34 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,90 @@ require (
dario.cat/mergo v1.0.2
github.com/Masterminds/sprig/v3 v3.3.0
github.com/alecthomas/kong v1.13.0
github.com/crossplane/crossplane-runtime v1.20.0
github.com/crossplane/function-sdk-go v0.4.0
github.com/crossplane/crossplane-runtime/v2 v2.1.0
github.com/crossplane/function-sdk-go v0.5.0
github.com/google/go-cmp v0.7.0
google.golang.org/protobuf v1.36.10
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.31.0
k8s.io/apimachinery v0.31.0
k8s.io/api v0.34.0
k8s.io/apimachinery v0.34.0
k8s.io/utils v0.0.0-20251002143259-bc988d571ff4
sigs.k8s.io/controller-tools v0.16.0
sigs.k8s.io/controller-tools v0.18.0
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.3.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
github.com/evanphx/json-patch/v5 v5.9.11 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/go-json-experiment/json v0.0.0-20240815175050-ebd3a8989ca1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gobuffalo/flect v1.0.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gobuffalo/flect v1.0.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.1.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.0 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/prometheus/client_golang v1.22.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/cobra v1.10.1 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.45.0 // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/net v0.47.0 // indirect
golang.org/x/oauth2 v0.27.0 // indirect
golang.org/x/oauth2 v0.30.0 // indirect
golang.org/x/sync v0.18.0 // indirect
golang.org/x/sys v0.38.0 // indirect
golang.org/x/term v0.37.0 // indirect
golang.org/x/text v0.31.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/time v0.11.0 // indirect
golang.org/x/tools v0.38.0 // indirect
golang.org/x/tools/go/packages/packagestest v0.1.1-deprecated // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.67.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20251007200510-49b9836ed3ff // indirect
google.golang.org/grpc v1.75.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/client-go v0.31.0 // indirect
k8s.io/apiextensions-apiserver v0.34.0 // indirect
k8s.io/client-go v0.34.0 // indirect
k8s.io/code-generator v0.34.0 // indirect
k8s.io/gengo/v2 v2.0.0-20250604051438-85fd79dbfd9f // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
sigs.k8s.io/controller-runtime v0.19.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect
sigs.k8s.io/controller-runtime v0.22.0 // indirect
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
sigs.k8s.io/yaml v1.6.0 // indirect
)
Loading