Skip to content

Commit cdeddb0

Browse files
committed
Fix IPv6 metadata collection in agent, fix errTuple coercion for scripts
1 parent 12f7334 commit cdeddb0

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

pkg/agent/metadata.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,22 @@ func (agent *Agent) collectMetadata() {
6868
// Always update primary IP
6969
ipAddrs, err := iface.Addrs()
7070
if err != nil || len(ipAddrs) < 1 {
71-
agent.Log.Error("Failed to collect machine primary IP")
71+
agent.Log.Error("Failed to collect machine primary IP", zap.Error(err))
7272
return
7373
}
7474

75-
ip, _, err := net.ParseCIDR(ipAddrs[0].String())
76-
if err != nil {
77-
agent.Log.Error("Failed to collect machine primary IP")
78-
return
75+
for _, ipAddr := range ipAddrs {
76+
ip, _, err := net.ParseCIDR(ipAddr.String())
77+
if err != nil {
78+
agent.Log.Error("Failed to collect machine primary IP", zap.Error(err))
79+
continue
80+
}
81+
if !ip.IsLoopback() && !ip.IsLinkLocalUnicast() && !ip.IsGlobalUnicast() {
82+
continue
83+
}
84+
agent.Metadata.PrimaryIP = ip.String()
7985
}
80-
agent.Metadata.PrimaryIP = ip.String()
86+
8187
}
8288

8389
// isMulticastCapable reports whether ifi is an IP multicast-capable
@@ -150,7 +156,7 @@ func routableIP(network string, ip net.IP) net.IP {
150156
return ip
151157
}
152158
case "ip6":
153-
if ip.IsLoopback() { // addressing scope of the loopback address depends on each implementation
159+
if ip.IsLoopback() || ip.IsLinkLocalUnicast() { // addressing scope of the loopback address depends on each implementation
154160
return nil
155161
}
156162
if ip := ip.To16(); ip != nil && ip.To4() == nil {

pkg/script/convert.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ func convertToStarlark(value interface{}) (starlark.Value, error) {
1616
case starlark.Value:
1717
return v, nil
1818
case errTuple:
19+
var starlarkErr starlark.Value = starlark.None
1920
if v.err != nil {
20-
return starlark.Tuple{starlark.None, starlark.String(v.err.Error())}, nil
21+
starlarkErr = starlark.String(v.err.Error())
2122
}
22-
2323
retVal, err := convertToStarlark(v.val)
2424
if err != nil {
2525
return starlark.None, fmt.Errorf("failed to convert contained value: %w", err)
2626
}
27-
return starlark.Tuple{retVal, starlark.None}, nil
27+
return starlark.Tuple{retVal, starlarkErr}, nil
2828
case bool:
2929
return starlark.Bool(v), nil
3030
case int:

0 commit comments

Comments
 (0)