From 0ae2bf47d21cf99dc66be7627b6254251b3b22fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=C5=9Awi=C4=85tek?= Date: Tue, 25 Jul 2023 14:33:17 +0200 Subject: [PATCH] Move TargetAllocator flags to a separate file --- cmd/otel-allocator/config/config.go | 25 +++++----------- cmd/otel-allocator/config/flags.go | 45 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 cmd/otel-allocator/config/flags.go diff --git a/cmd/otel-allocator/config/config.go b/cmd/otel-allocator/config/config.go index 35660bf97a..83ab6da65f 100644 --- a/cmd/otel-allocator/config/config.go +++ b/cmd/otel-allocator/config/config.go @@ -16,11 +16,9 @@ package config import ( "errors" - "flag" "fmt" "io/fs" "os" - "path/filepath" "time" "github.com/go-logr/logr" @@ -31,7 +29,6 @@ import ( "gopkg.in/yaml.v2" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" - "k8s.io/client-go/util/homedir" "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -103,29 +100,21 @@ func createDefaultConfig() Config { } func FromCLI() (*Config, string, error) { - // parse the CLI flags - configFilePath := pflag.String("config-file", DefaultConfigFilePath, "The path to the config file.") - listenAddr := pflag.String("listen-addr", ":8080", "The address where this service serves.") - prometheusCREnabled := pflag.Bool("enable-prometheus-cr-watcher", false, "Enable Prometheus CRs as target sources") - kubeconfigPath := pflag.String("kubeconfig-path", filepath.Join(homedir.HomeDir(), ".kube", "config"), "absolute path to the KubeconfigPath file") - opts := zap.Options{} - opts.BindFlags(flag.CommandLine) - pflag.Parse() // load the config from the config file - config, err := Load(*configFilePath) + config, err := Load(*configFilePathFlag) if err != nil { return nil, "", err } // set the rest of the config attributes based on command-line flag values - config.RootLogger = zap.New(zap.UseFlagOptions(&opts)) + config.RootLogger = zap.New(zap.UseFlagOptions(&zapCmdLineOpts)) klog.SetLogger(config.RootLogger) ctrl.SetLogger(config.RootLogger) - config.KubeConfigFilePath = *kubeconfigPath - clusterConfig, err := clientcmd.BuildConfigFromFlags("", *kubeconfigPath) + config.KubeConfigFilePath = *kubeConfigPathFlag + clusterConfig, err := clientcmd.BuildConfigFromFlags("", *kubeConfigPathFlag) if err != nil { pathError := &fs.PathError{} if ok := errors.As(err, &pathError); !ok { @@ -136,10 +125,10 @@ func FromCLI() (*Config, string, error) { return nil, "", err } } - config.ListenAddr = listenAddr - config.PrometheusCR.Enabled = *prometheusCREnabled + config.ListenAddr = listenAddrFlag + config.PrometheusCR.Enabled = *prometheusCREnabledFlag config.ClusterConfig = clusterConfig - return &config, *configFilePath, nil + return &config, *configFilePathFlag, nil } // ValidateConfig validates the cli and file configs together. diff --git a/cmd/otel-allocator/config/flags.go b/cmd/otel-allocator/config/flags.go new file mode 100644 index 0000000000..2e87eb57ef --- /dev/null +++ b/cmd/otel-allocator/config/flags.go @@ -0,0 +1,45 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package config + +import ( + "flag" + "path/filepath" + + "github.com/spf13/pflag" + "k8s.io/client-go/util/homedir" + "sigs.k8s.io/controller-runtime/pkg/log/zap" +) + +// flag definitions +var ( + configFilePathFlag *string + listenAddrFlag *string + prometheusCREnabledFlag *bool + kubeConfigPathFlag *string + zapCmdLineOpts zap.Options +) + +func initFlags() { + configFilePathFlag = pflag.String("config-file", DefaultConfigFilePath, "The path to the config file.") + listenAddrFlag = pflag.String("listen-addr", ":8080", "The address where this service serves.") + prometheusCREnabledFlag = pflag.Bool("enable-prometheus-cr-watcher", false, "Enable Prometheus CRs as target sources") + kubeConfigPathFlag = pflag.String("kubeconfig-path", filepath.Join(homedir.HomeDir(), ".kube", "config"), "absolute path to the KubeconfigPath file") + zapCmdLineOpts.BindFlags(flag.CommandLine) +} + +func init() { + initFlags() +}