package test // Author: Weisen Pan // Date: 2023-10-24 import ( "encoding/json" simontype "github.com/hkust-adsl/kubernetes-scheduler-simulator/knets_pkg/type" "github.com/hkust-adsl/kubernetes-scheduler-simulator/knets_pkg/utils" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/knets_pkg/api/resource" metav1 "k8s.io/apimachinery/knets_pkg/apis/meta/v1" ) // FakeNodeOption represents a function that configures a fake Node. type FakeNodeOption func(*corev1.Node) // MakeFakeNode creates a fake Node with the provided name, CPU, memory, and options. func MakeFakeNode(name string, cpu, memory string, opts ...FakeNodeOption) *corev1.Node { res := corev1.ResourceList{} if cpu != "" { res[corev1.ResourceCPU] = resource.MustParse(cpu) } if memory != "" { res[corev1.ResourceMemory] = resource.MustParse(memory) } res["pods"] = *resource.NewQuantity(110, resource.DecimalSI) node := &corev1.Node{ ObjectMeta: metav1.ObjectMeta{ Name: name, }, Status: corev1.NodeStatus{ Capacity: res, Allocatable: res, }, } // Apply the specified options to the node. for _, opt := range opts { opt(node) } return node } // WithNodeAnnotations is a FakeNodeOption that sets annotations for the fake Node. func WithNodeAnnotations(annotations map[string]string) FakeNodeOption { return func(node *corev1.Node) { node.ObjectMeta.Annotations = annotations } } // WithNodeLabels is a FakeNodeOption that sets labels for the fake Node. func WithNodeLabels(labels map[string]string) FakeNodeOption { return func(node *corev1.Node) { node.ObjectMeta.Labels = labels } } // WithNodeTaints is a FakeNodeOption that sets taints for the fake Node. func WithNodeTaints(taints []corev1.Taint) FakeNodeOption { return func(node *corev1.Node) { node.Spec.Taints = taints } } // WithNodeLocalStorage is a FakeNodeOption that sets local storage information for the fake Node. func WithNodeLocalStorage(storage utils.NodeStorage) FakeNodeOption { return func(node *corev1.Node) { // Marshal the storage to JSON and set it as an annotation. b, _ := json.Marshal(storage) metav1.SetMetaDataAnnotation(&node.ObjectMeta, simontype.AnnoNodeLocalStorage, string(b)) } }