Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
cd117ca
Update k8s.io/utils digest to b307cd5
renovate[bot] Nov 27, 2023
0f60986
fix(deps): update module github.com/crossplane/crossplane-runtime to …
renovate[bot] Dec 1, 2023
76b1a8a
chore(deps): update actions/setup-go action to v5
renovate[bot] Dec 6, 2023
1d3ff7b
add simple guidance for including results from previous functions
jbw976 Dec 7, 2023
2a999f0
Merge pull request #61 from crossplane-contrib/renovate/actions-setup…
phisco Dec 8, 2023
8945886
Merge pull request #60 from crossplane-contrib/renovate/github.com-cr…
phisco Dec 8, 2023
66ffa57
chore(deps): update actions/download-artifact action to v4
renovate[bot] Dec 14, 2023
f0dd584
chore(deps): update actions/upload-artifact action to v4
renovate[bot] Dec 14, 2023
aefe1e9
Merge pull request #58 from crossplane-contrib/renovate/k8s.io-utils-…
negz Jan 2, 2024
1293867
fix(deps): update kubernetes packages to v0.29.0
renovate[bot] Jan 2, 2024
1d6d623
Merge pull request #65 from crossplane-contrib/renovate/actions-downl…
negz Jan 2, 2024
e8b8d78
Merge pull request #64 from crossplane-contrib/renovate/kubernetes-go
negz Jan 2, 2024
f9ee71f
fix(deps): update module google.golang.org/protobuf to v1.32.0
renovate[bot] Jan 2, 2024
fb597eb
Merge pull request #62 from jbw976/mix-and-patch
negz Jan 2, 2024
d551eee
Merge pull request #66 from crossplane-contrib/renovate/actions-uploa…
negz Jan 2, 2024
80efd20
Fix for v4 of the upload and download artifacts actiosn
negz Jan 2, 2024
acb8025
Merge pull request #71 from crossplane-contrib/renovate/google.golang…
negz Jan 2, 2024
8bb6568
Merge pull request #73 from negz/facts
negz Jan 3, 2024
2b74c2a
fix(deps): update k8s.io/utils digest to e7106e6
renovate[bot] Jan 3, 2024
cf7a6f7
Merge pull request #72 from crossplane-contrib/renovate/k8s.io-utils-…
negz Jan 4, 2024
fa41741
Always use p.GetType(), not p.Type
negz Jan 9, 2024
11be501
Merge pull request #77 from negz/typical
phisco Jan 9, 2024
06869e5
fix(environment): Validate against correct patch types
MisterMX Jan 10, 2024
8b8f2a4
Merge pull request #78 from MisterMX/fix/environment-correct-patches
phisco Jan 11, 2024
bc836a4
fix(deps): update module sigs.k8s.io/controller-tools to v0.14.0
renovate[bot] Jan 11, 2024
50cee58
Merge pull request #79 from crossplane-contrib/renovate/sigs.k8s.io-c…
negz Jan 11, 2024
0d640e0
fix(deps): update kubernetes packages to v0.29.1
renovate[bot] Jan 18, 2024
e863479
Merge pull request #80 from crossplane-contrib/renovate/kubernetes-go
negz Jan 18, 2024
27b41e8
Treat all errors encountered while patching as fatal
negz Jan 19, 2024
6bae35a
Don't delete resources when required field paths don't exist
negz Jan 20, 2024
1004f14
Regenerate input CRD
negz Jan 20, 2024
c8bd5a1
Test handling of FieldPathNotFound errors
negz Jan 24, 2024
a7c2fe8
Fix broken example
negz Jan 24, 2024
e8c859d
fix(deps): update module github.com/crossplane/crossplane-runtime to …
renovate[bot] Jan 29, 2024
aa352e7
Merge pull request #83 from crossplane-contrib/renovate/github.com-cr…
negz Jan 29, 2024
0d69580
Merge pull request #81 from negz/fatalistic
negz Feb 1, 2024
e2d77c2
Remove RenderJSON function
negz Feb 9, 2024
f67d152
Merge pull request #89 from negz/crump
phisco Feb 13, 2024
defe62c
fix(deps): update kubernetes packages to v0.29.2
renovate[bot] Feb 15, 2024
03f2829
feat(upboundcare): switch to upboundcare
haarchri Feb 15, 2024
6d164cb
Merge pull request #1 from UpboundCare/feature/upboundcare-adoption
haarchri Feb 15, 2024
f030a76
fix(deps): update module github.com/crossplane/crossplane-runtime to …
renovate[bot] Feb 21, 2024
393ab40
Merge pull request #93 from crossplane-contrib/renovate/github.com-cr…
negz Feb 21, 2024
cd151b4
fix(deps): update module github.com/crossplane/function-sdk-go to v0.2.0
renovate[bot] Feb 22, 2024
22b30dc
Merge pull request #94 from crossplane-contrib/renovate/github.com-cr…
negz Feb 22, 2024
c732bf9
Merge pull request #92 from crossplane-contrib/renovate/kubernetes-go
negz Feb 22, 2024
14ea3fd
feat: toFieldPath policies, replacing upstream MergeOptions
phisco Feb 13, 2024
0193c1d
Merge pull request #91 from phisco/respect-merge-options
negz Feb 22, 2024
511de38
add conditional rendering
stevendborrelli Jan 30, 2024
49d4297
update go modules
stevendborrelli Jan 30, 2024
51e7be6
Merge branch 'main' into cel-conditionals
stevendborrelli Feb 24, 2024
4b208e6
remove render.go
stevendborrelli Feb 24, 2024
faf3317
remove old examples
stevendborrelli Feb 24, 2024
a98d431
change push org
stevendborrelli Feb 24, 2024
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
20 changes: 12 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache: false # The golangci-lint action does its own caching.
Expand All @@ -60,7 +60,7 @@ jobs:
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}

