Skip to content

Commit

Permalink
Add dapr component config for cron binding.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed May 25, 2023
1 parent e85e646 commit 97b6f0e
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 6 deletions.
14 changes: 14 additions & 0 deletions cloud/azure/deploy/exec/containerapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pulumi/pulumi-azure-native-sdk/authorization"
"github.com/pulumi/pulumi-azure-native-sdk/containerregistry"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/samber/lo"

"github.com/nitrictech/nitric/cloud/azure/deploy/config"
"github.com/nitrictech/nitric/cloud/azure/deploy/policy"
Expand All @@ -50,6 +51,7 @@ type ContainerAppArgs struct {
MongoDatabaseName pulumi.StringInput
MongoDatabaseConnectionString pulumi.StringInput
Config config.AzureContainerAppsConfig
Schedules []*deploy.Resource
}

type ContainerApp struct {
Expand Down Expand Up @@ -166,6 +168,11 @@ func NewContainerApp(ctx *pulumi.Context, name string, args *ContainerAppArgs, o
}
}

// if this instance contains a schedule set the minimum instances to 1
if len(args.Schedules) > 0 {
args.Config.MinReplicas = lo.Max([]int{args.Config.MinReplicas, 1})
}

env := app.EnvironmentVarArray{
app.EnvironmentVarArgs{
Name: pulumi.String("NITRIC_ENVIRONMENT"),
Expand Down Expand Up @@ -227,6 +234,7 @@ func NewContainerApp(ctx *pulumi.Context, name string, args *ContainerAppArgs, o
Location: args.Location,
ManagedEnvironmentId: args.ManagedEnv.ID(),
Configuration: app.ConfigurationArgs{
ActiveRevisionsMode: pulumi.String("Single"),
Ingress: app.IngressArgs{
External: pulumi.BoolPtr(true),
TargetPort: pulumi.Int(9001),
Expand All @@ -238,6 +246,12 @@ func NewContainerApp(ctx *pulumi.Context, name string, args *ContainerAppArgs, o
PasswordSecretRef: pulumi.String("pwd"),
},
},
Dapr: &app.DaprArgs{
AppId: pulumi.String(appName),
AppPort: pulumi.Int(9001),
AppProtocol: pulumi.String("http"),
Enabled: pulumi.Bool(true),
},
Secrets: app.SecretArray{
app.SecretArgs{
Name: pulumi.String("pwd"),
Expand Down
78 changes: 78 additions & 0 deletions cloud/azure/deploy/schedule/dapr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2021 Nitric Technologies Pty Ltd.
//
// 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 schedule

import (
"strings"

"github.com/nitrictech/nitric/cloud/azure/deploy/exec"
"github.com/pkg/errors"
"github.com/pulumi/pulumi-azure-native-sdk/app"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

type ScheduleArgs struct {
ResourceGroupName pulumi.StringInput
Target *exec.ContainerApp
Environment *app.ManagedEnvironment
Cron string
}

type Schedule struct {
pulumi.ResourceState

Name string
Component *app.DaprComponent
}

func NewDaprCronBindingSchedule(ctx *pulumi.Context, name string, args *ScheduleArgs, opts ...pulumi.ResourceOption) (*Schedule, error) {
res := &Schedule{
Name: name,
}
normalizedName := strings.ToLower(strings.ReplaceAll(name, " ", "-"))
err := ctx.RegisterComponentResource("nitric:func:ContainerApp", name, res, opts...)
if err != nil {
return nil, err
}

res.Component, err = app.NewDaprComponent(ctx, normalizedName, &app.DaprComponentArgs{
ResourceGroupName: args.ResourceGroupName,
EnvironmentName: args.Environment.Name,
ComponentName: pulumi.String(strings.ReplaceAll(strings.ToLower(name), " ", "-")),
ComponentType: pulumi.String("bindings.cron"),
Version: pulumi.String("v1"),
Metadata: app.DaprMetadataArray{
app.DaprMetadataArgs{
Name: pulumi.String("schedule"),
Value: pulumi.String(args.Cron),
},
// TODO: Validate that the route metadata will override the component name for routing
app.DaprMetadataArgs{
Name: pulumi.String("route"),
Value: pulumi.Sprintf("/x-nitric-schedule/%s", strings.ReplaceAll(strings.ToLower(name), " ", "-")),
},
},
Scopes: pulumi.StringArray{
// Limit the scope to the target container app
args.Target.App.Configuration.Dapr().AppId().Elem(),
},
})

if err != nil {
return nil, errors.WithMessage(err, "could not create DaprComponent for app")
}

return res, nil
}
35 changes: 29 additions & 6 deletions cloud/azure/deploy/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/nitrictech/nitric/cloud/azure/deploy/config"
"github.com/nitrictech/nitric/cloud/azure/deploy/exec"
"github.com/nitrictech/nitric/cloud/azure/deploy/queue"
"github.com/nitrictech/nitric/cloud/azure/deploy/schedule"
"github.com/nitrictech/nitric/cloud/azure/deploy/topic"
"github.com/nitrictech/nitric/cloud/azure/deploy/utils"
"github.com/nitrictech/nitric/cloud/common/deploy/image"
Expand Down Expand Up @@ -99,12 +100,12 @@ func (d *DeployServer) Up(request *deploy.DeployUpRequest, stream deploy.DeployS
return item.GetSchedule() != nil
})

if len(schedules) > 0 {
// TODO: Add schedule support
// NOTE: Currently CRONTAB support is required, we either need to revisit the design of
// our scheduled expressions or implement a workaround or request a feature.
return fmt.Errorf("schedules are not currently supported for Azure deployments")
}
// if len(schedules) > 0 {
// // TODO: Add schedule support
// // NOTE: Currently CRONTAB support is required, we either need to revisit the design of
// // our scheduled expressions or implement a workaround or request a feature.
// return fmt.Errorf("schedules are not currently supported for Azure deployments")
// }

apis := lo.Filter[*deploy.Resource](request.Spec.Resources, func(item *deploy.Resource, index int) bool {
return item.GetApi() != nil
Expand Down Expand Up @@ -256,6 +257,10 @@ func (d *DeployServer) Up(request *deploy.DeployUpRequest, stream deploy.DeployS
}

if euConfig.ContainerApps != nil {
schedules := lo.Filter(schedules, func(item *deploy.Resource, idx int) bool {
return item.GetSchedule().Target.GetExecutionUnit() == eu.Name
})

apps[eu.Name], err = exec.NewContainerApp(ctx, eu.Name, &exec.ContainerAppArgs{
ResourceGroupName: rg.Name,
Location: pulumi.String(details.Region),
Expand All @@ -271,6 +276,7 @@ func (d *DeployServer) Up(request *deploy.DeployUpRequest, stream deploy.DeployS
MongoDatabaseName: mongodbName,
MongoDatabaseConnectionString: mongoConnectionString,
Config: *euConfig.ContainerApps,
Schedules: schedules,
}, pulumi.Parent(contEnv))
if err != nil {
return status.Errorf(codes.Internal, "error occurred whilst creating container app %s", eu.Name)
Expand All @@ -281,6 +287,23 @@ func (d *DeployServer) Up(request *deploy.DeployUpRequest, stream deploy.DeployS
}
}

for _, s := range schedules {
cAppTarget, ok := apps[s.GetSchedule().Target.GetExecutionUnit()]
if !ok {
return fmt.Errorf("could not find target %s for schedule %s", s.GetSchedule().Target, s.Name)
}

_, err := schedule.NewDaprCronBindingSchedule(ctx, s.Name, &schedule.ScheduleArgs{
ResourceGroupName: rg.Name,
Target: cAppTarget,
Environment: contEnv.ManagedEnv,
Cron: s.GetSchedule().Cron,
})
if err != nil {
return err
}
}

// For each bucket create a new bucket
for _, b := range buckets {
azBucket, err := bucket.NewAzureStorageBucket(ctx, b.Name, &bucket.AzureStorageBucketArgs{
Expand Down
1 change: 1 addition & 0 deletions cloud/azure/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/pulumi/pulumi-azure-native-sdk/operationalinsights v1.92.0
github.com/pulumi/pulumi-azure-native-sdk/resources v1.92.0
github.com/pulumi/pulumi-azure-native-sdk/storage v1.92.0
github.com/pulumi/pulumi-azure-native/sdk v1.93.0
github.com/pulumi/pulumi-azure/sdk/v4 v4.42.0
github.com/pulumi/pulumi-azuread/sdk/v5 v5.33.0
github.com/pulumi/pulumi-random/sdk/v4 v4.8.2
Expand Down
Loading

0 comments on commit 97b6f0e

Please sign in to comment.