Skip to content

Commit

Permalink
Add a log-level flag to set the log verbosity level (vmware-tanzu#380)
Browse files Browse the repository at this point in the history
Enabled k8s complaint logging, and set default info log level at 0, and installer info log at 1. This also enables a v flag to explicitly set the verbosity level.
  • Loading branch information
Nilanjan Daw authored Feb 21, 2022
1 parent d3cc0d7 commit fb51a6d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
21 changes: 12 additions & 9 deletions agent/help_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,33 @@ var _ = Describe("Help flag for host agent", func() {
Context("When the help flag is provided", func() {
var (
expectedOptions = []string{
"-downloadpath string",
"-kubeconfig string",
"-label value",
"-metricsbindaddress string",
"-namespace string",
"-skip-installation",
"-version",
"--downloadpath string",
"--kubeconfig string",
"--label labelFlags",
"--metricsbindaddress string",
"--namespace string",
"--skip-installation",
"--version",
"-v, --v",
}
)

It("should output the expected option", func() {
command := exec.Command(pathToHostAgentBinary, "--help")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session, "5s").Should(gexec.Exit(0))
Eventually(session, "5s").Should(gexec.Exit())

output := string(session.Err.Contents())
for _, line := range strings.Split(strings.TrimRight(output, "\n"), "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "-") {
continue
}
words := strings.Split(line, " ")
line = (words[0] + " " + words[1]) // checking the first two words
// Any option not belongs to expectedOptions is not allowed.
Expect(line).To(BeElementOf(expectedOptions))
Expect(strings.TrimSpace(line)).To(BeElementOf(expectedOptions))
}

})
Expand Down
32 changes: 24 additions & 8 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"strings"

pflag "github.com/spf13/pflag"
"github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/cloudinit"
"github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/installer"
"github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/agent/reconciler"
Expand All @@ -19,6 +20,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
"k8s.io/klog/klogr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -72,6 +74,24 @@ func (l *labelFlags) Set(value string) error {
}
}

func setupflags() {
klog.InitFlags(nil)

flag.StringVar(&namespace, "namespace", "default", "Namespace in the management cluster where you would like to register this host")
flag.Var(&labels, "label", "labels to attach to the ByoHost CR in the form labelname=labelVal for e.g. '--label site=apac --label cores=2'")
flag.StringVar(&metricsbindaddress, "metricsbindaddress", ":8080", "metricsbindaddress is the TCP address that the controller should bind to for serving prometheus metrics.It can be set to \"0\" to disable the metrics serving")
flag.StringVar(&downloadpath, "downloadpath", "/var/lib/byoh/bundles", "File System path to keep the downloads")
flag.BoolVar(&skipInstallation, "skip-installation", false, "If you want to skip installation of the kubernetes component binaries")
flag.BoolVar(&printVersion, "version", false, "Print the version of the agent")

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
hiddenFlags := [] string{"log-flush-frequency", "alsologtostderr", "log-backtrace-at", "log-dir", "logtostderr", "stderrthreshold", "vmodule", "azure-container-registry-config",
"log_backtrace_at", "log_dir", "log_file", "log_file_max_size", "add_dir_header", "skip_headers", "skip_log_headers"}
for _, hiddenFlag := range hiddenFlags {
_ = pflag.CommandLine.MarkHidden(hiddenFlag)
}
}

var (
namespace string
scheme *runtime.Scheme
Expand All @@ -86,13 +106,8 @@ var (
// TODO - fix logging

func main() {
flag.StringVar(&namespace, "namespace", "default", "Namespace in the management cluster where you would like to register this host")
flag.Var(&labels, "label", "labels to attach to the ByoHost CR in the form labelname=labelVal for e.g. '--label site=apac --label cores=2'")
flag.StringVar(&metricsbindaddress, "metricsbindaddress", ":8080", "metricsbindaddress is the TCP address that the controller should bind to for serving prometheus metrics.It can be set to \"0\" to disable the metrics serving")
flag.StringVar(&downloadpath, "downloadpath", "/var/lib/byoh/bundles", "File System path to keep the downloads")
flag.BoolVar(&skipInstallation, "skip-installation", false, "If you want to skip installation of the kubernetes component binaries")
flag.BoolVar(&printVersion, "version", false, "Print the version of the agent")
flag.Parse()
setupflags()
pflag.Parse()

if printVersion {
info := version.Get()
Expand Down Expand Up @@ -155,7 +170,8 @@ func main() {
k8sInstaller = nil
logger.Info("skip-installation flag set, skipping installer initialisation")
} else {
k8sInstaller, err = installer.New(downloadpath, logger)
// increasing installer log level to 1, so that it wont be logged by default
k8sInstaller, err = installer.New(downloadpath, logger.V(1))
if err != nil {
logger.Error(err, "failed to instantiate installer")
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/onsi/gomega v1.18.1
github.com/opencontainers/runc v1.0.3 // indirect
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5 // indirect
github.com/theupdateframework/notary v0.7.0 // indirect
k8s.io/api v0.22.2
k8s.io/apimachinery v0.22.2
Expand Down

0 comments on commit fb51a6d

Please sign in to comment.