79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package test
|
|
|
|
// Author: Weisen Pan
|
|
// Date: 2023-10-24
|
|
|
|
|
|
import (
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/knets_pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/knets_pkg/apis/meta/v1"
|
|
)
|
|
|
|
// FakeDaemonSetOption is a functional option type for configuring a FakeDaemonSet.
|
|
type FakeDaemonSetOption func(*appsv1.DaemonSet)
|
|
|
|
// MakeFakeDaemonSet creates a fake DaemonSet for testing purposes with customizable options.
|
|
func MakeFakeDaemonSet(name, namespace string, cpu, memory string, opts ...FakeDaemonSetOption) *appsv1.DaemonSet {
|
|
// Initialize a resource list to hold CPU and memory requirements.
|
|
res := corev1.ResourceList{}
|
|
if cpu != "" {
|
|
res[corev1.ResourceCPU] = resource.MustParse(cpu)
|
|
}
|
|
if memory != "" {
|
|
res[corev1.ResourceMemory] = resource.MustParse(memory)
|
|
}
|
|
|
|
// Create the DaemonSet object with default values.
|
|
ds := &appsv1.DaemonSet{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: name,
|
|
Namespace: namespace,
|
|
},
|
|
Spec: appsv1.DaemonSetSpec{
|
|
Template: corev1.PodTemplateSpec{
|
|
Spec: corev1.PodSpec{
|
|
Containers: []corev1.Container{
|
|
{
|
|
Name: "container",
|
|
Image: "nginx",
|
|
Resources: corev1.ResourceRequirements{
|
|
Requests: res,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
// Apply custom options to the DaemonSet.
|
|
for _, opt := range opts {
|
|
opt(ds)
|
|
}
|
|
|
|
return ds
|
|
}
|
|
|
|
// WithDaemonSetTolerations sets tolerations for the DaemonSet.
|
|
func WithDaemonSetTolerations(tolerations []corev1.Toleration) FakeDaemonSetOption {
|
|
return func(ds *appsv1.DaemonSet) {
|
|
ds.Spec.Template.Spec.Tolerations = tolerations
|
|
}
|
|
}
|
|
|
|
// WithDaemonSetAffinity sets affinity rules for the DaemonSet.
|
|
func WithDaemonSetAffinity(affinity *corev1.Affinity) FakeDaemonSetOption {
|
|
return func(ds *appsv1.DaemonSet) {
|
|
ds.Spec.Template.Spec.Affinity = affinity
|
|
}
|
|
}
|
|
|
|
// WithDaemonSetNodeSelector sets node selector rules for the DaemonSet.
|
|
func WithDaemonSetNodeSelector(nodeSelector map[string]string) FakeDaemonSetOption {
|
|
return func(ds *appsv1.DaemonSet) {
|
|
ds.Spec.Template.Spec.NodeSelector = nodeSelector
|
|
}
|
|
}
|