package middlechild import ( "context" "fmt" "io" "strings" store "github.com/Shopify/go-storage" "edge-infra.dev/pkg/lib/fog" ) const argoLogsBucket = "edge-argo-logs" // path of where the log file should be placed in the test-infra bucket func buildArgoResultPath(mcj *Job) string { return fmt.Sprintf("argo/edge-infra/%s/%s/logs/logs.txt", mcj.JobData.Workflow, mcj.JobData.Number) } // path of the argo log file as stored by argo in the argo-log bucket func buildArgoLogPath(mcj *Job, id string) string { y, m, d := mcj.JobData.Started.Date() return fmt.Sprintf("%d/%02d/%02d/%s/%s/main.log", y, int(m), d, mcj.JobData.Run, id) } // getArgoPodName searches for the Pod ID in the metadata. // If no ID is found it returns false func getArgoPodName(mcj *Job) (bool, []string) { found := false id := "" for _, meta := range mcj.Metadata { if meta.Key == "ID" { found = true id = meta.Value } } if !found { return found, []string{} } splitID := strings.Split(id, "-") s := splitID[len(splitID)-1] pods := []string{ strings.Replace(id, fmt.Sprint("-", s), fmt.Sprint("-rosa-", s), 1), strings.Replace(id, fmt.Sprint("-", s), fmt.Sprint("-rosa-with-kubeconfig-", s), 1), } return found, pods } // argoLogs attempts to find the pod name and copies the log file from the argo bucket to the test-infra bucket func argoLogs(ctx context.Context, mcj *Job, testStore, logStore store.FS) error { log := fog.FromContext(ctx) found, ids := getArgoPodName(mcj) if !found { log.Info("no pod found") return nil } if len(ids) != 2 { return fmt.Errorf("ids should always have a length of 2") } log.Info("pod found in metadata", "podID", ids) // get the two possible pod names pods := []string{buildArgoLogPath(mcj, ids[0]), buildArgoLogPath(mcj, ids[1])} log.Info("attempting to copy the argo log files") err := copyArgoLogs(ctx, testStore, logStore, pods, buildArgoResultPath(mcj)) if err != nil { return err } return nil } // copyArgoLogs copies a log from from the argo bucket to the test-infra bucket func copyArgoLogs(ctx context.Context, testStore, logStore store.FS, sources []string, destination string) error { log := fog.FromContext(ctx) log.Info("copying log files", "sources", sources, "destination", destination) log.Info("opening source log file") // try to find the rosa log file f, err := logStore.Open(context.Background(), sources[0], nil) if err != nil { // try to find the rosa-with-kubeconfig log file f, err = logStore.Open(context.Background(), sources[1], nil) if err != nil { return fmt.Errorf("Couldnt find rosa or rosa-with-kubeconfig log files: %w", err) } } log.Info("creating destination log file") wc, err := testStore.Create(ctx, destination, nil) if err != nil { return err } log.Info("copying file contents") _, err = io.Copy(wc, f) if err != nil { return err } err = wc.Close() if err != nil { return err } return f.Close() }