Skip to content

Commit 86b2ec3

Browse files
committed
Allowing for lazy loading of interfaces
1 parent 5900163 commit 86b2ec3

1 file changed

Lines changed: 50 additions & 48 deletions

File tree

pkg/api/api.go

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
KT_INTERFACE_LOOKUP_TEXT_FILTER = "KT_INTERFACE_LOOKUP_TEXT_FILTER"
4545
KT_LOAD_CUSTOM_COLUMNS = "KT_LOAD_CUSTOM_COLUMNS"
4646
KT_API_LAZY_LOAD_CUSTOMS = "KT_API_LAZY_LOAD_CUSTOMS"
47+
KT_API_LAZY_LOAD_INTERFACES = "KT_API_LAZY_LOAD_INTERFACES"
4748
)
4849

4950
var (
@@ -74,6 +75,7 @@ type KentikApi struct {
7475
loadInterfaces bool
7576
lazyLoadCustoms bool
7677
fullLoadCustoms bool
78+
lazyLoadInts bool
7779
}
7880

7981
func NewKentikApi(ctx context.Context, log logger.ContextL, cfg *ktranslate.Config) (*KentikApi, error) {
@@ -103,9 +105,11 @@ func NewKentikApi(ctx context.Context, log logger.ContextL, cfg *ktranslate.Conf
103105
loadInterfaces: kt.LookupEnvBool(KT_API_LOAD_INTERFACES, false),
104106
lazyLoadCustoms: kt.LookupEnvBool(KT_API_LAZY_LOAD_CUSTOMS, false),
105107
fullLoadCustoms: kt.LookupEnvBool(KT_LOAD_CUSTOM_COLUMNS, false),
108+
lazyLoadInts: kt.LookupEnvBool(KT_API_LAZY_LOAD_INTERFACES, false),
106109
}
107110

108-
log.Infof("Setting API timeout to %v, loadInterfaces=%v, lazyLoadCustoms=%v, fullyLoadCustoms=%v", apiTimeout, kapi.loadInterfaces, kapi.lazyLoadCustoms, kapi.fullLoadCustoms)
111+
log.Infof("Setting API timeout to %v, loadInterfaces=%v, lazyLoadInterfaces=%v, lazyLoadCustoms=%v, fullyLoadCustoms=%v",
112+
apiTimeout, kapi.loadInterfaces, kapi.lazyLoadInts, kapi.lazyLoadCustoms, kapi.fullLoadCustoms)
109113

110114
// Now, check to see if synthetics API works.
111115
err := kapi.connectSynthAndLookup(ctx)
@@ -261,6 +265,12 @@ func (api *KentikApi) GetDevice(ctx context.Context, cid kt.Cid, did kt.DeviceID
261265
if err != nil {
262266
api.Warnf("Cannot load customs for %s %s, %v", dev.IDStr, info.APIEmail, err)
263267
} else {
268+
if api.lazyLoadInts {
269+
err := api.getInterfaces(ctxo, []string{dev.IDStr})
270+
if err != nil {
271+
api.Warnf("Cannot load interfaces for %s %s, %v", dev.IDStr, info.APIEmail, err)
272+
}
273+
}
264274
return
265275
}
266276
}
@@ -430,7 +440,7 @@ func (api *KentikApi) connectSynthAndLookup(ctxIn context.Context) error {
430440
api.Infof("Connected to Device Lookup API server at %s", address)
431441
api.deviceClient = deviceLookup
432442

433-
if api.loadInterfaces {
443+
if api.loadInterfaces || api.lazyLoadInts {
434444
interfaceLookup := interfacepb.NewInterfaceServiceClient(conn)
435445
api.Infof("Connected to Interface Lookup API server at %s", address)
436446
api.interfaceClient = interfaceLookup
@@ -557,6 +567,18 @@ func (api *KentikApi) getDeviceInfoNew(ctx context.Context) error {
557567
num++
558568
}
559569

570+
// If we are loading interfaces, pull in interface data now also.
571+
if api.loadInterfaces {
572+
const batchSize = 100
573+
for i := 0; i < len(deviceIds); i += batchSize {
574+
end := min(i+batchSize, len(deviceIds))
575+
err := api.getInterfaces(ctxo, deviceIds[i:end])
576+
if err != nil {
577+
return err
578+
}
579+
}
580+
}
581+
560582
api.Infof("Loaded %d Kentik Devices via GRPC for %s", len(r.GetDevices()), info.APIEmail)
561583
}
562584

@@ -568,65 +590,45 @@ func (api *KentikApi) getDeviceInfoNew(ctx context.Context) error {
568590
return err
569591
}
570592

571-
// If we are loading interfaces, pull in interface data now also.
572-
if api.loadInterfaces {
573-
const batchSize = 100
574-
for i := 0; i < len(deviceIds); i += batchSize {
575-
end := min(i+batchSize, len(deviceIds))
576-
err := api.getInterfaces(ctx, deviceIds[i:end])
577-
if err != nil {
578-
return err
579-
}
580-
}
581-
}
582-
583593
api.setTime = time.Now()
584594
api.Infof("Loaded %d Kentik Devices total via GRPC in %v", num, api.setTime.Sub(stime))
585595
return nil
586596
}
587597

588598
func (api *KentikApi) getInterfaces(ctx context.Context, deviceIds []string) error {
589-
for _, info := range api.config.KentikCreds {
590-
api.Infof("Loading %d device interfaces for %s", len(deviceIds), info.APIEmail)
591-
md := metadata.New(map[string]string{
592-
"X-CH-Auth-Email": info.APIEmail,
593-
"X-CH-Auth-API-Token": info.APIToken,
594-
})
595-
ctxo := metadata.NewOutgoingContext(ctx, md)
596599

597-
lt := &interfacepb.ListInterfaceRequest{
598-
Filters: &interfacepb.InterfaceFilter{DeviceIds: deviceIds, Text: kt.LookupEnvString(KT_INTERFACE_LOOKUP_TEXT_FILTER, "")},
599-
}
600-
r, err := api.interfaceClient.ListInterface(ctxo, lt)
601-
if err != nil {
602-
if status.Code(err) == codes.Unimplemented {
603-
api.Warnf("Interface ListInterface endpoint not implemented (deprecated API); skipping.")
604-
return nil
605-
}
606-
return err
600+
api.Debugf("Loading interfaces for %v", deviceIds)
601+
lt := &interfacepb.ListInterfaceRequest{
602+
Filters: &interfacepb.InterfaceFilter{DeviceIds: deviceIds, Text: kt.LookupEnvString(KT_INTERFACE_LOOKUP_TEXT_FILTER, "")},
603+
}
604+
r, err := api.interfaceClient.ListInterface(ctx, lt)
605+
if err != nil {
606+
if status.Code(err) == codes.Unimplemented {
607+
api.Warnf("Interface ListInterface endpoint not implemented (deprecated API); skipping.")
608+
return nil
607609
}
610+
return err
611+
}
608612

609-
deviceIndex := make(map[kt.DeviceID]*kt.Device)
610-
for _, clist := range api.devices {
611-
for did, d := range clist {
612-
deviceIndex[did] = d
613-
}
613+
deviceIndex := make(map[kt.DeviceID]*kt.Device)
614+
for _, clist := range api.devices {
615+
for did, d := range clist {
616+
deviceIndex[did] = d
614617
}
618+
}
615619

616-
found := 0
617-
for _, intf := range r.GetInterfaces() {
618-
deviceID, err := strconv.ParseInt(intf.GetDeviceId(), 10, 64)
619-
if err != nil {
620-
continue
621-
}
622-
if dd, ok := deviceIndex[kt.DeviceID(deviceID)]; ok {
623-
dd.AddInterface(intf)
624-
found++
625-
}
620+
found := 0
621+
for _, intf := range r.GetInterfaces() {
622+
deviceID, err := strconv.ParseInt(intf.GetDeviceId(), 10, 64)
623+
if err != nil {
624+
continue
625+
}
626+
if dd, ok := deviceIndex[kt.DeviceID(deviceID)]; ok {
627+
dd.AddInterface(intf)
628+
found++
626629
}
627-
628-
api.Infof("Loaded %d Kentik Interfaces via GRPC for %s", found, info.APIEmail)
629630
}
631+
api.Debugf("Added %d interfaces for %d devices", found, len(deviceIds))
630632

631633
return nil
632634
}

0 commit comments

Comments
 (0)