Skip to content

Commit

Permalink
MGMT-12038: Controller should upload logs from file on summary logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsorya committed Sep 13, 2022
1 parent 9bec593 commit 6d33aa9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
13 changes: 3 additions & 10 deletions src/assisted_installer_controller/assisted_installer_controller.go
Expand Up @@ -1180,16 +1180,9 @@ func (c controller) uploadSummaryLogs(podName string, namespace string, sinceSec
}

c.log.Infof("Uploading logs for %s in %s", podName, namespace)
if podLogs, err := c.kc.GetPodLogsAsBuffer(namespace, podName, sinceSeconds); err == nil {
tarentries = append(tarentries,
*utils.NewTarEntry(podLogs, nil, int64(podLogs.Len()), fmt.Sprintf("%s.logs", podName)))
} else {
ok = false
}

if len(tarentries) == 0 {
return errors.New("No logs are available for sending summary logs")
}
podLogs := common.GetControllerPodLogs(c.kc, podName, namespace, sinceSeconds, c.log)
tarentries = append(tarentries,
*utils.NewTarEntry(podLogs, nil, int64(podLogs.Len()), fmt.Sprintf("%s.logs", podName)))

//write the combined input of the summary sources into a pipe and offload it
//to the UploadLogs request to the assisted-service
Expand Down
Expand Up @@ -1071,7 +1071,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
Context("Upload logs", func() {
var pod v1.Pod
BeforeEach(func() {
_, err := os.OpenFile(common.ControllerLogFIle, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
_, err := os.OpenFile(common.ControllerLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
Expect(err).ShouldNot(HaveOccurred())
LogsUploadPeriod = 100 * time.Millisecond
dnsValidationTimeout = 1 * time.Millisecond
Expand All @@ -1093,7 +1093,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
It("Validate upload log still sent even if GetPodLogsAsBuffer fails and log file doesn't exists", func() {
logClusterOperatorsSuccess()
reportLogProgressSuccess()
err := os.Remove(common.ControllerLogFIle)
err := os.Remove(common.ControllerLogFile)
Expect(err).ShouldNot(HaveOccurred())
mockk8sclient.EXPECT().GetPods(assistedController.Namespace, gomock.Any(), fmt.Sprintf("status.phase=%s", v1.PodRunning)).Return([]v1.Pod{pod}, nil).MinTimes(1)
mockk8sclient.EXPECT().GetPodLogsAsBuffer(assistedController.Namespace, "test", gomock.Any()).Return(nil, fmt.Errorf("dummy")).MinTimes(1)
Expand Down
33 changes: 20 additions & 13 deletions src/common/common.go
Expand Up @@ -22,7 +22,7 @@ const (
ControllerLogsSecondsAgo = 60 * 60
AssistedControllerIsReadyEvent = "AssistedControllerIsReady"
AssistedControllerPrefix = "assisted-installer-controller"
ControllerLogFIle = "/tmp/contoller_logs.log"
ControllerLogFile = "/tmp/controller_logs.log"
)

func GetHostsInStatus(hosts map[string]inventory_client.HostData, status []string, isMatch bool) map[string]inventory_client.HostData {
Expand Down Expand Up @@ -91,14 +91,7 @@ func GetPodInStatus(k8Client k8s_client.K8SClient, podNamePrefix string, namespa
return nil
}

// get pod logs,
// write tar.gz to pipe in a routine
// upload tar.gz from pipe to assisted service.
// close read and write pipes
// if podName is empty, the controller will upload its own logs from the local controller log file
func UploadPodLogs(kc k8s_client.K8SClient, ic inventory_client.InventoryClient, clusterId string, podName string, namespace string,
sinceSeconds int64, log logrus.FieldLogger) error {
log.Infof("Uploading logs for %s in %s", podName, namespace)
func GetControllerPodLogs(kc k8s_client.K8SClient, podName string, namespace string, sinceSeconds int64, log logrus.FieldLogger) *bytes.Buffer {
var podLogs *bytes.Buffer
var err error
if podName != "" {
Expand All @@ -107,31 +100,45 @@ func UploadPodLogs(kc k8s_client.K8SClient, ic inventory_client.InventoryClient,
log.WithError(err).Error("Failed to get logs from kube-api, reading from file")
}
}
// This is fallback in case we don't have kube-api and we failed to get controller podname or logs
// we will fallback to reading from file
if podName == "" || err != nil {
log.Infof("Reading logs from file")
logs, errF := ioutil.ReadFile(ControllerLogFIle)
logs, errF := ioutil.ReadFile(ControllerLogFile)
if errF != nil {
log.WithError(errF).Warnf("Failed to read %s", ControllerLogFIle)
log.WithError(errF).Warnf("Failed to read %s", ControllerLogFile)
logs = []byte(fmt.Sprintf("Failed to get logs from kube-api and from file. Read file err %e, kube-api err %e", errF, err))
}
podLogs = bytes.NewBuffer(logs)
}
return podLogs
}

// get pod logs,
// write tar.gz to pipe in a routine
// upload tar.gz from pipe to assisted service.
// close read and write pipes
// if podName is empty, the controller will upload its own logs from the local controller log file
func UploadPodLogs(kc k8s_client.K8SClient, ic inventory_client.InventoryClient, clusterId string, podName string, namespace string,
sinceSeconds int64, log logrus.FieldLogger) error {
log.Infof("Uploading logs for %s in %s", podName, namespace)
podLogs := GetControllerPodLogs(kc, podName, namespace, sinceSeconds, log)

pr, pw := io.Pipe()
defer pr.Close()

go func() {
defer pw.Close()
tarEntry := utils.NewTarEntry(podLogs, nil, int64(podLogs.Len()), fmt.Sprintf("%s.logs", podName))
err = utils.WriteToTarGz(pw, []utils.TarEntry{*tarEntry})
err := utils.WriteToTarGz(pw, []utils.TarEntry{*tarEntry})
if err != nil {
log.WithError(err).Warnf("Failed to create tar.gz")
}
}()
// if error will occur in goroutine above
//it will close writer and upload will fail
ctx := utils.GenerateRequestContext()
err = ic.UploadLogs(ctx, clusterId, models.LogsTypeController, pr)
err := ic.UploadLogs(ctx, clusterId, models.LogsTypeController, pr)
log.Infof("Done uploading controller logs to the service")
if err != nil {
utils.RequestIDLogger(ctx, log).WithError(err).Error("Failed to upload logs")
Expand Down
Expand Up @@ -70,7 +70,7 @@ func main() {
}
}

logFile, err := os.OpenFile(common.ControllerLogFIle, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
logFile, err := os.OpenFile(common.ControllerLogFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Fatal(err.Error())
}
Expand Down

0 comments on commit 6d33aa9

Please sign in to comment.