cloud/agent: generate hwreport only if a first heartbeat was successful

This is mostly a hack but as we dont have a way (yet) to know when all devices
are registered, we just wait until the first heartbeat was sent and only after
that sent a hwreport. This should at least make sure that all important network
interfaces are registered.

Change-Id: Icdfc43c212470dcc37a2f916524b5f29ee42ae42
Reviewed-on: https://review.monogon.dev/c/monogon/+/1823
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
Tested-by: Jenkins CI
diff --git a/cloud/agent/agent.go b/cloud/agent/agent.go
index 75888ce..6432586 100644
--- a/cloud/agent/agent.go
+++ b/cloud/agent/agent.go
@@ -103,14 +103,20 @@
 
 	supervisor.Signal(ctx, supervisor.SignalHealthy)
 
-	report, warnings := gatherHWReport()
-	var warningStrings []string
-	for _, w := range warnings {
-		l.Warningf("Hardware Report Warning: %v", w)
-		warningStrings = append(warningStrings, w.Error())
+	assembleHWReport := func() *bpb.AgentHardwareReport {
+		report, warnings := gatherHWReport()
+		var warningStrings []string
+		for _, w := range warnings {
+			l.Warningf("Hardware Report Warning: %v", w)
+			warningStrings = append(warningStrings, w.Error())
+		}
+		return &bpb.AgentHardwareReport{
+			Report:  report,
+			Warning: warningStrings,
+		}
 	}
 
-	var hwReportSent bool
+	var sentFirstHeartBeat, hwReportSent bool
 	var installationReport *bpb.OSInstallationReport
 	var installationGeneration int64
 	b := backoff.NewExponentialBackOff()
@@ -121,11 +127,8 @@
 		req := bpb.AgentHeartbeatRequest{
 			MachineId: agentInit.TakeoverInit.MachineId,
 		}
-		if !hwReportSent {
-			req.HardwareReport = &bpb.AgentHardwareReport{
-				Report:  report,
-				Warning: warningStrings,
-			}
+		if sentFirstHeartBeat && !hwReportSent {
+			req.HardwareReport = assembleHWReport()
 		}
 		if installationReport != nil {
 			req.InstallationReport = installationReport
@@ -139,7 +142,10 @@
 			continue
 		}
 		b.Reset()
-		hwReportSent = true
+		sentFirstHeartBeat = true
+		if req.HardwareReport != nil {
+			hwReportSent = true
+		}
 		if installationReport != nil {
 			l.Infof("Installation report sent successfully, rebooting")
 			// Close connection and wait 1s to make sure that the RST