Skip to content

Commit

Permalink
兼容当前新老调用 KubeClient 的用法,特别是 fake 替换时
Browse files Browse the repository at this point in the history
Signed-off-by: yangshiqi <[email protected]>
  • Loading branch information
yangshiqi committed Mar 4, 2025
1 parent 2504f39 commit 272766d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/device/hygon/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -673,6 +674,7 @@ func Test_GenerateResourceRequests(t *testing.T) {
}

func Test_NodeCleanUp(t *testing.T) {
client.KubeClient = fake.NewSimpleClientset()
tests := []struct {
name string
args string
Expand Down
25 changes: 22 additions & 3 deletions pkg/util/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,44 @@ func NewInstance() KubeInterface {
func GetFactory() *KubeClientFactory {
factoryOnce.Do(func() {
instance = &KubeClientFactory{}
instance.SetReal() // Use the real client by default
instance.SetReal() // Use the real client by default.
})
return instance

Check warning on line 46 in pkg/util/client/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/client/factory.go#L41-L46

Added lines #L41 - L46 were not covered by tests
}

func (f *KubeClientFactory) GetClient() KubeInterface {
if KubeClient == nil {
f.client = &K8sClient{
client: GetK8sClient().client,
}
} else {
f.client = &K8sClient{
client: KubeClient,
}
}
return f.client

Check warning on line 59 in pkg/util/client/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/client/factory.go#L49-L59

Added lines #L49 - L59 were not covered by tests
}

func (f *KubeClientFactory) SetFake() *KubeClientFactory {
f.client = &K8sClient{
client: fake.NewSimpleClientset(),
}
// For compatibility with other direct assignment call points, this line needs to be removed after replacement.
KubeClient = fake.NewSimpleClientset()
return f

Check warning on line 68 in pkg/util/client/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/client/factory.go#L62-L68

Added lines #L62 - L68 were not covered by tests
}

func (f *KubeClientFactory) SetReal() *KubeClientFactory {
f.client = &K8sClient{
client: GetK8sClient().client,
// For compatibility with other direct assignment call points, this line needs to be removed after replacement.
if KubeClient == nil {
f.client = &K8sClient{
client: GetK8sClient().client,
}
KubeClient = GetK8sClient().client
} else {
f.client = &K8sClient{
client: KubeClient,
}
}
return f

Check warning on line 83 in pkg/util/client/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/client/factory.go#L71-L83

Added lines #L71 - L83 were not covered by tests
}

0 comments on commit 272766d

Please sign in to comment.