From acd116932503cf0dbb9eaca262e23bcbe1549c32 Mon Sep 17 00:00:00 2001 From: zc Date: Thu, 24 Feb 2022 19:18:50 +0800 Subject: [PATCH] add logs (#8) --- app/cnicmd.go | 22 +++++++++++++++++----- app/ocicmd.go | 13 +++++++------ handler/cni/create.go | 4 +++- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/app/cnicmd.go b/app/cnicmd.go index 1083ac6..1aed677 100644 --- a/app/cnicmd.go +++ b/app/cnicmd.go @@ -13,23 +13,34 @@ import ( "github.com/projecteru2/docker-cni/cni" "github.com/projecteru2/docker-cni/config" "github.com/projecteru2/docker-cni/handler" + log "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) func runCNI(handler handler.Handler) func(*cli.Context) error { return func(c *cli.Context) (err error) { - stateBuf, err := ioutil.ReadAll(os.Stdin) + defer func() { + if err != nil { + log.Errorf("[hook] failed to preceed: %+v", err) + } + }() + + conf, err := config.LoadConfig(c.String("config")) if err != nil { return errors.WithStack(err) } - var state specs.State - if err = json.Unmarshal(stateBuf, &state); err != nil { + + if err = conf.SetupLog(); err != nil { return errors.WithStack(err) } - conf, err := config.LoadConfig(c.String("config")) + stateBuf, err := ioutil.ReadAll(os.Stdin) if err != nil { - return + return errors.WithStack(err) + } + var state specs.State + if err = json.Unmarshal(stateBuf, &state); err != nil { + return errors.WithStack(err) } cniFilename, cniConfigFilename, err := cni.FindCNI(conf.CNIConfDir, conf.CNIBinDir) @@ -79,6 +90,7 @@ func runCNI(handler handler.Handler) func(*cli.Context) error { return errors.WithStack(err) } + log.Infof("[hook] cni running: %+v %s", strings.Join(env, " "), cniFilename) return errors.WithStack(syscall.Exec(cniFilename, []string{cniFilename}, env)) } } diff --git a/app/ocicmd.go b/app/ocicmd.go index 5c20532..210cad4 100644 --- a/app/ocicmd.go +++ b/app/ocicmd.go @@ -14,18 +14,20 @@ import ( func runOCI(handler handler.Handler) func(*cli.Context) error { return func(c *cli.Context) (err error) { + defer func() { + if err != nil { + log.Errorf("[oci] failed to preceed: %+v", err) + } + }() + configPath, ociArgs := c.String("config"), c.Args().Slice() conf, err := setup(configPath, ociArgs) if err != nil { - log.Errorf("failed to setup: %+v", err) return } - log.Infof("docker-cni running: %+v", os.Args) - defer func() { - log.Infof("docker-cni finishing: %+v", err) - }() + log.Infof("[oci] docker-cni running: %+v", os.Args) containerMeta, err := oci.LoadContainerMeta(conf.OCISpecFilename) if err != nil { @@ -72,6 +74,5 @@ func setup(configPath string, ociArgs []string) (conf config.Config, err error) return } - log.Debugf("config: %+v", conf) return conf, conf.Validate() } diff --git a/handler/cni/create.go b/handler/cni/create.go index 87f34fc..d5d98e0 100644 --- a/handler/cni/create.go +++ b/handler/cni/create.go @@ -26,7 +26,9 @@ func (h *CNIHandler) AddCNIStartHook(conf config.Config, containerMeta *oci.Cont if containerMeta.RequiresSpecificIP() { cniArgs = append(cniArgs, "IP="+containerMeta.SpecificIP()) } - env = append(env, "CNI_ARGS="+strings.Join(cniArgs, ";")) + if len(cniArgs) != 0 { + env = append(env, "CNI_ARGS="+strings.Join(cniArgs, ";")) + } containerMeta.AppendHook("prestart", conf.BinPathname, []string{conf.BinPathname, "cni", "--config", conf.Filename, "--command", "add"}, // args