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

feat: Azure Schedules #438

Merged
merged 4 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions cloud/azure/deploy/exec/containerapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pulumi/pulumi-azure-native-sdk/containerregistry"
"github.com/pulumi/pulumi-random/sdk/v4/go/random"
"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 @@ -52,6 +53,7 @@ type ContainerAppArgs struct {
MongoDatabaseName pulumi.StringInput
MongoDatabaseConnectionString pulumi.StringInput
Config config.AzureContainerAppsConfig
Schedules []*deploy.Resource
}

type ContainerApp struct {
Expand Down Expand Up @@ -182,6 +184,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("EVENT_TOKEN"),
Expand Down Expand Up @@ -247,6 +254,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 @@ -258,6 +266,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 (
"fmt"
"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, " ", "-"))
tjholm marked this conversation as resolved.
Show resolved Hide resolved
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(normalizedName),
ComponentType: pulumi.String("bindings.cron"),
Version: pulumi.String("v1"),
Metadata: app.DaprMetadataArray{
app.DaprMetadataArgs{
Name: pulumi.String("schedule"),
Value: pulumi.String(args.Cron),
},
app.DaprMetadataArgs{
Name: pulumi.String("route"),
Value: pulumi.Sprintf("/x-nitric-schedule/%s", normalizedName),
},
},
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, fmt.Sprintf("unable to create nitric schedule %s: failed to create DaprComponent for app", name))
}

return res, nil
}
31 changes: 23 additions & 8 deletions cloud/azure/deploy/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 @@ -107,14 +108,6 @@ func (d *DeployServer) Up(request *deploy.DeployUpRequest, stream deploy.DeployS
schedules := lo.Filter[*deploy.Resource](request.Spec.Resources, func(item *deploy.Resource, index int) bool {
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")
}

apis := lo.Filter[*deploy.Resource](request.Spec.Resources, func(item *deploy.Resource, index int) bool {
return item.GetApi() != nil
})
Expand Down Expand Up @@ -265,6 +258,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 @@ -280,6 +277,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 @@ -290,6 +288,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 @@ -34,6 +34,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
2 changes: 2 additions & 0 deletions cloud/azure/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,8 @@ github.com/pulumi/pulumi-azure-native-sdk/resources v1.92.0 h1:JU4X8/lVgtt/019Tu
github.com/pulumi/pulumi-azure-native-sdk/resources v1.92.0/go.mod h1:hcrDiuRrruKdGbfA9KSSDsu/ywC29FKk02bsvIVxlQE=
github.com/pulumi/pulumi-azure-native-sdk/storage v1.92.0 h1:pKOZCj84srz9uVEQJuzL6NAUBMwU/wZe3hFW6IJw9vQ=
github.com/pulumi/pulumi-azure-native-sdk/storage v1.92.0/go.mod h1:d2qFMWBm21L9R3aHqx+mJ4Xvcs77WFFfG1Dn72twwvo=
github.com/pulumi/pulumi-azure-native/sdk v1.93.0 h1:8vj8O3ZQ24SF9QT5wV76KWh3DOM5jTwKYoEy4TYX5FE=
github.com/pulumi/pulumi-azure-native/sdk v1.93.0/go.mod h1:lCTXVgZKSgw5n+CMW9iBmiTcwVgQJ8nwGLmPZedj578=
github.com/pulumi/pulumi-azure/sdk/v4 v4.42.0 h1:DOeBB0fJ/2IcEu1rT0IL8S2KNNf3bMXtTeLuPj13cPU=
github.com/pulumi/pulumi-azure/sdk/v4 v4.42.0/go.mod h1:7zcnKiAlh4fut19e5HcWO1F3cwi03a5pM121/lZ8TyE=
github.com/pulumi/pulumi-azuread/sdk/v5 v5.33.0 h1:/KXC518s5uvNrtk12HQCKgtzHm69v+IYMTmMInB5rCY=
Expand Down