Skip to content

Commit

Permalink
Merge pull request #18 from negz/bootstrap
Browse files Browse the repository at this point in the history
Add the beginnings of a Function SDK
  • Loading branch information
negz authored Sep 14, 2023
2 parents efbc0c1 + 8dd124b commit 245eb19
Show file tree
Hide file tree
Showing 7 changed files with 597 additions and 8 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ linters-settings:
- standard
- default
- prefix(github.com/crossplane/crossplane-runtime)
- prefix(github.com/crossplane/function-sdk-go)
- blank
- dot

Expand Down
22 changes: 19 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ go 1.20

require (
github.com/bufbuild/buf v1.26.1
github.com/crossplane/crossplane-runtime v1.13.0
github.com/go-logr/zapr v1.2.4
github.com/pkg/errors v0.9.1
go.uber.org/zap v1.24.0
google.golang.org/grpc v1.58.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0
google.golang.org/protobuf v1.31.0
k8s.io/apimachinery v0.28.2
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/bufbuild/connect-go v1.9.0 // indirect
Expand All @@ -31,18 +37,21 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-containerregistry v0.15.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20230705174524-200ffdc848b8 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jdxcode/netrc v0.0.0-20221124155335-4616370d1a84 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/profile v1.7.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/cors v1.9.0 // indirect
Expand All @@ -57,16 +66,23 @@ require (
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.11.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.27.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/utils v0.0.0-20230505201702-9f6742963106 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
82 changes: 77 additions & 5 deletions go.sum

Large diffs are not rendered by default.

80 changes: 80 additions & 0 deletions request/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2023 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package request contains utilities for working with RunFunctionRequests.
package request

import (
"k8s.io/apimachinery/pkg/runtime"

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composed"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composite"

"github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/resource"
)

// GetInput from the supplied request. Input is loaded into the supplied object.
func GetInput(req *v1beta1.RunFunctionRequest, into runtime.Object) error {
return errors.Wrap(resource.AsObject(req.GetInput(), into), "cannot get Function input %T from %T, into, req")
}

// GetObservedCompositeResource from the supplied request.
func GetObservedCompositeResource(req *v1beta1.RunFunctionRequest) (*resource.Composite, error) {
xr := &resource.Composite{
Resource: composite.New(),
ConnectionDetails: req.GetObserved().GetComposite().GetConnectionDetails(),
}
err := resource.AsObject(req.GetObserved().GetComposite().GetResource(), xr.Resource)
return xr, err
}

// GetObservedComposedResources from the supplied request.
func GetObservedComposedResources(req *v1beta1.RunFunctionRequest) (resource.ObservedComposedResources, error) {
ocds := resource.ObservedComposedResources{}
for name, r := range req.GetObserved().GetResources() {
ocd := resource.ObservedComposed{Resource: composed.New(), ConnectionDetails: r.GetConnectionDetails()}
if err := resource.AsObject(r.GetResource(), ocd.Resource); err != nil {
return nil, err
}
ocds[resource.Name(name)] = ocd
}
return ocds, nil
}

// GetDesiredCompositeResource from the supplied request.
func GetDesiredCompositeResource(req *v1beta1.RunFunctionRequest) (*resource.Composite, error) {
xr := &resource.Composite{
Resource: composite.New(),
ConnectionDetails: req.GetDesired().GetComposite().GetConnectionDetails(),
}
err := resource.AsObject(req.GetDesired().GetComposite().GetResource(), xr.Resource)
return xr, err
}

// GetDesiredComposedResources from the supplied request.
func GetDesiredComposedResources(req *v1beta1.RunFunctionRequest) (resource.DesiredComposedResources, error) {
ocds := resource.DesiredComposedResources{}
for name, r := range req.GetDesired().GetResources() {
ocd := resource.DesiredComposed{Resource: composed.New()}
if err := resource.AsObject(r.GetResource(), ocd.Resource); err != nil {
return nil, err
}
ocds[resource.Name(name)] = ocd
}
return ocds, nil
}
144 changes: 144 additions & 0 deletions resource/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
Copyright 2021 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package resource contains utilities to convert protobuf representations of
// Crossplane resources to unstructured Go types, often with convenient getters
// and setters.
package resource

import (
"encoding/json"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composed"
"github.com/crossplane/crossplane-runtime/pkg/resource/unstructured/composite"
)

// ConnectionDetails created or updated during an operation on an external
// resource, for example usernames, passwords, endpoints, ports, etc.
type ConnectionDetails map[string][]byte

// A Composite resource - aka an XR.
type Composite struct {
Resource *composite.Unstructured
ConnectionDetails ConnectionDetails
}

// A Name uniquely identifies a composed resource within a Composition Function
// pipeline. It's not the resource's metadata.name.
type Name string

// DesiredComposed reflects the desired state of a composed resource.
type DesiredComposed struct {
Resource *composed.Unstructured

Ready Ready
}

// Ready indicates whether a composed resource should be considered ready.
type Ready int

// Composed resource readiness.
const (
ReadyUnspecified Ready = iota
ReadyTrue
ReadyFalse
)

// NewDesiredComposedResource returns a new, empty desired composed resource.
func NewDesiredComposedResource() DesiredComposed {
return DesiredComposed{Resource: composed.New()}
}

// DesiredComposedResources indexed by resource name.
type DesiredComposedResources map[Name]DesiredComposed

// ObservedComposed reflects the observed state of a composed resource.
type ObservedComposed struct {
Resource *composed.Unstructured
ConnectionDetails ConnectionDetails
}

// ObservedComposedResources indexed by resource name.
type ObservedComposedResources map[Name]ObservedComposed

// AsObject gets the supplied Kubernetes object from the supplied struct.
func AsObject(s *structpb.Struct, o runtime.Object) error {
// We try to avoid a JSON round-trip if o is backed by unstructured data.
switch u := o.(type) {
case *unstructured.Unstructured:
u.Object = s.AsMap()
return nil
case wrapped:
u.GetUnstructured().Object = s.AsMap()
return nil
default:
b, err := protojson.Marshal(s)
if err != nil {
return errors.Wrapf(err, "cannot marshal %T to JSON", s)
}
return errors.Wrapf(json.Unmarshal(b, o), "cannot unmarshal JSON from %T into %T", s, o)
}
}

// AsStruct gets the supplied struct from the supplied Kubernetes object.
func AsStruct(o runtime.Object) (*structpb.Struct, error) {
// We try to avoid a JSON round-trip if o is backed by unstructured data.
switch u := o.(type) {
case *unstructured.Unstructured:
s, err := structpb.NewStruct(u.Object)
return s, errors.Wrapf(err, "cannot create new Struct from %T", u)
case wrapped:
s, err := structpb.NewStruct(u.GetUnstructured().Object)
return s, errors.Wrapf(err, "cannot create new Struct from %T", u)
default:
b, err := json.Marshal(o)
if err != nil {
return nil, errors.Wrapf(err, "cannot marshal %T to JSON", o)
}
s := &structpb.Struct{}
return s, errors.Wrapf(protojson.Unmarshal(b, s), "cannot unmarshal JSON from %T into %T", o, s)
}
}

type wrapped interface {
GetUnstructured() *unstructured.Unstructured
}

// MustStructObject is intended only for use in tests. It returns the supplied
// object as a struct. It panics if it can't.
func MustStructObject(o runtime.Object) *structpb.Struct {
s, err := AsStruct(o)
if err != nil {
panic(err)
}
return s
}

// MustStructJSON is intended only for use in tests. It returns the supplied
// JSON string as a struct. It panics if it can't.
func MustStructJSON(j string) *structpb.Struct {
s := &structpb.Struct{}
if err := protojson.Unmarshal([]byte(j), s); err != nil {
panic(err)
}
return s
}
Loading

0 comments on commit 245eb19

Please sign in to comment.