Skip to content
Draft
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ on:

env:
# Common versions
GO_VERSION: '1.23.12'
GOLANGCI_VERSION: 'v1.62.0'
GO_VERSION: '1.24.9'
GOLANGCI_VERSION: 'v2.4.0'
DOCKER_BUILDX_VERSION: 'v0.23.0'

# These environment variables are important to the Crossplane CLI install.sh
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:
run: go mod tidy && git diff --exit-code go.mod go.sum

- name: Lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8
with:
version: ${{ env.GOLANGCI_VERSION }}

Expand Down
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
37 changes: 26 additions & 11 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 @@ -187,7 +187,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
}

// Initialize the requirements.
requirements := &fnv1.Requirements{ExtraResources: make(map[string]*fnv1.ResourceSelector)}
requirements := &fnv1.Requirements{Resources: make(map[string]*fnv1.ResourceSelector)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching from ExtraResources to Resources will break things
See crossplane/function-sdk-go#219

Copy link

@tenitski tenitski Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My PR #496 was breaking my Crossplane environment until I undid the related change ec3ecd1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's why I put it back in Draft. We need to maintain the existing API to the user, which means adding the "extraResources" key into the template context with the same content as "requiredResources". Not ideal, but here we are.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @tenitski, thanks for the reminder here! 😥


// Convert the rendered manifests to a list of desired composed resources.
for _, obj := range objs {
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.Resources[k]; found {
response.Fatal(rsp, errors.Errorf("duplicate extra resource key %q", k))
return rsp, nil
}
requirements.ExtraResources[k] = v.ToResourceSelector()
requirements.Resources[k] = v.ToResourceSelector()
}
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.Resources) > 0 {
rsp.Requirements = requirements
}

if len(req.ExtraResources) > 0 {
if len(req.ExtraResources) > 0 { // nolint:staticcheck // need to support existing clients
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
12 changes: 6 additions & 6 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 @@ -1044,7 +1044,7 @@ func TestRunFunction(t *testing.T) {
Meta: &fnv1.ResponseMeta{Ttl: durationpb.New(response.DefaultTTL)},
Results: []*fnv1.Result{},
Requirements: &fnv1.Requirements{
ExtraResources: map[string]*fnv1.ResourceSelector{
Resources: map[string]*fnv1.ResourceSelector{
"cool-extra-resource": {
ApiVersion: "example.org/v1",
Kind: "CoolExtraResource",
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 Expand Up @@ -1290,7 +1290,7 @@ func TestRunFunction(t *testing.T) {
},
},
Requirements: &fnv1.Requirements{
ExtraResources: map[string]*fnv1.ResourceSelector{
Resources: map[string]*fnv1.ResourceSelector{
"cool-extra-resource": {
ApiVersion: "example.org/v1",
Kind: "CoolExtraResource",
Expand Down Expand Up @@ -1330,7 +1330,7 @@ func TestRunFunction(t *testing.T) {
}
}
}`),
ExtraResources: map[string]*fnv1.Resources{
RequiredResources: map[string]*fnv1.Resources{
"cool-extra-resource": {
Items: []*fnv1.Resource{
{
Expand Down Expand Up @@ -1385,7 +1385,7 @@ func TestRunFunction(t *testing.T) {
},
},
Requirements: &fnv1.Requirements{
ExtraResources: map[string]*fnv1.ResourceSelector{
Resources: map[string]*fnv1.ResourceSelector{
"cool-extra-resource": {
ApiVersion: "example.org/v1",
Kind: "CoolExtraResource",
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
Loading