-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make CreateOrUpdate controller async for link resources (#5006)
# Description - Refactor to have single DeploymentObject for linkrp and corerp - refactor the deployment.Delete signature to take only outputresources - add a new async controller for createorupdate for linkrp ## Issue reference <!-- We strive to have all PR being opened based on an issue, where the problem or feature have been discussed prior to implementation. --> Fixes: https://dev.azure.com/azure-octo/Incubations/_workitems/edit/5788 ## Checklist Please make sure you've completed the relevant tasks for this PR, out of the following list: * [x] Code compiles correctly * [ ] Adds necessary unit tests for change * [ ] Adds necessary E2E tests for change * [x] Unit tests passing * [ ] Extended the documentation / Created issue for it
- Loading branch information
1 parent
c9eebd3
commit 430a961
Showing
80 changed files
with
933 additions
and
1,553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
pkg/linkrp/backend/controller/createorupdateresource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
if errors.Is(&store.ErrNotFound{}, err) { | ||
isNewResource = true | ||
} | ||
|
||
opType, _ := v1.ParseOperationType(req.OperationType) | ||
if opType.Method == http.MethodPatch && errors.Is(&store.ErrNotFound{}, err) { | ||
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 | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.