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 restconfig flag: qps burst #32

Merged
merged 1 commit into from
May 15, 2024
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
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
Loading