@@ -18,6 +18,7 @@ package logging
1818
1919import (
2020 "context"
21+ "encoding/json"
2122 "errors"
2223 "fmt"
2324 "os"
@@ -40,24 +41,64 @@ const (
4041// Main is the entrypoint for the containerd runtime v2 logging plugin mode.
4142//
4243// Should be called only if argv1 == MagicArgv1.
43- func Main (argsMap map [ string ] string ) error {
44- fn , err := getLoggerFunc (argsMap )
44+ func Main (argv2 string ) error {
45+ fn , err := getLoggerFunc (argv2 )
4546 if err != nil {
4647 return err
4748 }
4849 logging .Run (fn )
4950 return nil
5051}
5152
52- func getLoggerFunc (argsMap map [string ]string ) (logging.LoggerFunc , error ) {
53- if argsMap [MagicArgv1 ] == "" {
53+ // LogConfig is marshalled as "log-config.json"
54+ type LogConfig struct {
55+ Drivers []LogDriverConfig `json:"drivers,omitempty"`
56+ }
57+
58+ // LogDriverConfig is defined per a driver
59+ type LogDriverConfig struct {
60+ Driver string `json:"driver"`
61+ Opts map [string ]string `json:"opts,omitempty"`
62+ }
63+
64+ // LogConfigFilePath returns the path of log-config.json
65+ func LogConfigFilePath (dataStore , ns , id string ) string {
66+ return filepath .Join (dataStore , "containers" , ns , id , "log-config.json" )
67+ }
68+
69+ func getLoggerFunc (dataStore string ) (logging.LoggerFunc , error ) {
70+ if dataStore == "" {
5471 return nil , errors .New ("got empty data store" )
5572 }
5673 return func (_ context.Context , config * logging.Config , ready func () error ) error {
5774 if config .Namespace == "" || config .ID == "" {
5875 return errors .New ("got invalid config" )
5976 }
60- logJSONFilePath := jsonfile .Path (argsMap [MagicArgv1 ], config .Namespace , config .ID )
77+ jsonFileDriverOpts := make (map [string ]string )
78+ logConfigFilePath := LogConfigFilePath (dataStore , config .Namespace , config .ID )
79+ if _ , err := os .Stat (logConfigFilePath ); err == nil {
80+ var logConfig LogConfig
81+ logConfigFileB , err := os .ReadFile (logConfigFilePath )
82+ if err != nil {
83+ return err
84+ }
85+ if err = json .Unmarshal (logConfigFileB , & logConfig ); err != nil {
86+ return err
87+ }
88+ for _ , f := range logConfig .Drivers {
89+ switch f .Driver {
90+ case "json-file" :
91+ jsonFileDriverOpts = f .Opts
92+ default :
93+ return fmt .Errorf ("unknown driver %q" , f .Driver )
94+ }
95+ }
96+ } else if ! errors .Is (err , os .ErrNotExist ) {
97+ // the file does not exist if the container was created with nerdctl < 0.20
98+ return err
99+ }
100+
101+ logJSONFilePath := jsonfile .Path (dataStore , config .Namespace , config .ID )
61102 if err := os .MkdirAll (filepath .Dir (logJSONFilePath ), 0700 ); err != nil {
62103 return err
63104 }
@@ -70,7 +111,7 @@ func getLoggerFunc(argsMap map[string]string) (logging.LoggerFunc, error) {
70111 //maxSize Defaults to unlimited.
71112 var capVal int64
72113 capVal = - 1
73- if capacity , ok := argsMap [MaxSize ]; ok {
114+ if capacity , ok := jsonFileDriverOpts [MaxSize ]; ok {
74115 var err error
75116 capVal , err = units .FromHumanSize (capacity )
76117 if err != nil {
@@ -82,7 +123,7 @@ func getLoggerFunc(argsMap map[string]string) (logging.LoggerFunc, error) {
82123 }
83124 l .MaxBytes = capVal
84125 maxFile := 1
85- if maxFileString , ok := argsMap [MaxFile ]; ok {
126+ if maxFileString , ok := jsonFileDriverOpts [MaxFile ]; ok {
86127 var err error
87128 maxFile , err = strconv .Atoi (maxFileString )
88129 if err != nil {
0 commit comments