Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: segregate internal logic to a package #153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
*.xpkg

# Test binary, built with `go test -c`
*.test
Expand Down
3 changes: 2 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/resource"

"github.com/crossplane-contrib/function-patch-and-transform/input/v1beta1"
"github.com/crossplane-contrib/function-patch-and-transform/pt"
)

// ConnectionDetailsExtractor extracts the connection details of a resource.
Expand All @@ -33,7 +34,7 @@ func (fn ConnectionDetailsExtractorFn) ExtractConnection(cd resource.Composed, c
func ExtractConnectionDetails(cd resource.Composed, data managed.ConnectionDetails, cfgs ...v1beta1.ConnectionDetail) (managed.ConnectionDetails, error) {
out := map[string][]byte{}
for _, cfg := range cfgs {
if err := ValidateConnectionDetail(cfg); err != nil {
if err := pt.ValidateConnectionDetail(cfg); err != nil {
return nil, errors.Wrap(err, "invalid")
}
switch cfg.Type {
Expand Down
18 changes: 12 additions & 6 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

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

"github.com/crossplane/crossplane-runtime/pkg/errors"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/crossplane/function-sdk-go/response"

"github.com/crossplane-contrib/function-patch-and-transform/input/v1beta1"
"github.com/crossplane-contrib/function-patch-and-transform/pt"
)

// Function performs patch-and-transform style Composition.
Expand All @@ -29,6 +31,10 @@ type Function struct {
log logging.Logger
}

var (
internalEnvironmentGVK = schema.GroupVersionKind{Group: "internal.crossplane.io", Version: "v1alpha1", Kind: "Environment"}
)

// RunFunction runs the Function.
func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRequest) (*fnv1beta1.RunFunctionResponse, error) { //nolint:gocyclo // See below.
// This loop is fairly complex, but more readable with less abstraction.
Expand All @@ -47,7 +53,7 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe

// Our input is an opaque object nested in a Composition, so unfortunately
// it won't handle validation for us.
if err := ValidateResources(input); err != nil {
if err := pt.ValidateResources(input); err != nil {
response.Fatal(rsp, errors.Wrap(err, "invalid Function input"))
return rsp, nil
}
Expand Down Expand Up @@ -98,7 +104,7 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
return rsp, nil
}

cts, err := ComposedTemplates(input.PatchSets, input.Resources)
cts, err := pt.ComposedTemplates(input.PatchSets, input.Resources)
if err != nil {
response.Fatal(rsp, errors.Wrap(err, "cannot resolve PatchSets"))
return rsp, nil
Expand Down Expand Up @@ -128,7 +134,7 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
// 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 {
if err := pt.ApplyEnvironmentPatch(p, env, oxr.Resource, dxr.Resource); err != nil {

// Ignore not found errors if patch policy is set to Optional
if fieldpath.IsNotFound(err) && p.GetPolicy().GetFromFieldPathPolicy() == v1beta1.FromFieldPathPolicyOptional {
Expand Down Expand Up @@ -217,7 +223,7 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
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 err := pt.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
Expand All @@ -232,7 +238,7 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe
// we'd treat a patch from an optional field path and skip
// it.
if p.GetPolicy().GetFromFieldPathPolicy() == v1beta1.FromFieldPathPolicyRequired {
if ToComposedResource(p) && !exists {
if pt.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.
Expand All @@ -247,7 +253,7 @@ func (f *Function) RunFunction(ctx context.Context, req *fnv1beta1.RunFunctionRe

// 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.
// subsequent pt.
continue
}
response.Fatal(rsp, errors.Wrapf(err, "cannot render composed resource %q %q patch at index %d", t.Name, p.GetType(), i))
Expand Down
7 changes: 1 addition & 6 deletions patches.go → pt/patches.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pt

import (
"fmt"
Expand All @@ -7,7 +7,6 @@ import (
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/utils/ptr"

Expand All @@ -31,10 +30,6 @@ const (
errFmtInvalidPatchPolicy = "invalid patch policy %s"
)

var (
internalEnvironmentGVK = schema.GroupVersionKind{Group: "internal.crossplane.io", Version: "v1alpha1", Kind: "Environment"}
)

// A PatchInterface is a patch that can be applied between resources.
type PatchInterface interface {
GetType() v1beta1.PatchType
Expand Down
2 changes: 1 addition & 1 deletion patches_test.go → pt/patches_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pt

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion transforms.go → pt/transforms.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pt

import (
"crypto/sha1" //nolint:gosec // Not used for secure hashing
Expand Down
2 changes: 1 addition & 1 deletion transforms_test.go → pt/transforms_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pt

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion validate.go → pt/validate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pt

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion validate_test.go → pt/validate_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package pt

import (
"testing"
Expand Down
3 changes: 2 additions & 1 deletion ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/resource"

"github.com/crossplane-contrib/function-patch-and-transform/input/v1beta1"
"github.com/crossplane-contrib/function-patch-and-transform/pt"
)

// Error strings
Expand Down Expand Up @@ -59,7 +60,7 @@ func IsReady(_ context.Context, o ConditionedObject, rc ...v1beta1.ReadinessChec

// RunReadinessCheck runs the readiness check against the supplied object.
func RunReadinessCheck(c v1beta1.ReadinessCheck, o ConditionedObject) (bool, error) { //nolint:gocyclo // just a switch
if err := ValidateReadinessCheck(c); err != nil {
if err := pt.ValidateReadinessCheck(c); err != nil {
return false, errors.Wrap(err, errInvalidCheck)
}

Expand Down