@@ -24,6 +24,7 @@ import (
2424 "github.com/samber/lo"
2525 "go.uber.org/multierr"
2626 v1 "k8s.io/api/core/v1"
27+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728 "k8s.io/client-go/util/workqueue"
2829 "k8s.io/klog/v2"
2930 "k8s.io/utils/clock"
@@ -201,13 +202,36 @@ func (c *Controller) handleNodeClaim(ctx context.Context, msg messages.Message,
201202 if zone != "" && instanceType != "" {
202203 c .unavailableOfferingsCache .MarkUnavailable (ctx , string (msg .Kind ()), instanceType , zone , v1beta1 .CapacityTypeSpot )
203204 }
205+ spotTotal .WithLabelValues (instanceType , zone , nodeClaim .Status .NodeName , nodeClaim .Labels ["karpenter.sh/nodepool" ]).Inc ()
206+ // try to create a new nodeclaim immediately but ignore error if it fails
207+ if err := c .createNodeClaim (ctx , nodeClaim ); err != nil {
208+ log .FromContext (ctx ).Error (err , "[interruption handling]failed to create a new nodeclaim" )
209+ } else {
210+ log .FromContext (ctx ).Info ("Created new nodeclaim due to spot interruption" )
211+ // wait for the node provisioning before draining
212+ time .Sleep (60 * time .Second )
213+ }
204214 }
205215 if action != NoAction {
206216 return c .deleteNodeClaim (ctx , nodeClaim , node )
207217 }
208218 return nil
209219}
210220
221+ // createNodeClaim creates a new NodeClaim with the same spec of the interrupted one
222+ func (c * Controller ) createNodeClaim (ctx context.Context , oldNodeClaim * v1beta1.NodeClaim ) error {
223+ newNodeClaim := & v1beta1.NodeClaim {
224+ ObjectMeta : metav1.ObjectMeta {
225+ GenerateName : oldNodeClaim .ObjectMeta .GenerateName ,
226+ Annotations : oldNodeClaim .ObjectMeta .Annotations ,
227+ Labels : oldNodeClaim .ObjectMeta .Labels ,
228+ OwnerReferences : oldNodeClaim .ObjectMeta .OwnerReferences ,
229+ },
230+ Spec : oldNodeClaim .Spec ,
231+ }
232+ return c .kubeClient .Create (ctx , newNodeClaim )
233+ }
234+
211235// deleteNodeClaim removes the NodeClaim from the api-server
212236func (c * Controller ) deleteNodeClaim (ctx context.Context , nodeClaim * v1beta1.NodeClaim , node * v1.Node ) error {
213237 if ! nodeClaim .DeletionTimestamp .IsZero () {
0 commit comments