From 5aabba9c99687735807dde65d04ea732be9ce8cb Mon Sep 17 00:00:00 2001 From: purpleclay Date: Sun, 28 Aug 2022 09:43:52 +0100 Subject: [PATCH] hibernation is immediate --- pkg/imds/patch/spot.go | 9 ++++++--- pkg/imds/patch/spot_test.go | 24 ++++++++++++++---------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/pkg/imds/patch/spot.go b/pkg/imds/patch/spot.go index daa5890..358727d 100644 --- a/pkg/imds/patch/spot.go +++ b/pkg/imds/patch/spot.go @@ -83,10 +83,13 @@ type Spot struct { // document will conform to the IMDS specification and expose spot details within // the IMDS metadata func (p Spot) Patch(in []byte) ([]byte, error) { - // TODO: do not add 2 minutes onto a hibernate interruption notice + // A spot interruption notice can be issued two minutes in advance during a best case scenario + nowTime := time.Now().UTC() + if p.InstanceAction != HibernateSpotInstanceAction { + nowTime = nowTime.Add(2 * time.Minute) + } - // A spot interruption notice is issued two minutes in advance during a best case scenario - now := time.Now().UTC().Add(2 * time.Minute).Format(time.RFC3339) + now := nowTime.Format(time.RFC3339) spotDetails := struct { Action SpotInstanceAction diff --git a/pkg/imds/patch/spot_test.go b/pkg/imds/patch/spot_test.go index f7b289f..ef1fb8d 100644 --- a/pkg/imds/patch/spot_test.go +++ b/pkg/imds/patch/spot_test.go @@ -52,22 +52,26 @@ type spotPatchJSON struct { } `json:"events"` } -func TestSpotPatch_TerminateAction(t *testing.T) { +func TestSpotPatch(t *testing.T) { tests := []struct { - name string - action patch.SpotInstanceAction + name string + action patch.SpotInstanceAction + duration time.Duration }{ { - name: "TerminateAction", - action: patch.TerminateSpotInstanceAction, + name: "TerminateAction", + action: patch.TerminateSpotInstanceAction, + duration: 2 * time.Minute, }, { - name: "StopAction", - action: patch.StopSpotInstanceAction, + name: "StopAction", + action: patch.StopSpotInstanceAction, + duration: 2 * time.Minute, }, { - name: "HibernateAction", - action: patch.HibernateSpotInstanceAction, + name: "HibernateAction", + action: patch.HibernateSpotInstanceAction, + duration: 0, }, } for _, tt := range tests { @@ -82,7 +86,7 @@ func TestSpotPatch_TerminateAction(t *testing.T) { var spotJSON spotPatchJSON json.Unmarshal(out, &spotJSON) - now := time.Now().UTC().Add(2 * time.Minute) + now := time.Now().UTC().Add(tt.duration) assert.Equal(t, "spot", spotJSON.InstanceLifeCycle) assert.Equal(t, string(tt.action), spotJSON.Spot.InstanceAction.Action)