diff --git a/pkg/k8sutil/client.go b/pkg/k8sutil/client.go deleted file mode 100644 index 65ca108b4..000000000 --- a/pkg/k8sutil/client.go +++ /dev/null @@ -1,43 +0,0 @@ -/* -Copyright 2024 The HAMi 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 k8sutil - -import ( - "os" - "path/filepath" - - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" -) - -// NewClient connects to an API server. -func NewClient() (kubernetes.Interface, error) { - kubeConfig := os.Getenv("KUBECONFIG") - if kubeConfig == "" { - kubeConfig = filepath.Join(os.Getenv("HOME"), ".kube", "config") - } - config, err := rest.InClusterConfig() - if err != nil { - config, err = clientcmd.BuildConfigFromFlags("", kubeConfig) - if err != nil { - return nil, err - } - } - client, err := kubernetes.NewForConfig(config) - return client, err -} diff --git a/pkg/scheduler/scheduler.go b/pkg/scheduler/scheduler.go index 14c2f6806..536584bfc 100644 --- a/pkg/scheduler/scheduler.go +++ b/pkg/scheduler/scheduler.go @@ -29,6 +29,7 @@ import ( "github.com/Project-HAMi/HAMi/pkg/scheduler/config" "github.com/Project-HAMi/HAMi/pkg/scheduler/policy" "github.com/Project-HAMi/HAMi/pkg/util" + "github.com/Project-HAMi/HAMi/pkg/util/client" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -125,7 +126,7 @@ func (s *Scheduler) onDelPod(obj interface{}) { } func (s *Scheduler) Start() { - kubeClient, err := k8sutil.NewClient() + kubeClient, err := client.NewClient() check(err) s.kubeClient = kubeClient informerFactory := informers.NewSharedInformerFactoryWithOptions(s.kubeClient, time.Hour*1) diff --git a/pkg/util/client/client.go b/pkg/util/client/client.go index 768cc5d64..4fae75963 100644 --- a/pkg/util/client/client.go +++ b/pkg/util/client/client.go @@ -30,15 +30,14 @@ var ( KubeClient kubernetes.Interface ) -func init() { +func GetClient() kubernetes.Interface { var err error - KubeClient, err = NewClient() - if err != nil { - panic(err) + if KubeClient == nil { + KubeClient, err = NewClient() + if err != nil { + panic(err) + } } -} - -func GetClient() kubernetes.Interface { return KubeClient }