Skip to content
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

Add support for template kind in Environment Recipe schema #5575

Merged
merged 7 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@
## EnvironmentRecipeProperties
### Properties
* **parameters**: any: Any object
* **templateKind**: string: Format of the template provided by the recipe. Allowed values: bicep
* **templatePath**: string (Required): Path to the template provided by the recipe. Currently only link to Azure Container Registry is supported.

## TrackedResourceTags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,18 @@ func (src *EnvironmentResource) ConvertTo() (v1.DataModelInterface, error) {
envRecipes[resourceType] = map[string]datamodel.EnvironmentRecipeProperties{}
for recipeName, recipeDetails := range recipes {
if recipeDetails != nil {
// Temporary check until we make templateKind required and enum in the schema
var templateKind = "bicep"
if recipeDetails.TemplateKind != nil {
templateKind = *recipeDetails.TemplateKind
}

if !isValidTemplateKind(templateKind) {
return &datamodel.Environment{}, v1.NewClientErrInvalidRequest(fmt.Sprintf("invalid template kind: %q", *recipeDetails.TemplateKind))
}

envRecipes[resourceType][recipeName] = datamodel.EnvironmentRecipeProperties{
TemplateKind: templateKind,
TemplatePath: to.String(recipeDetails.TemplatePath),
Parameters: recipeDetails.Parameters,
}
Expand Down Expand Up @@ -122,6 +133,7 @@ func (dst *EnvironmentResource) ConvertFrom(src v1.DataModelInterface) error {
recipes[resourceType] = map[string]*EnvironmentRecipeProperties{}
for recipeName, recipeDetails := range recipe {
recipes[resourceType][recipeName] = &EnvironmentRecipeProperties{
TemplateKind: to.Ptr(recipeDetails.TemplateKind),
TemplatePath: to.Ptr(recipeDetails.TemplatePath),
Parameters: recipeDetails.Parameters,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
v1 "github.com/project-radius/radius/pkg/armrpc/api/v1"
"github.com/project-radius/radius/pkg/corerp/datamodel"
"github.com/project-radius/radius/pkg/linkrp"
"github.com/project-radius/radius/pkg/recipes"
rpv1 "github.com/project-radius/radius/pkg/rp/v1"
"github.com/project-radius/radius/pkg/to"
"github.com/project-radius/radius/test/testutil"
Expand Down Expand Up @@ -62,6 +63,7 @@ func TestConvertVersionedToDataModel(t *testing.T) {
Recipes: map[string]map[string]datamodel.EnvironmentRecipeProperties{
linkrp.MongoDatabasesResourceType: {
"cosmos-recipe": datamodel.EnvironmentRecipeProperties{
TemplateKind: recipes.TemplateKindBicep,
TemplatePath: "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb",
},
},
Expand Down Expand Up @@ -105,6 +107,7 @@ func TestConvertVersionedToDataModel(t *testing.T) {
Recipes: map[string]map[string]datamodel.EnvironmentRecipeProperties{
linkrp.MongoDatabasesResourceType: {
"cosmos-recipe": datamodel.EnvironmentRecipeProperties{
TemplateKind: recipes.TemplateKindBicep,
TemplatePath: "br:sampleregistry.azureacr.io/radius/recipes/mongodatabases",
Parameters: map[string]any{
"throughput": float64(400),
Expand All @@ -113,9 +116,16 @@ func TestConvertVersionedToDataModel(t *testing.T) {
},
linkrp.RedisCachesResourceType: {
"redis-recipe": datamodel.EnvironmentRecipeProperties{
TemplateKind: recipes.TemplateKindBicep,
TemplatePath: "br:sampleregistry.azureacr.io/radius/recipes/rediscaches",
},
},
linkrp.DaprStateStoresResourceType: {
"statestore-recipe": datamodel.EnvironmentRecipeProperties{
TemplateKind: recipes.TemplateKindTerraform,
kachawla marked this conversation as resolved.
Show resolved Hide resolved
TemplatePath: "Azure/storage/azurerm",
},
},
},
Extensions: getTestKubernetesMetadataExtensions(t),
},
Expand Down Expand Up @@ -154,6 +164,7 @@ func TestConvertVersionedToDataModel(t *testing.T) {
Recipes: map[string]map[string]datamodel.EnvironmentRecipeProperties{
linkrp.MongoDatabasesResourceType: {
"cosmos-recipe": datamodel.EnvironmentRecipeProperties{
TemplateKind: recipes.TemplateKindBicep,
TemplatePath: "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb",
},
},
Expand Down Expand Up @@ -195,6 +206,7 @@ func TestConvertVersionedToDataModel(t *testing.T) {
Recipes: map[string]map[string]datamodel.EnvironmentRecipeProperties{
linkrp.MongoDatabasesResourceType: {
"cosmos-recipe": datamodel.EnvironmentRecipeProperties{
TemplateKind: recipes.TemplateKindBicep,
TemplatePath: "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb",
},
},
Expand All @@ -216,6 +228,10 @@ func TestConvertVersionedToDataModel(t *testing.T) {
filename: "environmentresource-invalid-linktype.json",
err: &v1.ErrClientRP{Code: v1.CodeInvalid, Message: "invalid link type: \"Applications.Link/pubsub\""},
},
{
filename: "environmentresource-invalid-templatekind.json",
err: &v1.ErrClientRP{Code: v1.CodeInvalid, Message: "invalid template kind: \"helm\""},
},
}

for _, tt := range conversionTests {
Expand All @@ -230,6 +246,7 @@ func TestConvertVersionedToDataModel(t *testing.T) {

if tt.err != nil {
require.ErrorIs(t, err, tt.err)
require.Equal(t, tt.err.Error(), err.Error())
} else {
require.NoError(t, err)
ct := dm.(*datamodel.Environment)
Expand Down Expand Up @@ -280,6 +297,7 @@ func TestConvertDataModelToVersioned(t *testing.T) {
require.Equal(t, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup/providers/Microsoft.ContainerService/managedClusters/radiusTestCluster", string(*versioned.Properties.Compute.GetEnvironmentCompute().ResourceID))
require.Equal(t, 1, len(versioned.Properties.Recipes))
require.Equal(t, "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb", string(*versioned.Properties.Recipes[linkrp.MongoDatabasesResourceType]["cosmos-recipe"].TemplatePath))
require.Equal(t, recipes.TemplateKindBicep, string(*versioned.Properties.Recipes[linkrp.MongoDatabasesResourceType]["cosmos-recipe"].TemplateKind))
require.Equal(t, map[string]any{"throughput": float64(400)}, versioned.Properties.Recipes[linkrp.MongoDatabasesResourceType]["cosmos-recipe"].Parameters)
require.Equal(t, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup", string(*versioned.Properties.Providers.Azure.Scope))
require.Equal(t, "/planes/aws/aws/accounts/140313373712/regions/us-west-2", string(*versioned.Properties.Providers.Aws.Scope))
Expand Down Expand Up @@ -310,6 +328,7 @@ func TestConvertDataModelWithIdentityToVersioned(t *testing.T) {
require.Equal(t, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup/providers/Microsoft.ContainerService/managedClusters/radiusTestCluster", string(*versioned.Properties.Compute.GetEnvironmentCompute().ResourceID))
require.Equal(t, 1, len(versioned.Properties.Recipes))
require.Equal(t, "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb", string(*versioned.Properties.Recipes[linkrp.MongoDatabasesResourceType]["cosmos-recipe"].TemplatePath))
require.Equal(t, recipes.TemplateKindBicep, string(*versioned.Properties.Recipes[linkrp.MongoDatabasesResourceType]["cosmos-recipe"].TemplateKind))
require.Equal(t, "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup", string(*versioned.Properties.Providers.Azure.Scope))

require.Equal(t, &IdentitySettings{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (dst *EnvironmentRecipeProperties) ConvertFrom(src v1.DataModelInterface) e
if !ok {
return v1.ErrInvalidModelConversion
}
dst.TemplateKind = to.Ptr(recipe.TemplateKind)
dst.TemplatePath = to.Ptr(recipe.TemplatePath)
dst.Parameters = recipe.Parameters
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/project-radius/radius/pkg/corerp/datamodel"
"github.com/project-radius/radius/pkg/linkrp"
"github.com/project-radius/radius/pkg/recipes"
"github.com/project-radius/radius/test/testutil"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -50,6 +51,7 @@ func TestEnvironmentRecipePropertiesConvertDataModelToVersioned(t *testing.T) {
// assert
require.NoError(t, err)
require.Equal(t, "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb", string(*versioned.TemplatePath))
require.Equal(t, recipes.TemplateKindBicep, string(*versioned.TemplateKind))
kachawla marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, expectedOutput, versioned.Parameters)
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb",
"parameters": {
"throughput": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"recipes": {
"Applications.Link/pubsub":{
"cosmos-recipe": {
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/pubsub"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/radius-test-rg/providers/Applications.Core/environments/env0",
"name": "env0",
"type": "Applications.Core/environments",
"properties": {
"compute": {
"kind": "kubernetes",
"resourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup/providers/Microsoft.ContainerService/managedClusters/radiusTestCluster",
"namespace": "default"
},
"providers": {
"azure": {
"scope": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup"
}
},
"recipes": {
"Applications.Link/mongoDatabases":{
"cosmos-recipe": {
"templateKind": "helm",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/mongo"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"cosmos-recipe": {
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/mongodatabases",
"parameters":{
"throughput": 400
Expand All @@ -27,8 +28,15 @@
},
"Applications.Link/redisCaches":{
"redis-recipe": {
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/rediscaches"
}
},
"Applications.Link/daprStateStores":{
"statestore-recipe": {
"templateKind": "terraform",
"templatePath": "Azure/storage/azurerm"
}
}
},
"extensions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"cosmos-recipe": {
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"cosmos-recipe": {
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb",
"parameters" : {
"throughput": 400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"cosmos-recipe": {
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb",
"parameters" : {
"throughput": 400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"cosmos-recipe": {
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"cosmos-recipe": {
"templateKind": "bicep",
"templatePath": "br:sampleregistry.azureacr.io/radius/recipes/cosmosdb"
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/corerp/api/v20220315privatepreview/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

v1 "github.com/project-radius/radius/pkg/armrpc/api/v1"
"github.com/project-radius/radius/pkg/linkrp"
"github.com/project-radius/radius/pkg/recipes"
rpv1 "github.com/project-radius/radius/pkg/rp/v1"
"github.com/project-radius/radius/pkg/to"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -124,3 +125,7 @@ func isValidLinkType(link string) bool {
}
return slices.Contains(linkTypes, link)
}

func isValidTemplateKind(link string) bool {
return slices.Contains(recipes.SupportedTemplateKind, link)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

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

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

1 change: 1 addition & 0 deletions pkg/corerp/datamodel/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type EnvironmentProperties struct {

// EnvironmentRecipeProperties represents the properties of environment's recipe.
type EnvironmentRecipeProperties struct {
TemplateKind string `json:"templateKind,omitempty"`
TemplatePath string `json:"templatePath,omitempty"`
Parameters map[string]any `json:"parameters,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/project-radius/radius/pkg/corerp/datamodel/converter"
"github.com/project-radius/radius/pkg/corerp/frontend/controller/util"
"github.com/project-radius/radius/pkg/linkrp"
recipe "github.com/project-radius/radius/pkg/recipes"
"github.com/project-radius/radius/pkg/ucp/ucplog"
"golang.org/x/exp/slices"
"oras.land/oras-go/v2/registry/remote"
Expand Down Expand Up @@ -162,7 +163,8 @@ var getDevRecipes = func(ctx context.Context) (map[string]map[string]datamodel.E
return fmt.Errorf("error occurred while finding highest version for repo %s - %s", repoPath, err.Error())
}
recipes[linkType] = map[string]datamodel.EnvironmentRecipeProperties{
name: datamodel.EnvironmentRecipeProperties{
name: {
TemplateKind: recipe.TemplateKindBicep, // TODO revisit how do we plan to support dev recipes for multiple formats
shalabhms marked this conversation as resolved.
Show resolved Hide resolved
TemplatePath: repoPath + ":" + fmt.Sprintf("%.1f", version),
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/project-radius/radius/pkg/corerp/api/v20220315privatepreview"
"github.com/project-radius/radius/pkg/corerp/datamodel"
"github.com/project-radius/radius/pkg/linkrp"
"github.com/project-radius/radius/pkg/recipes"
"github.com/project-radius/radius/pkg/to"
"github.com/project-radius/radius/pkg/ucp/store"
"github.com/project-radius/radius/test/testutil"
Expand Down Expand Up @@ -403,11 +404,13 @@ var mockgetDevRecipes = func(ctx context.Context) (map[string]map[string]datamod
recipes := map[string]map[string]datamodel.EnvironmentRecipeProperties{
linkrp.RedisCachesResourceType: {
"redis-kubernetes": {
TemplateKind: recipes.TemplateKindBicep,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test for TemplateKindTerraform?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for dev recipes?

TemplatePath: "radius.azurecr.io/recipes/rediscaches/kubernetes:1.0",
},
},
linkrp.MongoDatabasesResourceType: {
"mongo-azure": {
TemplateKind: recipes.TemplateKindBicep,
TemplatePath: "radius.azurecr.io/recipes/mongodatabases/azure:1.0",
},
},
Expand Down Expand Up @@ -630,14 +633,17 @@ func TestCreateOrUpdateRunDevRecipes(t *testing.T) {
Recipes: map[string]map[string]*v20220315privatepreview.EnvironmentRecipeProperties{
linkrp.RedisCachesResourceType: {
"redis": {
TemplateKind: to.Ptr(recipes.TemplateKindBicep),
TemplatePath: to.Ptr("radiusdev.azurecr.io/redis:1.0"),
},
"redis-kubernetes": {
TemplateKind: to.Ptr(recipes.TemplateKindBicep),
TemplatePath: to.Ptr("radius.azurecr.io/recipes/rediscaches/kubernetes:1.0"),
},
},
linkrp.MongoDatabasesResourceType: {
"mongo-azure": {
TemplateKind: to.Ptr(recipes.TemplateKindBicep),
TemplatePath: to.Ptr("radius.azurecr.io/recipes/mongodatabases/azure:1.0"),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (r *GetRecipeMetadata) Run(ctx context.Context, w http.ResponseWriter, req
}

ret := datamodel.EnvironmentRecipeProperties{
TemplateKind: recipeProperties.TemplateKind,
TemplatePath: recipeProperties.TemplatePath,
Parameters: recipeParams,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"mongo-azure": {
"templateKind": "bicep",
"templatePath": "radiusdev.azurecr.io/recipes/mongodatabases/azure:1.0",
"parameters": {
"throughput": 400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"mongo-azure": {
"templateKind": "bicep",
"templatePath": "radiusdev.azurecr.io/recipes/mongodatabases/azure:1.0",
"parameters": {
"throughput": 400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"recipes": {
"Applications.Link/mongoDatabases":{
"mongo-azure": {
"templateKind": "bicep",
"templatePath": "radiusdev.azurecr.io/recipes/mongodatabases/azure:1.0",
"parameters": {
"throughput": 400
Expand Down
Loading