|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/UDL-TF/UpdateController/internal/k8s" |
| 9 | + "github.com/UDL-TF/UpdateController/internal/steamcmd" |
| 10 | + "k8s.io/klog/v2" |
| 11 | +) |
| 12 | + |
| 13 | +// UpdateController manages TF2 server updates and pod restarts |
| 14 | +type UpdateController struct { |
| 15 | + config *Config |
| 16 | + k8sClient *k8s.Client |
| 17 | + steamClient *steamcmd.Client |
| 18 | + retryCount int |
| 19 | +} |
| 20 | + |
| 21 | +// NewUpdateController creates a new UpdateController instance |
| 22 | +func NewUpdateController(config *Config, k8sClient *k8s.Client, steamClient *steamcmd.Client) *UpdateController { |
| 23 | + return &UpdateController{ |
| 24 | + config: config, |
| 25 | + k8sClient: k8sClient, |
| 26 | + steamClient: steamClient, |
| 27 | + retryCount: 0, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Run starts the controller's main loop |
| 32 | +func (uc *UpdateController) Run(ctx context.Context) error { |
| 33 | + klog.Info("UpdateController started") |
| 34 | + |
| 35 | + ticker := time.NewTicker(uc.config.CheckInterval) |
| 36 | + defer ticker.Stop() |
| 37 | + |
| 38 | + // Perform initial check |
| 39 | + if err := uc.performUpdateCheck(ctx); err != nil { |
| 40 | + klog.Errorf("Initial update check failed: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + for { |
| 44 | + select { |
| 45 | + case <-ctx.Done(): |
| 46 | + klog.Info("UpdateController stopping") |
| 47 | + return ctx.Err() |
| 48 | + case <-ticker.C: |
| 49 | + if err := uc.performUpdateCheck(ctx); err != nil { |
| 50 | + klog.Errorf("Update check failed: %v", err) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// performUpdateCheck checks for updates and applies them if available |
| 57 | +func (uc *UpdateController) performUpdateCheck(ctx context.Context) error { |
| 58 | + klog.Info("Checking for TF2 updates...") |
| 59 | + |
| 60 | + // Check if update is available |
| 61 | + updateAvailable, err := uc.steamClient.CheckUpdate(ctx) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to check for updates: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + if !updateAvailable { |
| 67 | + klog.Info("No updates available, continuing monitoring") |
| 68 | + return nil |
| 69 | + } |
| 70 | + |
| 71 | + klog.Info("Update available! Starting update process...") |
| 72 | + return uc.applyUpdate(ctx) |
| 73 | +} |
| 74 | + |
| 75 | +// applyUpdate downloads and applies the update, then restarts pods |
| 76 | +func (uc *UpdateController) applyUpdate(ctx context.Context) error { |
| 77 | + // Download and install update |
| 78 | + klog.Info("Downloading and installing update...") |
| 79 | + if err := uc.steamClient.ApplyUpdate(ctx); err != nil { |
| 80 | + return uc.handleUpdateFailure(err) |
| 81 | + } |
| 82 | + |
| 83 | + // Validate update |
| 84 | + klog.Info("Validating update...") |
| 85 | + if err := uc.steamClient.ValidateUpdate(ctx); err != nil { |
| 86 | + return uc.handleUpdateFailure(fmt.Errorf("update validation failed: %w", err)) |
| 87 | + } |
| 88 | + |
| 89 | + // Restart affected pods |
| 90 | + klog.Info("Update successful! Restarting affected pods...") |
| 91 | + if err := uc.restartPods(ctx); err != nil { |
| 92 | + return uc.handleUpdateFailure(fmt.Errorf("failed to restart pods: %w", err)) |
| 93 | + } |
| 94 | + |
| 95 | + klog.Info("Update process completed successfully") |
| 96 | + uc.retryCount = 0 |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// handleUpdateFailure handles update failures with retry logic |
| 101 | +func (uc *UpdateController) handleUpdateFailure(err error) error { |
| 102 | + uc.retryCount++ |
| 103 | + klog.Errorf("Update failed (attempt %d/%d): %v", uc.retryCount, uc.config.MaxRetries, err) |
| 104 | + |
| 105 | + if uc.retryCount >= uc.config.MaxRetries { |
| 106 | + klog.Errorf("Max retries exceeded, giving up on this update") |
| 107 | + uc.retryCount = 0 |
| 108 | + return fmt.Errorf("update failed after %d attempts: %w", uc.config.MaxRetries, err) |
| 109 | + } |
| 110 | + |
| 111 | + klog.Infof("Will retry in %s", uc.config.RetryDelay) |
| 112 | + time.Sleep(uc.config.RetryDelay) |
| 113 | + |
| 114 | + return err |
| 115 | +} |
0 commit comments