c/a/api: reorganize and add AgentInit
Move hardware reporting-related data into a separate file for better
organization.
Also add an AgentInit message which will be used to pass data to the
Agent.
Change-Id: I1eecbd5a78da03170651f76f9f24e134dddaca4f
Reviewed-on: https://review.monogon.dev/c/monogon/+/1140
Reviewed-by: Leopold Schabel <leo@monogon.tech>
Tested-by: Leopold Schabel <leo@monogon.tech>
diff --git a/cloud/shepherd/equinix/manager/initializer.go b/cloud/shepherd/equinix/manager/initializer.go
index 6194f0d..a60f352 100644
--- a/cloud/shepherd/equinix/manager/initializer.go
+++ b/cloud/shepherd/equinix/manager/initializer.go
@@ -323,14 +323,23 @@
if err := proto.Unmarshal(stdout, &arsp); err != nil {
return nil, fmt.Errorf("agent reply couldn't be unmarshaled: %w", err)
}
- if !proto.Equal(&imsg, arsp.InitMessage) {
+ var successResp *apb.TakeoverSuccess
+ switch r := arsp.Result.(type) {
+ case *apb.TakeoverResponse_Error:
+ return nil, fmt.Errorf("agent returned error: %v", r.Error.Message)
+ case *apb.TakeoverResponse_Success:
+ successResp = r.Success
+ default:
+ return nil, fmt.Errorf("agent returned unknown result of type %T", arsp.Result)
+ }
+ if !proto.Equal(&imsg, successResp.InitMessage) {
return nil, fmt.Errorf("agent did not send back the init message.")
}
- if len(arsp.Key) != ed25519.PublicKeySize {
+ if len(successResp.Key) != ed25519.PublicKeySize {
return nil, fmt.Errorf("agent key length mismatch.")
}
- klog.Infof("Started the agent (provider ID: %s, key: %s).", d.ID, hex.EncodeToString(arsp.Key))
- return arsp.Key, nil
+ klog.Infof("Started the agent (provider ID: %s, key: %s).", d.ID, hex.EncodeToString(successResp.Key))
+ return successResp.Key, nil
}
// init initializes the server described by t, using BMDB session 'sess' to set
diff --git a/cloud/shepherd/equinix/manager/initializer_test.go b/cloud/shepherd/equinix/manager/initializer_test.go
index f07341c..6e82b98 100644
--- a/cloud/shepherd/equinix/manager/initializer_test.go
+++ b/cloud/shepherd/equinix/manager/initializer_test.go
@@ -21,11 +21,9 @@
// fakeSSHClient is an SSHClient that pretends to start an agent, but in reality
// just responds with what an agent would respond on every execution attempt.
-type fakeSSHClient struct {
-}
+type fakeSSHClient struct{}
-type fakeSSHConnection struct {
-}
+type fakeSSHConnection struct{}
func (f *fakeSSHClient) Dial(ctx context.Context, address, username string, sshkey ssh.Signer, timeout time.Duration) (SSHConnection, error) {
return &fakeSSHConnection{}, nil
@@ -43,8 +41,10 @@
return nil, nil, fmt.Errorf("while generating agent public key: %v", err)
}
arsp := apb.TakeoverResponse{
- InitMessage: &aim,
- Key: pub,
+ Result: &apb.TakeoverResponse_Success{Success: &apb.TakeoverSuccess{
+ InitMessage: &aim,
+ Key: pub,
+ }},
}
arspb, err := proto.Marshal(&arsp)
if err != nil {
diff --git a/cloud/shepherd/equinix/manager/test_agent/main.go b/cloud/shepherd/equinix/manager/test_agent/main.go
index 5dd5ccd..8f29c30 100644
--- a/cloud/shepherd/equinix/manager/test_agent/main.go
+++ b/cloud/shepherd/equinix/manager/test_agent/main.go
@@ -36,8 +36,10 @@
return
}
arsp := apb.TakeoverResponse{
- InitMessage: &aim,
- Key: pub,
+ Result: &apb.TakeoverResponse_Success{Success: &apb.TakeoverSuccess{
+ InitMessage: &aim,
+ Key: pub,
+ }},
}
arspb, err := proto.Marshal(&arsp)
if err != nil {