-
Notifications
You must be signed in to change notification settings - Fork 332
/
dpu_annotations.go
248 lines (223 loc) · 8.08 KB
/
dpu_annotations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package util
import (
"encoding/json"
"fmt"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/kube"
"github.com/ovn-org/ovn-kubernetes/go-controller/pkg/types"
v1 "k8s.io/api/core/v1"
listers "k8s.io/client-go/listers/core/v1"
)
/*
This Handles DPU related annotations in ovn-kubernetes.
The following annotations are handled:
Annotation: "k8s.ovn.org/dpu.connection-details"
Applied on: Pods
Used for: convey the required information to setup network plubming on DPU for a given Pod
Example:
annotations:
k8s.ovn.org/dpu.connection-details: |
{"default":
{
"pfId": “0”,
“vfId”: "3",
"sandboxId": "35b82dbe2c39768d9874861aee38cf569766d4855b525ae02bff2bfbda73392a"
}
}
Annotation: "k8s.ovn.org/dpu.connection-status"
Applied on: Pods
Used for: convey the DPU connection status for a given Pod
Example:
annotations:
k8s.ovn.org/dpu.connection-status: |
{"default":
{
"status": “Ready”,
"reason": ""
}
}
*/
const (
DPUConnectionDetailsAnnot = "k8s.ovn.org/dpu.connection-details"
DPUConnectionStatusAnnot = "k8s.ovn.org/dpu.connection-status"
DPUConnectionStatusReady = "Ready"
DPUConnectionStatusError = "Error"
)
type DPUConnectionDetails struct {
PfId string `json:"pfId"`
VfId string `json:"vfId"`
SandboxId string `json:"sandboxId"`
VfNetdevName string `json:"vfNetdevName,omitempty"`
}
type DPUConnectionStatus struct {
Status string `json:"Status"`
Reason string `json:"Reason,omitempty"`
}
// UnmarshalPodDPUConnDetailsAllNetworks returns the DPUConnectionDetails map of all networks from the given Pod annotation
func UnmarshalPodDPUConnDetailsAllNetworks(annotations map[string]string) (map[string]DPUConnectionDetails, error) {
podDcds := make(map[string]DPUConnectionDetails)
ovnAnnotation, ok := annotations[DPUConnectionDetailsAnnot]
if ok {
if err := json.Unmarshal([]byte(ovnAnnotation), &podDcds); err != nil {
// DPU connection details annotation could be in the legacy format
var legacyScd DPUConnectionDetails
if err := json.Unmarshal([]byte(ovnAnnotation), &legacyScd); err == nil {
podDcds[types.DefaultNetworkName] = legacyScd
} else {
return nil, fmt.Errorf("failed to unmarshal OVN pod %s annotation %q: %v",
DPUConnectionDetailsAnnot, annotations, err)
}
}
}
return podDcds, nil
}
// MarshalPodDPUConnDetails adds the pod's connection details of the specified NAD to the corresponding pod annotation;
// if dcd is nil, delete the pod's connection details of the specified NAD
func MarshalPodDPUConnDetails(annotations map[string]string, dcd *DPUConnectionDetails, nadName string) (map[string]string, error) {
if annotations == nil {
annotations = make(map[string]string)
}
podDcds, err := UnmarshalPodDPUConnDetailsAllNetworks(annotations)
if err != nil {
return nil, err
}
dc, ok := podDcds[nadName]
if dcd != nil {
if ok && dc == *dcd {
return nil, newAnnotationAlreadySetError("OVN pod %s annotation for NAD %s already exists in %v",
DPUConnectionDetailsAnnot, nadName, annotations)
}
podDcds[nadName] = *dcd
} else {
if !ok {
return nil, newAnnotationAlreadySetError("OVN pod %s annotation for NAD %s already removed",
DPUConnectionDetailsAnnot, nadName)
}
delete(podDcds, nadName)
}
bytes, err := json.Marshal(podDcds)
if err != nil {
return nil, fmt.Errorf("failed marshaling pod annotation map %v: %v", podDcds, err)
}
annotations[DPUConnectionDetailsAnnot] = string(bytes)
return annotations, nil
}
// UnmarshalPodDPUConnDetails returns dpu connection details for the specified NAD
func UnmarshalPodDPUConnDetails(annotations map[string]string, nadName string) (*DPUConnectionDetails, error) {
ovnAnnotation, ok := annotations[DPUConnectionDetailsAnnot]
if !ok {
return nil, newAnnotationNotSetError("could not find OVN pod %s annotation in %v",
DPUConnectionDetailsAnnot, annotations)
}
podDcds, err := UnmarshalPodDPUConnDetailsAllNetworks(annotations)
if err != nil {
return nil, err
}
dcd, ok := podDcds[nadName]
if !ok {
return nil, newAnnotationNotSetError("no OVN %s annotation for network %s: %q",
DPUConnectionDetailsAnnot, nadName, ovnAnnotation)
}
return &dcd, nil
}
// UnmarshalPodDPUConnStatusAllNetworks returns the DPUConnectionStatus map of all networks from the given Pod annotation
func UnmarshalPodDPUConnStatusAllNetworks(annotations map[string]string) (map[string]DPUConnectionStatus, error) {
podDcss := make(map[string]DPUConnectionStatus)
ovnAnnotation, ok := annotations[DPUConnectionStatusAnnot]
if ok {
if err := json.Unmarshal([]byte(ovnAnnotation), &podDcss); err != nil {
// DPU connection status annotation could be in the legacy format
var legacyScs DPUConnectionStatus
if err := json.Unmarshal([]byte(ovnAnnotation), &legacyScs); err == nil {
podDcss[types.DefaultNetworkName] = legacyScs
} else {
return nil, fmt.Errorf("failed to unmarshal OVN pod %s annotation %q: %v",
DPUConnectionStatusAnnot, annotations, err)
}
}
}
return podDcss, nil
}
// MarshalPodDPUConnStatus adds the pod's connection status of the specified NAD to the corresponding pod annotation.
// if scs is nil, delete the pod's connection status of the specified NAD
func MarshalPodDPUConnStatus(annotations map[string]string, scs *DPUConnectionStatus, nadName string) (map[string]string, error) {
if annotations == nil {
annotations = make(map[string]string)
}
podScss, err := UnmarshalPodDPUConnStatusAllNetworks(annotations)
if err != nil {
return nil, err
}
sc, ok := podScss[nadName]
if scs != nil {
if ok && sc == *scs {
return nil, newAnnotationAlreadySetError("OVN pod %s annotation for NAD %s already exists in %v",
DPUConnectionStatusAnnot, nadName, annotations)
}
podScss[nadName] = *scs
} else {
if !ok {
return nil, newAnnotationAlreadySetError("OVN pod %s annotation for NAD %s already removed",
DPUConnectionStatusAnnot, nadName)
}
delete(podScss, nadName)
}
bytes, err := json.Marshal(podScss)
if err != nil {
return nil, fmt.Errorf("failed marshaling pod annotation map %v: %v", podScss, err)
}
annotations[DPUConnectionStatusAnnot] = string(bytes)
return annotations, nil
}
// UnmarshalPodDPUConnStatus returns DPU connection status for the specified NAD
func UnmarshalPodDPUConnStatus(annotations map[string]string, nadName string) (*DPUConnectionStatus, error) {
ovnAnnotation, ok := annotations[DPUConnectionStatusAnnot]
if !ok {
return nil, newAnnotationNotSetError("could not find OVN pod annotation in %v", annotations)
}
podScss, err := UnmarshalPodDPUConnStatusAllNetworks(annotations)
if err != nil {
return nil, err
}
scs, ok := podScss[nadName]
if !ok {
return nil, newAnnotationNotSetError("no OVN %s annotation for network %s: %q",
DPUConnectionStatusAnnot, nadName, ovnAnnotation)
}
return &scs, nil
}
// UpdatePodDPUConnStatusWithRetry updates the DPU connection status annotation
// on the pod retrying on conflict
func UpdatePodDPUConnStatusWithRetry(podLister listers.PodLister, kube kube.Interface, pod *v1.Pod, dpuConnStatus *DPUConnectionStatus, nadName string) error {
updatePodAnnotationNoRollback := func(pod *v1.Pod) (*v1.Pod, func(), error) {
var err error
pod.Annotations, err = MarshalPodDPUConnStatus(pod.Annotations, dpuConnStatus, nadName)
if err != nil {
return nil, nil, err
}
return pod, nil, nil
}
return UpdatePodWithRetryOrRollback(
podLister,
kube,
pod,
updatePodAnnotationNoRollback,
)
}
// UpdatePodDPUConnDetailsWithRetry updates the DPU connection details
// annotation on the pod retrying on conflict
func UpdatePodDPUConnDetailsWithRetry(podLister listers.PodLister, kube kube.Interface, pod *v1.Pod, dpuConnDetails *DPUConnectionDetails, nadName string) error {
updatePodAnnotationNoRollback := func(pod *v1.Pod) (*v1.Pod, func(), error) {
var err error
pod.Annotations, err = MarshalPodDPUConnDetails(pod.Annotations, dpuConnDetails, nadName)
if err != nil {
return nil, nil, err
}
return pod, nil, nil
}
return UpdatePodWithRetryOrRollback(
podLister,
kube,
pod,
updatePodAnnotationNoRollback,
)
}