Skip to content

Commit 9eae574

Browse files
authored
fix(webhook): set pod namespace to 'default' if empty and add related tests (#155)
1 parent 13d5d42 commit 9eae574

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

internal/webhook/v1/pod_webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func (m *TensorFusionPodMutator) Handle(ctx context.Context, req admission.Reque
6767
return admission.Errored(http.StatusBadRequest, err)
6868
}
6969

70+
// Default namespace to 'default' if empty
71+
if len(pod.Namespace) == 0 {
72+
pod.Namespace = "default"
73+
}
7074
log := log.FromContext(ctx)
7175
log.Info("Mutating pod", "generateName", pod.GenerateName, "namespace", pod.Namespace)
7276

internal/webhook/v1/pod_webhook_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ var _ = Describe("TensorFusionPodMutator", func() {
6060
})
6161

6262
Context("Handle", func() {
63+
It("should handle pod with empty namespace and set workload.Namespace to 'default' (via Handle)", func() {
64+
// Create a pod with empty namespace
65+
pod := &corev1.Pod{
66+
ObjectMeta: metav1.ObjectMeta{
67+
Name: "test-pod-empty-ns",
68+
// empty namespace
69+
Namespace: "",
70+
Labels: map[string]string{
71+
constants.TensorFusionEnabledLabelKey: "true",
72+
},
73+
Annotations: map[string]string{
74+
constants.GpuPoolKey: "mock",
75+
constants.InjectContainerAnnotation: "main",
76+
constants.WorkloadKey: "test-workload-empty-ns",
77+
constants.GenWorkloadAnnotation: "true",
78+
},
79+
},
80+
Spec: corev1.PodSpec{
81+
Containers: []corev1.Container{{
82+
Name: "main",
83+
Image: "test-image",
84+
}},
85+
},
86+
}
87+
podBytes, err := json.Marshal(pod)
88+
Expect(err).NotTo(HaveOccurred())
89+
90+
// Construct an admission request with the pod
91+
req := admission.Request{
92+
AdmissionRequest: admissionv1.AdmissionRequest{
93+
Object: runtime.RawExtension{
94+
Raw: podBytes,
95+
},
96+
Operation: admissionv1.Create,
97+
},
98+
}
99+
100+
// Call mutator.Handle to process the admission request
101+
resp := mutator.Handle(ctx, req)
102+
Expect(resp.Allowed).To(BeTrue())
103+
})
104+
63105
It("should successfully mutate a pod with TF resources", func() {
64106
// Set up a workload profile for testing
65107
workloadProfile := &tfv1.WorkloadProfile{

internal/webhook/v1/tf_parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type TensorFusionInfo struct {
3232
GenWorkload bool
3333
}
3434

35-
func ParseTensorFusionInfo(ctx context.Context, k8sclient client.Client, pod *corev1.Pod) (TensorFusionInfo, error) {
35+
func ParseTensorFusionInfo(ctx context.Context, k8sClient client.Client, pod *corev1.Pod) (TensorFusionInfo, error) {
3636
var info TensorFusionInfo
3737
if pod.Annotations == nil {
3838
return info, fmt.Errorf("no annotations found")
@@ -72,7 +72,7 @@ func ParseTensorFusionInfo(ctx context.Context, k8sclient client.Client, pod *co
7272
workloadProfileName, ok := pod.Annotations[constants.WorkloadProfileAnnotation]
7373
workloadProfile := &tfv1.WorkloadProfile{}
7474
if ok {
75-
if err := k8sclient.Get(ctx, client.ObjectKey{Name: workloadProfileName, Namespace: pod.Namespace}, workloadProfile); err != nil {
75+
if err := k8sClient.Get(ctx, client.ObjectKey{Name: workloadProfileName, Namespace: pod.Namespace}, workloadProfile); err != nil {
7676
return info, fmt.Errorf("get workload profile(%s) : %w", workloadProfileName, err)
7777
}
7878
}

0 commit comments

Comments
 (0)