Skip to content

Commit c607537

Browse files
committed
add 'none' logger
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 49a19ed commit c607537

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

pkg/logging/log_viewer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ func InitContainerLogViewer(containerLabels map[string]string, lvopts LogViewOpt
135135
return nil, fmt.Errorf("the `cri` log viewer requires nerdctl to be running in experimental mode")
136136
}
137137

138+
if lcfg.Driver == "none" {
139+
return nil, fmt.Errorf("log type `none` was selected, nothing to log")
140+
}
141+
138142
lv := &ContainerLogViewer{
139143
loggingConfig: lcfg,
140144
logViewingOptions: lvopts,

pkg/logging/logging.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ func GetDriver(name string, opts map[string]string) (Driver, error) {
9090
}
9191

9292
func init() {
93+
RegisterDriver("none", func(opts map[string]string) (Driver, error) {
94+
return &NoneLogger{}, nil
95+
}, NoneLogOptsValidate)
9396
RegisterDriver("json-file", func(opts map[string]string) (Driver, error) {
9497
return &JSONLogger{Opts: opts}, nil
9598
}, JSONFileLogOptsValidate)

pkg/logging/none_logger.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logging
18+
19+
import (
20+
"github.com/containerd/containerd/v2/core/runtime/v2/logging"
21+
)
22+
23+
type NoneLogger struct {
24+
Opts map[string]string
25+
}
26+
27+
func (n *NoneLogger) Init(dataStore, ns, id string) error {
28+
return nil
29+
}
30+
31+
func (n *NoneLogger) PreProcess(dataStore string, config *logging.Config) error {
32+
return nil
33+
}
34+
35+
func (n *NoneLogger) Process(stdout <-chan string, stderr <-chan string) error {
36+
return nil
37+
}
38+
39+
func (n *NoneLogger) PostProcess() error {
40+
return nil
41+
}
42+
43+
func NoneLogOptsValidate(_ map[string]string) error {
44+
return nil
45+
}

pkg/logging/none_logger_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logging
18+
19+
import (
20+
"os"
21+
"testing"
22+
"time"
23+
24+
"gotest.tools/v3/assert"
25+
26+
"github.com/containerd/containerd/v2/core/runtime/v2/logging"
27+
)
28+
29+
func TestNoneLogger(t *testing.T) {
30+
// Create a temporary directory for potential log files
31+
tmpDir := t.TempDir()
32+
33+
logger := &NoneLogger{
34+
Opts: map[string]string{},
35+
}
36+
37+
t.Run("NoLoggingOccurs", func(t *testing.T) {
38+
initialFiles, err := os.ReadDir(tmpDir)
39+
assert.NilError(t, err, "Failed to read temp dir")
40+
41+
// Run all logger methods
42+
logger.Init(tmpDir, "namespace", "id")
43+
logger.PreProcess(tmpDir, &logging.Config{})
44+
45+
stdout := make(chan string)
46+
stderr := make(chan string)
47+
48+
go func() {
49+
for i := 0; i < 10; i++ {
50+
stdout <- "test stdout"
51+
stderr <- "test stderr"
52+
}
53+
close(stdout)
54+
close(stderr)
55+
}()
56+
57+
err = logger.Process(stdout, stderr)
58+
assert.NilError(t, err, "Process() returned unexpected error")
59+
60+
logger.PostProcess()
61+
62+
// Wait a bit to ensure any potential writes would have occurred
63+
time.Sleep(100 * time.Millisecond)
64+
65+
// Check if any new files were created
66+
afterFiles, err := os.ReadDir(tmpDir)
67+
assert.NilError(t, err, "Failed to read temp dir after operations")
68+
69+
assert.Equal(t, len(afterFiles), len(initialFiles), "Expected no new files, but directory content changed")
70+
71+
})
72+
}

0 commit comments

Comments
 (0)