From 257d0b42435c60cd6fda407ec1c6cfef262d612a Mon Sep 17 00:00:00 2001 From: "K. Saito" <16172948+site0801@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:12:38 +0900 Subject: [PATCH] Add error handling for invalid labels to AddInstance() (#195) * Add error handling for invalid labels to AddInstance() * fix to use const type --- pkg/shoes/shoes.go | 6 ++++++ pkg/starter/starter.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/pkg/shoes/shoes.go b/pkg/shoes/shoes.go index b6426a6..6730d14 100644 --- a/pkg/shoes/shoes.go +++ b/pkg/shoes/shoes.go @@ -13,6 +13,8 @@ import ( "github.com/whywaita/myshoes/pkg/datastore" "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // GetClient retrieve ShoesClient use shoes-plugin @@ -88,6 +90,10 @@ func (c *GRPCClient) AddInstance(ctx context.Context, runnerName, setupScript st } resp, err := c.client.AddInstance(ctx, req) if err != nil { + // will delete a job if labels of a job are invalid + if stat, _ := status.FromError(err); stat.Code() == codes.InvalidArgument { + return "", "", "", datastore.ResourceTypeUnknown, err + } return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to AddInstance: %w", err) } diff --git a/pkg/starter/starter.go b/pkg/starter/starter.go index 9a3ccac..996e613 100644 --- a/pkg/starter/starter.go +++ b/pkg/starter/starter.go @@ -14,6 +14,8 @@ import ( "golang.org/x/sync/errgroup" "golang.org/x/sync/semaphore" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" "github.com/google/go-github/v47/github" uuid "github.com/satori/go.uuid" @@ -198,6 +200,18 @@ func (s *Starter) ProcessJob(ctx context.Context, job datastore.Job) error { if err != nil { logger.Logf(false, "failed to bung (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err) + if stat, _ := status.FromError(err); stat.Code() == codes.InvalidArgument { + if err := s.ds.DeleteJob(ctx, job.UUID); err != nil { + logger.Logf(false, "failed to delete job: %+v\n", err) + + if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("job id: %s", job.UUID)); err != nil { + return fmt.Errorf("failed to update target status (target ID: %s, job ID: %s): %w", job.TargetID, job.UUID, err) + } + + return fmt.Errorf("failed to delete job: %w", err) + } + } + if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusErr, fmt.Sprintf("failed to create an instance (job ID: %s)", job.UUID)); err != nil { return fmt.Errorf("failed to update target status (target ID: %s, job ID: %s): %w", job.TargetID, job.UUID, err) } @@ -288,6 +302,9 @@ func (s *Starter) bung(ctx context.Context, job datastore.Job, target datastore. cloudID, ipAddress, shoesType, resourceType, err := client.AddInstance(ctx, runnerName, script, target.ResourceType, labels) if err != nil { + if stat, _ := status.FromError(err); stat.Code() == codes.InvalidArgument { + return "", "", "", datastore.ResourceTypeUnknown, err + } return "", "", "", datastore.ResourceTypeUnknown, fmt.Errorf("failed to add instance: %w", err) }