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(api): support allowedlocation for batch worker #898

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 9 additions & 8 deletions server/api/internal/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ type (
Auth_JWKSURI *string `pp:",omitempty"`

// worker
Worker_BatchSAEmail string `envconfig:"WORKER_BATCH_SA_EMAIL" pp:",omitempty"`
Worker_BinaryPath string `envconfig:"WORKER_BINARY_PATH" default:"reearth-flow-worker" pp:",omitempty"`
Worker_BootDiskSizeGB string `envconfig:"WORKER_BOOT_DISK_SIZE_GB" default:"50" pp:",omitempty"`
Worker_BootDiskType string `envconfig:"WORKER_BOOT_DISK_TYPE" default:"pd-balanced" pp:",omitempty"`
Worker_ImageURL string `envconfig:"WORKER_IMAGE_URL" pp:",omitempty"`
Worker_MachineType string `envconfig:"WORKER_MACHINE_TYPE" default:"e2-standard-4" pp:",omitempty"`
Worker_MaxConcurrency string `envconfig:"WORKER_MAX_CONCURRENCY" default:"4" pp:",omitempty"`
Worker_TaskCount string `envconfig:"WORKER_TASK_COUNT" default:"1" pp:",omitempty"`
Worker_AllowedLocations []string `envconfig:"WORKER_ALLOWED_LOCATIONS" pp:",omitempty"`
Worker_BatchSAEmail string `envconfig:"WORKER_BATCH_SA_EMAIL" pp:",omitempty"`
Worker_BinaryPath string `envconfig:"WORKER_BINARY_PATH" default:"reearth-flow-worker" pp:",omitempty"`
Worker_BootDiskSizeGB string `envconfig:"WORKER_BOOT_DISK_SIZE_GB" default:"50" pp:",omitempty"`
Worker_BootDiskType string `envconfig:"WORKER_BOOT_DISK_TYPE" default:"pd-balanced" pp:",omitempty"`
Worker_ImageURL string `envconfig:"WORKER_IMAGE_URL" pp:",omitempty"`
Worker_MachineType string `envconfig:"WORKER_MACHINE_TYPE" default:"e2-standard-4" pp:",omitempty"`
Worker_MaxConcurrency string `envconfig:"WORKER_MAX_CONCURRENCY" default:"4" pp:",omitempty"`
Worker_TaskCount string `envconfig:"WORKER_TASK_COUNT" default:"1" pp:",omitempty"`
}
)

Expand Down
68 changes: 37 additions & 31 deletions server/api/internal/app/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,38 +104,44 @@ func initFile(ctx context.Context, conf *config.Config) (fileRepo gateway.File)
}

func initBatch(ctx context.Context, conf *config.Config) (batchRepo gateway.Batch) {
var err error
if conf.Worker_ImageURL != "" {
config := gcpbatch.BatchConfig{
BinaryPath: conf.Worker_BinaryPath,
BootDiskSizeGB: func() int {
tc, err := strconv.Atoi(conf.Worker_BootDiskSizeGB)
if err != nil {
log.Fatalf("Failed to convert BootDiskSizeDB: %v", err)
}
return tc
}(),
BootDiskType: conf.Worker_BootDiskType,
ImageURI: conf.Worker_ImageURL,
MachineType: conf.Worker_MachineType,
ProjectID: conf.GCPProject,
Region: conf.GCPRegion,
SAEmail: conf.Worker_BatchSAEmail,
TaskCount: func() int {
tc, err := strconv.Atoi(conf.Worker_TaskCount)
if err != nil {
log.Fatalf("Failed to convert TaskCount: %v", err)
}
return tc
}(),
}
if conf.Worker_ImageURL == "" {
return nil
}

batchRepo, err = gcpbatch.NewBatch(ctx, config)
if err != nil {
log.Fatalf("Failed to create Batch repository: %v", err)
}
return
if conf.GCPProject == "" {
log.Fatal("GCP project ID is required")
}
if conf.GCPRegion == "" {
log.Fatal("GCP region is required")
}

bootDiskSize, err := strconv.Atoi(conf.Worker_BootDiskSizeGB)
if err != nil {
log.Fatalf("invalid boot disk size: %v", err)
}

taskCount, err := strconv.Atoi(conf.Worker_TaskCount)
if err != nil {
log.Fatalf("invalid task count: %v", err)
}

config := gcpbatch.BatchConfig{
AllowedLocations: conf.Worker_AllowedLocations,
BinaryPath: conf.Worker_BinaryPath,
BootDiskSizeGB: bootDiskSize,
BootDiskType: conf.Worker_BootDiskType,
ImageURI: conf.Worker_ImageURL,
MachineType: conf.Worker_MachineType,
ProjectID: conf.GCPProject,
Region: conf.GCPRegion,
SAEmail: conf.Worker_BatchSAEmail,
TaskCount: taskCount,
}

batchRepo, err = gcpbatch.NewBatch(ctx, config)
if err != nil {
log.Fatalf("failed to create Batch repository: %v", err)
}

return batchRepo
return
}
22 changes: 13 additions & 9 deletions server/api/internal/infrastructure/gcpbatch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ import (
)

type BatchConfig struct {
BinaryPath string
BootDiskSizeGB int
BootDiskType string
ImageURI string
MachineType string
ProjectID string
Region string
SAEmail string
TaskCount int
AllowedLocations []string
BinaryPath string
BootDiskSizeGB int
BootDiskType string
ImageURI string
MachineType string
ProjectID string
Region string
SAEmail string
TaskCount int
}

type BatchClient interface {
Expand Down Expand Up @@ -157,6 +158,9 @@ func (b *BatchRepo) SubmitJob(ctx context.Context, jobID id.JobID, workflowsURL,
Instances: []*batchpb.AllocationPolicy_InstancePolicyOrTemplate{
instancePolicyOrTemplate,
},
Location: &batchpb.AllocationPolicy_LocationPolicy{
AllowedLocations: b.config.AllowedLocations,
},
ServiceAccount: &batchpb.ServiceAccount{
Email: b.config.SAEmail,
},
Expand Down