-
Notifications
You must be signed in to change notification settings - Fork 9
/
diagnostics.go
41 lines (33 loc) · 1.42 KB
/
diagnostics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package kubernetes
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime/kubernetes/kubeobserver"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
)
func (r *Cluster) FetchDiagnostics(ctx context.Context, reference *runtimepb.ContainerReference) (*runtimepb.Diagnostics, error) {
opaque := &kubedef.ContainerPodReference{}
if err := reference.Opaque.UnmarshalTo(opaque); err != nil {
return &runtimepb.Diagnostics{}, fnerrors.InternalError("invalid reference: %w", err)
}
pod, err := r.cli.CoreV1().Pods(opaque.Namespace).Get(ctx, opaque.PodName, metav1.GetOptions{})
if err != nil {
return &runtimepb.Diagnostics{}, err
}
for _, init := range pod.Status.InitContainerStatuses {
if init.Name == opaque.Container {
return kubeobserver.StatusToDiagnostic(init), nil
}
}
for _, ctr := range pod.Status.ContainerStatuses {
if ctr.Name == opaque.Container {
return kubeobserver.StatusToDiagnostic(ctr), nil
}
}
return &runtimepb.Diagnostics{}, fnerrors.UserError(nil, "%s/%s: no such container %q", opaque.Namespace, opaque.PodName, opaque.Container)
}