Skip to content

Commit

Permalink
Move TargetAllocator flags to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Jul 25, 2023
1 parent 41461b9 commit 0ae2bf4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
25 changes: 7 additions & 18 deletions cmd/otel-allocator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ package config

import (
"errors"
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"time"

"github.com/go-logr/logr"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions cmd/otel-allocator/config/flags.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 0ae2bf4

Please sign in to comment.