|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + corev1 "k8s.io/api/core/v1" |
| 7 | + resourceapi "k8s.io/api/resource/v1" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/utils/ptr" |
| 10 | +) |
| 11 | + |
| 12 | +// DriverConfig holds driver-specific parameters for building DRA test resources |
| 13 | +type DriverConfig struct { |
| 14 | + DriverName string |
| 15 | + DefaultClass string |
| 16 | + RequestName string |
| 17 | + ContainerImage string |
| 18 | + ContainerCommand []string |
| 19 | + LongRunCommand []string |
| 20 | +} |
| 21 | + |
| 22 | +// ResourceBuilder helps build DRA resource objects |
| 23 | +type ResourceBuilder struct { |
| 24 | + namespace string |
| 25 | + config DriverConfig |
| 26 | +} |
| 27 | + |
| 28 | +// NewResourceBuilder creates a new builder |
| 29 | +func NewResourceBuilder(namespace string, config DriverConfig) *ResourceBuilder { |
| 30 | + return &ResourceBuilder{namespace: namespace, config: config} |
| 31 | +} |
| 32 | + |
| 33 | +// BuildDeviceClass creates a DeviceClass for the configured driver |
| 34 | +func (rb *ResourceBuilder) BuildDeviceClass(name string) *resourceapi.DeviceClass { |
| 35 | + if name == "" { |
| 36 | + name = rb.config.DefaultClass |
| 37 | + } |
| 38 | + |
| 39 | + return &resourceapi.DeviceClass{ |
| 40 | + ObjectMeta: metav1.ObjectMeta{ |
| 41 | + Name: name, |
| 42 | + }, |
| 43 | + Spec: resourceapi.DeviceClassSpec{ |
| 44 | + Selectors: []resourceapi.DeviceSelector{ |
| 45 | + { |
| 46 | + CEL: &resourceapi.CELDeviceSelector{ |
| 47 | + Expression: fmt.Sprintf("device.driver == %q", rb.config.DriverName), |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// BuildResourceClaim creates a ResourceClaim requesting devices |
| 56 | +func (rb *ResourceBuilder) BuildResourceClaim(name, deviceClassName string, count int) *resourceapi.ResourceClaim { |
| 57 | + if count <= 0 { |
| 58 | + panic(fmt.Sprintf("BuildResourceClaim: count must be > 0, got %d", count)) |
| 59 | + } |
| 60 | + if deviceClassName == "" { |
| 61 | + deviceClassName = rb.config.DefaultClass |
| 62 | + } |
| 63 | + |
| 64 | + return &resourceapi.ResourceClaim{ |
| 65 | + ObjectMeta: metav1.ObjectMeta{ |
| 66 | + Name: name, |
| 67 | + Namespace: rb.namespace, |
| 68 | + }, |
| 69 | + Spec: resourceapi.ResourceClaimSpec{ |
| 70 | + Devices: resourceapi.DeviceClaim{ |
| 71 | + Requests: []resourceapi.DeviceRequest{ |
| 72 | + { |
| 73 | + Name: rb.config.RequestName, |
| 74 | + Exactly: &resourceapi.ExactDeviceRequest{ |
| 75 | + DeviceClassName: deviceClassName, |
| 76 | + Count: int64(count), |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// BuildPodWithClaim creates a Pod that uses a ResourceClaim |
| 86 | +func (rb *ResourceBuilder) BuildPodWithClaim(name, claimName, img string) *corev1.Pod { |
| 87 | + if img == "" { |
| 88 | + img = rb.config.ContainerImage |
| 89 | + } |
| 90 | + |
| 91 | + return &corev1.Pod{ |
| 92 | + ObjectMeta: metav1.ObjectMeta{ |
| 93 | + Name: name, |
| 94 | + Namespace: rb.namespace, |
| 95 | + }, |
| 96 | + Spec: corev1.PodSpec{ |
| 97 | + RestartPolicy: corev1.RestartPolicyNever, |
| 98 | + Containers: []corev1.Container{ |
| 99 | + { |
| 100 | + Name: "test-container", |
| 101 | + Image: img, |
| 102 | + Command: rb.config.ContainerCommand, |
| 103 | + Resources: corev1.ResourceRequirements{ |
| 104 | + Claims: []corev1.ResourceClaim{ |
| 105 | + { |
| 106 | + Name: rb.config.RequestName, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + SecurityContext: &corev1.SecurityContext{ |
| 111 | + AllowPrivilegeEscalation: ptr.To(false), |
| 112 | + Capabilities: &corev1.Capabilities{ |
| 113 | + Drop: []corev1.Capability{"ALL"}, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + ResourceClaims: []corev1.PodResourceClaim{ |
| 119 | + { |
| 120 | + Name: rb.config.RequestName, |
| 121 | + ResourceClaimName: &claimName, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// BuildLongRunningPodWithClaim creates a long-running Pod for testing |
| 129 | +func (rb *ResourceBuilder) BuildLongRunningPodWithClaim(name, claimName, img string) *corev1.Pod { |
| 130 | + pod := rb.BuildPodWithClaim(name, claimName, img) |
| 131 | + pod.Spec.Containers[0].Command = rb.config.LongRunCommand |
| 132 | + return pod |
| 133 | +} |
| 134 | + |
| 135 | +// BuildPodWithInlineClaim creates a Pod with inline ResourceClaim |
| 136 | +func (rb *ResourceBuilder) BuildPodWithInlineClaim(name string) *corev1.Pod { |
| 137 | + templateName := name + "-template" |
| 138 | + pod := rb.BuildPodWithClaim(name, "", "") |
| 139 | + pod.Spec.ResourceClaims[0].ResourceClaimName = nil |
| 140 | + pod.Spec.ResourceClaims[0].ResourceClaimTemplateName = &templateName |
| 141 | + return pod |
| 142 | +} |
| 143 | + |
| 144 | +// BuildResourceClaimTemplate creates a ResourceClaimTemplate |
| 145 | +func (rb *ResourceBuilder) BuildResourceClaimTemplate(name, deviceClassName string, count int) *resourceapi.ResourceClaimTemplate { |
| 146 | + if count <= 0 { |
| 147 | + panic(fmt.Sprintf("BuildResourceClaimTemplate: count must be > 0, got %d", count)) |
| 148 | + } |
| 149 | + if deviceClassName == "" { |
| 150 | + deviceClassName = rb.config.DefaultClass |
| 151 | + } |
| 152 | + |
| 153 | + return &resourceapi.ResourceClaimTemplate{ |
| 154 | + ObjectMeta: metav1.ObjectMeta{ |
| 155 | + Name: name, |
| 156 | + Namespace: rb.namespace, |
| 157 | + }, |
| 158 | + Spec: resourceapi.ResourceClaimTemplateSpec{ |
| 159 | + Spec: resourceapi.ResourceClaimSpec{ |
| 160 | + Devices: resourceapi.DeviceClaim{ |
| 161 | + Requests: []resourceapi.DeviceRequest{ |
| 162 | + { |
| 163 | + Name: rb.config.RequestName, |
| 164 | + Exactly: &resourceapi.ExactDeviceRequest{ |
| 165 | + DeviceClassName: deviceClassName, |
| 166 | + Count: int64(count), |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + } |
| 174 | +} |
0 commit comments