|
17 | 17 | package status |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
20 | 22 | "net" |
| 23 | + "strings" |
21 | 24 |
|
22 | 25 | "kmesh.net/kmesh/api/v2/workloadapi" |
23 | 26 | "kmesh.net/kmesh/api/v2/workloadapi/security" |
| 27 | + "kmesh.net/kmesh/pkg/controller/workload/bpfcache" |
| 28 | + "kmesh.net/kmesh/pkg/nets" |
| 29 | + "kmesh.net/kmesh/pkg/utils" |
24 | 30 | ) |
25 | 31 |
|
26 | 32 | type Workload struct { |
@@ -183,3 +189,161 @@ func ConvertAuthorizationPolicy(p *security.Authorization) *AuthorizationPolicy |
183 | 189 |
|
184 | 190 | return out |
185 | 191 | } |
| 192 | + |
| 193 | +type prettyArray[T any] []T |
| 194 | + |
| 195 | +func (a prettyArray[T]) MarshalJSON() ([]byte, error) { |
| 196 | + prettified := make([]string, len(a)) |
| 197 | + for i, elem := range a { |
| 198 | + prettified[i] = fmt.Sprintf("%v", elem) |
| 199 | + } |
| 200 | + |
| 201 | + return json.Marshal(strings.Join(prettified, ", ")) |
| 202 | +} |
| 203 | + |
| 204 | +type BpfServiceValue struct { |
| 205 | + // EndpointCount is the number of endpoints for each priority. |
| 206 | + EndpointCount prettyArray[uint32] `json:"endpointCount"` |
| 207 | + LbPolicy string `json:"lbPolicy"` |
| 208 | + ServicePort prettyArray[uint32] `json:"servicePort,omitempty"` |
| 209 | + TargetPort prettyArray[uint32] `json:"targetPort,omitempty"` |
| 210 | + WaypointAddr string `json:"waypointAddr,omitempty"` |
| 211 | + WaypointPort uint32 `json:"waypointPort,omitempty"` |
| 212 | +} |
| 213 | + |
| 214 | +type BpfBackendValue struct { |
| 215 | + Ip string `json:"ip"` |
| 216 | + ServiceCount uint32 `json:"serviceCount"` |
| 217 | + Services []string `json:"services"` |
| 218 | + WaypointAddr string `json:"waypointAddr,omitempty"` |
| 219 | + WaypointPort uint32 `json:"waypointPort,omitempty"` |
| 220 | +} |
| 221 | + |
| 222 | +type BpfFrontendValue struct { |
| 223 | + UpstreamId string `json:"upstreamId,omitempty"` |
| 224 | +} |
| 225 | + |
| 226 | +type BpfWorkloadPolicyValue struct { |
| 227 | + PolicyIds []string `json:"policyIds,omitempty"` |
| 228 | +} |
| 229 | + |
| 230 | +type BpfEndpointValue struct { |
| 231 | + BackendUid string `json:"backendUid,omitempty"` |
| 232 | +} |
| 233 | + |
| 234 | +type WorkloadBpfDump struct { |
| 235 | + hashName *utils.HashName |
| 236 | + |
| 237 | + WorkloadPolicies []BpfWorkloadPolicyValue `json:"workloadPolicies"` |
| 238 | + Backends []BpfBackendValue `json:"backends"` |
| 239 | + Endpoints []BpfEndpointValue `json:"endpoints"` |
| 240 | + Frontends []BpfFrontendValue `json:"frontends"` |
| 241 | + Services []BpfServiceValue `json:"services"` |
| 242 | +} |
| 243 | + |
| 244 | +func NewWorkloadBpfDump(hashName *utils.HashName) WorkloadBpfDump { |
| 245 | + return WorkloadBpfDump{hashName: hashName} |
| 246 | +} |
| 247 | + |
| 248 | +func (wd WorkloadBpfDump) WithWorkloadPolicies(workloadPolicies []bpfcache.WorkloadPolicyValue) WorkloadBpfDump { |
| 249 | + converted := make([]BpfWorkloadPolicyValue, 0, len(workloadPolicies)) |
| 250 | + for _, policy := range workloadPolicies { |
| 251 | + policyIds := []string{} |
| 252 | + for _, id := range policy.PolicyIds { |
| 253 | + policyIds = append(policyIds, wd.hashName.NumToStr(id)) |
| 254 | + } |
| 255 | + converted = append(converted, BpfWorkloadPolicyValue{ |
| 256 | + PolicyIds: policyIds, |
| 257 | + }) |
| 258 | + } |
| 259 | + wd.WorkloadPolicies = converted |
| 260 | + return wd |
| 261 | +} |
| 262 | + |
| 263 | +func (wd WorkloadBpfDump) WithBackends(backends []bpfcache.BackendValue) WorkloadBpfDump { |
| 264 | + converted := make([]BpfBackendValue, 0, len(backends)) |
| 265 | + for _, backend := range backends { |
| 266 | + waypointAddr := "" |
| 267 | + if backend.WaypointAddr != [16]byte{} { |
| 268 | + waypointAddr = nets.IpString(backend.WaypointAddr) |
| 269 | + } |
| 270 | + bac := BpfBackendValue{ |
| 271 | + Ip: nets.IpString(backend.Ip), |
| 272 | + ServiceCount: backend.ServiceCount, |
| 273 | + WaypointAddr: waypointAddr, |
| 274 | + WaypointPort: nets.ConvertPortToLittleEndian(backend.WaypointPort), |
| 275 | + } |
| 276 | + services := make([]string, 0, len(backend.Services)) |
| 277 | + for _, s := range backend.Services { |
| 278 | + svc := wd.hashName.NumToStr(s) |
| 279 | + if svc == "" { |
| 280 | + continue |
| 281 | + } |
| 282 | + services = append(services, svc) |
| 283 | + } |
| 284 | + bac.Services = services |
| 285 | + converted = append(converted, bac) |
| 286 | + } |
| 287 | + wd.Backends = converted |
| 288 | + return wd |
| 289 | +} |
| 290 | + |
| 291 | +func (wd WorkloadBpfDump) WithEndpoints(endpoints []bpfcache.EndpointValue) WorkloadBpfDump { |
| 292 | + converted := make([]BpfEndpointValue, 0, len(endpoints)) |
| 293 | + for _, endpoint := range endpoints { |
| 294 | + converted = append(converted, BpfEndpointValue{ |
| 295 | + BackendUid: wd.hashName.NumToStr(endpoint.BackendUid), |
| 296 | + }) |
| 297 | + } |
| 298 | + wd.Endpoints = converted |
| 299 | + return wd |
| 300 | +} |
| 301 | + |
| 302 | +func (wd WorkloadBpfDump) WithFrontends(frontends []bpfcache.FrontendValue) WorkloadBpfDump { |
| 303 | + converted := make([]BpfFrontendValue, 0, len(frontends)) |
| 304 | + for _, frontend := range frontends { |
| 305 | + converted = append(converted, BpfFrontendValue{ |
| 306 | + UpstreamId: wd.hashName.NumToStr(frontend.UpstreamId), |
| 307 | + }) |
| 308 | + } |
| 309 | + wd.Frontends = converted |
| 310 | + return wd |
| 311 | +} |
| 312 | + |
| 313 | +func (wd WorkloadBpfDump) WithServices(services []bpfcache.ServiceValue) WorkloadBpfDump { |
| 314 | + converted := make([]BpfServiceValue, 0, len(services)) |
| 315 | + for _, s := range services { |
| 316 | + waypointAddr := "" |
| 317 | + if s.WaypointAddr != [16]byte{} { |
| 318 | + waypointAddr = nets.IpString(s.WaypointAddr) |
| 319 | + } |
| 320 | + svc := BpfServiceValue{ |
| 321 | + EndpointCount: []uint32{}, |
| 322 | + LbPolicy: workloadapi.LoadBalancing_Mode_name[int32(s.LbPolicy)], |
| 323 | + WaypointAddr: waypointAddr, |
| 324 | + WaypointPort: nets.ConvertPortToLittleEndian(s.WaypointPort), |
| 325 | + } |
| 326 | + |
| 327 | + for _, c := range s.EndpointCount { |
| 328 | + svc.EndpointCount = append(svc.EndpointCount, c) |
| 329 | + } |
| 330 | + |
| 331 | + for _, p := range s.ServicePort { |
| 332 | + if p == 0 { |
| 333 | + continue |
| 334 | + } |
| 335 | + svc.ServicePort = append(svc.ServicePort, nets.ConvertPortToLittleEndian(p)) |
| 336 | + } |
| 337 | + |
| 338 | + for _, p := range s.TargetPort { |
| 339 | + if p == 0 { |
| 340 | + continue |
| 341 | + } |
| 342 | + svc.TargetPort = append(svc.TargetPort, nets.ConvertPortToLittleEndian(p)) |
| 343 | + } |
| 344 | + |
| 345 | + converted = append(converted, svc) |
| 346 | + } |
| 347 | + wd.Services = converted |
| 348 | + return wd |
| 349 | +} |
0 commit comments