Skip to content

Commit 4af7598

Browse files
committed
convert ip bytes to string, fix test
Signed-off-by: Kuromesi <blackfacepan@163.com>
1 parent d823f61 commit 4af7598

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

pkg/nets/nets.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ func CopyIpByteFromSlice(dst *[16]byte, src []byte) {
7272
copy(dst[:], src)
7373
}
7474

75+
// IpString converts ip bytes to string, for IpV4, it checks
76+
// whether the last 12 bytes are all zeros.
77+
// TODO: this may conflict with IpV6 addresses with the same pattern,
78+
// we should find a better way to indicate the ipv4 address.
79+
func IpString(ip [16]byte) string {
80+
if isZeros(ip[5:]) {
81+
return net.IP(ip[:4]).String()
82+
}
83+
return net.IP(ip[:]).String()
84+
}
85+
86+
func isZeros(p []byte) bool {
87+
for i := 0; i < len(p); i++ {
88+
if p[i] != 0 {
89+
return false
90+
}
91+
}
92+
return true
93+
}
94+
7595
func checkIPVersion() (ipv4, ipv6 bool) {
7696
addrs, err := net.InterfaceAddrs()
7797
if err != nil {

pkg/nets/nets_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,21 @@ func TestConvertPortToLittleEndian(t *testing.T) {
136136
}
137137
}
138138
}
139+
140+
func TestIpString(t *testing.T) {
141+
tests := []struct {
142+
ip [16]byte
143+
expected string
144+
}{
145+
{[16]byte{192, 168, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, "192.168.0.1"},
146+
{[16]byte{192, 168, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, "c0a8:1::1"},
147+
{[16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, "0.0.0.0"},
148+
}
149+
150+
for _, test := range tests {
151+
actual := IpString(test.ip)
152+
if actual != test.expected {
153+
t.Errorf("IpString(%v) = %v; expected %v", test.ip, actual, test.expected)
154+
}
155+
}
156+
}

pkg/status/api.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,16 @@ type BpfServiceValue struct {
207207
LbPolicy string `json:"lbPolicy"`
208208
ServicePort prettyArray[uint32] `json:"servicePort,omitempty"`
209209
TargetPort prettyArray[uint32] `json:"targetPort,omitempty"`
210-
WaypointAddr prettyArray[byte] `json:"waypointAddr,omitempty"`
210+
WaypointAddr string `json:"waypointAddr,omitempty"`
211211
WaypointPort uint32 `json:"waypointPort,omitempty"`
212212
}
213213

214214
type BpfBackendValue struct {
215-
Ip prettyArray[byte] `json:"ip"`
216-
ServiceCount uint32 `json:"serviceCount"`
217-
Services []string `json:"services"`
218-
WaypointAddr prettyArray[byte] `json:"waypointAddr,omitempty"`
219-
WaypointPort uint32 `json:"waypointPort,omitempty"`
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"`
220220
}
221221

222222
type BpfFrontendValue struct {
@@ -267,14 +267,12 @@ func (wd WorkloadBpfDump) WithBackends(backends []bpfcache.BackendValue) Workloa
267267
for _, b := range backend.Ip {
268268
ip = append(ip, b)
269269
}
270-
waypointAddr := []byte{}
270+
waypointAddr := ""
271271
if backend.WaypointAddr != [16]byte{} {
272-
for _, b := range backend.WaypointAddr {
273-
waypointAddr = append(waypointAddr, b)
274-
}
272+
waypointAddr = nets.IpString(backend.WaypointAddr)
275273
}
276274
bac := BpfBackendValue{
277-
Ip: ip,
275+
Ip: nets.IpString(backend.Ip),
278276
ServiceCount: backend.ServiceCount,
279277
WaypointAddr: waypointAddr,
280278
WaypointPort: nets.ConvertPortToLittleEndian(backend.WaypointPort),
@@ -285,7 +283,7 @@ func (wd WorkloadBpfDump) WithBackends(backends []bpfcache.BackendValue) Workloa
285283
if svc == "" {
286284
continue
287285
}
288-
services = append(services, wd.hashName.NumToStr(s))
286+
services = append(services, svc)
289287
}
290288
bac.Services = services
291289
converted = append(converted, bac)
@@ -319,11 +317,9 @@ func (wd WorkloadBpfDump) WithFrontends(frontends []bpfcache.FrontendValue) Work
319317
func (wd WorkloadBpfDump) WithServices(services []bpfcache.ServiceValue) WorkloadBpfDump {
320318
converted := make([]BpfServiceValue, 0, len(services))
321319
for _, s := range services {
322-
waypointAddr := []byte{}
320+
waypointAddr := ""
323321
if s.WaypointAddr != [16]byte{} {
324-
for _, b := range s.WaypointAddr {
325-
waypointAddr = append(waypointAddr, b)
326-
}
322+
waypointAddr = nets.IpString(s.WaypointAddr)
327323
}
328324
svc := BpfServiceValue{
329325
EndpointCount: []uint32{},

pkg/status/testdata/workload_configdump.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"Workloads": [
2+
"workloads": [
33
{
44
"uid": "cluster0//Pod/ns/name",
55
"addresses": [
@@ -27,7 +27,7 @@
2727
]
2828
}
2929
],
30-
"Services": [
30+
"services": [
3131
{
3232
"name": "svc",
3333
"namespace": "ns",
@@ -51,7 +51,7 @@
5151
}
5252
}
5353
],
54-
"Policies": [
54+
"policies": [
5555
{
5656
"name": "policy",
5757
"namespace": "ns",

0 commit comments

Comments
 (0)