Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a log-level flag to set the log verbosity level #380

Merged
merged 8 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
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