|
| 1 | +package helpers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const defaultTestBuildkitTag = "buildx-stable-1" |
| 14 | + |
| 15 | +func KubernetesBuildkitImage() string { |
| 16 | + if v := os.Getenv("TEST_BUILDKIT_IMAGE"); v != "" { |
| 17 | + return v |
| 18 | + } |
| 19 | + tag := os.Getenv("TEST_BUILDKIT_TAG") |
| 20 | + if tag == "" { |
| 21 | + tag = defaultTestBuildkitTag |
| 22 | + } |
| 23 | + return "moby/buildkit:" + tag |
| 24 | +} |
| 25 | + |
| 26 | +func KubernetesDiagnostics(clusterName, dockerContext string) string { |
| 27 | + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 28 | + defer cancel() |
| 29 | + |
| 30 | + var buf bytes.Buffer |
| 31 | + appendK3dDiagnostics(ctx, &buf, clusterName, dockerContext) |
| 32 | + appendDockerDiagnostics(ctx, &buf, dockerContext) |
| 33 | + appendK3sServerDiagnostics(ctx, &buf, clusterName, dockerContext) |
| 34 | + return strings.TrimSpace(buf.String()) |
| 35 | +} |
| 36 | + |
| 37 | +func appendK3dDiagnostics(ctx context.Context, buf *bytes.Buffer, clusterName, dockerContext string) { |
| 38 | + appendCommandOutput(ctx, buf, "k3d cluster list", "k3d", []string{"cluster", "list", clusterName}, []string{"DOCKER_CONTEXT=" + dockerContext}) |
| 39 | + appendCommandOutput(ctx, buf, "k3d node list", "k3d", []string{"node", "list"}, []string{"DOCKER_CONTEXT=" + dockerContext}) |
| 40 | +} |
| 41 | + |
| 42 | +func appendDockerDiagnostics(ctx context.Context, buf *bytes.Buffer, dockerContext string) { |
| 43 | + args := []string{"ps", "-a", "--format", "{{.Names}}\t{{.Image}}\t{{.Status}}"} |
| 44 | + appendCommandOutput(ctx, buf, "docker ps", "docker", args, []string{"DOCKER_CONTEXT=" + dockerContext}) |
| 45 | +} |
| 46 | + |
| 47 | +func appendK3sServerDiagnostics(ctx context.Context, buf *bytes.Buffer, clusterName, dockerContext string) { |
| 48 | + nodeNames, err := clusterNodeNames(ctx, clusterName, dockerContext) |
| 49 | + if err != nil { |
| 50 | + fmt.Fprintf(buf, "cluster node discovery error: %v\n", err) |
| 51 | + return |
| 52 | + } |
| 53 | + if len(nodeNames) == 0 { |
| 54 | + fmt.Fprintln(buf, "cluster node discovery: no matching k3d containers found") |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + for _, nodeName := range nodeNames { |
| 59 | + appendCommandOutput(ctx, buf, "docker logs "+nodeName, "docker", []string{"logs", "--tail", "80", nodeName}, []string{"DOCKER_CONTEXT=" + dockerContext}) |
| 60 | + } |
| 61 | + |
| 62 | + for _, nodeName := range nodeNames { |
| 63 | + if !strings.Contains(nodeName, "-server-") { |
| 64 | + continue |
| 65 | + } |
| 66 | + appendCommandOutput(ctx, buf, "docker exec "+nodeName+" k3s kubectl get pods", "docker", []string{"exec", nodeName, "k3s", "kubectl", "get", "pods", "-A", "-o", "wide"}, []string{"DOCKER_CONTEXT=" + dockerContext}) |
| 67 | + appendCommandOutput(ctx, buf, "docker exec "+nodeName+" k3s kubectl get events", "docker", []string{"exec", nodeName, "k3s", "kubectl", "get", "events", "-A", "--sort-by=.lastTimestamp"}, []string{"DOCKER_CONTEXT=" + dockerContext}) |
| 68 | + appendCommandOutput(ctx, buf, "docker exec "+nodeName+" k3s kubectl describe pods", "docker", []string{"exec", nodeName, "k3s", "kubectl", "describe", "pods", "-A"}, []string{"DOCKER_CONTEXT=" + dockerContext}) |
| 69 | + break |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func clusterNodeNames(ctx context.Context, clusterName, dockerContext string) ([]string, error) { |
| 74 | + out, err := runCommand(ctx, "docker", []string{ |
| 75 | + "ps", "-a", |
| 76 | + "--filter", "name=k3d-" + clusterName, |
| 77 | + "--format", "{{.Names}}", |
| 78 | + }, []string{"DOCKER_CONTEXT=" + dockerContext}) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + var names []string |
| 83 | + for _, line := range strings.Split(strings.TrimSpace(out), "\n") { |
| 84 | + line = strings.TrimSpace(line) |
| 85 | + if line == "" { |
| 86 | + continue |
| 87 | + } |
| 88 | + names = append(names, line) |
| 89 | + } |
| 90 | + return names, nil |
| 91 | +} |
| 92 | + |
| 93 | +func appendCommandOutput(ctx context.Context, buf *bytes.Buffer, title, name string, args []string, env []string) { |
| 94 | + out, err := runCommand(ctx, name, args, env) |
| 95 | + fmt.Fprintf(buf, "== %s ==\n", title) |
| 96 | + if err != nil { |
| 97 | + fmt.Fprintf(buf, "error: %v\n", err) |
| 98 | + } |
| 99 | + if strings.TrimSpace(out) == "" { |
| 100 | + fmt.Fprintln(buf, "<empty>") |
| 101 | + } else { |
| 102 | + fmt.Fprintf(buf, "%s\n", strings.TrimSpace(out)) |
| 103 | + } |
| 104 | + fmt.Fprintln(buf) |
| 105 | +} |
| 106 | + |
| 107 | +func runCommand(ctx context.Context, name string, args []string, env []string) (string, error) { |
| 108 | + cmd := exec.CommandContext(ctx, name, args...) |
| 109 | + if len(env) > 0 { |
| 110 | + cmd.Env = append(os.Environ(), env...) |
| 111 | + } |
| 112 | + out, err := cmd.CombinedOutput() |
| 113 | + return string(out), err |
| 114 | +} |
0 commit comments