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

分离 client 中的真实和 fake 调用 k8s api,以便于 ut 不依赖于当前的 k8s 环境。 #914

Closed
wants to merge 2 commits into from
Closed
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
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
26 changes: 23 additions & 3 deletions pkg/util/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,41 @@ import (
)

var (
// KubeClient is the global Kubernetes client instance.
// Note: It is recommended to use the GetClient() method instead of directly accessing this variable.
// KubeClient will be deprecated in the future.
KubeClient kubernetes.Interface
once sync.Once
)

func init() {
KubeClient = nil
}

// GetClient returns the singleton Kubernetes client instance.
// It initializes the client on first call if not already set.
func GetClient() kubernetes.Interface {
once.Do(func() {
var err error
KubeClient, err = newClient()
if err != nil {
klog.Fatalf("Failed to create Kubernetes client: %v", err)
if KubeClient == nil {
KubeClient, err = newClient()
if err != nil {
klog.Fatalf("Failed to create Kubernetes client: %v", err)
}
}
})
return KubeClient
}

// SetClient sets the Kubernetes client instance.
// This is primarily used for testing purposes to inject a fake client.
// Example: client.SetClient(fake.NewSimpleClientset())
func SetClient(client kubernetes.Interface) {
KubeClient = client
// Reset once so that GetClient will use the new client
once = sync.Once{}
}

// newClient initializes a new Kubernetes client.
func newClient() (kubernetes.Interface, error) {
kubeConfigPath := os.Getenv("KUBECONFIG")
Expand Down
7 changes: 3 additions & 4 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func GetNode(nodename string) (*corev1.Node, error) {
}

klog.InfoS("Fetching node", "nodeName", nodename)

n, err := client.GetClient().CoreV1().Nodes().Get(context.Background(), nodename, metav1.GetOptions{})
if err != nil {
switch {
Expand Down Expand Up @@ -342,8 +343,7 @@ func PatchNodeAnnotations(node *corev1.Node, annotations map[string]string) erro
if err != nil {
return err
}
_, err = client.GetClient().CoreV1().Nodes().
Patch(context.Background(), node.Name, k8stypes.StrategicMergePatchType, bytes, metav1.PatchOptions{})
_, err = client.GetClient().CoreV1().Nodes().Patch(context.Background(), node.Name, k8stypes.StrategicMergePatchType, bytes, metav1.PatchOptions{})
if err != nil {
klog.Infoln("annotations=", annotations)
klog.Infof("patch pod %v failed, %v", node.Name, err)
Expand Down Expand Up @@ -374,8 +374,7 @@ func PatchPodAnnotations(pod *corev1.Pod, annotations map[string]string) error {
return err
}
klog.V(5).Infof("patch pod %s/%s annotation content is %s", pod.Namespace, pod.Name, string(bytes))
_, err = client.GetClient().CoreV1().Pods(pod.Namespace).
Patch(context.Background(), pod.Name, k8stypes.StrategicMergePatchType, bytes, metav1.PatchOptions{})
_, err = client.GetClient().CoreV1().Pods(pod.Namespace).Patch(context.Background(), pod.Name, k8stypes.StrategicMergePatchType, bytes, metav1.PatchOptions{})
if err != nil {
klog.Infof("patch pod %v failed, %v", pod.Name, err)
}
Expand Down
35 changes: 21 additions & 14 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import (
"fmt"
"testing"

"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/client-go/kubernetes/fake"

"github.com/Project-HAMi/HAMi/pkg/util/client"
fake "k8s.io/client-go/kubernetes/fake"
)

var inRequestDevices map[string]string
Expand All @@ -38,7 +37,7 @@ func init() {
inRequestDevices["NVIDIA"] = "hami.io/vgpu-devices-to-allocate"
}

func TestExtractMigTemplatesFromUUID(t *testing.T) {
func Test_ExtractMigTemplatesFromUUID(t *testing.T) {
testCases := []struct {
name string
uuid string
Expand Down Expand Up @@ -110,23 +109,23 @@ func TestExtractMigTemplatesFromUUID(t *testing.T) {
}
}

func TestEmptyContainerDevicesCoding(t *testing.T) {
func Test_EmptyContainerDevicesCoding(t *testing.T) {
cd1 := ContainerDevices{}
s := EncodeContainerDevices(cd1)
fmt.Println(s)
cd2, _ := DecodeContainerDevices(s)
assert.DeepEqual(t, cd1, cd2)
}

func TestEmptyPodDeviceCoding(t *testing.T) {
func Test_EmptyPodDeviceCoding(t *testing.T) {
pd1 := PodDevices{}
s := EncodePodDevices(inRequestDevices, pd1)
fmt.Println(s)
pd2, _ := DecodePodDevices(inRequestDevices, s)
assert.DeepEqual(t, pd1, pd2)
}

func TestPodDevicesCoding(t *testing.T) {
func Test_PodDevicesCoding(t *testing.T) {
tests := []struct {
name string
args PodDevices
Expand Down Expand Up @@ -252,7 +251,7 @@ func Test_DecodePodDevices(t *testing.T) {
}
}

func TestMarshalNodeDevices(t *testing.T) {
func Test_MarshalNodeDevices(t *testing.T) {
type args struct {
dlist []*DeviceInfo
}
Expand Down Expand Up @@ -332,7 +331,7 @@ func TestMarshalNodeDevices(t *testing.T) {
}
}

func TestUnMarshalNodeDevices(t *testing.T) {
func Test_UnMarshalNodeDevices(t *testing.T) {
type args struct {
str string
}
Expand Down Expand Up @@ -625,11 +624,19 @@ func Test_CheckHealth(t *testing.T) {
}
}

func TestMarkAnnotationsToDelete(t *testing.T) {
client.KubeClient = fake.NewSimpleClientset()
client.KubeClient.CoreV1().Nodes().Create(context.TODO(), &corev1.Node{
ObjectMeta: metav1.ObjectMeta{Name: "node-worker2"},
}, metav1.CreateOptions{})
func Test_MarkAnnotationsToDelete(t *testing.T) {
fakeclient := fake.NewSimpleClientset()
client.SetClient(fakeclient)
client.GetClient().CoreV1().Nodes().Create(
context.TODO(),
&corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-worker2",
},
},
metav1.CreateOptions{},
)

type args struct {
devType string
nn string
Expand Down