Skip to content

Commit

Permalink
change Ip conversion
Browse files Browse the repository at this point in the history
Signed-off-by: LiZhenCheng9527 <[email protected]>
  • Loading branch information
LiZhenCheng9527 committed Sep 14, 2024
1 parent 7cd3f85 commit f493505
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 50 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/telemetry/accesslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type logInfo struct {
}

func OutputAccesslog(data requestMetric, accesslog logInfo) {
logStr := buildAccesslog(data, accesslog)
if accesslog.destinationService != "" || data.dstPort == uint16(15019) {
if accesslog.destinationService != "-" || data.dstPort == uint16(15019) {
logStr := buildAccesslog(data, accesslog)
fmt.Println("accesslog:", logStr)
}
}
Expand Down
42 changes: 9 additions & 33 deletions pkg/controller/telemetry/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,16 @@ func (m *MetricController) buildServiceMetric(data *requestMetric) (serviceMetri
srcAddr = binary.LittleEndian.AppendUint32(srcAddr, data.src[i])
}

dstWorkload, _ := m.getWorkloadByAddress(restoreIPv4(dstAddr))
srcWorkload, _ := m.getWorkloadByAddress(restoreIPv4(srcAddr))
dstWorkload, dstIp := m.getWorkloadByAddress(restoreIPv4(dstAddr))
srcWorkload, srcIp := m.getWorkloadByAddress(restoreIPv4(srcAddr))

trafficLabels, accesslog := buildServiceMetric(dstWorkload, srcWorkload, data.dstPort)
trafficLabels.requestProtocol = "tcp"
trafficLabels.responseFlags = "-"
trafficLabels.connectionSecurityPolicy = "mutual_tls"
accesslog.destinationAddress = bytesToIp(restoreIPv4(dstAddr)) + ":" + fmt.Sprintf("%d", data.dstPort)
accesslog.sourceAddress = bytesToIp(restoreIPv4(srcAddr)) + ":" + fmt.Sprintf("%d", data.srcPort)

accesslog.destinationAddress = dstIp + ":" + fmt.Sprintf("%d", data.dstPort)
accesslog.sourceAddress = srcIp + ":" + fmt.Sprintf("%d", data.srcPort)

return trafficLabels, accesslog
}
Expand All @@ -360,8 +361,12 @@ func (m *MetricController) getWorkloadByAddress(address []byte) (*workloadapi.Wo
networkAddr.Address, _ = netip.AddrFromSlice(address)
workload := m.workloadCache.GetWorkloadByAddr(networkAddr)
if workload == nil {
<<<<<<< HEAD
log.Debugf("cannot find workload %s", networkAddr.Address.String())
return nil, ""
=======
return nil, networkAddr.Address.String()
>>>>>>> 6abe3a1 (change Ip conversion)
}
return workload, networkAddr.Address.String()
}
Expand Down Expand Up @@ -599,16 +604,6 @@ func struct2map(labels interface{}) map[string]string {
return nil
}

func bytesToIp(ip []byte) string {
if len(ip) == 4 {
return bytesToIPv4(ip)
}
if len(ip) == 16 {
return bytesToIPv6(ip)
}
return ""
}

// Converting IPv4 data reported in IPv6 form to IPv4
func restoreIPv4(bytes []byte) []byte {
for i := 4; i < 16; i++ {
Expand All @@ -619,22 +614,3 @@ func restoreIPv4(bytes []byte) []byte {

return bytes[:4]
}

func bytesToIPv4(ip []byte) string {
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}

func bytesToIPv6(bytes []byte) string {
// IPv6 addresses are represented by eight 16-bit integers
var ip [8]uint16

// Align to read 16-bit integers
for i := 0; i < len(bytes); i += 2 {
ip[i/2] = uint16(bytes[i])<<8 + uint16(bytes[i+1])
}

// Formatted output as IPv6 address strings
return fmt.Sprintf("%x:%x:%x:%x:%x:%x:%x:%x",
ip[0], ip[1], ip[2], ip[3],
ip[4], ip[5], ip[6], ip[7])
}
15 changes: 0 additions & 15 deletions pkg/controller/telemetry/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,18 +957,3 @@ func TestMetricController_updatePrometheusMetric(t *testing.T) {
})
}
}

func TestBytesToIP(t *testing.T) {
t.Run("normal []byte to Ipv4", func(t *testing.T) {
testData := []byte{10, 244, 0, 1}
want := "10.244.0.1"
got := bytesToIp(testData)
assert.Equal(t, want, got)
})
t.Run("normal []byte to Ipv6", func(t *testing.T) {
testData := []byte{10, 244, 0, 1, 10, 244, 0, 1, 10, 244, 0, 1, 10, 244, 0, 1}
want := "af4:1:af4:1:af4:1:af4:1"
got := bytesToIp(testData)
assert.Equal(t, want, got)
})
}

0 comments on commit f493505

Please sign in to comment.