Skip to content

Commit 0e4b60f

Browse files
authored
feat: injecting the GetConnectionURLEnv environment variable (#13)
1 parent de4fbe9 commit 0e4b60f

File tree

7 files changed

+81
-10
lines changed

7 files changed

+81
-10
lines changed

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ go 1.22.0
55
require (
66
github.com/gin-contrib/gzip v1.0.1
77
github.com/gin-gonic/gin v1.10.0
8+
github.com/lithammer/shortuuid/v4 v4.2.0
89
github.com/onsi/ginkgo/v2 v2.19.0
910
github.com/onsi/gomega v1.33.1
11+
github.com/samber/lo v1.47.0
1012
gomodules.xyz/jsonpatch/v2 v2.4.0
11-
gopkg.in/yaml.v2 v2.4.0
1213
k8s.io/api v0.31.0
1314
k8s.io/apimachinery v0.31.0
1415
k8s.io/client-go v0.31.0
1516
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
1617
sigs.k8s.io/controller-runtime v0.19.1
18+
sigs.k8s.io/yaml v1.4.0
1719
)
1820

1921
require (
@@ -106,6 +108,7 @@ require (
106108
google.golang.org/protobuf v1.34.2 // indirect
107109
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
108110
gopkg.in/inf.v0 v0.9.1 // indirect
111+
gopkg.in/yaml.v2 v2.4.0 // indirect
109112
gopkg.in/yaml.v3 v3.0.1 // indirect
110113
k8s.io/apiextensions-apiserver v0.31.0 // indirect
111114
k8s.io/apiserver v0.31.0 // indirect
@@ -115,5 +118,4 @@ require (
115118
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3 // indirect
116119
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
117120
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
118-
sigs.k8s.io/yaml v1.4.0 // indirect
119121
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
115115
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
116116
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
117117
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
118+
github.com/lithammer/shortuuid/v4 v4.2.0 h1:LMFOzVB3996a7b8aBuEXxqOBflbfPQAiVzkIcHO0h8c=
119+
github.com/lithammer/shortuuid/v4 v4.2.0/go.mod h1:D5noHZ2oFw/YaKCfGy0YxyE7M0wMbezmMjPdhyEFe6Y=
118120
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
119121
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
120122
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
@@ -148,6 +150,8 @@ github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoG
148150
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
149151
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
150152
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
153+
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
154+
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
151155
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
152156
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
153157
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Worker struct {
2020
}
2121

2222
type PodMutation struct {
23+
OperatorEndpoint string `json:"operatorEndpoint"`
2324
PatchToPod map[string]any `json:"patchToPod"`
2425
PatchToContainer map[string]any `json:"patchToContainer"`
2526
}
@@ -58,6 +59,7 @@ func NewDefaultConfig() *Config {
5859
},
5960
},
6061
PodMutation: PodMutation{
62+
OperatorEndpoint: "http://localhost:8080",
6163
PatchToPod: map[string]any{
6264
"spec": map[string]any{
6365
"initContainers": []corev1.Container{

internal/constants/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ const (
1616
VRAMContainerAnnotationFormat = TensorFusionDomain + "/vram-%s"
1717

1818
PendingRequeueDuration = time.Second * 3
19+
20+
GetConnectionURLEnv = "TENSOR_FUSION_OPERATOR_GET_CONNECTION_URL"
21+
ConnectionNameEnv = "TENSOR_FUSION_CONNECTION_NAME"
22+
ConnectionNamespaceEnv = "TENSOR_FUSION_CONNECTION_NAMESPACE"
1923
)

internal/controller/pod_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ func GenerateTensorFusionConnection(pod *corev1.Pod, tfReq []webhookv1.TFReq) []
8282
for _, req := range tfReq {
8383
connection := &tfv1.TensorFusionConnection{
8484
ObjectMeta: metav1.ObjectMeta{
85-
Name: fmt.Sprintf("%s-tf-%s", pod.Name, req.ContainerName),
86-
Namespace: pod.Namespace,
85+
Name: req.ConnectionName,
86+
Namespace: req.ConnectionNamespace,
8787
OwnerReferences: []metav1.OwnerReference{
8888
{
8989
APIVersion: "v1",

internal/webhook/v1/pod_webhook.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"net/http"
24+
"path"
2425

2526
"gomodules.xyz/jsonpatch/v2"
2627
corev1 "k8s.io/api/core/v1"
@@ -34,6 +35,8 @@ import (
3435

3536
"github.com/NexusGPU/tensor-fusion-operator/internal/config"
3637
"github.com/NexusGPU/tensor-fusion-operator/internal/constants"
38+
"github.com/lithammer/shortuuid/v4"
39+
"github.com/samber/lo"
3740
)
3841

3942
// SetupPodWebhookWithManager registers the webhook for Pod in the manager.
@@ -65,7 +68,7 @@ func (m *TensorFusionPodMutator) Handle(ctx context.Context, req admission.Reque
6568
}
6669

6770
log := log.FromContext(ctx)
68-
log.Info("Mutating pod", "name", pod.Name, "namespace", pod.Namespace)
71+
log.Info("Mutating pod", "generateName", pod.GenerateName, "namespace", pod.Namespace)
6972

7073
reqs := ParseTFReq(pod)
7174
if len(reqs) == 0 {
@@ -88,9 +91,11 @@ func (m *TensorFusionPodMutator) InjectDecoder(d admission.Decoder) error {
8891
}
8992

9093
type TFReq struct {
91-
ContainerName string
92-
Tflops resource.Quantity
93-
Vram resource.Quantity
94+
ContainerName string
95+
ConnectionName string
96+
ConnectionNamespace string
97+
Tflops resource.Quantity
98+
Vram resource.Quantity
9499
}
95100

96101
func ParseTFReq(pod *corev1.Pod) []TFReq {
@@ -114,8 +119,25 @@ func ParseTFReq(pod *corev1.Pod) []TFReq {
114119
continue
115120
}
116121

122+
connectionNameEnv, ok := lo.Find(container.Env, func(e corev1.EnvVar) bool {
123+
return e.Name == constants.ConnectionNameEnv
124+
})
125+
126+
if !ok {
127+
continue
128+
}
129+
connectionNamespaceEnv, ok := lo.Find(container.Env, func(e corev1.EnvVar) bool {
130+
return e.Name == constants.ConnectionNamespaceEnv
131+
})
132+
133+
if !ok {
134+
continue
135+
}
136+
117137
req := TFReq{
118-
ContainerName: containerName,
138+
ContainerName: containerName,
139+
ConnectionName: connectionNameEnv.Name,
140+
ConnectionNamespace: connectionNamespaceEnv.Name,
119141
}
120142

121143
// Parse TFLOPS requirement
@@ -152,6 +174,7 @@ func (m *TensorFusionPodMutator) patchTFClient(pod *corev1.Pod, tfReq []TFReq) (
152174
for i := range pod.Spec.Containers {
153175
container := &pod.Spec.Containers[i]
154176
if container.Name == req.ContainerName {
177+
// patch from config
155178
containerJSON, err := json.Marshal(container)
156179
if err != nil {
157180
return nil, fmt.Errorf("marshal container: %v", err)
@@ -165,10 +188,26 @@ func (m *TensorFusionPodMutator) patchTFClient(pod *corev1.Pod, tfReq []TFReq) (
165188
if err != nil {
166189
return nil, fmt.Errorf("apply strategic merge patch to container: %v", err)
167190
}
168-
169191
if err := json.Unmarshal(patchedJSON, container); err != nil {
170192
return nil, fmt.Errorf("unmarshal patched container: %v", err)
171193
}
194+
195+
// add connection env
196+
connectionName := fmt.Sprintf("%s-tf-worker-%s", pod.GenerateName+container.Name, shortuuid.New())
197+
connectionNamespace := pod.Namespace
198+
199+
container.Env = append(container.Env, corev1.EnvVar{
200+
Name: constants.ConnectionNameEnv,
201+
Value: connectionName,
202+
})
203+
container.Env = append(container.Env, corev1.EnvVar{
204+
Name: constants.ConnectionNamespaceEnv,
205+
Value: connectionNamespace,
206+
})
207+
container.Env = append(container.Env, corev1.EnvVar{
208+
Name: constants.GetConnectionURLEnv,
209+
Value: path.Join(m.Config.OperatorEndpoint, fmt.Sprintf("/api/connection?name=%s,namespace=%s", connectionName, connectionNamespace)),
210+
})
172211
}
173212
}
174213
}

internal/webhook/v1/pod_webhook_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ var _ = Describe("TensorFusionPodMutator", func() {
7878
{
7979
Name: "main",
8080
Image: "test-image",
81+
Env: []corev1.EnvVar{
82+
{
83+
Name: constants.ConnectionNameEnv,
84+
Value: "conn1",
85+
},
86+
{
87+
Name: constants.ConnectionNamespaceEnv,
88+
Value: "ns",
89+
},
90+
},
8191
},
8292
},
8393
},
@@ -163,6 +173,16 @@ var _ = Describe("TensorFusionPodMutator", func() {
163173
Containers: []corev1.Container{
164174
{
165175
Name: "test-container",
176+
Env: []corev1.EnvVar{
177+
{
178+
Name: constants.ConnectionNameEnv,
179+
Value: "conn1",
180+
},
181+
{
182+
Name: constants.ConnectionNamespaceEnv,
183+
Value: "ns",
184+
},
185+
},
166186
},
167187
},
168188
},

0 commit comments

Comments
 (0)