Expand Down Expand Up @@ -116,9 +116,9 @@ jobs:
run: ./crossplane xpkg build --package-file=${{ matrix.arch }}.xpkg --package-root=package/ --embed-runtime-image-tarball=runtime-${{ matrix.arch }}.tar

- name: Upload Single-Platform Package
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: packages
name: package-${{ matrix.arch }}
path: "*.xpkg"
if-no-files-found: error
retention-days: 1
Expand All @@ -135,18 +135,22 @@ jobs:
uses: actions/checkout@v4

- name: Download Single-Platform Packages
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: packages
path: .
merge-multiple: true

- name: Setup the Crossplane CLI
run: "curl -sL https://raw.githubusercontent.com/crossplane/crossplane/master/install.sh | sh"

- name: Login to Upbound
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3
if: env.XPKG_ACCESS_ID != ''
run: "./crossplane xpkg login -a borrelli-org -t ${{ secrets.XPKG_TOKEN }}"

with:
registry: xpkg.upbound.io
username: ${{ secrets.XPKG_ACCESS_ID }}
password: ${{ secrets.XPKG_TOKEN }}

# If a version wasn't explicitly passed as a workflow_dispatch input we
# default to version v0.0.0-<git-commit-date>-<git-short-sha>, for example
# v0.0.0-20231101115142-1091066df799. This is a simple implementation of
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ linters-settings:
- default
- prefix(github.com/crossplane/crossplane-runtime)
- prefix(github.com/crossplane/function-sdk-go)
- prefix(github.com/stevendborrelli/function-conditional-patch-and-transform)
- prefix(github.com/upboundcare/function-conditional-patch-and-transform)
- blank
- dot

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# function-conditional-patch-and-transform

