1- // Copyright 2018 Oracle and/or its affiliates. All rights reserved .
1+ // Copyright (C) 2018, 2025, Oracle and/or its affiliates.
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@ package client
1616
1717import (
1818 "context"
19+ "sync"
1920
2021 "go.uber.org/zap"
2122 "k8s.io/apimachinery/pkg/util/wait"
@@ -26,15 +27,30 @@ import (
2627)
2728
2829type networkLoadbalancer struct {
30+ nameToOcid sync.Map
2931 networkloadbalancer networkLoadBalancerClient
3032 requestMetadata common.RequestMetadata
3133 rateLimiter RateLimiter
3234}
3335
3436const (
3537 NetworkLoadBalancerEntityType = "NetworkLoadBalancer"
38+ // TODO move to utils?
39+ dns1123LabelFmt = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
40+ uuidFmt = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
41+ // <ns>/<svc>/<svc UID>
42+ LBNameRegex = "^" + dns1123LabelFmt + "/" + dns1123LabelFmt + "/" + uuidFmt + "$"
3643)
3744
45+ func NewNLBClient (nlb networkLoadBalancerClient , rm common.RequestMetadata , lim * RateLimiter ) * networkLoadbalancer {
46+ n := networkLoadbalancer {
47+ networkloadbalancer : nlb ,
48+ requestMetadata : rm ,
49+ rateLimiter : * lim ,
50+ }
51+ return & n
52+ }
53+
3854func (c * networkLoadbalancer ) GetLoadBalancer (ctx context.Context , id string ) (* GenericLoadBalancer , error ) {
3955 if ! c .rateLimiter .Reader .TryAccept () {
4056 return nil , RateLimitError (false , "GetLoadBalancer" )
@@ -54,6 +70,29 @@ func (c *networkLoadbalancer) GetLoadBalancer(ctx context.Context, id string) (*
5470}
5571
5672func (c * networkLoadbalancer ) GetLoadBalancerByName (ctx context.Context , compartmentID string , name string ) (* GenericLoadBalancer , error ) {
73+ logger := zap .L ().Sugar () // TODO refactor after pull-requests/1389
74+ logger = logger .With ("lbName" , name ,
75+ "compartment-id" , compartmentID ,
76+ "loadBalancerType" , "nlb" ,
77+ )
78+
79+ if ocid , ok := c .nameToOcid .Load (name ); ok {
80+ var err error
81+ ocidStr , ok := ocid .(string )
82+ if ok {
83+ lb , err := c .GetLoadBalancer (ctx , ocidStr )
84+ if err == nil && * lb .DisplayName == name {
85+ return lb , err
86+ }
87+ }
88+
89+ if ! ok || IsNotFound (err ) { // Only remove the cached value on 404, not on a 5XX
90+ c .nameToOcid .Delete (name )
91+ }
92+ } else {
93+ logger .Info ("NLB name to OCID cache miss" )
94+ }
95+
5796 var page * string
5897 for {
5998 if ! c .rateLimiter .Reader .TryAccept () {
@@ -72,6 +111,7 @@ func (c *networkLoadbalancer) GetLoadBalancerByName(ctx context.Context, compart
72111 }
73112 for _ , lb := range resp .Items {
74113 if * lb .DisplayName == name {
114+ c .nameToOcid .Store (name , * lb .Id )
75115 return c .networkLoadbalancerSummaryToGenericLoadbalancer (& lb ), nil
76116 }
77117 }
0 commit comments