Add cache for network security groups

This commit is contained in:
Pengfei Ni 2018-02-07 21:57:11 +08:00
parent d22b6d9ebe
commit 21c8a63689
5 changed files with 52 additions and 6 deletions

View File

@ -135,8 +135,9 @@ type Cloud struct {
VirtualMachineScaleSetsClient VirtualMachineScaleSetsClient
VirtualMachineScaleSetVMsClient VirtualMachineScaleSetVMsClient
vmCache *timedCache
lbCache *timedCache
vmCache *timedCache
lbCache *timedCache
nsgCache *timedCache
*BlobDiskController
*ManagedDiskController
@ -259,6 +260,12 @@ func NewCloud(configReader io.Reader) (cloudprovider.Interface, error) {
}
az.lbCache = lbCache
nsgCache, err := az.newNSGCache()
if err != nil {
return nil, err
}
az.nsgCache = nsgCache
if err := initDiskControllers(&az); err != nil {
return nil, err
}

View File

@ -131,7 +131,12 @@ func (az *Cloud) CreateOrUpdateSGWithRetry(sg network.SecurityGroup) error {
resp := <-respChan
err := <-errChan
glog.V(10).Infof("SecurityGroupsClient.CreateOrUpdate(%s): end", *sg.Name)
return processRetryResponse(resp.Response, err)
done, err := processRetryResponse(resp.Response, err)
if done && err == nil {
// Invalidate the cache right after updating
az.lbCache.Delete(*sg.Name)
}
return done, err
})
}

View File

@ -819,7 +819,7 @@ func (az *Cloud) reconcileSecurityGroup(clusterName string, service *v1.Service,
ports = []v1.ServicePort{}
}
sg, err := az.SecurityGroupsClient.Get(az.ResourceGroup, az.SecurityGroupName, "")
sg, err := az.getSecurityGroup()
if err != nil {
return nil, err
}

View File

@ -880,6 +880,7 @@ func getTestCloud() (az *Cloud) {
az.vmSet = newAvailabilitySet(az)
az.vmCache, _ = az.newVMCache()
az.lbCache, _ = az.newLBCache()
az.nsgCache, _ = az.newNSGCache()
return az
}

View File

@ -17,6 +17,7 @@ limitations under the License.
package azure
import (
"fmt"
"net/http"
"time"
@ -29,8 +30,9 @@ import (
)
var (
vmCacheTTL = time.Minute
lbCacheTTL = 2 * time.Minute
vmCacheTTL = time.Minute
lbCacheTTL = 2 * time.Minute
nsgCacheTTL = 2 * time.Minute
)
// checkExistsFromError inspects an error and returns a true if err is nil,
@ -152,6 +154,19 @@ func (az *Cloud) getAzureLoadBalancer(name string) (lb network.LoadBalancer, exi
return *(cachedLB.(*network.LoadBalancer)), true, nil
}
func (az *Cloud) getSecurityGroup() (nsg network.SecurityGroup, err error) {
securityGroup, err := az.nsgCache.Get(az.SecurityGroupName)
if err != nil {
return nsg, err
}
if securityGroup == nil {
return nsg, fmt.Errorf("nsg %q not found", az.SecurityGroupName)
}
return *(securityGroup.(*network.SecurityGroup)), nil
}
func (az *Cloud) newVMCache() (*timedCache, error) {
getter := func(key string) (interface{}, error) {
vm, err := az.VirtualMachinesClient.Get(az.ResourceGroup, key, compute.InstanceView)
@ -187,3 +202,21 @@ func (az *Cloud) newLBCache() (*timedCache, error) {
return newTimedcache(lbCacheTTL, getter)
}
func (az *Cloud) newNSGCache() (*timedCache, error) {
getter := func(key string) (interface{}, error) {
nsg, err := az.SecurityGroupsClient.Get(az.ResourceGroup, key, "")
exists, realErr := checkResourceExistsFromError(err)
if realErr != nil {
return nil, realErr
}
if !exists {
return nil, nil
}
return &nsg, nil
}
return newTimedcache(nsgCacheTTL, getter)
}