Skip to content

Commit

Permalink
controller setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ksaritek committed Oct 11, 2023
1 parent bf2efd2 commit 5220571
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 88 deletions.
12 changes: 11 additions & 1 deletion apis/playground/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions examples/sample/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: playground.userprovider.crossplane.io/v1alpha1
kind: User
metadata:
name: example
spec:
forProvider:
id: "example"
name: "example"
email: "john@example.com"
# providerConfigRef:
# name: example
9 changes: 0 additions & 9 deletions examples/sample/mytype.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/crossplane/crossplane-tools v0.0.0-20230714144037-2684f4bc7638
github.com/google/go-cmp v0.5.9
github.com/pkg/errors v0.9.1
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4
Expand Down Expand Up @@ -70,8 +72,6 @@ require (
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // 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
Expand Down
16 changes: 0 additions & 16 deletions grpc-server/go.mod

This file was deleted.

21 changes: 0 additions & 21 deletions grpc-server/go.sum

This file was deleted.

2 changes: 1 addition & 1 deletion grpc-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net"
"sync"

"github.com/ksaritek/crossplane-provider-grpc/grpc-server/proto/gen/go/userapi"
"github.com/crossplane/provider-userprovider/grpc-server/proto/gen/go/userapi"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

Expand Down
2 changes: 1 addition & 1 deletion grpc-server/proto/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: v1
managed:
enabled: true
go_package_prefix:
default: https://github.com/ksaritek/crossplane-provider-grpc/grpc-server/proto/gen/go
default: github.com/crossplane/provider-userprovider/grpc-server/proto/gen/go
plugins:
- plugin: buf.build/protocolbuffers/go:v1.30.0
out: gen/go
Expand Down
21 changes: 10 additions & 11 deletions grpc-server/proto/gen/go/userapi/user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 64 additions & 24 deletions internal/controller/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ package user
import (
"context"
"fmt"
"log"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -33,8 +37,8 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/provider-userprovider/apis/playground/v1alpha1"
apisv1alpha1 "github.com/crossplane/provider-userprovider/apis/v1alpha1"
"github.com/crossplane/provider-userprovider/grpc-server/proto/gen/go/userapi"
"github.com/crossplane/provider-userprovider/internal/features"
"github.com/ksaritek/crossplane-provider-grpc/grpc-server/proto/gen/go/userapi"
)

const (
Expand All @@ -52,7 +56,14 @@ type UserService struct {
}

var (
newNoOpService = func(_ []byte) (interface{}, error) { return &UserService{}, nil }
newUserService = func(_ []byte) (*UserService, error) {
conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("Did not connect: %v", err)
}
cli := userapi.NewUserServiceClient(conn)
return &UserService{userCli: cli}, nil
}
)

// Setup adds a controller that reconciles User managed resources.
Expand All @@ -69,7 +80,7 @@ func Setup(mgr ctrl.Manager, o controller.Options) error {
managed.WithExternalConnecter(&connector{
kube: mgr.GetClient(),
usage: resource.NewProviderConfigUsageTracker(mgr.GetClient(), &apisv1alpha1.ProviderConfigUsage{}),
newServiceFn: newNoOpService}),
newServiceFn: newUserService}),
managed.WithLogger(o.Logger.WithValues("controller", name)),
managed.WithPollInterval(o.PollInterval),
managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))),
Expand All @@ -88,7 +99,7 @@ func Setup(mgr ctrl.Manager, o controller.Options) error {
type connector struct {
kube client.Client
usage resource.Tracker
newServiceFn func(creds []byte) (interface{}, error)
newServiceFn func(creds []byte) (*UserService, error)
}

// Connect typically produces an ExternalClient by:
Expand All @@ -97,27 +108,29 @@ type connector struct {
// 3. Getting the credentials specified by the ProviderConfig.
// 4. Using the credentials to form a client.
func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.ExternalClient, error) {
cr, ok := mg.(*v1alpha1.User)
if !ok {
return nil, errors.New(errNotUser)
}
// cr, ok := mg.(*v1alpha1.User)
// if !ok {
// return nil, errors.New(errNotUser)
// }

if err := c.usage.Track(ctx, mg); err != nil {
return nil, errors.Wrap(err, errTrackPCUsage)
}
// if err := c.usage.Track(ctx, mg); err != nil {
// return nil, errors.Wrap(err, errTrackPCUsage)
// }

pc := &apisv1alpha1.ProviderConfig{}
if err := c.kube.Get(ctx, types.NamespacedName{Name: cr.GetProviderConfigReference().Name}, pc); err != nil {
return nil, errors.Wrap(err, errGetPC)
}
// pc := &apisv1alpha1.ProviderConfig{}
// if err := c.kube.Get(ctx, types.NamespacedName{Name: cr.GetProviderConfigReference().Name}, pc); err != nil {
// return nil, errors.Wrap(err, errGetPC)
// }

cd := pc.Spec.Credentials
data, err := resource.CommonCredentialExtractor(ctx, cd.Source, c.kube, cd.CommonCredentialSelectors)
if err != nil {
return nil, errors.Wrap(err, errGetCreds)
}
// cd := pc.Spec.Credentials
// data, err := resource.CommonCredentialExtractor(ctx, cd.Source, c.kube, cd.CommonCredentialSelectors)
// if err != nil {
// return nil, errors.Wrap(err, errGetCreds)
// }

svc, err := c.newServiceFn(data)
fmt.Println(">>> connecting to grpc server")

svc, err := c.newServiceFn([]byte("fake creds"))
if err != nil {
return nil, errors.Wrap(err, errNewClient)
}
Expand All @@ -130,7 +143,7 @@ func (c *connector) Connect(ctx context.Context, mg resource.Managed) (managed.E
type external struct {
// A 'client' used to connect to the external resource API. In practice this
// would be something like an AWS SDK client.
service interface{}
service *UserService
}

func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {
Expand All @@ -139,8 +152,18 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.New(errNotUser)
}

user, err := c.service.userCli.GetUser(ctx, &userapi.GetRequest{Id: cr.Spec.ForProvider.Id})
if err != nil {
if status.Code(err) == codes.NotFound {
return managed.ExternalObservation{ResourceExists: false}, nil
}

return managed.ExternalObservation{}, errors.Wrap(err, "cannot get user")
}

// These fmt statements should be removed in the real implementation.
fmt.Printf("Observing: %+v", cr)
fmt.Printf("Observed: %+v", user)

return managed.ExternalObservation{
// Return false when the external resource does not exist. This lets
Expand All @@ -160,18 +183,35 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
}

func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
fmt.Println("Create is called")
cr, ok := mg.(*v1alpha1.User)
if !ok {
return managed.ExternalCreation{}, errors.New(errNotUser)
}

fmt.Printf("Creating: %+v", cr)

name := ""
if cr.Spec.ForProvider.Name != nil {
name = *cr.Spec.ForProvider.Name
}

email := ""
if cr.Spec.ForProvider.Email != nil {
email = *cr.Spec.ForProvider.Email
}

_, err := c.service.userCli.CreateUser(ctx,
&userapi.User{
Id: cr.Spec.ForProvider.Id,
Name: name,
Email: email})

return managed.ExternalCreation{
// Optionally return any details that may be required to connect to the
// external resource. These will be stored as the connection secret.
ConnectionDetails: managed.ConnectionDetails{},
}, nil
}, err
}

func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) {
Expand Down
8 changes: 6 additions & 2 deletions package/crds/playground.userprovider.crossplane.io_users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ spec:
forProvider:
description: UserParameters are the configurable fields of a User.
properties:
configurableField:
email:
type: string
id:
type: string
name:
type: string
required:
- configurableField
- id
type: object
managementPolicies:
default:
Expand Down

0 comments on commit 5220571

Please sign in to comment.