1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

feat: add logging parameters in configuration file for nfd master

Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>
This commit is contained in:
AhmedGrati 2023-09-06 15:27:13 +01:00
parent ade5833ee3
commit b0be40aa09
3 changed files with 93 additions and 2 deletions

View file

@ -20,6 +20,7 @@ import (
"flag"
"fmt"
"os"
"strings"
"time"
"k8s.io/klog/v2"
@ -40,8 +41,6 @@ func main() {
printVersion := flags.Bool("version", false, "Print version and exit.")
args, overrides := initFlags(flags)
// Inject klog flags
klog.InitFlags(flags)
_ = flags.Parse(os.Args[1:])
if len(flags.Args()) > 0 {
@ -137,6 +136,8 @@ func initFlags(flagset *flag.FlagSet) (*master.Args, *master.ConfigOverrideArgs)
flagset.BoolVar(&args.EnableLeaderElection, "enable-leader-election", false,
"Enables a leader election. Enable this when running more than one replica on nfd master.")
initKlogFlags(flagset, args)
overrides := &master.ConfigOverrideArgs{
LabelWhiteList: &utils.RegexpVal{},
DenyLabelNs: &utils.StringSetVal{},
@ -165,3 +166,24 @@ func initFlags(flagset *flag.FlagSet) (*master.Args, *master.ConfigOverrideArgs)
return args, overrides
}
func initKlogFlags(flagset *flag.FlagSet, args *master.Args) {
args.Klog = make(map[string]*utils.KlogFlagVal)
flags := flag.NewFlagSet("klog flags", flag.ContinueOnError)
//flags.SetOutput(ioutil.Discard)
klog.InitFlags(flags)
flags.VisitAll(func(f *flag.Flag) {
name := klogConfigOptName(f.Name)
args.Klog[name] = utils.NewKlogFlagVal(f)
flagset.Var(args.Klog[name], f.Name, f.Usage)
})
}
func klogConfigOptName(flagName string) string {
split := strings.Split(flagName, "_")
for i, v := range split[1:] {
split[i+1] = strings.ToUpper(v[0:1]) + v[1:]
}
return strings.Join(split, "")
}

View file

@ -0,0 +1,39 @@
/*
Copyright 2023 The Kubernetes 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 main
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestKlogConfigOptName(t *testing.T) {
Convey("When converting names of klog command line flags", t, func() {
tcs := map[string]string{
"": "",
"a": "a",
"an_arg": "anArg",
"arg_with_many_parts": "argWithManyParts",
}
Convey("resulting config option names should be as expected", func() {
for input, expected := range tcs {
So(klogConfigOptName(input), ShouldEqual, expected)
}
})
})
}

View file

@ -78,6 +78,7 @@ type NFDConfig struct {
ResyncPeriod utils.DurationVal
LeaderElection LeaderElectionConfig
NfdApiParallelism int
Klog map[string]string
}
// LeaderElectionConfig contains the configuration for leader election
@ -106,6 +107,7 @@ type Args struct {
ConfigFile string
Instance string
KeyFile string
Klog map[string]*utils.KlogFlagVal
Kubeconfig string
CrdController bool
EnableNodeFeatureApi bool
@ -201,6 +203,7 @@ func newDefaultConfig() *NFDConfig {
RetryPeriod: utils.DurationVal{Duration: time.Duration(2) * time.Second},
RenewDeadline: utils.DurationVal{Duration: time.Duration(10) * time.Second},
},
Klog: make(map[string]string),
}
}
@ -1166,6 +1169,28 @@ func (m *nfdMaster) createExtendedResourcePatches(n *corev1.Node, extendedResour
return patches
}
func (m *nfdMaster) configureKlog(c *NFDConfig) error {
// Handle klog
for k, a := range m.args.Klog {
if !a.IsSetFromCmdline() {
v, ok := c.Klog[k]
if !ok {
v = a.DefValue()
}
if err := a.SetFromConfig(v); err != nil {
return fmt.Errorf("failed to set logger option klog.%s = %v: %v", k, v, err)
}
}
}
for k := range c.Klog {
if _, ok := m.args.Klog[k]; !ok {
klog.InfoS("unknown logger option in config", "optionName", k)
}
}
return nil
}
// Parse configuration options
func (m *nfdMaster) configure(filepath string, overrides string) error {
// Create a new default config
@ -1224,6 +1249,11 @@ func (m *nfdMaster) configure(filepath string, overrides string) error {
}
m.config = c
if err := m.configureKlog(c); err != nil {
return err
}
if !c.NoPublish {
kubeconfig, err := m.getKubeconfig()
if err != nil {