[![CI](https://github.com/stevendborrelli/function-conditional-patch-and-transform/actions/workflows/ci.yml/badge.svg)](https://github.com/stevendborrelli/function-conditional-patch-and-transform/actions/workflows/ci.yml) ![GitHub release (latest SemVer)](https://img.shields.io/github/release/crossplane-contrib/function-conditional-patch-and-transform)
[![CI](https://github.com/upboundcare/function-conditional-patch-and-transform/actions/workflows/ci.yml/badge.svg)](https://github.com/upboundcare/function-conditional-patch-and-transform/actions/workflows/ci.yml) ![GitHub release (latest SemVer)](https://img.shields.io/github/release/crossplane-contrib/function-conditional-patch-and-transform)

This composition function is a fork of the upstream [function-patch-and-transform](https://github.com/crossplane-contrib/function-patch-and-transform)
that adds support for Conditional invocation of the function and the rendering
Expand All @@ -18,7 +18,7 @@ metadata:
annotations:
render.crossplane.io/runtime: Development
spec:
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.4.0
package: xpkg.upbound.io/upboundcare/function-conditional-patch-and-transform:v0.4.0
```

## What this function does
Expand Down Expand Up @@ -240,9 +240,9 @@ $ crossplane xpkg build -f package --embed-runtime-image=runtime
[docs-composition]: https://docs.crossplane.io/v1.14/getting-started/provider-aws-part-2/#create-a-deployment-template
[docs-functions]: https://docs.crossplane.io/v1.14/concepts/composition-functions/
[docs-pandt]: https://docs.crossplane.io/v1.14/concepts/patch-and-transform/
[fn-go-templating]: https://github.com/stevendborrelli/function-go-templating
[fn-go-templating]: https://github.com/upboundcare/function-go-templating
[#4617]: https://github.com/crossplane/crossplane/issues/4617
[#4746]: https://github.com/crossplane/crossplane/issues/4746
[go]: https://go.dev
[docker]: https://www.docker.com
[cli]: https://docs.crossplane.io/latest/cli
[cli]: https://docs.crossplane.io/latest/cli
2 changes: 1 addition & 1 deletion condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/resource"

"github.com/stevendborrelli/function-conditional-patch-and-transform/input/v1beta1"
"github.com/upboundcare/function-conditional-patch-and-transform/input/v1beta1"
)

func TestEvaluateCondition(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"

"github.com/stevendborrelli/function-conditional-patch-and-transform/input/v1beta1"
"github.com/upboundcare/function-conditional-patch-and-transform/input/v1beta1"
)

// ConnectionDetailsExtractor extracts the connection details of a resource.
Expand Down
2 changes: 1 addition & 1 deletion connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/resource/fake"
"github.com/crossplane/crossplane-runtime/pkg/test"

"github.com/stevendborrelli/function-conditional-patch-and-transform/input/v1beta1"
"github.com/upboundcare/function-conditional-patch-and-transform/input/v1beta1"
)

func TestExtractConnectionDetails(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion examples/conditional-rendering/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ kind: Function
metadata:
name: function-conditional-patch-and-transform
spec:
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.4.0
package: xpkg.upbound.io/upboundcare/function-conditional-patch-and-transform:v0.4.0
packagePullPolicy: Always
2 changes: 1 addition & 1 deletion examples/conditional-resources/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ kind: Function
metadata:
name: function-conditional-patch-and-transform
spec:
package: xpkg.upbound.io/borrelli-org/function-conditional-patch-and-transform:v0.4.0
package: xpkg.upbound.io/upboundcare/function-conditional-patch-and-transform:v0.4.0
packagePullPolicy: Always
77 changes: 61 additions & 16 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (

"google.golang.org/protobuf/types/known/structpb"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"

Expand All @@ -17,7 +19,7 @@ import (
"github.com/crossplane/function-sdk-go/resource/composed"
"github.com/crossplane/function-sdk-go/response"

"github.com/stevendborrelli/function-conditional-patch-and-transform/input/v1beta1"
"github.com/upboundcare/function-conditional-patch-and-transform/input/v1beta1"
)

const conditionError = "Condition error"
Expand Down Expand Up @@ -131,10 +133,14 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
}

if input.Environment != nil {
// Run all patches that are from the (observed) XR to the environment or from the environment to the (desired) XR.
if err := RenderEnvironmentPatches(env, oxr.Resource, dxr.Resource, input.Environment.Patches); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot render ToEnvironment patches from the composite resource"))
return rsp, nil
// Run all patches that are from the (observed) XR to the environment or
// from the environment to the (desired) XR.
for i := range input.Environment.Patches {
p := &input.Environment.Patches[i]
if err := ApplyEnvironmentPatch(p, env, oxr.Resource, dxr.Resource); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot apply the %q environment patch at index %d", p.GetType(), i))
return rsp, nil
}
}
}

Expand Down Expand Up @@ -181,14 +187,14 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
// We want to return this resource unmutated if rendering fails.
dcd.Resource = cd.Resource.DeepCopy()
default:
if err := RenderFromJSON(dcd.Resource, t.Base.Raw); err != nil {
if err := json.Unmarshal(t.Base.Raw, dcd.Resource); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot parse base template of composed resource %q", t.Name))
return rsp, nil
}
}

ocd, ok := observed[resource.Name(t.Name)]
if ok {
ocd, exists := observed[resource.Name(t.Name)]
if exists {
existing++
log.Debug("Resource template corresponds to existing composed resource", "metadata-name", ocd.Resource.GetName())

Expand Down Expand Up @@ -224,17 +230,56 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
"name", ocd.Resource.GetName())
}

errs, store := RenderComposedPatches(ocd.Resource, dcd.Resource, oxr.Resource, dxr.Resource, env, t.Patches)
for _, err := range errs {
response.Warning(rsp, errors.Wrapf(err, "cannot render patches for composed resource %q", t.Name))
log.Info("Cannot render patches for composed resource", "warning", err)
warnings++
// Run all patches that are to a desired composed resource, or from an
// observed composed resource.
skip := false
for i := range t.Patches {
p := &t.Patches[i]
if err := ApplyComposedPatch(p, ocd.Resource, dcd.Resource, oxr.Resource, dxr.Resource, env); err != nil {
if fieldpath.IsNotFound(err) {
// This is a patch from a required field path that does not
// exist. The point of FromFieldPathPolicyRequired is to
// block creation of the new 'to' resource until the 'from'
// field path exists.
//
// The only kind of resource we could be patching to that
// might not exist at this point is a composed resource. So
// if we're patching to a composed resource that doesn't
// exist we want to avoid creating it. Otherwise, we just
// treat the patch from a required field path the same way
// we'd treat a patch from an optional field path and skip
// it.
if p.GetPolicy().GetFromFieldPathPolicy() == v1beta1.FromFieldPathPolicyRequired {
if ToComposedResource(p) && !exists {
response.Warning(rsp, errors.Wrapf(err, "not adding new composed resource %q to desired state because %q patch at index %d has 'policy.fromFieldPath: Required'", t.Name, p.GetType(), i))

// There's no point processing further patches.
// They'll either be from an observed composed
// resource that doesn't exist yet, or to a desired
// composed resource that we'll discard.
skip = true
break
}
response.Warning(rsp, errors.Wrapf(err, "cannot render composed resource %q %q patch at index %d: ignoring 'policy.fromFieldPath: Required' because 'to' resource already exists", t.Name, p.GetType(), i))
}

// If any optional field path isn't found we just skip this
// patch and move on. The path may be populated by a
// subsequent patch.
continue
}
response.Fatal(rsp, errors.Wrapf(err, "cannot render composed resource %q %q patch at index %d", t.Name, p.GetType(), i))
return rsp, nil
}
}

if store {
// Add or replace our desired resource.
desired[resource.Name(t.Name)] = dcd
// Skip adding this resource to the desired state because it doesn't
// exist yet, and a required FromFieldPath was not (yet) found.
if skip {
continue
}

desired[resource.Name(t.Name)] = dcd
}

if err := response.SetDesiredCompositeResource(rsp, dxr); err != nil {
Expand Down
Loading