Skip to content
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
14 changes: 8 additions & 6 deletions cmd/device-plugin/nvidia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ import (
"syscall"
"time"

"github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/info"
"github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/plugin"
"github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/rm"
"github.com/Project-HAMi/HAMi/pkg/util"
flagutil "github.com/Project-HAMi/HAMi/pkg/util/flag"

spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1"
"github.com/fsnotify/fsnotify"
cli "github.com/urfave/cli/v2"
errorsutil "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
kubeletdevicepluginv1beta1 "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"

"github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/info"
"github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/plugin"
"github.com/Project-HAMi/HAMi/pkg/device-plugin/nvidiadevice/nvinternal/rm"
"github.com/Project-HAMi/HAMi/pkg/util"
"github.com/Project-HAMi/HAMi/pkg/util/client"
flagutil "github.com/Project-HAMi/HAMi/pkg/util/flag"
)

func main() {
Expand Down Expand Up @@ -183,6 +184,7 @@ func loadConfig(c *cli.Context, flags []cli.Flag) (*spec.Config, error) {
func start(c *cli.Context, flags []cli.Flag) error {
klog.Info("Starting FS watcher.")
util.NodeName = os.Getenv(util.NodeNameEnvName)
client.InitGlobalClient()
watcher, err := newFSWatcher(kubeletdevicepluginv1beta1.DevicePluginPath)
if err != nil {
return fmt.Errorf("failed to create FS watcher: %v", err)
Expand Down
14 changes: 10 additions & 4 deletions cmd/scheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ package main
import (
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/spf13/cobra"
klog "k8s.io/klog/v2"

"github.com/Project-HAMi/HAMi/pkg/device"
"github.com/Project-HAMi/HAMi/pkg/scheduler"
"github.com/Project-HAMi/HAMi/pkg/scheduler/config"
"github.com/Project-HAMi/HAMi/pkg/scheduler/routes"
"github.com/Project-HAMi/HAMi/pkg/util"
"github.com/Project-HAMi/HAMi/pkg/util/client"
"github.com/Project-HAMi/HAMi/pkg/util/flag"
"github.com/Project-HAMi/HAMi/pkg/version"

"github.com/julienschmidt/httprouter"
"github.com/spf13/cobra"
klog "k8s.io/klog/v2"
)

//var version string
Expand Down Expand Up @@ -63,12 +64,17 @@ func init() {
rootCmd.Flags().StringVar(&config.GPUSchedulerPolicy, "gpu-scheduler-policy", util.GPUSchedulerPolicySpread.String(), "GPU scheduler policy")
rootCmd.Flags().StringVar(&config.MetricsBindAddress, "metrics-bind-address", ":9395", "The TCP address that the scheduler should bind to for serving prometheus metrics(e.g. 127.0.0.1:9395, :9395)")
rootCmd.Flags().StringToStringVar(&config.NodeLabelSelector, "node-label-selector", nil, "key=value pairs separated by commas")
// add QPS and Burst to the global flagset
// qps and burst settings for the client-go client
rootCmd.Flags().Float32Var(&config.QPS, "kube-qps", 5.0, "QPS to use while talking with kube-apiserver.")
rootCmd.Flags().IntVar(&config.Burst, "kube-burst", 10, "Burst to use while talking with kube-apiserver.")
rootCmd.PersistentFlags().AddGoFlagSet(device.GlobalFlagSet())
rootCmd.AddCommand(version.VersionCmd)
rootCmd.Flags().AddGoFlagSet(util.InitKlogFlags())
}

func start() {
client.InitGlobalClient(client.WithBurst(config.Burst), client.WithQPS(config.QPS))
device.InitDevices()
sher = scheduler.NewScheduler()
sher.Start()
Expand Down
7 changes: 4 additions & 3 deletions pkg/device/cambricon/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"testing"
"time"

"github.com/Project-HAMi/HAMi/pkg/util"
"github.com/Project-HAMi/HAMi/pkg/util/client"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

"github.com/Project-HAMi/HAMi/pkg/util"
"github.com/Project-HAMi/HAMi/pkg/util/client"
)

func Test_GetNodeDevices(t *testing.T) {
Expand Down Expand Up @@ -451,6 +451,7 @@ func Test_PatchAnnotations(t *testing.T) {
}

func Test_setNodeLock(t *testing.T) {
client.InitGlobalClient(client.WithBurst(10), client.WithQPS(5.0))
tests := []struct {
name string
node corev1.Node
Expand Down
10 changes: 5 additions & 5 deletions pkg/device/hygon/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ import (
"flag"
"testing"

"github.com/Project-HAMi/HAMi/pkg/util"
"github.com/Project-HAMi/HAMi/pkg/util/client"

"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"

"github.com/Project-HAMi/HAMi/pkg/util"
"github.com/Project-HAMi/HAMi/pkg/util/client"
)

func Test_MutateAdmission(t *testing.T) {
Expand Down Expand Up @@ -673,6 +672,7 @@ func Test_GenerateResourceRequests(t *testing.T) {
}

func Test_NodeCleanUp(t *testing.T) {
client.InitGlobalClient()
tests := []struct {
name string
args string
Expand Down
6 changes: 3 additions & 3 deletions pkg/device/nvidia/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"strconv"
"strings"

"github.com/Project-HAMi/HAMi/pkg/util"
"github.com/Project-HAMi/HAMi/pkg/util/nodelock"

spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/klog/v2"

"github.com/Project-HAMi/HAMi/pkg/util"
"github.com/Project-HAMi/HAMi/pkg/util/nodelock"
)

const (
Expand Down
2 changes: 2 additions & 0 deletions pkg/scheduler/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package config
import "github.com/Project-HAMi/HAMi/pkg/util"

var (
QPS float32
Burst int
HTTPBind string
SchedulerName string
MetricsBindAddress string
Expand Down
62 changes: 47 additions & 15 deletions pkg/util/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,50 @@ func init() {
}

func GetClient() kubernetes.Interface {
once.Do(func() {
var err error
if KubeClient == nil {
KubeClient, err = newClient()
if err != nil {
klog.Fatalf("Failed to create Kubernetes client: %v", err)
}
}
})
return KubeClient
}

// newClient initializes a new Kubernetes client.
func newClient() (kubernetes.Interface, error) {
// Client is a kubernetes client.
type Client struct {
Client kubernetes.Interface
QPS float32
Burst int
}

// WithQPS sets the QPS of the client.
func WithQPS(qps float32) func(*Client) {
return func(c *Client) {
c.QPS = qps
}
}

func WithBurst(burst int) func(*Client) {
return func(c *Client) {
c.Burst = burst
}
}

// NewClientWithConfig creates a new client with a given config.
func NewClientWithConfig(config *rest.Config, opts ...func(*Client)) (*Client, error) {
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
c := &Client{
Client: client,
}
for _, opt := range opts {
opt(c)
}
return c, nil
}

// NewClient creates a new client.
func NewClient(ops ...func(*Client)) (*Client, error) {
kubeConfigPath := os.Getenv("KUBECONFIG")
if kubeConfigPath == "" {
kubeConfigPath = filepath.Join(os.Getenv("HOME"), ".kube", "config")
}

config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
klog.Infof("BuildConfigFromFlags failed for file %s: %v. Using in-cluster config.", kubeConfigPath, err)
Expand All @@ -65,11 +90,18 @@ func newClient() (kubernetes.Interface, error) {
return nil, fmt.Errorf("failed to get in-cluster config: %w", err)
}
}

clientset, err := kubernetes.NewForConfig(config)
c, err := NewClientWithConfig(config, ops...)
if err != nil {
return nil, fmt.Errorf("failed to create kubernetes client: %w", err)
}
return c, err
}

return clientset, nil
// InitGlobalClient creates a new global client.
func InitGlobalClient(ops ...func(*Client)) {
c, err := NewClient(ops...)
if err != nil {
klog.Fatalf("new client error %s", err.Error())
}
KubeClient = c.Client
}
1 change: 1 addition & 0 deletions pkg/util/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (m *MockClientConfig) ConfigAccess() clientcmd.ConfigAccess {

// TestGetClient tests the GetClient function.
func TestGetClient(t *testing.T) {
InitGlobalClient()
tests := []struct {
name string
kubeConfig string
Expand Down