Skip to content

Commit

Permalink
chore: create new error type to wrap message and error (#1811)
Browse files Browse the repository at this point in the history
  • Loading branch information
jigisha620 authored Nov 15, 2024
1 parent c194a9a commit 7786f76
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/cloudprovider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,16 @@ func IsNodeClassNotReadyError(err error) bool {
var nrError *NodeClassNotReadyError
return errors.As(err, &nrError)
}

// CreateError is an error type returned by CloudProviders when instance creation fails
type CreateError struct {
error
ConditionMessage string
}

func NewCreateError(err error, message string) *CreateError {
return &CreateError{
error: err,
ConditionMessage: message,
}
}
8 changes: 7 additions & 1 deletion pkg/controllers/nodeclaim/lifecycle/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package lifecycle

import (
"context"
"errors"
"fmt"

"github.com/patrickmn/go-cache"
Expand Down Expand Up @@ -98,7 +99,12 @@ func (l *Launch) launchNodeClaim(ctx context.Context, nodeClaim *v1.NodeClaim) (
})
return nil, nil
default:
nodeClaim.StatusConditions().SetUnknownWithReason(v1.ConditionTypeLaunched, "LaunchFailed", truncateMessage(err.Error()))
var createError *cloudprovider.CreateError
if errors.As(err, &createError) {
nodeClaim.StatusConditions().SetUnknownWithReason(v1.ConditionTypeLaunched, "LaunchFailed", createError.ConditionMessage)
} else {
nodeClaim.StatusConditions().SetUnknownWithReason(v1.ConditionTypeLaunched, "LaunchFailed", truncateMessage(err.Error()))
}
return nil, fmt.Errorf("launching nodeclaim, %w", err)
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/controllers/nodeclaim/lifecycle/launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ var _ = Describe("Launch", func() {
ExpectFinalizersRemoved(ctx, env.Client, nodeClaim)
ExpectNotFound(ctx, env.Client, nodeClaim)
})
It("should set nodeClaim status condition from the condition message received if error returned is CreateError", func() {
conditionMessage := "instance creation failed"
cloudProvider.NextCreateErr = cloudprovider.NewCreateError(fmt.Errorf("error launching instance"), conditionMessage)
nodeClaim := test.NodeClaim()
ExpectApplied(ctx, env.Client, nodeClaim)
_ = ExpectObjectReconcileFailed(ctx, env.Client, nodeClaimController, nodeClaim)
nodeClaim = ExpectExists(ctx, env.Client, nodeClaim)
condition := ExpectStatusConditionExists(nodeClaim, v1.ConditionTypeLaunched)
Expect(condition.Status).To(Equal(metav1.ConditionUnknown))
Expect(condition.Message).To(Equal(conditionMessage))
})
})

0 comments on commit 7786f76

Please sign in to comment.