Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sk593 committed Sep 12, 2023
1 parent 5bcd38d commit aa1efca
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"errors"

ctrl "github.com/radius-project/radius/pkg/armrpc/asyncoperation/controller"
"github.com/radius-project/radius/pkg/portableresources"
"github.com/radius-project/radius/pkg/portableresources/datamodel"
"github.com/radius-project/radius/pkg/portableresources/processors"
"github.com/radius-project/radius/pkg/recipes"
"github.com/radius-project/radius/pkg/recipes/configloader"
"github.com/radius-project/radius/pkg/recipes/engine"
"github.com/radius-project/radius/pkg/recipes/util"
rpv1 "github.com/radius-project/radius/pkg/rp/v1"
"github.com/radius-project/radius/pkg/ucp/store"
)
Expand Down Expand Up @@ -82,7 +82,7 @@ func (c *CreateOrUpdateResource[P, T]) Run(ctx context.Context, req *ctrl.Reques
if err != nil {
if recipeError, ok := err.(*recipes.RecipeError); ok {
// Set the deployment status to the recipe error code.
recipeDataModel.Recipe().DeploymentStatus = portableresources.RecipeDeploymentStatus(recipeError.DeploymentStatus)
recipeDataModel.Recipe().DeploymentStatus = util.RecipeDeploymentStatus(recipeError.DeploymentStatus)
update := &store.Object{
Metadata: store.Metadata{
ID: req.ResourceID,
Expand Down Expand Up @@ -111,7 +111,9 @@ func (c *CreateOrUpdateResource[P, T]) Run(ctx context.Context, req *ctrl.Reques
return ctrl.Result{}, err
}

recipeDataModel.Recipe().DeploymentStatus = portableresources.Success
if recipeDataModel.Recipe() != nil {
recipeDataModel.Recipe().DeploymentStatus = util.Success
}
update := &store.Object{
Metadata: store.Metadata{
ID: req.ResourceID,
Expand All @@ -136,7 +138,10 @@ func (c *CreateOrUpdateResource[P, T]) copyOutputResources(data P) []string {

func (c *CreateOrUpdateResource[P, T]) executeRecipeIfNeeded(ctx context.Context, data P, prevState []string) (*recipes.RecipeOutput, error) {
// 'any' is required here to convert to an interface type, only then can we use a type assertion.
recipeDataModel := any(data).(datamodel.RecipeDataModel)
recipeDataModel, supportsRecipes := any(data).(datamodel.RecipeDataModel)
if !supportsRecipes {
return nil, nil
}

input := recipeDataModel.Recipe()
if input == nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/portableresources/backend/controller/deleteresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
ctrl "github.com/radius-project/radius/pkg/armrpc/asyncoperation/controller"
"github.com/radius-project/radius/pkg/portableresources"
"github.com/radius-project/radius/pkg/portableresources/datamodel"
"github.com/radius-project/radius/pkg/portableresources/processors"
"github.com/radius-project/radius/pkg/recipes"
"github.com/radius-project/radius/pkg/recipes/configloader"
"github.com/radius-project/radius/pkg/recipes/engine"
"github.com/radius-project/radius/pkg/recipes/util"
rpv1 "github.com/radius-project/radius/pkg/rp/v1"
"github.com/radius-project/radius/pkg/ucp/resources"
)
Expand Down Expand Up @@ -74,11 +74,11 @@ func (c *DeleteResource[P, T]) Run(ctx context.Context, request *ctrl.Request) (
return ctrl.Result{}, err
}

recipeDataModel := any(data).(datamodel.RecipeDataModel)
recipeDataModel, supportsRecipes := any(data).(datamodel.RecipeDataModel)

// If we have a setup error (error before recipe and output resources are executed, we skip engine/driver deletion.
// If we have an execution error, we call engine/driver deletion.
if recipeDataModel.Recipe() != nil && recipeDataModel.Recipe().DeploymentStatus != portableresources.RecipeSetupError {
if supportsRecipes && recipeDataModel.Recipe() != nil && recipeDataModel.Recipe().DeploymentStatus != util.RecipeSetupError {
recipeData := recipes.ResourceMetadata{
Name: recipeDataModel.Recipe().Name,
EnvironmentID: data.ResourceMetadata().Environment,
Expand Down
15 changes: 3 additions & 12 deletions pkg/portableresources/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package portableresources

import (
"strings"

"github.com/radius-project/radius/pkg/recipes/util"
)

const (
Expand Down Expand Up @@ -81,7 +83,7 @@ type ResourceRecipe struct {
// Parameters are key/value parameters to pass into the recipe at deployment
Parameters map[string]any `json:"parameters,omitempty"`
// Deployment status of the recipe
DeploymentStatus RecipeDeploymentStatus `json:"recipeStatus,omitempty"`
DeploymentStatus util.RecipeDeploymentStatus `json:"recipeStatus,omitempty"`
}

// ResourceReference represents a reference to a resource that was deployed by the user
Expand Down Expand Up @@ -128,17 +130,6 @@ type Kubernetes struct {
EnvironmentNamespace string `json:"environmentNamespace"`
}

type RecipeDeploymentStatus string

const (
// ExecutionError represents a failure status during recipe execution.
ExecutionError RecipeDeploymentStatus = "executionError"
// RecipeSetupError represents a failure that happens before a recipe or output resources are deployed.
RecipeSetupError RecipeDeploymentStatus = "setupError"
// Success represents a successful recipe execution.
Success RecipeDeploymentStatus = "success"
)

// IsValidPortableResourceType checks if the provided resource type is a valid portable resource type.
// Returns true if the resource type is valid, false otherwise.
func IsValidPortableResourceType(resourceType string) bool {
Expand Down
8 changes: 4 additions & 4 deletions pkg/recipes/configloader/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/radius-project/radius/pkg/corerp/api/v20220315privatepreview"
"github.com/radius-project/radius/pkg/corerp/datamodel"
"github.com/radius-project/radius/pkg/portableresources"
"github.com/radius-project/radius/pkg/recipes"
recipes_util "github.com/radius-project/radius/pkg/recipes/util"
"github.com/radius-project/radius/pkg/rp/kube"
"github.com/radius-project/radius/pkg/rp/util"
"github.com/radius-project/radius/pkg/to"
Expand Down Expand Up @@ -126,19 +126,19 @@ func (e *environmentLoader) LoadRecipe(ctx context.Context, recipe *recipes.Reso
func getRecipeDefinition(environment *v20220315privatepreview.EnvironmentResource, recipe *recipes.ResourceMetadata) (*recipes.EnvironmentDefinition, error) {
if environment.Properties.Recipes == nil {
msg := &recipes.ErrRecipeNotFound{Name: recipe.Name, Environment: recipe.EnvironmentID}
return nil, recipes.NewRecipeError(recipes.RecipeNotFoundFailure, msg.Error(), portableresources.RecipeSetupError, recipes.GetRecipeErrorDetails(msg))
return nil, recipes.NewRecipeError(recipes.RecipeNotFoundFailure, msg.Error(), recipes_util.RecipeSetupError, recipes.GetRecipeErrorDetails(msg))
}

resource, err := resources.ParseResource(recipe.ResourceID)
if err != nil {
msg := fmt.Errorf("failed to parse resourceID: %q %w", recipe.ResourceID, err)
return nil, recipes.NewRecipeError(recipes.RecipeValidationFailed, err.Error(), portableresources.RecipeSetupError, recipes.GetRecipeErrorDetails(msg))
return nil, recipes.NewRecipeError(recipes.RecipeValidationFailed, err.Error(), recipes_util.RecipeSetupError, recipes.GetRecipeErrorDetails(msg))
}
recipeName := recipe.Name
found, ok := environment.Properties.Recipes[resource.Type()][recipeName]
if !ok {
msg := &recipes.ErrRecipeNotFound{Name: recipe.Name, Environment: recipe.EnvironmentID}
return nil, recipes.NewRecipeError(recipes.RecipeNotFoundFailure, msg.Error(), portableresources.RecipeSetupError, recipes.GetRecipeErrorDetails(msg))
return nil, recipes.NewRecipeError(recipes.RecipeNotFoundFailure, msg.Error(), recipes_util.RecipeSetupError, recipes.GetRecipeErrorDetails(msg))
}

definition := &recipes.EnvironmentDefinition{
Expand Down
16 changes: 8 additions & 8 deletions pkg/recipes/driver/bicep.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
deployments "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/go-logr/logr"
"github.com/radius-project/radius/pkg/metrics"
"github.com/radius-project/radius/pkg/portableresources"
"github.com/radius-project/radius/pkg/portableresources/datamodel"
"github.com/radius-project/radius/pkg/portableresources/processors"
"github.com/radius-project/radius/pkg/recipes"
"github.com/radius-project/radius/pkg/recipes/recipecontext"
recipes_util "github.com/radius-project/radius/pkg/recipes/util"
"github.com/radius-project/radius/pkg/rp/util"
rpv1 "github.com/radius-project/radius/pkg/rp/v1"
clients "github.com/radius-project/radius/pkg/sdk/clients"
Expand Down Expand Up @@ -80,15 +80,15 @@ func (d *bicepDriver) Execute(ctx context.Context, opts ExecuteOptions) (*recipe
if err != nil {
metrics.DefaultRecipeEngineMetrics.RecordRecipeDownloadDuration(ctx, downloadStartTime,
metrics.NewRecipeAttributes(metrics.RecipeEngineOperationDownloadRecipe, opts.Recipe.Name, &opts.Definition, recipes.RecipeDownloadFailed))
return nil, recipes.NewRecipeError(recipes.RecipeDownloadFailed, err.Error(), portableresources.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.RecipeDownloadFailed, err.Error(), recipes_util.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
}
metrics.DefaultRecipeEngineMetrics.RecordRecipeDownloadDuration(ctx, downloadStartTime,
metrics.NewRecipeAttributes(metrics.RecipeEngineOperationDownloadRecipe, opts.Recipe.Name, &opts.Definition, metrics.SuccessfulOperationState))

// create the context object to be passed to the recipe deployment
recipeContext, err := recipecontext.New(&opts.Recipe, &opts.Configuration)
if err != nil {
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), portableresources.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), recipes_util.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
}

// get the parameters after resolving the conflict between developer and operator parameters
Expand All @@ -99,7 +99,7 @@ func (d *bicepDriver) Execute(ctx context.Context, opts ExecuteOptions) (*recipe
deploymentName := deploymentPrefix + strconv.FormatInt(time.Now().UnixNano(), 10)
deploymentID, err := createDeploymentID(recipeContext.Resource.ID, deploymentName)
if err != nil {
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), portableresources.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), recipes_util.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
}

// Provider config will specify the Azure and AWS scopes (if provided).
Expand Down Expand Up @@ -127,17 +127,17 @@ func (d *bicepDriver) Execute(ctx context.Context, opts ExecuteOptions) (*recipe
clients.DeploymentsClientAPIVersion,
)
if err != nil {
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), portableresources.ExecutionError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), recipes_util.ExecutionError, recipes.GetRecipeErrorDetails(err))
}

resp, err := poller.PollUntilDone(ctx, &runtime.PollUntilDoneOptions{Frequency: pollFrequency})
if err != nil {
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), portableresources.ExecutionError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), recipes_util.ExecutionError, recipes.GetRecipeErrorDetails(err))
}

recipeResponse, err := d.prepareRecipeResponse(resp.Properties.Outputs, resp.Properties.OutputResources)
if err != nil {
return nil, recipes.NewRecipeError(recipes.InvalidRecipeOutputs, fmt.Sprintf("failed to read the recipe output %q: %s", recipes.ResultPropertyName, err.Error()), portableresources.ExecutionError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.InvalidRecipeOutputs, fmt.Sprintf("failed to read the recipe output %q: %s", recipes.ResultPropertyName, err.Error()), recipes_util.ExecutionError, recipes.GetRecipeErrorDetails(err))
}

// When a Radius portable resource consuming a recipe is redeployed, Garbage collection of the recipe resources that aren't included
Expand All @@ -149,7 +149,7 @@ func (d *bicepDriver) Execute(ctx context.Context, opts ExecuteOptions) (*recipe
// Deleting obsolete output resources.
err = d.deleteGCOutputResources(ctx, diff)
if err != nil {
return nil, recipes.NewRecipeError(recipes.RecipeGarbageCollectionFailed, err.Error(), portableresources.ExecutionError, nil)
return nil, recipes.NewRecipeError(recipes.RecipeGarbageCollectionFailed, err.Error(), recipes_util.ExecutionError, nil)
}

return recipeResponse, nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/recipes/driver/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (

"github.com/google/uuid"
v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/portableresources"
"github.com/radius-project/radius/pkg/recipes"

"github.com/radius-project/radius/pkg/recipes/terraform"
recipes_util "github.com/radius-project/radius/pkg/recipes/util"
"github.com/radius-project/radius/pkg/sdk"
ucp_provider "github.com/radius-project/radius/pkg/ucp/secret/provider"
"github.com/radius-project/radius/pkg/ucp/ucplog"
Expand Down Expand Up @@ -70,7 +70,7 @@ func (d *terraformDriver) Execute(ctx context.Context, opts ExecuteOptions) (*re

requestDirPath, err := d.createExecutionDirectory(ctx, opts.Recipe, opts.Definition)
if err != nil {
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), portableresources.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), recipes_util.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
}
defer func() {
if err := os.RemoveAll(requestDirPath); err != nil {
Expand All @@ -85,12 +85,12 @@ func (d *terraformDriver) Execute(ctx context.Context, opts ExecuteOptions) (*re
EnvRecipe: &opts.Definition,
})
if err != nil {
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), portableresources.ExecutionError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.RecipeDeploymentFailed, err.Error(), recipes_util.ExecutionError, recipes.GetRecipeErrorDetails(err))
}

recipeOutputs, err := d.prepareRecipeResponse(tfState)
if err != nil {
return nil, recipes.NewRecipeError(recipes.InvalidRecipeOutputs, fmt.Sprintf("failed to read the recipe output %q: %s", recipes.ResultPropertyName, err.Error()), portableresources.ExecutionError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.InvalidRecipeOutputs, fmt.Sprintf("failed to read the recipe output %q: %s", recipes.ResultPropertyName, err.Error()), recipes_util.ExecutionError, recipes.GetRecipeErrorDetails(err))
}

return recipeOutputs, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/recipes/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
"fmt"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/portableresources"
"github.com/radius-project/radius/pkg/recipes/util"
)

type RecipeError struct {
ErrorDetails v1.ErrorDetails
DeploymentStatus portableresources.RecipeDeploymentStatus
DeploymentStatus util.RecipeDeploymentStatus
}

// Error returns an error string describing the error code and message.
Expand All @@ -36,7 +36,7 @@ func (e *RecipeError) Is(target error) bool {
}

// NewRecipeError creates a new RecipeError error with a given code, message and error details.
func NewRecipeError(code string, message string, deploymentStatus portableresources.RecipeDeploymentStatus, details ...*v1.ErrorDetails) *RecipeError {
func NewRecipeError(code string, message string, deploymentStatus util.RecipeDeploymentStatus, details ...*v1.ErrorDetails) *RecipeError {
err := new(RecipeError)
err.ErrorDetails.Message = message
err.ErrorDetails.Code = code
Expand Down
8 changes: 4 additions & 4 deletions pkg/recipes/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"

v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
"github.com/radius-project/radius/pkg/portableresources"
"github.com/radius-project/radius/pkg/recipes/util"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -52,7 +52,7 @@ func TestNewRecipeError(t *testing.T) {
},
},
},
portableresources.RecipeSetupError,
util.RecipeSetupError,
},
},
{
Expand All @@ -65,7 +65,7 @@ func TestNewRecipeError(t *testing.T) {
Code: RecipeDeploymentFailed,
Message: "test-recipe-deployment-failed-message",
},
portableresources.ExecutionError,
util.ExecutionError,
},
},
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestGetRecipeErrorDetails(t *testing.T) {
Code: RecipeDeploymentFailed,
Message: "test-recipe-deployment-failed-message",
},
portableresources.RecipeSetupError,
util.RecipeSetupError,
},
expErrorDetails: &v1.ErrorDetails{
Code: RecipeDeploymentFailed,
Expand Down
4 changes: 2 additions & 2 deletions pkg/recipes/terraform/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import (
install "github.com/hashicorp/hc-install"
tfjson "github.com/hashicorp/terraform-json"
"github.com/radius-project/radius/pkg/metrics"
"github.com/radius-project/radius/pkg/portableresources"
"github.com/radius-project/radius/pkg/recipes"
"github.com/radius-project/radius/pkg/recipes/recipecontext"
"github.com/radius-project/radius/pkg/recipes/terraform/config"
"github.com/radius-project/radius/pkg/recipes/terraform/config/backends"
"github.com/radius-project/radius/pkg/recipes/terraform/config/providers"
"github.com/radius-project/radius/pkg/recipes/util"
"github.com/radius-project/radius/pkg/sdk"
ucp_provider "github.com/radius-project/radius/pkg/ucp/secret/provider"
"github.com/radius-project/radius/pkg/ucp/ucplog"
Expand Down Expand Up @@ -294,7 +294,7 @@ func downloadAndInspect(ctx context.Context, workingDir string, execPath string,
metrics.DefaultRecipeEngineMetrics.RecordRecipeDownloadDuration(ctx, downloadStartTime,
metrics.NewRecipeAttributes(metrics.RecipeEngineOperationDownloadRecipe, options.EnvRecipe.Name,
options.EnvRecipe, recipes.RecipeDownloadFailed))
return nil, recipes.NewRecipeError(recipes.RecipeDownloadFailed, err.Error(), portableresources.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
return nil, recipes.NewRecipeError(recipes.RecipeDownloadFailed, err.Error(), util.RecipeSetupError, recipes.GetRecipeErrorDetails(err))
}
metrics.DefaultRecipeEngineMetrics.RecordRecipeDownloadDuration(ctx, downloadStartTime,
metrics.NewRecipeAttributes(metrics.RecipeEngineOperationDownloadRecipe, options.EnvRecipe.Name,
Expand Down
25 changes: 25 additions & 0 deletions pkg/recipes/util/deploymentStatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
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 util

type RecipeDeploymentStatus string

const (
// ExecutionError represents a failure status during recipe execution.
ExecutionError RecipeDeploymentStatus = "executionError"
// RecipeSetupError represents a failure that happens before a recipe or output resources are deployed.
RecipeSetupError RecipeDeploymentStatus = "setupError"
// Success represents a successful recipe execution.
Success RecipeDeploymentStatus = "success"
)
Loading

0 comments on commit aa1efca

Please sign in to comment.