Skip to content

Commit 4bf9dca

Browse files
authored
feat: refactor pod patch configuration and patching logic (#10)
- Renamed WorkerTemplate to Worker in config - Updated patching logic to use PatchToPod and PatchToContainer - Refined container environment variable patching in pod webhook
1 parent b79b3da commit 4bf9dca

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func main() {
167167
Scheme: mgr.GetScheme(),
168168
Scheduler: scheduler,
169169
WorkerGenerator: &worker.WorkerGenerator{
170-
PodTemplate: &config.WorkerTemplate,
170+
PodTemplate: &config.Worker,
171171
},
172172
}).SetupWithManager(mgr); err != nil {
173173
setupLog.Error(err, "unable to create controller", "controller", "TensorFusionConnection")

internal/config/config.go

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,14 @@ import (
88
"k8s.io/utils/ptr"
99
)
1010

11-
type Pod struct {
12-
Spec PodSpec `json:"spec,omitempty"`
13-
}
14-
15-
type PodSpec struct {
16-
InitContainers []corev1.Container `json:"initContainers,omitempty" patchStrategy:"merge" patchMergeKey:"name" protobuf:"bytes,20,rep,name=initContainers"`
17-
Containers []corev1.Container `json:"containers,omitempty" patchStrategy:"merge" patchMergeKey:"name" protobuf:"bytes,2,rep,name=containers"`
18-
RuntimeClassName *string `json:"runtimeClassName,omitempty" protobuf:"bytes,29,opt,name=runtimeClassName"`
19-
}
20-
2111
type Config struct {
22-
WorkerTemplate corev1.PodTemplate `json:"workerTemplate"`
23-
PodMutator PodMutator `json:"podMutator"`
12+
Worker corev1.PodTemplate `json:"worker"`
13+
PodMutator PodMutator `json:"podMutator"`
2414
}
2515

2616
type PodMutator struct {
27-
PatchStrategicMerge Pod `json:"patchStrategicMerge"`
28-
PatchEnvVars []corev1.EnvVar `json:"envVars"`
17+
PatchToPod any `json:"patchToPod"`
18+
PatchToContainer any `json:"patchToContainer"`
2919
}
3020

3121
func LoadConfig(filename string) (*Config, error) {
@@ -43,7 +33,7 @@ func LoadConfig(filename string) (*Config, error) {
4333

4434
func NewDefaultConfig() *Config {
4535
return &Config{
46-
WorkerTemplate: corev1.PodTemplate{
36+
Worker: corev1.PodTemplate{
4737
Template: corev1.PodTemplateSpec{
4838
Spec: corev1.PodSpec{
4939
TerminationGracePeriodSeconds: ptr.To[int64](0),
@@ -58,20 +48,22 @@ func NewDefaultConfig() *Config {
5848
},
5949
},
6050
PodMutator: PodMutator{
61-
PatchStrategicMerge: Pod{
62-
Spec: PodSpec{
63-
InitContainers: []corev1.Container{
51+
PatchToPod: map[string]any{
52+
"spec": map[string]any{
53+
"initContainers": []corev1.Container{
6454
{
6555
Name: "inject-lib",
6656
Image: "busybox:stable-glibc",
6757
},
6858
},
6959
},
7060
},
71-
PatchEnvVars: []corev1.EnvVar{
72-
{
73-
Name: "LD_PRELOAD",
74-
Value: "tensorfusion.so",
61+
PatchToContainer: map[string]any{
62+
"env": []corev1.EnvVar{
63+
{
64+
Name: "LD_PRELOAD",
65+
Value: "tensorfusion.so",
66+
},
7567
},
7668
},
7769
},

internal/controller/tensorfusionconnection_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var _ = Describe("TensorFusionConnection Controller", func() {
7575
Client: k8sClient,
7676
Scheme: k8sClient.Scheme(),
7777
WorkerGenerator: &worker.WorkerGenerator{
78-
PodTemplate: &config.WorkerTemplate,
78+
PodTemplate: &config.Worker,
7979
},
8080
}
8181
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{

internal/webhook/v1/pod_webhook.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"net/http"
2424

25-
"github.com/NexusGPU/tensor-fusion-operator/internal/config"
2625
"gomodules.xyz/jsonpatch/v2"
2726
corev1 "k8s.io/api/core/v1"
2827
"k8s.io/apimachinery/pkg/api/resource"
@@ -33,6 +32,7 @@ import (
3332
"sigs.k8s.io/controller-runtime/pkg/log"
3433
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3534

35+
"github.com/NexusGPU/tensor-fusion-operator/internal/config"
3636
"github.com/NexusGPU/tensor-fusion-operator/internal/constants"
3737
)
3838

@@ -147,30 +147,43 @@ func (m *TensorFusionPodMutator) patchTFClient(pod *corev1.Pod, tfReq []TFReq) (
147147
return nil, fmt.Errorf("marshal current pod: %v", err)
148148
}
149149

150-
// Patch env vars
150+
// Patch to Container
151151
for _, req := range tfReq {
152152
for i := range pod.Spec.Containers {
153153
container := &pod.Spec.Containers[i]
154154
if container.Name == req.ContainerName {
155-
container.Env = append(container.Env, m.Config.PatchEnvVars...)
155+
containerJSON, err := json.Marshal(container)
156+
if err != nil {
157+
return nil, fmt.Errorf("marshal container: %v", err)
158+
}
159+
patchJSON, err := json.Marshal(m.Config.PatchToContainer)
160+
if err != nil {
161+
return nil, fmt.Errorf("marshal patchToContainer: %v", err)
162+
}
163+
164+
patchedJSON, err := strategicpatch.StrategicMergePatch(containerJSON, patchJSON, corev1.Container{})
165+
if err != nil {
166+
return nil, fmt.Errorf("apply strategic merge patch to container: %v", err)
167+
}
168+
169+
if err := json.Unmarshal(patchedJSON, container); err != nil {
170+
return nil, fmt.Errorf("unmarshal patched container: %v", err)
171+
}
156172
}
157173
}
158174
}
159-
envPatchedStatus, err := json.Marshal(pod)
175+
176+
containerPatchedJSON, err := json.Marshal(pod)
160177
if err != nil {
161178
return nil, fmt.Errorf("marshal current pod: %v", err)
162179
}
163-
patches, err := jsonpatch.CreatePatch(currentBytes, envPatchedStatus)
180+
patches, err := jsonpatch.CreatePatch(currentBytes, containerPatchedJSON)
164181
if err != nil {
165-
return nil, fmt.Errorf("patch env: %v", err)
182+
return nil, fmt.Errorf("patch to container: %v", err)
166183
}
167184

168-
podPatch := m.Config.PatchStrategicMerge
169-
// Copy containers
170-
podPatch.Spec.Containers = append([]corev1.Container{}, podPatch.Spec.Containers...)
171-
172185
// Convert the strategic merge patch to JSON
173-
patchBytes, err := json.Marshal(m.Config.PatchStrategicMerge)
186+
patchBytes, err := json.Marshal(m.Config.PatchToPod)
174187

175188
if err != nil {
176189
return nil, fmt.Errorf("marshal patch: %v", err)

0 commit comments

Comments
 (0)