-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bottlerocket host update api integration
Adds support for updating via the Bottlerocket Update API. The new platform is used if the node is labeled bottlerocket.aws/updater-interface-version=2.0.0
- Loading branch information
Showing
8 changed files
with
485 additions
and
23 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
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,163 @@ | ||
package update_api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/Masterminds/semver" | ||
"github.com/bottlerocket-os/bottlerocket-update-operator/pkg/logging" | ||
"github.com/bottlerocket-os/bottlerocket-update-operator/pkg/platform" | ||
"github.com/pkg/errors" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// Assert Update-API as a platform implementor. | ||
var _ platform.Platform = (*Platform)(nil) | ||
|
||
type Platform struct { | ||
log logging.Logger | ||
httpClient http.Client | ||
} | ||
|
||
func New() (*Platform, error) { | ||
return &Platform{log: logging.New("platform"), httpClient: http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", bottlerocketApiSock) | ||
}, | ||
}, | ||
// Set a 10 second timeout for all requests | ||
Timeout: 10 * time.Second, | ||
}}, nil | ||
} | ||
|
||
type statusResponse struct { | ||
osVersion *semver.Version | ||
} | ||
|
||
func (sr *statusResponse) OK() bool { | ||
// Bottlerocket OS version needs to be at least 0.4.1 to support the Update API | ||
constraint, _ := semver.NewConstraint(">= 0.4.1") | ||
return constraint.Check(sr.osVersion) | ||
} | ||
|
||
// Try to determine if the Bottlerocket version at least 0.4.1 | ||
func (p Platform) Status() (platform.Status, error) { | ||
response, err := p.httpClient.Get("http://unix" + "/os") | ||
if err != nil { | ||
p.log.WithError(err).Error("error when trying to GET '/actions/refresh-updates'") | ||
return nil, err | ||
} | ||
var osInfo interface{} | ||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = json.Unmarshal(body, osInfo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Assert 'version_id' in the OS info json output as string | ||
osVersion, err := semver.NewVersion(osInfo.(map[string]interface{})["version_id"].(string)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to parse 'version_id' field as semver") | ||
} | ||
return &statusResponse{osVersion: osVersion}, nil | ||
} | ||
|
||
type listAvailableResponse struct { | ||
chosenUpdate *UpdateImage | ||
} | ||
|
||
func (lar *listAvailableResponse) Updates() []platform.Update { | ||
if lar.chosenUpdate == nil { | ||
return nil | ||
} | ||
updates := make([]platform.Update, 1) | ||
updates[0] = lar.chosenUpdate | ||
return updates | ||
} | ||
|
||
func (p Platform) ListAvailable() (platform.Available, error) { | ||
p.log.Debug("fetching list of available updates") | ||
|
||
// Refresh list of updates and check if there are any available | ||
err := postAction(p.httpClient, "/actions/refresh-updates") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
updateStatus, err := getUpdateStatus(p.httpClient) | ||
if updateStatus.MostRecentCommand.CmdType == refresh && updateStatus.MostRecentCommand.CmdStatus == Success { | ||
// If there is no available update to update to | ||
if updateStatus.ChosenUpdate == nil { | ||
return &listAvailableResponse{chosenUpdate: nil}, err | ||
} | ||
return &listAvailableResponse{chosenUpdate: updateStatus.ChosenUpdate}, err | ||
} else { | ||
return &listAvailableResponse{chosenUpdate: nil}, errors.New("failed to refresh updates or update action performed out of band") | ||
} | ||
} | ||
|
||
func (p Platform) Prepare(target platform.Update) error { | ||
updateStatus, err := getUpdateStatus(p.httpClient) | ||
if err != nil { | ||
return err | ||
} | ||
if updateStatus.UpdateState != Available && updateStatus.UpdateState != Staged { | ||
return errors.Errorf("unexpected update state: %s, expecting state to be 'Available' or 'Staged'. update action performed out of band?", updateStatus.UpdateState) | ||
} | ||
|
||
// Download the update and apply it to the inactive partition | ||
err = postAction(p.httpClient, "/actions/prepare-update") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
updateStatus, err = getUpdateStatus(p.httpClient) | ||
if updateStatus.MostRecentCommand.CmdType != prepare || updateStatus.MostRecentCommand.CmdStatus != Success { | ||
return errors.New("failed to prepare update or update action performed out of band") | ||
} | ||
return nil | ||
} | ||
|
||
func (p Platform) Update(target platform.Update) error { | ||
updateStatus, err := getUpdateStatus(p.httpClient) | ||
if err != nil { | ||
return err | ||
} | ||
if updateStatus.UpdateState != Staged { | ||
return errors.Errorf("unexpected update state: %s, expecting state to be 'Staged'. update action performed out of band?", updateStatus.UpdateState) | ||
} | ||
|
||
// Activate the prepared update | ||
err = postAction(p.httpClient, "/actions/activate-update") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
updateStatus, err = getUpdateStatus(p.httpClient) | ||
if updateStatus.MostRecentCommand.CmdType != activate || updateStatus.MostRecentCommand.CmdStatus != Success { | ||
return errors.New("failed to activate update or update action performed out of band") | ||
} | ||
return nil | ||
} | ||
|
||
func (p Platform) BootUpdate(target platform.Update, rebootNow bool) error { | ||
updateStatus, err := getUpdateStatus(p.httpClient) | ||
if err != nil { | ||
return err | ||
} | ||
if updateStatus.UpdateState != Ready { | ||
return errors.Errorf("unexpected update state: %s, expecting state to be 'Ready'. update action performed out of band?", updateStatus.UpdateState) | ||
} | ||
|
||
// Reboot the host into the activated update | ||
err = postAction(p.httpClient, "/actions/reboot") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.