Skip to content

Commit

Permalink
add restconfig flag: qps burst (#32)
Browse files Browse the repository at this point in the history
Use flags param to set QPS and Burst values for kubeconfig, enabling
more flexibility in config

Signed-off-by: googs1025 <[email protected]>
  • Loading branch information
googs1025 authored May 15, 2024
1 parent a6f0d0c commit 8116fa9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
13 changes: 11 additions & 2 deletions cmd/knavigator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ import (

func mainInternal() error {
var kubeConfigPath, kubeCtx, taskConfigs string
var qps float64
var burst int
flag.StringVar(&kubeConfigPath, "kubeconfig", "", "kubeconfig file path")
flag.StringVar(&kubeCtx, "kubectx", "", "kube context")
flag.StringVar(&taskConfigs, "tasks", "", "comma-separated list of task config files and dirs")
flag.Float64Var(&qps, "kube-api-qps", 500, "Maximum QPS to use while talking with Kubernetes API")
flag.IntVar(&burst, "kube-api-burst", 500, "Maximum burst for throttle while talking with Kubernetes API")

klog.InitFlags(nil)
flag.Parse()
Expand All @@ -53,8 +57,13 @@ func mainInternal() error {
}

log := textlogger.NewLogger(textlogger.NewConfig(textlogger.Verbosity(utils.Flag2Verbosity(flag.Lookup("v")))))

restConfig, err := utils.GetK8sConfig(log, kubeConfigPath, kubeCtx)
cfg := &config.KubeConfig{
KubeConfigPath: kubeConfigPath,
KubeCtx: kubeCtx,
QPS: float32(qps),
Burst: burst,
}
restConfig, err := utils.GetK8sConfig(log, cfg)
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ import (
"gopkg.in/yaml.v3"
)

type KubeConfig struct {
KubeConfigPath string
KubeCtx string
QPS float32
Burst int
}

type TaskConfig struct {
Name string `yaml:"name"`
Description string `yaml:"description,omitempty"`
Expand Down
18 changes: 11 additions & 7 deletions pkg/utils/k8s_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

cfg "github.com/NVIDIA/knavigator/pkg/config"
)

func GetK8sConfig(log logr.Logger, kubeConfigPath, kubectx string) (*rest.Config, error) {
func GetK8sConfig(log logr.Logger, cfg *cfg.KubeConfig) (*rest.Config, error) {
// checking in-cluster kubeconfig
restConfig, err := rest.InClusterConfig()
if err == nil {
restConfig.Burst = cfg.Burst
restConfig.QPS = cfg.QPS
log.Info("Using in-cluster kubeconfig")
return restConfig, err
}

// checking external kubeconfig
log.Info("Using external kubeconfig")
configAccess := clientcmd.NewDefaultPathOptions()
if len(kubeConfigPath) != 0 {
configAccess.GlobalFile = kubeConfigPath
if len(cfg.KubeConfigPath) != 0 {
configAccess.GlobalFile = cfg.KubeConfigPath
}

config, err := configAccess.GetStartingConfig()
Expand All @@ -54,14 +58,14 @@ func GetK8sConfig(log logr.Logger, kubeConfigPath, kubectx string) (*rest.Config
}
}

if len(kubectx) != 0 {
log.Info("Setting kubecontext", "name", kubectx)
if len(cfg.KubeCtx) != 0 {
log.Info("Setting kubecontext", "name", cfg.KubeCtx)

err = validateKubeContext(config, kubectx)
err = validateKubeContext(config, cfg.KubeCtx)
if err != nil {
return nil, err
}
config.CurrentContext = kubectx
config.CurrentContext = cfg.KubeCtx
}

return clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{}).ClientConfig()
Expand Down
10 changes: 9 additions & 1 deletion pkg/utils/k8s_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

"github.com/stretchr/testify/require"
"k8s.io/klog/v2/textlogger"

"github.com/NVIDIA/knavigator/pkg/config"
)

const testKubeCfg1 = `
Expand Down Expand Up @@ -134,7 +136,13 @@ func TestGetK8sConfig(t *testing.T) {

cfgPath = f.Name()
}
cfg, err := GetK8sConfig(testLogger, cfgPath, tc.kubeCtx)
c := &config.KubeConfig{
KubeConfigPath: cfgPath,
KubeCtx: tc.kubeCtx,
QPS: 10,
Burst: 10,
}
cfg, err := GetK8sConfig(testLogger, c)
if len(tc.expectedErr) != 0 {
require.EqualError(t, err, tc.expectedErr)
} else {
Expand Down

0 comments on commit 8116fa9

Please sign in to comment.