|
9 | 9 | "os" |
10 | 10 | "path/filepath" |
11 | 11 | "reflect" |
| 12 | + "time" |
12 | 13 |
|
13 | 14 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
14 | 15 | controllerruntime "sigs.k8s.io/controller-runtime" |
@@ -100,6 +101,83 @@ func (c *nodeDiagnosticController) Reconcile(ctx context.Context, nodeDiagnostic |
100 | 101 | } |
101 | 102 |
|
102 | 103 | log.Info("uploading logs", "url", nodeDiagnostic.Spec.UploadDestination) |
| 104 | + |
| 105 | + if nodeDiagnostic.Spec.UploadDestination == "node" { |
| 106 | + log.Info("saving logs to /var/log/support for download via node proxy") |
| 107 | + supportDir := filepath.Join(config.HostRoot(), "var/log/support") |
| 108 | + if err := os.MkdirAll(supportDir, 0600); err != nil { |
| 109 | + log.Error(err, "failed to create support directory") |
| 110 | + captureStatus.State = v1alpha1.CaptureState{ |
| 111 | + Completed: &v1alpha1.CaptureStateCompleted{ |
| 112 | + Reason: v1alpha1.CaptureStateFailure, |
| 113 | + Message: "failed to create support directory", |
| 114 | + StartedAt: captureStatus.State.Running.StartedAt, |
| 115 | + FinishedAt: metav1.Now(), |
| 116 | + }, |
| 117 | + } |
| 118 | + stored := nodeDiagnostic.DeepCopy() |
| 119 | + nodeDiagnostic.Status.SetCaptureStatus(captureStatus) |
| 120 | + return reconcile.Result{}, c.kubeClient.Status().Patch(ctx, nodeDiagnostic, client.MergeFrom(stored)) |
| 121 | + } |
| 122 | + destPath := filepath.Join(supportDir, fmt.Sprintf("%s-logs.tar.gz", c.nodeName)) |
| 123 | + destFile, err := os.Create(destPath) |
| 124 | + if err != nil { |
| 125 | + log.Error(err, "failed to create log file") |
| 126 | + captureStatus.State = v1alpha1.CaptureState{ |
| 127 | + Completed: &v1alpha1.CaptureStateCompleted{ |
| 128 | + Reason: v1alpha1.CaptureStateFailure, |
| 129 | + Message: "failed to create log file", |
| 130 | + StartedAt: captureStatus.State.Running.StartedAt, |
| 131 | + FinishedAt: metav1.Now(), |
| 132 | + }, |
| 133 | + } |
| 134 | + stored := nodeDiagnostic.DeepCopy() |
| 135 | + nodeDiagnostic.Status.SetCaptureStatus(captureStatus) |
| 136 | + return reconcile.Result{}, c.kubeClient.Status().Patch(ctx, nodeDiagnostic, client.MergeFrom(stored)) |
| 137 | + } |
| 138 | + defer destFile.Close() |
| 139 | + if _, err := io.Copy(destFile, archiveReader); err != nil { |
| 140 | + log.Error(err, "failed to write logs to file") |
| 141 | + captureStatus.State = v1alpha1.CaptureState{ |
| 142 | + Completed: &v1alpha1.CaptureStateCompleted{ |
| 143 | + Reason: v1alpha1.CaptureStateFailure, |
| 144 | + Message: "failed to write logs to file", |
| 145 | + StartedAt: captureStatus.State.Running.StartedAt, |
| 146 | + FinishedAt: metav1.Now(), |
| 147 | + }, |
| 148 | + } |
| 149 | + stored := nodeDiagnostic.DeepCopy() |
| 150 | + nodeDiagnostic.Status.SetCaptureStatus(captureStatus) |
| 151 | + return reconcile.Result{}, c.kubeClient.Status().Patch(ctx, nodeDiagnostic, client.MergeFrom(stored)) |
| 152 | + } |
| 153 | + log.Info("logs saved successfully", "path", destPath) |
| 154 | + captureStatus.State = v1alpha1.CaptureState{ |
| 155 | + Completed: &v1alpha1.CaptureStateCompleted{ |
| 156 | + Reason: v1alpha1.CaptureStateSuccess, |
| 157 | + Message: fmt.Sprintf("successfully saved logs to %s", destPath), |
| 158 | + StartedAt: captureStatus.State.Running.StartedAt, |
| 159 | + FinishedAt: metav1.Now(), |
| 160 | + }, |
| 161 | + } |
| 162 | + if issueCount > 0 { |
| 163 | + captureStatus.State.Completed.Reason = v1alpha1.CaptureStateSuccessWithErrors |
| 164 | + captureStatus.State.Completed.Message = fmt.Sprintf("successfully saved logs to %s with some errors", destPath) |
| 165 | + } |
| 166 | + stored := nodeDiagnostic.DeepCopy() |
| 167 | + nodeDiagnostic.Status.SetCaptureStatus(captureStatus) |
| 168 | + // Delete file from /var/log/support after 10 minutes |
| 169 | + go func() { |
| 170 | + time.Sleep(600 * time.Second) |
| 171 | + if err := os.Remove(destPath); err != nil { |
| 172 | + if !os.IsNotExist(err) { |
| 173 | + log.Error(err, "failed to delete log file after timeout", "path", destPath) |
| 174 | + } |
| 175 | + } else { |
| 176 | + log.Info("successfully deleted log file after timeout", "path", destPath) |
| 177 | + } |
| 178 | + }() |
| 179 | + return reconcile.Result{}, c.kubeClient.Status().Patch(ctx, nodeDiagnostic, client.MergeFrom(stored)) |
| 180 | + } |
103 | 181 | // wrapping this setup into one function to avoid redundant failure code |
104 | 182 | doUpload := func() error { |
105 | 183 | uploadRequest, err := http.NewRequestWithContext(ctx, http.MethodPut, string(nodeDiagnostic.Spec.UploadDestination), archiveReader) |
|
0 commit comments