feat(log): Support of kubectl.kubernetes.io/default-logs-container an… (#1133)

* feat(log): Support of kubectl.kubernetes.io/default-logs-container annotation

Closes #1057

* feat(log): Support of kubectl.kubernetes.io/default-logs-container annotation

refactored to return ok bool with the container

Closes #1057

* feat(log): Support of kubectl.kubernetes.io/default-logs-container annotation

refactored getDefaultLogContainer
log if container was not found
mine
Raul Cabello Martin 2021-05-26 23:33:03 +02:00 committed by GitHub
parent 16501be33a
commit c9783fac61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 2 deletions

View File

@ -32,8 +32,9 @@ var (
)
const (
logRetryCount = 20
logRetryWait = 1 * time.Second
logRetryCount = 20
logRetryWait = 1 * time.Second
defaultLogContainerAnnotation = "kubectl.kubernetes.io/default-logs-container"
)
// Pod represents a pod resource.
@ -195,6 +196,13 @@ func (p *Pod) TailLogs(ctx context.Context, c LogChan, opts LogOptions) error {
opts.SingleContainer = true
return tailLogs(ctx, p, c, opts)
}
if defaultContainer, ok := getDefaultLogContainer(po); ok {
opts.SingleContainer = true
opts.Container = defaultContainer
return tailLogs(ctx, p, c, opts)
}
if len(po.Spec.InitContainers)+len(po.Spec.Containers) == 1 {
opts.SingleContainer = true
}
@ -491,3 +499,17 @@ func (p *Pod) isControlled(path string) (string, bool, error) {
}
return "", false, nil
}
func getDefaultLogContainer(po v1.Pod) (string, bool) {
defaultContainer, ok := po.GetAnnotations()[defaultLogContainerAnnotation]
if ok {
for _, container := range po.Spec.Containers {
if container.Name == defaultContainer {
return defaultContainer, true
}
}
log.Warn().Msg(defaultContainer + " container not found. " + defaultLogContainerAnnotation + " annotation will be ignored")
}
return "", false
}

44
internal/dao/pod_test.go Normal file
View File

@ -0,0 +1,44 @@
package dao
import (
"testing"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestGetDefaultLogContainer(t *testing.T) {
type args struct {
imageSpecs ImageSpecs
}
uu := map[string]struct {
po v1.Pod
wantContainer string
wantOk bool
}{
"no_annotation": {
po: v1.Pod{},
wantContainer: "",
wantOk: false,
},
"container_not_present": {
po: v1.Pod{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{"kubectl.kubernetes.io/default-logs-container": "container1"}}},
wantContainer: "",
wantOk: false,
},
"container_found": {
po: v1.Pod{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{"kubectl.kubernetes.io/default-logs-container": "container1"}}, Spec: v1.PodSpec{Containers: []v1.Container{{Name: "container1"}}}},
wantContainer: "container1",
wantOk: true,
},
}
for k := range uu {
u := uu[k]
t.Run(k, func(t *testing.T) {
container, ok := getDefaultLogContainer(u.po)
assert.Equal(t, u.wantContainer, container)
assert.Equal(t, u.wantOk, ok)
})
}
}