Skip to content

Commit

Permalink
Fix 'linkerd check'
Browse files Browse the repository at this point in the history
  • Loading branch information
alpeb committed Apr 23, 2024
1 parent 7359f37 commit 39db823
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 17 deletions.
6 changes: 4 additions & 2 deletions pkg/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2257,7 +2257,8 @@ func GetMeshedPodsIdentityData(ctx context.Context, api kubernetes.Interface, da
}
pods := []MeshedPodIdentityData{}
for _, pod := range podList.Items {
for _, containerSpec := range pod.Spec.Containers {
containers := append(pod.Spec.InitContainers, pod.Spec.Containers...)
for _, containerSpec := range containers {
if containerSpec.Name != k8s.ProxyContainerName {
continue
}
Expand Down Expand Up @@ -2937,7 +2938,8 @@ func CheckIfDataPlanePodsExist(pods []corev1.Pod) error {
}

func containsProxy(pod corev1.Pod) bool {
for _, containerSpec := range pod.Spec.Containers {
containers := append(pod.Spec.InitContainers, pod.Spec.Containers...)
for _, containerSpec := range containers {
if containerSpec.Name == k8s.ProxyContainerName {
return true
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/k8s/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,27 @@ func (kubeAPI *KubernetesAPI) GetNamespaceWithExtensionLabel(ctx context.Context

// GetPodStatus receives a pod and returns the pod status, based on `kubectl` logic.
// This logic is imported and adapted from the github.com/kubernetes/kubernetes project:
// https://github.com/kubernetes/kubernetes/blob/33a3e325f754d179b25558dee116fca1c67d353a/pkg/printers/internalversion/printers.go#L558-L640
// https://github.com/kubernetes/kubernetes/blob/v1.31.0-alpha.0/pkg/printers/internalversion/printers.go#L860
func GetPodStatus(pod corev1.Pod) string {
reason := string(pod.Status.Phase)
if pod.Status.Reason != "" {
reason = pod.Status.Reason
}

initContainers := make(map[string]*corev1.Container)
for i := range pod.Spec.InitContainers {
initContainers[pod.Spec.InitContainers[i].Name] = &pod.Spec.InitContainers[i]
}

initializing := false
for i := range pod.Status.InitContainerStatuses {
container := pod.Status.InitContainerStatuses[i]
switch {
case container.State.Terminated != nil && container.State.Terminated.ExitCode == 0 && container.State.Terminated.Signal == 0:
continue
case isRestartableInitContainer(initContainers[container.Name]) &&
container.Started != nil && *container.Started && container.Ready:
continue
case container.State.Terminated != nil:
// initialization is failed
if container.State.Terminated.Reason == "" {
Expand Down Expand Up @@ -292,9 +300,20 @@ func GetPodStatus(pod corev1.Pod) string {
return reason
}

// Borrowed from
// https://github.com/kubernetes/kubernetes/blob/v1.31.0-alpha.0/pkg/printers/internalversion/printers.go#L3209
func isRestartableInitContainer(initContainer *corev1.Container) bool {
if initContainer.RestartPolicy == nil {
return false
}

return *initContainer.RestartPolicy == corev1.ContainerRestartPolicyAlways
}

// GetProxyReady returns true if the pod contains a proxy that is ready
func GetProxyReady(pod corev1.Pod) bool {
for _, container := range pod.Status.ContainerStatuses {
statuses := append(pod.Status.InitContainerStatuses, pod.Status.ContainerStatuses...)
for _, container := range statuses {
if container.Name == ProxyContainerName {
return container.Ready
}
Expand Down
72 changes: 63 additions & 9 deletions pkg/k8s/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ metadata:
linkerd.io/control-plane-ns: linkerd
status:
phase: Running
`,
},
{
desc: "Pod with proxy native sidecar is running",
expected: "Running",
pod: `
apiVersion: v1
kind: Pod
metadata:
name: emoji
namespace: emojivoto
labels:
app: emoji-svc
linkerd.io/control-plane-ns: linkerd
spec:
initContainers:
- name: linkerd-proxy
restartPolicy: Always
status:
phase: Running
initContainerStatuses:
- name: linkerd-proxy
ready: true
started: true
`,
},
{
Expand Down Expand Up @@ -165,14 +189,18 @@ metadata:
labels:
app: emoji-svc
linkerd.io/control-plane-ns: linkerd
spec:
initContainers:
- name: foo
status:
phase: Running
containerStatuses:
- state:
running:
startedAt: 1995-02-10T00:42:42Z
initContainerStatuses:
- state:
- name: foo
state:
terminated:
exitCode: 0
reason: Completed
Expand All @@ -190,14 +218,21 @@ metadata:
labels:
app: emoji-svc
linkerd.io/control-plane-ns: linkerd
spec:
initContainers:
- name: foo
containers:
- name: bar
status:
phase: Running
containerStatuses:
- state:
- name: bar
state:
running:
startedAt: 1995-02-10T00:42:42Z
initContainerStatuses:
- state:
- name: foo
state:
terminated:
exitCode: 2
`,
Expand All @@ -214,14 +249,21 @@ metadata:
labels:
app: emoji-svc
linkerd.io/control-plane-ns: linkerd
spec:
initContainers:
- name: foo
containers:
- name: bar
status:
phase: Running
containerStatuses:
- state:
- name: bar
state:
running:
startedAt: 1995-02-10T00:42:42Z
initContainerStatuses:
- state:
- name: foo
state:
terminated:
signal: 9
`,
Expand All @@ -238,10 +280,14 @@ metadata:
labels:
app: emoji-svc
linkerd.io/control-plane-ns: linkerd
spec:
initContainers:
- name: foo
status:
phase: Pending
initContainerStatuses:
- state:
- name: foo
state:
terminated:
exitCode: 2
reason: CrashLoopBackOff
Expand All @@ -259,17 +305,21 @@ metadata:
labels:
app: emoji-svc
linkerd.io/control-plane-ns: linkerd
spec:
initContainers:
- name: foo
status:
phase: Pending
initContainerStatuses:
- state:
- name: foo
state:
waiting:
reason: someReason
`,
},
{
desc: "Pod init container is waiting on PodInitializing",
expected: "Init:0/0",
expected: "Init:0/1",
pod: `
apiVersion: v1
kind: Pod
Expand All @@ -279,10 +329,14 @@ metadata:
labels:
app: emoji-svc
linkerd.io/control-plane-ns: linkerd
spec:
initContainers:
- name: foo
status:
phase: Pending
initContainerStatuses:
- state:
- name: foo
state:
waiting:
reason: PodInitializing
`,
Expand Down
11 changes: 8 additions & 3 deletions viz/metrics-api/stat_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ metadata:
labels:
app: emoji-svc
linkerd.io/control-plane-ns: linkerd
spec:
initContainers:
- name: foo
status:
phase: Pending
initContainerStatuses:
- state:
- name: foo
state:
waiting:
reason: PodInitializing
`,
Expand All @@ -249,7 +253,7 @@ status:
TimeWindow: "1m",
},
expectedResponse: GenStatSummaryResponse("emoji", pkgK8s.Pod, []string{"emojivoto"}, &PodCounts{
Status: "Init:0/0",
Status: "Init:0/1",
MeshedPods: 1,
RunningPods: 1,
FailedPods: 0,
Expand All @@ -259,7 +263,8 @@ status:
{
Error: &pb.PodErrors_PodError_Container{
Container: &pb.PodErrors_PodError_ContainerError{
Reason: "PodInitializing",
Container: "foo",
Reason: "PodInitializing",
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion viz/pkg/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ func (hc *HealthChecker) checkPromAuthorized(ctx context.Context) error {
// default inbound policy is `deny`, there won't be an override
// annotation, but the proxy-injector will have set the env variable
// directly.
for _, c := range pod.Spec.Containers {
containers := append(pod.Spec.InitContainers, pod.Spec.Containers...)
for _, c := range containers {
if c.Name == k8s.ProxyContainerName {
for _, env := range c.Env {
if env.Name == "LINKERD2_PROXY_INBOUND_DEFAULT_POLICY" && env.Value == "deny" {
Expand Down

0 comments on commit 39db823

Please sign in to comment.