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 1 commit
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
17 changes: 0 additions & 17 deletions example/composition.yaml

This file was deleted.

12 changes: 9 additions & 3 deletions example/functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
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:
# spec:
# This is ignored when using the Development runtime.
package: function-template-go
---
apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-patch-and-transform
spec:
package: xpkg.upbound.io/crossplane-contrib/function-patch-and-transform:v0.1.4
6 changes: 0 additions & 6 deletions example/xr.yaml

This file was deleted.

50 changes: 38 additions & 12 deletions fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,63 @@

import (
"context"
"fmt"
"strings"

Check failure on line 7 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 12 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)
)

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

Check warning on line 15 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 warning on line 21 in fn.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method Function.RunFunction should have comment or be unexported (revive)
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)
s, _ := oxr.Resource.GetString("metadata.annotation.switcher")
evghen1 marked this conversation as resolved.
Show resolved Hide resolved

resourcesToSwitch := strings.Split(s, ",")

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

for r := range desired {
found := false
for _, rs := range resourcesToSwitch {
if string(r) == rs {
found = true
evghen1 marked this conversation as resolved.
Show resolved Hide resolved
break
}
}

if found {
fmt.Println(r)
evghen1 marked this conversation as resolved.
Show resolved Hide resolved
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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/crossplane/function-template-go
module github.com/kndpio/function-switcher

go 1.21

Expand Down
15 changes: 0 additions & 15 deletions input/generate.go

This file was deleted.

27 changes: 0 additions & 27 deletions input/v1beta1/input.go

This file was deleted.

34 changes: 0 additions & 34 deletions input/v1beta1/zz_generated.deepcopy.go

This file was deleted.

2 changes: 1 addition & 1 deletion package/crossplane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
apiVersion: meta.pkg.crossplane.io/v1beta1
kind: Function
metadata:
name: function-template-go
name: function-switcher
spec: {}
44 changes: 0 additions & 44 deletions package/input/template.fn.crossplane.io_inputs.yaml

This file was deleted.

Loading