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

Implemented logic for function #1

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
68 changes: 59 additions & 9 deletions example/composition.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: function-template-go
name: function-switcher
spec:
compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
mode: Pipeline
pipeline:
- step: run-the-template
- step: patch-and-transform
functionRef:
name: function-template-go
name: function-patch-and-transform
input:
apiVersion: template.fn.crossplane.io/v1beta1
kind: Input
example: "Hello world"
apiVersion: pt.fn.crossplane.io/v1beta1
kind: Resources
resources:

- name: resourceOne
base:
apiVersion: example.crossplane.io/v1
kind: Resource
spec:
field1: ""
field2: ""
patches:
- type: FromCompositeFieldPath
fromFieldPath: "spec.resourceOne.field1"
toFieldPath: "spec.field1"
- type: FromCompositeFieldPath
fromFieldPath: "spec.resourceOne.field2"
toFieldPath: "spec.field2"

- name: resourceTwo
base:
apiVersion: example.crossplane.io/v1
kind: Resource
spec:
field1: ""
field2: ""
patches:
- type: FromCompositeFieldPath
fromFieldPath: "spec.resourceTwo.field1"
toFieldPath: "spec.field1"
- type: FromCompositeFieldPath
fromFieldPath: "spec.resourceTwo.field2"
toFieldPath: "spec.field2"

- name: resourceThree
base:
apiVersion: example.crossplane.io/v1
kind: Resource
spec:
field1: ""
field2: ""
patches:
- type: FromCompositeFieldPath
fromFieldPath: "spec.resourceThree.field1"
toFieldPath: "spec.field1"
- type: FromCompositeFieldPath
fromFieldPath: "spec.resourceThree.field2"
toFieldPath: "spec.field2"

- step: enable-disable
functionRef:
name: function-switcher

compositeTypeRef:
apiVersion: example.crossplane.io/v1
kind: XR
13 changes: 9 additions & 4 deletions example/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-template-go
name: function-switcher
annotations:
# This tells crossplane beta render to connect to the function locally.
render.crossplane.io/runtime: Development
# spec:
# package: ghcr.io/kndpio/kndp/function-switcher:0.1.0
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-patch-and-transform
spec:
# This is ignored when using the Development runtime.
package: function-template-go
package: xpkg.upbound.io/crossplane-contrib/function-patch-and-transform:v0.1.4
14 changes: 12 additions & 2 deletions example/xr.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Replace this with your XR!
apiVersion: example.crossplane.io/v1
kind: XR
metadata:
name: example-xr
spec: {}
annotations:
switcher.fn.kndp.io/disabled: "resourceTwo"
spec:
resourceOne:
field1: "one"
field2: "two"
resourceTwo:
field1: "three"
field2: "four"
resourceThree:
field1: "five"
field2: "six"
64 changes: 53 additions & 11 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,79 @@

import (
"context"
"encoding/json"
"slices"
"strings"

Check failure on line 8 in fn.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/crossplane) -s prefix(github.com/crossplane-contrib) -s blank -s dot --custom-order (gci)
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/function-sdk-go/logging"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/response"
"github.com/crossplane/function-template-go/input/v1beta1"
"github.com/pkg/errors"

Check failure on line 13 in fn.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/crossplane) -s prefix(github.com/crossplane-contrib) -s blank -s dot --custom-order (gci)
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Function returns whatever response you ask it to.
type Function struct {

Check warning on line 17 in fn.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported type Function should have comment or be unexported (revive)
fnv1beta1.UnimplementedFunctionRunnerServiceServer

log logging.Logger
}

// RunFunction runs the Function.
func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequest) (*fnv1beta1.RunFunctionResponse, error) {

Check failure on line 23 in fn.go

View workflow job for this annotation

GitHub Actions / lint

cyclomatic complexity 14 of func `(*Function).RunFunction` is high (> 10) (gocyclo)
f.log.Info("Running function", "tag", req.GetMeta().GetTag())
evghen1 marked this conversation as resolved.
Show resolved Hide resolved

rsp := response.To(req, response.DefaultTTL)

in := &v1beta1.Input{}
if err := request.GetInput(req, in); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
oxr, err := request.GetObservedCompositeResource(req)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get observed composite resource from %T", req))
return rsp, nil
}

// TODO: Add your Function logic here!
response.Normalf(rsp, "I was run with input %q!", in.Example)
f.log.Info("I was run!", "input", in.Example)
var switchOn []string
var switchOff []string
meta := v1.ObjectMeta{}
m := oxr.Resource.Object["metadata"]
mm, _ := json.Marshal(m)

Check failure on line 38 in fn.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `encoding/json.Marshal` is not checked: unsafe type `interface{}` found (errchkjson)
json.Unmarshal(mm, &meta)

Check failure on line 39 in fn.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `json.Unmarshal` is not checked (errcheck)

for k, v := range meta.Annotations {
if strings.Contains(k, "switcher.fn.kndp.io/enabled") {
if switchOn == nil {
switchOn = []string{}
}
switchOn = append(switchOn, strings.Split(v, ",")...)
}

if strings.Contains(k, "switcher.fn.kndp.io/disabled") {
if switchOff == nil {
switchOff = []string{}
}
switchOff = append(switchOff, strings.Split(v, ",")...)
}
}

desired, err := request.GetDesiredComposedResources(req)
if err != nil {
response.Fatal(rsp, err)
return rsp, nil
}

for r := range desired {
if switchOff != nil && slices.Contains[[]string, string](switchOff, string(r)) {
delete(desired, r)
}
if switchOn != nil && !slices.Contains[[]string, string](switchOn, string(r)) {
delete(desired, r)
}
}

rsp.Desired.Resources = nil

if err := response.SetDesiredComposedResources(rsp, desired); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot set desired composed resources in %T", rsp))
return rsp, nil
}

return rsp, nil
}
Loading
Loading