-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic spot instance termination simulation (#24)
- Loading branch information
1 parent
0632f32
commit f43ccf4
Showing
7 changed files
with
272 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
Copyright (c) 2022 Purple Clay | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package patch | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
"time" | ||
|
||
jsonpatch "github.com/evanphx/json-patch/v5" | ||
) | ||
|
||
const spotTemplate = `[ | ||
{ | ||
"op": "replace", | ||
"path": "/instance-life-cycle", | ||
"value": "spot" | ||
}, | ||
{ | ||
"op": "add", | ||
"path": "/spot", | ||
"value": { | ||
"instance-action": { | ||
"action": "{{ .Action }}", | ||
"time": "{{ .ActionTime }}" | ||
}{{ if .TerminationTime }}, | ||
"termination-time": "{{ .TerminationTime }}"{{ end }} | ||
} | ||
}, | ||
{ | ||
"op": "add", | ||
"path": "/events", | ||
"value": { | ||
"recommendations": { | ||
"rebalance": { | ||
"noticeTime": "{{ .RebalanceTime }}" | ||
} | ||
} | ||
} | ||
} | ||
]` | ||
|
||
var spotPatch = template.Must(template.New("SpotPatch"). | ||
Parse(spotTemplate)) | ||
|
||
// SpotInstanceAction is used to represent the lifecycle event (or action) of a spot instance | ||
type SpotInstanceAction string | ||
|
||
const ( | ||
TerminateSpotInstanceAction SpotInstanceAction = "terminate" | ||
StopSpotInstanceAction SpotInstanceAction = "stop" | ||
HibernateSpotInstanceAction SpotInstanceAction = "hibernate" | ||
) | ||
|
||
// Spot is used to patch a JSON document and replicate the use and lifecycle of a spot EC2 instance. | ||
// The lifecycle of a spot instance is exposed through the use of an instance action category, | ||
// see: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-instance-termination-notices.html#instance-action-metadata | ||
type Spot struct { | ||
InstanceAction SpotInstanceAction | ||
} | ||
|
||
// Patch the document based on the provided instance action. The resulting JSON | ||
// document will conform to the IMDS specification and expose spot details within | ||
// the IMDS metadata | ||
func (p Spot) Patch(in []byte) ([]byte, error) { | ||
now := time.Now().UTC().Format(time.RFC3339) | ||
|
||
spotDetails := struct { | ||
Action SpotInstanceAction | ||
ActionTime string | ||
TerminationTime string | ||
RebalanceTime string | ||
}{ | ||
Action: p.InstanceAction, | ||
ActionTime: now, | ||
RebalanceTime: now, | ||
} | ||
|
||
if spotDetails.Action == TerminateSpotInstanceAction { | ||
// For backwards compatibility set the termination time | ||
spotDetails.TerminationTime = now | ||
} | ||
|
||
var buf bytes.Buffer | ||
spotPatch.Execute(&buf, spotDetails) | ||
|
||
patch, _ := jsonpatch.DecodePatch(buf.Bytes()) | ||
|
||
out, err := patch.Apply(in) | ||
if err != nil { | ||
return in, err | ||
} | ||
|
||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright (c) 2022 Purple Clay | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package patch_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/purpleclay/imds-mock/pkg/imds/patch" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Only used for serialising the patch into a struct for assertions | ||
type spotPatchJSON struct { | ||
InstanceLifeCycle string `json:"instance-life-cycle"` | ||
Spot struct { | ||
InstanceAction struct { | ||
Action string `json:"action"` | ||
ActionTime time.Time `json:"time"` | ||
} `json:"instance-action"` | ||
|
||
TerminationTime *time.Time `json:"termination-time"` | ||
} `json:"spot"` | ||
Events struct { | ||
Recommendations struct { | ||
Rebalance struct { | ||
NoticeTime time.Time `json:"noticeTime"` | ||
} `json:"rebalance"` | ||
} `json:"recommendations"` | ||
} `json:"events"` | ||
} | ||
|
||
func TestSpotPatch_TerminateAction(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
action patch.SpotInstanceAction | ||
}{ | ||
{ | ||
name: "TerminateAction", | ||
action: patch.TerminateSpotInstanceAction, | ||
}, | ||
{ | ||
name: "StopAction", | ||
action: patch.StopSpotInstanceAction, | ||
}, | ||
{ | ||
name: "HibernateAction", | ||
action: patch.HibernateSpotInstanceAction, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
spotPatch := patch.Spot{ | ||
InstanceAction: tt.action, | ||
} | ||
|
||
out, err := spotPatch.Patch([]byte(`{"instance-life-cycle":"on-demand"}`)) | ||
require.NoError(t, err) | ||
|
||
var spotJSON spotPatchJSON | ||
json.Unmarshal(out, &spotJSON) | ||
|
||
now := time.Now().UTC() | ||
|
||
assert.Equal(t, "spot", spotJSON.InstanceLifeCycle) | ||
assert.Equal(t, string(tt.action), spotJSON.Spot.InstanceAction.Action) | ||
assert.WithinDuration(t, now, spotJSON.Spot.InstanceAction.ActionTime, 1*time.Second) | ||
|
||
if tt.action == patch.TerminateSpotInstanceAction { | ||
require.NotNil(t, spotJSON.Spot.TerminationTime) | ||
assert.WithinDuration(t, now, *spotJSON.Spot.TerminationTime, 1*time.Second) | ||
} else { | ||
require.Nil(t, spotJSON.Spot.TerminationTime) | ||
} | ||
|
||
assert.WithinDuration(t, now, spotJSON.Events.Recommendations.Rebalance.NoticeTime, 1*time.Second) | ||
}) | ||
} | ||
} | ||
|
||
func TestSpotPatch_InvalidInputJSON(t *testing.T) { | ||
spotPatch := patch.Spot{ | ||
InstanceAction: patch.HibernateSpotInstanceAction, | ||
} | ||
|
||
_, err := spotPatch.Patch([]byte(`{`)) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters