Skip to content

Commit

Permalink
Initial implementation of user-defined-types (radius-project#7686)
Browse files Browse the repository at this point in the history
This change implements the skeleton of user-defined types. The changes
here enable the following:

- Users can author a resource of type
`System.Resources/resourceProviders` to create a user-defined-type.
- Users can use the UCP API to register and query `resourceProviders`.
- Users can use the UCP API to execute the full lifecycle of a
user-defined-type.

Right now the user-defined-type RP will use our default operation
(synchronous) controllers to implement the resource lifecycle. There is
no background processing.

The next step will include the ability to execute asynchronous
operations like recipes.

- This pull request fixes a bug in Radius and has an approved issue
(issue link required).
- This pull request adds or changes features of Radius and has an
approved issue (issue link required).

Part of: radius-project#6688

**note: This change is going into a feature-branch where we can iterate
on the user-defined-type design before integrating it with main. The PR
is an FYI 😆.**

---------

Signed-off-by: ytimocin <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: willdavsmith <[email protected]>
Signed-off-by: Ryan Nowak <[email protected]>
Co-authored-by: Yetkin Timocin <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Will Smith <[email protected]>
Signed-off-by: Ryan Nowak <[email protected]>
  • Loading branch information
4 people committed Nov 11, 2024
1 parent 100e8cc commit 8433c6a
Show file tree
Hide file tree
Showing 38 changed files with 1,793 additions and 84 deletions.
43 changes: 40 additions & 3 deletions pkg/armrpc/asyncoperation/worker/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ import (
"github.com/radius-project/radius/pkg/ucp/dataprovider"
)

const (
// ResourceTypeAny is a wildcard for any resource type.
ResourceTypeAny = "*"

// OperationMethodAny is a wildcard for any operation method.
OperationMethodAny = "*"
)

// ControllerFactoryFunc is a factory function to create a controller.
type ControllerFactoryFunc func(opts ctrl.Options) (ctrl.Controller, error)

// ControllerRegistry is an registry to register async controllers.
type ControllerRegistry struct {
ctrlMap map[string]ctrl.Controller
ctrlMapMu sync.RWMutex
sp dataprovider.DataStorageProvider

// Fallback allows the registration of a controller that will be used
// for operations that don't match any other operation type.
fallbackFactory ControllerFactoryFunc
fallbackOpts ctrl.Options
}

// NewControllerRegistry creates an ControllerRegistry instance.
Expand All @@ -48,6 +62,13 @@ func (h *ControllerRegistry) Register(ctx context.Context, resourceType string,
defer h.ctrlMapMu.Unlock()

ot := v1.OperationType{Type: resourceType, Method: method}
if resourceType == ResourceTypeAny && method == OperationMethodAny {
// This is a fallback controller. Skip registration for now so we can create instances
// dynamically when needed.
h.fallbackFactory = factoryFn
h.fallbackOpts = opts
return nil
}

storageClient, err := h.sp.GetStorageClient(ctx, resourceType)
if err != nil {
Expand All @@ -66,13 +87,29 @@ func (h *ControllerRegistry) Register(ctx context.Context, resourceType string,
}

// Get gets the registered async controller instance.
func (h *ControllerRegistry) Get(operationType v1.OperationType) ctrl.Controller {
func (h *ControllerRegistry) Get(ctx context.Context, operationType v1.OperationType) (ctrl.Controller, error) {
h.ctrlMapMu.RLock()
defer h.ctrlMapMu.RUnlock()

if h, ok := h.ctrlMap[operationType.String()]; ok {
return h
return h, nil
}

return nil
// If no controller is found, then look for a default controller.
if h.fallbackFactory == nil {
return nil, nil
}

storageClient, err := h.sp.GetStorageClient(ctx, operationType.Type)
if err != nil {
return nil, err
}

// Copy the options so we can update it.
opts := h.fallbackOpts

opts.StorageClient = storageClient
opts.ResourceType = operationType.Type

return h.fallbackFactory(opts)
}
7 changes: 5 additions & 2 deletions pkg/armrpc/asyncoperation/worker/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ func TestRegister_Get(t *testing.T) {
}, ctrlOpts)
require.NoError(t, err)

ctrl := registry.Get(opGet)
ctrl, err := registry.Get(context.Background(), opGet)
require.NoError(t, err)
require.NotNil(t, ctrl)
ctrl = registry.Get(opPut)

ctrl, err = registry.Get(context.Background(), opPut)
require.NoError(t, err)
require.NotNil(t, ctrl)
}
10 changes: 9 additions & 1 deletion pkg/armrpc/asyncoperation/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,15 @@ func (w *AsyncRequestProcessWorker) Start(ctx context.Context) error {
}
reqCtx = v1.WithARMRequestContext(reqCtx, armReqCtx)

asyncCtrl := w.registry.Get(armReqCtx.OperationType)
asyncCtrl, err := w.registry.Get(reqCtx, armReqCtx.OperationType)
if err != nil {
opLogger.Error(err, "failed to get async controller.")
if err := w.requestQueue.FinishMessage(reqCtx, msgreq); err != nil {
opLogger.Error(err, "failed to finish the message")
}
return
}

if asyncCtrl == nil {
opLogger.Error(nil, "cannot process unknown operation: "+armReqCtx.OperationType.String())
if err := w.requestQueue.FinishMessage(reqCtx, msgreq); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/armrpc/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ func TestApplyAsyncHandler(t *testing.T) {
}

for _, op := range expectedOperations {
jobCtrl := registry.Get(op)
jobCtrl, err := registry.Get(context.Background(), op)
require.NoError(t, err)
require.NotNil(t, jobCtrl)
}
}
32 changes: 32 additions & 0 deletions pkg/dynamicrp/api/dynamicresource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2023 The Radius 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 api

// DynamicResource is used as the versioned resource model for dynamic resources.
//
// A dynamic resource is implemented internally to dynamic-rp, and uses a user-provided
// OpenAPI specification to define the resource schema. Since the resource is internal
// and dynamically generated, this struct is used to represent all dynamic resources.
type DynamicResource struct {
ID *string `json:"id"`
Name *string `json:"name"`
Type *string `json:"type"`
Location *string `json:"location"`
Tags map[string]*string `json:"tags,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
SystemData map[string]any `json:"systemData,omitempty"`
}
96 changes: 96 additions & 0 deletions pkg/dynamicrp/api/dynamicresource_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2023 The Radius 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 api

import (
"encoding/json"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/dynamicrp/datamodel"
"github.com/radius-project/radius/pkg/to"
)

const (
// TODO
Version = "2023-01-01"
)

func (d *DynamicResource) ConvertTo() (v1.DataModelInterface, error) {
dm := &datamodel.DynamicResource{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
ID: to.String(d.ID),
Name: to.String(d.Name),
Type: to.String(d.Type),
Location: to.String(d.Location),
Tags: to.StringMap(d.Tags),
},
InternalMetadata: v1.InternalMetadata{
UpdatedAPIVersion: Version,
},
},
Properties: d.Properties,
}

return dm, nil
}

func (d *DynamicResource) ConvertFrom(src v1.DataModelInterface) error {
dm, ok := src.(*datamodel.DynamicResource)
if !ok {
return v1.ErrInvalidModelConversion
}

d.ID = &dm.ID
d.Name = &dm.Name
d.Type = &dm.Type
d.Location = &dm.Location
d.Tags = *to.StringMapPtr(dm.Tags)
d.SystemData = fromSystemDataDataModel(dm.SystemData)
d.Properties = dm.Properties
if d.Properties == nil {
d.Properties = map[string]any{}
}
d.Properties["provisioningState"] = fromProvisioningStateDataModel(dm.AsyncProvisioningState)

return nil
}

func fromSystemDataDataModel(input v1.SystemData) map[string]any {
bs, err := json.Marshal(input)
if err != nil {
// This should never fail. We've designed the SystemData type to be serializable.
panic("marshalling system data failed: " + err.Error())
}

result := map[string]any{}
err = json.Unmarshal(bs, &result)
if err != nil {
// This should never fail. We've designed the SystemData type to be serializable.
panic("unmarshalling system data failed: " + err.Error())
}

return result
}

func fromProvisioningStateDataModel(input v1.ProvisioningState) string {
if input == "" {
return string(v1.ProvisioningStateSucceeded)
}

return string(input)
}
126 changes: 126 additions & 0 deletions pkg/dynamicrp/api/dynamicresource_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright 2023 The Radius 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 api

import (
"encoding/json"
"testing"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/dynamicrp/datamodel"
"github.com/radius-project/radius/pkg/to"
"github.com/radius-project/radius/test/testutil"

"github.com/stretchr/testify/require"
)

func Test_DynamicResource_ConvertVersionedToDataModel(t *testing.T) {
conversionTests := []struct {
filename string
expected *datamodel.DynamicResource
err error
}{
{
filename: "dynamicresource-resource.json",
expected: &datamodel.DynamicResource{
BaseResource: v1.BaseResource{
TrackedResource: v1.TrackedResource{
ID: "/planes/radius/local/resourceGroups/test/providers/Applications.Test/testResources/testResource",
Name: "testResource",
Type: "Applications.Test/testResources",
Location: "global",
Tags: map[string]string{
"env": "dev",
},
},
InternalMetadata: v1.InternalMetadata{
UpdatedAPIVersion: Version,
},
},
Properties: map[string]any{
"message": "Hello, world!",
},
},
},
}

for _, tt := range conversionTests {
t.Run(tt.filename, func(t *testing.T) {
rawPayload := testutil.ReadFixture(tt.filename)
r := &DynamicResource{}
err := json.Unmarshal(rawPayload, r)
require.NoError(t, err)

dm, err := r.ConvertTo()

if tt.err != nil {
require.ErrorIs(t, err, tt.err)
} else {
require.NoError(t, err)
ct := dm.(*datamodel.DynamicResource)
require.Equal(t, tt.expected, ct)
}
})
}
}

func Test_DynamicResource_ConvertDataModelToVersioned(t *testing.T) {
conversionTests := []struct {
filename string
expected *DynamicResource
err error
}{
{
filename: "dynamicresource-datamodel.json",
expected: &DynamicResource{
ID: to.Ptr("/planes/radius/local/resourceGroups/test/providers/Applications.Test/testResources/testResource"),
Name: to.Ptr("testResource"),
Type: to.Ptr("Applications.Test/testResources"),
Location: to.Ptr("global"),
Tags: map[string]*string{
"env": to.Ptr("dev"),
},
Properties: map[string]any{
"provisioningState": fromProvisioningStateDataModel(v1.ProvisioningStateSucceeded),
"message": "Hello, world!",
},
},
},
}

for _, tt := range conversionTests {
t.Run(tt.filename, func(t *testing.T) {
rawPayload := testutil.ReadFixture(tt.filename)
dm := &datamodel.DynamicResource{}
err := json.Unmarshal(rawPayload, dm)
require.NoError(t, err)

resource := &DynamicResource{}
err = resource.ConvertFrom(dm)

// Avoid hardcoding the SystemData field in tests.
tt.expected.SystemData = fromSystemDataDataModel(dm.SystemData)

if tt.err != nil {
require.ErrorIs(t, err, tt.err)
} else {
require.NoError(t, err)
require.Equal(t, tt.expected, resource)
}
})
}
}
20 changes: 20 additions & 0 deletions pkg/dynamicrp/api/testdata/dynamicresource-datamodel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": "/planes/radius/local/resourceGroups/test/providers/Applications.Test/testResources/testResource",
"name": "testResource",
"type": "Applications.Test/testResources",
"location": "global",
"systemData": {
"createdBy": "[email protected]",
"createdByType": "User",
"createdAt": "2021-09-24T19:09:54.2403864Z",
"lastModifiedBy": "[email protected]",
"lastModifiedByType": "User",
"lastModifiedAt": "2021-09-24T20:09:54.2403864Z"
},
"tags": {
"env": "dev"
},
"properties": {
"message": "Hello, world!"
}
}
Loading

0 comments on commit 8433c6a

Please sign in to comment.