Skip to content

Commit 490d49f

Browse files
committed
Create ssh connections always and cleanup
1 parent 17bea57 commit 490d49f

File tree

2 files changed

+67
-38
lines changed

2 files changed

+67
-38
lines changed

internal/exporter/host/host.go

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,42 @@ func getRetryItemDescription(retryItem RetryItem) string {
204204
}
205205

206206
// processExporterInstancesAndBootc processes exporter instances and adds failures to global retry queue
207-
func (e *ExporterHostSyncer) processExporterInstancesAndBootc(exporterInstances []*api.ExporterInstance, hostSsh ssh.HostManager, hostName string, renderedHost *api.ExporterHost, retryQueue *[]RetryItem) {
207+
func (e *ExporterHostSyncer) processExporterInstancesAndBootc(exporterInstances []*api.ExporterInstance, hostName string, renderedHost *api.ExporterHost, retryQueue *[]RetryItem) {
208+
// Create SSH connection
209+
hostSsh, err := ssh.NewSSHHostManager(renderedHost)
210+
if err == nil {
211+
_, err = hostSsh.Status()
212+
}
213+
if err != nil {
214+
fmt.Printf(" ❌ Failed to create/test SSH connection: %v\n", err)
215+
// Queue all exporter instances for retry
216+
for _, exporterInstance := range exporterInstances {
217+
*retryQueue = append(*retryQueue, RetryItem{
218+
ExporterInstance: exporterInstance,
219+
HostName: hostName,
220+
RenderedHost: renderedHost,
221+
Attempts: 1,
222+
LastError: err,
223+
LastAttemptTime: time.Now(),
224+
})
225+
}
226+
// Also queue bootc upgrade for retry
227+
*retryQueue = append(*retryQueue, RetryItem{
228+
ExporterInstance: nil,
229+
HostName: hostName,
230+
RenderedHost: renderedHost,
231+
Attempts: 1,
232+
LastError: err,
233+
LastAttemptTime: time.Now(),
234+
})
235+
return
236+
}
237+
238+
defer func() {
239+
_ = hostSsh.Close()
240+
}()
241+
242+
// Process exporter instances
208243
for _, exporterInstance := range exporterInstances {
209244
if err := e.processExporterInstance(exporterInstance, hostSsh); err != nil {
210245
fmt.Printf(" ❌ Failed to process %s: %v\n", exporterInstance.Name, err)
@@ -219,8 +254,8 @@ func (e *ExporterHostSyncer) processExporterInstancesAndBootc(exporterInstances
219254
}
220255
}
221256

257+
// Handle bootc upgrade
222258
if err := hostSsh.HandleBootcUpgrade(e.dryRun); err != nil {
223-
// For other errors, just log them and continue
224259
fmt.Printf(" ⚠️ Bootc upgrade error: %v\n", err)
225260
*retryQueue = append(*retryQueue, RetryItem{
226261
ExporterInstance: nil,
@@ -272,7 +307,6 @@ func (e *ExporterHostSyncer) processGlobalRetryQueue(retryQueue []RetryItem) err
272307

273308
// Second pass: retry items that are ready
274309
for _, retryItem := range itemsToRetry {
275-
// Always create a fresh SSH connection for every retry attempt
276310
fmt.Printf("🔄 Retrying %s on %s (attempt %d/%d)...\n",
277311
getRetryItemDescription(retryItem), retryItem.HostName, retryItem.Attempts+1, e.retryConfig.MaxAttempts)
278312

@@ -284,6 +318,10 @@ func (e *ExporterHostSyncer) processGlobalRetryQueue(retryQueue []RetryItem) err
284318
continue
285319
}
286320

321+
defer func() {
322+
_ = hostSsh.Close()
323+
}()
324+
287325
// Test the connection
288326
status, err := hostSsh.Status()
289327
if err != nil {
@@ -371,45 +409,15 @@ func (e *ExporterHostSyncer) SyncExporterHosts() error {
371409
if err := e.tapplier.Apply(hostCopy); err != nil {
372410
return fmt.Errorf("error applying template for %s: %w", host.Name, err)
373411
}
374-
375-
fmt.Printf("\n💻 Exporter host: %s\n", hostCopy.Spec.Addresses[0])
376-
377-
// Try to create SSH connection
378-
hostSsh, err := ssh.NewSSHHostManager(hostCopy)
379-
if err != nil {
380-
fmt.Printf(" ❌ Failed to create SSH connection: %v\n", err)
381-
// Add SSH connection failure to retry queue
382-
retryQueue = append(retryQueue, RetryItem{
383-
ExporterInstance: nil,
384-
HostName: host.Name,
385-
RenderedHost: hostCopy,
386-
Attempts: 1,
387-
LastError: err,
388-
LastAttemptTime: time.Now(),
389-
})
390-
continue
391-
}
392-
393-
status, err := hostSsh.Status()
394-
if err != nil {
395-
fmt.Printf(" ❌ Failed to get SSH status: %v\n", err)
396-
// Add SSH status failure to retry queue
397-
retryQueue = append(retryQueue, RetryItem{
398-
ExporterInstance: nil,
399-
HostName: host.Name,
400-
RenderedHost: hostCopy,
401-
Attempts: 1,
402-
LastError: err,
403-
LastAttemptTime: time.Now(),
404-
})
412+
// if there are no addresses, skip the host
413+
if len(hostCopy.Spec.Addresses) == 0 {
414+
fmt.Printf(" ❌ Skipping %s - no addresses\n", host.Name)
405415
continue
406416
}
407-
if e.dryRun {
408-
fmt.Printf(" ✅ Connection: %s\n", status)
409-
}
417+
fmt.Printf("\n💻 Exporter host: %s\n", hostCopy.Spec.Addresses[0])
410418

411419
// Process each exporter instance and add failures to global retry queue
412-
e.processExporterInstancesAndBootc(exporterInstances, hostSsh, host.Name, hostCopy, &retryQueue)
420+
e.processExporterInstancesAndBootc(exporterInstances, host.Name, hostCopy, &retryQueue)
413421
}
414422

415423
// Second pass: retry all failed instances globally

internal/exporter/ssh/ssh.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type HostManager interface {
3737
RunHostCommand(command string) (*CommandResult, error)
3838
GetBootcStatus() BootcStatus
3939
HandleBootcUpgrade(dryRun bool) error
40+
Close() error
4041
}
4142

4243
// CommandResult represents the result of running a command via SSH
@@ -545,3 +546,23 @@ func (m *SSHHostManager) createSshClient() (*ssh.Client, error) {
545546
return client, nil
546547

547548
}
549+
550+
func (m *SSHHostManager) Close() error {
551+
var sftpCloseError error = nil
552+
var sshCloseError error = nil
553+
if m.sftpClient != nil {
554+
sftpCloseError = m.sftpClient.Close()
555+
}
556+
if m.sshClient != nil {
557+
sshCloseError = m.sshClient.Close()
558+
}
559+
if sshCloseError != nil {
560+
return sshCloseError
561+
}
562+
563+
if sftpCloseError != nil {
564+
return sftpCloseError
565+
}
566+
567+
return nil
568+
}

0 commit comments

Comments
 (0)