93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
package test
|
|
|
|
// Author: Weisen Pan
|
|
// Date: 2023-10-24
|
|
|
|
|
|
import (
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/knets_pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/knets_pkg/apis/meta/v1"
|
|
)
|
|
|
|
// FakePodOption is a functional option type for configuring a fake Pod.
|
|
type FakePodOption func(*corev1.Pod)
|
|
|
|
// MakeFakePod creates a fake Pod with the given name, namespace, CPU, memory, and optional configuration options.
|
|
func MakeFakePod(name, namespace string, cpu, memory string, opts ...FakePodOption) *corev1.Pod {
|
|
res := corev1.ResourceList{}
|
|
if cpu != "" {
|
|
res[corev1.ResourceCPU] = resource.MustParse(cpu)
|
|
}
|
|
if memory != "" {
|
|
res[corev1.ResourceMemory] = resource.MustParse(memory)
|
|
}
|
|
|
|
pod := &corev1.Pod{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: name,
|
|
Namespace: namespace,
|
|
},
|
|
Spec: corev1.PodSpec{
|
|
Containers: []corev1.Container{
|
|
{
|
|
Name: "container",
|
|
Image: "nginx",
|
|
Resources: corev1.ResourceRequirements{
|
|
Requests: res,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
// Apply optional configuration options
|
|
for _, opt := range opts {
|
|
opt(pod)
|
|
}
|
|
|
|
return pod
|
|
}
|
|
|
|
// WithPodAnnotations is an option to set annotations for the fake Pod.
|
|
func WithPodAnnotations(annotations map[string]string) FakePodOption {
|
|
return func(pod *corev1.Pod) {
|
|
pod.ObjectMeta.Annotations = annotations
|
|
}
|
|
}
|
|
|
|
// WithPodLabels is an option to set labels for the fake Pod.
|
|
func WithPodLabels(labels map[string]string) FakePodOption {
|
|
return func(pod *corev1.Pod) {
|
|
pod.ObjectMeta.Labels = labels
|
|
}
|
|
}
|
|
|
|
// WithPodNodeName is an option to set the node name for the fake Pod.
|
|
func WithPodNodeName(nodeName string) FakePodOption {
|
|
return func(pod *corev1.Pod) {
|
|
pod.Spec.NodeName = nodeName
|
|
}
|
|
}
|
|
|
|
// WithPodTolerations is an option to set tolerations for the fake Pod.
|
|
func WithPodTolerations(tolerations []corev1.Toleration) FakePodOption {
|
|
return func(pod *corev1.Pod) {
|
|
pod.Spec.Tolerations = tolerations
|
|
}
|
|
}
|
|
|
|
// WithPodAffinity is an option to set affinity rules for the fake Pod.
|
|
func WithPodAffinity(affinity *corev1.Affinity) FakePodOption {
|
|
return func(pod *corev1.Pod) {
|
|
pod.Spec.Affinity = affinity
|
|
}
|
|
}
|
|
|
|
// WithPodNodeSelector is an option to set node selector rules for the fake Pod.
|
|
func WithPodNodeSelector(nodeSelector map[string]string) FakePodOption {
|
|
return func(pod *corev1.Pod) {
|
|
pod.Spec.NodeSelector = nodeSelector
|
|
}
|
|
}
|