-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathcachedresolvedip.go
79 lines (67 loc) · 2.13 KB
/
cachedresolvedip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) 2023 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package types
import (
"fmt"
"net"
"strings"
"time"
"github.com/lf-edge/eve/pkg/pillar/base"
)
// CachedIP : cached IP with time-limited validity.
type CachedIP struct {
IPAddress net.IP
ValidUntil time.Time
}
// String representation of CachedIP.
func (c CachedIP) String() string {
return fmt.Sprintf("IP %s valid until %v", c.IPAddress, c.ValidUntil)
}
// CachedResolvedIPs serves as a cache for storing the IP addresses obtained through
// DNS resolution for a given hostname.
type CachedResolvedIPs struct {
Hostname string
CachedIPs []CachedIP
}
// String representation of CachedResolvedIPs.
func (c CachedResolvedIPs) String() string {
cachedIPs := make([]string, 0, len(c.CachedIPs))
for _, ip := range c.CachedIPs {
cachedIPs = append(cachedIPs, ip.String())
}
return fmt.Sprintf("Hostname %s with cached resolved IPs: [%s]", c.Hostname,
strings.Join(cachedIPs, ", "))
}
// Key is used for pubsub
func (c CachedResolvedIPs) Key() string {
return c.Hostname
}
// LogCreate :
func (c CachedResolvedIPs) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.CachedResolvedIPsLogType, "",
nilUUID, c.LogKey())
logObject.Metricf("CachedResolvedIPs create %s", c.String())
}
// LogModify :
func (c CachedResolvedIPs) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.CachedResolvedIPsLogType, "",
nilUUID, c.LogKey())
oldVal, ok := old.(CachedResolvedIPs)
if !ok {
logObject.Clone().Fatalf(
"LogModify: Old object interface passed is not of CachedResolvedIPs type")
}
logObject.Metricf("CachedResolvedIPs modified from %s to %s",
oldVal.String(), c.String())
}
// LogDelete :
func (c CachedResolvedIPs) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.CachedResolvedIPsLogType, "",
nilUUID, c.LogKey())
logObject.Metricf("CachedResolvedIPs delete %s", c.String())
base.DeleteLogObject(logBase, c.LogKey())
}
// LogKey :
func (c CachedResolvedIPs) LogKey() string {
return string(base.CachedResolvedIPsLogType) + "-" + c.Key()
}