From 508fd9b94b1b12be3167342b03a47f8f97245e9c Mon Sep 17 00:00:00 2001
From: jiangpengfei <jiangpengfei9@huawei.com>
Date: Tue, 28 Jul 2020 10:53:30 +0800
Subject: [PATCH 09/50] kata-runtime: add kata-runtime global flag --debug

reason: add the same debug flag with runc to adapt to
containerd 1.2.0

Signed-off-by: jiangpengfei <jiangpengfei9@huawei.com>
---
 cli/kata-env_test.go         | 2 +-
 cli/main.go                  | 6 +++++-
 containerd-shim-v2/create.go | 2 +-
 pkg/katautils/config.go      | 4 ++--
 pkg/katautils/config_test.go | 8 ++++----
 5 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/cli/kata-env_test.go b/cli/kata-env_test.go
index b31b6cc2..75bb697f 100644
--- a/cli/kata-env_test.go
+++ b/cli/kata-env_test.go
@@ -178,7 +178,7 @@ func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeC
 		return "", oci.RuntimeConfig{}, err
 	}
 
-	_, config, err = katautils.LoadConfiguration(configFile, true, false)
+	_, config, err = katautils.LoadConfiguration(configFile, true, false, false)
 	if err != nil {
 		return "", oci.RuntimeConfig{}, err
 	}
diff --git a/cli/main.go b/cli/main.go
index df16c5f3..1362a8fb 100644
--- a/cli/main.go
+++ b/cli/main.go
@@ -115,6 +115,10 @@ var runtimeFlags = []cli.Flag{
 		Name:  "systemd-cgroup",
 		Usage: "enable systemd cgroup support, expects cgroupsPath to be of form \"slice:prefix:name\" for e.g. \"system.slice:runc:434234\"",
 	},
+	cli.BoolFlag{
+		Name:  "debug",
+		Usage: "enable debug output for logging",
+	},
 }
 
 // runtimeCommands is the list of supported command-line (sub-)
@@ -334,7 +338,7 @@ func beforeSubcommands(c *cli.Context) error {
 		}
 	}
 
-	configFile, runtimeConfig, err = katautils.LoadConfiguration(c.GlobalString(configFilePathOption), ignoreConfigLogs, false)
+	configFile, runtimeConfig, err = katautils.LoadConfiguration(c.GlobalString(configFilePathOption), ignoreConfigLogs, false, c.GlobalBool("debug"))
 	if err != nil {
 		fatal(err)
 	}
diff --git a/containerd-shim-v2/create.go b/containerd-shim-v2/create.go
index affdbae2..9749073d 100644
--- a/containerd-shim-v2/create.go
+++ b/containerd-shim-v2/create.go
@@ -167,7 +167,7 @@ func loadRuntimeConfig(s *service, r *taskAPI.CreateTaskRequest, anno map[string
 		configPath = os.Getenv("KATA_CONF_FILE")
 	}
 
-	_, runtimeConfig, err := katautils.LoadConfiguration(configPath, false, true)
+	_, runtimeConfig, err := katautils.LoadConfiguration(configPath, false, true, false)
 	if err != nil {
 		return nil, err
 	}
diff --git a/pkg/katautils/config.go b/pkg/katautils/config.go
index 349e667f..448d23ac 100644
--- a/pkg/katautils/config.go
+++ b/pkg/katautils/config.go
@@ -1141,7 +1141,7 @@ func initConfig() (config oci.RuntimeConfig, err error) {
 //
 // All paths are resolved fully meaning if this function does not return an
 // error, all paths are valid at the time of the call.
-func LoadConfiguration(configPath string, ignoreLogging, builtIn bool) (resolvedConfigPath string, config oci.RuntimeConfig, err error) {
+func LoadConfiguration(configPath string, ignoreLogging, builtIn bool, debugFlag bool) (resolvedConfigPath string, config oci.RuntimeConfig, err error) {
 
 	config, err = initConfig()
 	if err != nil {
@@ -1154,7 +1154,7 @@ func LoadConfiguration(configPath string, ignoreLogging, builtIn bool) (resolved
 	}
 
 	config.Debug = tomlConf.Runtime.Debug
-	if !tomlConf.Runtime.Debug {
+	if !tomlConf.Runtime.Debug && !debugFlag {
 		// If debug is not required, switch back to the original
 		// default log priority, otherwise continue in debug mode.
 		kataUtilsLogger.Logger.Level = originalLoggerLevel
diff --git a/pkg/katautils/config_test.go b/pkg/katautils/config_test.go
index 2eae1f6a..31afcca6 100644
--- a/pkg/katautils/config_test.go
+++ b/pkg/katautils/config_test.go
@@ -264,7 +264,7 @@ func testLoadConfiguration(t *testing.T, dir string,
 					assert.NoError(t, err)
 				}
 
-				resolvedConfigPath, config, err := LoadConfiguration(file, ignoreLogging, false)
+				resolvedConfigPath, config, err := LoadConfiguration(file, ignoreLogging, false, false)
 				if expectFail {
 					assert.Error(t, err)
 
@@ -578,7 +578,7 @@ func TestMinimalRuntimeConfig(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	_, config, err := LoadConfiguration(configPath, false, false)
+	_, config, err := LoadConfiguration(configPath, false, false, false)
 	if err == nil {
 		t.Fatalf("Expected loadConfiguration to fail as shim path does not exist: %+v", config)
 	}
@@ -608,7 +608,7 @@ func TestMinimalRuntimeConfig(t *testing.T) {
 		t.Error(err)
 	}
 
-	_, config, err = LoadConfiguration(configPath, false, false)
+	_, config, err = LoadConfiguration(configPath, false, false, false)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -748,7 +748,7 @@ func TestMinimalRuntimeConfigWithVsock(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	_, config, err := LoadConfiguration(configPath, false, false)
+	_, config, err := LoadConfiguration(configPath, false, false, false)
 	if err != nil {
 		t.Fatal(err)
 	}
-- 
2.14.3 (Apple Git-98)

