Skip to content

Commit

Permalink
Do not log warning if lifecycle extension not found from container
Browse files Browse the repository at this point in the history
Otherwise we fill the log files really quickly.
  • Loading branch information
ernoaapa committed Feb 15, 2018
1 parent 0d701fd commit e37cbb7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
24 changes: 24 additions & 0 deletions pkg/runtime/containerd/extensions/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package extensions

import (
"fmt"

"github.com/pkg/errors"
)

// Definitions of common error types from extensions
var (
ErrNotFound = errors.New("not found")
)

// IsNotFound returns true if the error is due to a missing resource
func IsNotFound(err error) bool {
return errors.Cause(err) == ErrNotFound
}

// ErrWithMessagef updates error message with formated message
// I.e. errors.WithMessage(err, fmt.Sprintf(...
// Hopefully we can change to errors.WithMessagef some day: /~https://github.com/pkg/errors/pull/118
func ErrWithMessagef(err error, format string, args ...interface{}) error {
return errors.WithMessage(err, fmt.Sprintf(format, args...))
}
2 changes: 1 addition & 1 deletion pkg/runtime/containerd/extensions/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func IncrementRestart(ctx context.Context, client *containerd.Client, c *contain
func GetLifecycleExtension(c containers.Container) (ContainerLifecycle, error) {
extension, ok := c.Extensions[lifecycleExtensionName]
if !ok {
return ContainerLifecycle{}, fmt.Errorf("ContainerLifecycle extension not found in container [%s]", c.ID)
return ContainerLifecycle{}, ErrWithMessagef(ErrNotFound, "ContainerLifecycle extension not found in container [%s]", c.ID)
}

decoded, err := typeurl.UnmarshalAny(&extension)
Expand Down
30 changes: 30 additions & 0 deletions pkg/runtime/containerd/extensions/lifecycle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package extensions

import (
"github.com/containerd/containerd/containers"
"github.com/containerd/typeurl"
"testing"

"github.com/gogo/protobuf/types"
"github.com/stretchr/testify/assert"
)

func TestGetLifecycleExtension(t *testing.T) {
lifecycle := &ContainerLifecycle{
StartCount: 666,
RestartPolicy: OnFailure,
}
any, _ := typeurl.MarshalAny(lifecycle)
extensions := make(map[string]types.Any)
extensions[lifecycleExtensionName] = *any

result, err := GetLifecycleExtension(containers.Container{Extensions: extensions})
assert.NoError(t, err)
assert.Equal(t, lifecycle.StartCount, result.StartCount)
assert.Equal(t, lifecycle.RestartPolicy, result.RestartPolicy)
}

func TestGetLifecycleExtensionReturnNotFoundErr(t *testing.T) {
_, err := GetLifecycleExtension(containers.Container{})
assert.True(t, IsNotFound(err))
}
4 changes: 2 additions & 2 deletions pkg/runtime/containerd/mapping/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func MapContainerStatusToInternalModel(container containers.Container, status co

func getRestartCount(container containers.Container) int {
lifecycle, err := extensions.GetLifecycleExtension(container)
if err != nil {
if err != nil && !extensions.IsNotFound(err) {
log.Warnf("Error while resolving container restart count, fallback to zero: %s", err)
}

Expand All @@ -189,7 +189,7 @@ func haveNamespace(container containers.Container, namespace specs.LinuxNamespac

func getRestartPolicy(container containers.Container) string {
lifecycle, err := extensions.GetLifecycleExtension(container)
if err != nil {
if err != nil && !extensions.IsNotFound(err) {
log.Warnf("Error while resolving container restart policy, fallback to default: %s", err)
}

Expand Down

0 comments on commit e37cbb7

Please sign in to comment.