Skip to content

Commit 7c958e2

Browse files
committed
Support get IPs for both guestagent and plain mode
Signed-off-by: Songpon Srisawai <[email protected]>
1 parent 791bcd9 commit 7c958e2

File tree

8 files changed

+234
-84
lines changed

8 files changed

+234
-84
lines changed

pkg/guestagent/api/guestservice.pb.desc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11

2-

2+

33
guestservice.protogoogle/protobuf/empty.protogoogle/protobuf/timestamp.proto"0
44
Info(
55
local_ports ( 2.IPPortR
6-
localPorts"�
6+
localPorts"�
77
Event.
88
time ( 2.google.protobuf.TimestampRtime3
99
local_ports_added ( 2.IPPortRlocalPortsAdded7
1010
local_ports_removed ( 2.IPPortRlocalPortsRemoved
11-
errors ( Rerrors"H
11+
errors ( Rerrors
12+
13+
json_bytes ( R jsonBytes"H
1214
IPPort
1315
protocol ( Rprotocol
1416
ip ( Rip

pkg/guestagent/api/guestservice.pb.go

Lines changed: 65 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/guestagent/api/guestservice.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ message Event {
2121
repeated IPPort local_ports_added = 2;
2222
repeated IPPort local_ports_removed = 3;
2323
repeated string errors = 4;
24+
bytes json_bytes = 5;
2425
}
2526

2627
message IPPort {

pkg/guestagent/api/guestservice_grpc.pb.go

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/guestagent/guestagent_linux.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package guestagent
55

66
import (
77
"context"
8+
"encoding/json"
89
"errors"
910
"os"
11+
"os/exec"
1012
"reflect"
1113
"sync"
1214
"syscall"
@@ -186,6 +188,24 @@ func (a *agent) collectEvent(ctx context.Context, st eventState) (*api.Event, ev
186188
}
187189
ev.LocalPortsAdded, ev.LocalPortsRemoved = comparePorts(st.ports, newSt.ports)
188190
ev.Time = timestamppb.Now()
191+
192+
// Get network interfaces info
193+
cmd := exec.Command("ip", "-j", "a")
194+
output, err := cmd.Output()
195+
if err != nil {
196+
logrus.Errorf("Cannot execute command `ip -j a`")
197+
}
198+
199+
// Wrap the output with a label key
200+
// Since multiple outputs will be sent to the hostagent via this jsonBytes,
201+
// this label acts as the key to identify each output separately.
202+
var data any
203+
if err := json.Unmarshal(output, &data); err != nil {
204+
logrus.Errorf("Cannot parsing output")
205+
}
206+
b, _ := json.Marshal(map[string]any{"ip": data})
207+
ev.JsonBytes = b
208+
189209
return ev, newSt
190210
}
191211

0 commit comments

Comments
 (0)