-
Notifications
You must be signed in to change notification settings - Fork 101
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
Make CreateOrUpdate controller async for link resources #5006
Changes from all commits
a0ca774
14b4fa0
e64eab9
4a3b531
779e69f
fd27b90
7223dba
8a680aa
f21ba19
c9d636b
91405bb
7ef7d11
a2f5d1a
bc8d8c9
e119321
af4dc96
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,6 +20,11 @@ import ( | |||||
"github.com/project-radius/radius/pkg/ucp/store" | ||||||
) | ||||||
|
||||||
const ( | ||||||
// defaultAsyncPutTimeout is the default timeout duration of async put operation. | ||||||
defaultAsyncPutTimeout = time.Duration(2) * time.Minute | ||||||
) | ||||||
|
||||||
// Operation is the base operation controller. | ||||||
type Operation[P interface { | ||||||
*T | ||||||
|
@@ -218,3 +223,11 @@ func (b *Operation[P, T]) DeleteFilters() []DeleteFilter[T] { | |||||
func (b *Operation[P, T]) UpdateFilters() []UpdateFilter[T] { | ||||||
return b.resourceOptions.UpdateFilters | ||||||
} | ||||||
|
||||||
// AsyncOperationTimeout returns the timeput for the operation. | ||||||
func (b *Operation[P, T]) AsyncOperationTimeout() time.Duration { | ||||||
if b.resourceOptions.AsyncOperationTimeout == 0 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add the simple unittest for this ?
Suggested change
|
||||||
return defaultAsyncPutTimeout | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but this function is not only used for put calls, right? delete timeout should be different? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. currently we use the same default timeout duration for put and delete, i.e 2 mins and we can override it by providing timeout in routes |
||||||
} | ||||||
return b.resourceOptions.AsyncOperationTimeout | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// ------------------------------------------------------------ | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
v1 "github.com/project-radius/radius/pkg/armrpc/api/v1" | ||
ctrl "github.com/project-radius/radius/pkg/armrpc/asyncoperation/controller" | ||
rpv1 "github.com/project-radius/radius/pkg/rp/v1" | ||
"github.com/project-radius/radius/pkg/ucp/resources" | ||
"github.com/project-radius/radius/pkg/ucp/store" | ||
) | ||
|
||
var _ ctrl.Controller = (*CreateOrUpdateResource)(nil) | ||
|
||
// CreateOrUpdateResource is the async operation controller to create or update Applications.Link resources. | ||
type CreateOrUpdateResource struct { | ||
ctrl.BaseController | ||
} | ||
|
||
// NewCreateOrUpdateResource creates the CreateOrUpdateResource controller instance. | ||
func NewCreateOrUpdateResource(opts ctrl.Options) (ctrl.Controller, error) { | ||
return &CreateOrUpdateResource{ctrl.NewBaseAsyncController(opts)}, nil | ||
} | ||
|
||
func (c *CreateOrUpdateResource) Run(ctx context.Context, req *ctrl.Request) (ctrl.Result, error) { | ||
obj, err := c.StorageClient().Get(ctx, req.ResourceID) | ||
if err != nil && !errors.Is(&store.ErrNotFound{}, err) { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
isNewResource := false | ||
ytimocin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if errors.Is(&store.ErrNotFound{}, err) { | ||
isNewResource = true | ||
} | ||
|
||
opType, _ := v1.ParseOperationType(req.OperationType) | ||
if opType.Method == http.MethodPatch && errors.Is(&store.ErrNotFound{}, err) { | ||
kachawla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ctrl.Result{}, err | ||
} | ||
|
||
// This code is general and we might be processing an async job for a resource or a scope, so using the general Parse function. | ||
id, err := resources.Parse(req.ResourceID) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
dataModel, err := getDataModel(id) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if err = obj.As(dataModel); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
rendererOutput, err := c.LinkDeploymentProcessor().Render(ctx, id, dataModel) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
deploymentOutput, err := c.LinkDeploymentProcessor().Deploy(ctx, id, rendererOutput) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
deploymentDataModel, ok := dataModel.(rpv1.DeploymentDataModel) | ||
if !ok { | ||
return ctrl.NewFailedResult(v1.ErrorDetails{Message: "deployment data model conversion error"}), err | ||
} | ||
Comment on lines
+73
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can move this check after L61 before rendering and deploying. |
||
|
||
oldOutputResources := deploymentDataModel.OutputResources() | ||
err = deploymentDataModel.ApplyDeploymentOutput(deploymentOutput) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if !isNewResource { | ||
diff := rpv1.GetGCOutputResources(deploymentDataModel.OutputResources(), oldOutputResources) | ||
err = c.LinkDeploymentProcessor().Delete(ctx, id, diff) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
nr := &store.Object{ | ||
Metadata: store.Metadata{ | ||
ID: req.ResourceID, | ||
}, | ||
Data: deploymentDataModel, | ||
} | ||
err = c.StorageClient().Save(ctx, nr, store.WithETag(obj.ETag)) | ||
if err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
return ctrl.Result{}, err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the best place to put this property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, as this can accessed for all the operations for the resource.