Skip to content

Commit

Permalink
Merge branch 'master' into 2213
Browse files Browse the repository at this point in the history
  • Loading branch information
arihantg authored Aug 12, 2024
2 parents 72f00bd + 4b33fd4 commit 57a95ff
Show file tree
Hide file tree
Showing 55 changed files with 1,237 additions and 720 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require (
github.com/openshift/api v3.9.0+incompatible
github.com/openshift/client-go v0.0.0-20201020082437-7737f16e53fc
github.com/prometheus/client_golang v1.17.0
github.com/vmware/alb-sdk v0.0.0-20240422063246-6f25c71c5791
github.com/vmware/load-balancer-and-ingress-services-for-kubernetes v0.0.0-20240426075151-cf4b379fde72
github.com/vmware/alb-sdk v0.0.0-20240619053936-3103500da639
github.com/vmware/load-balancer-and-ingress-services-for-kubernetes v0.0.0-20240621074433-756d124a591e
google.golang.org/protobuf v1.33.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v2 v2.4.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,10 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw=
github.com/vmware-tanzu/service-apis v0.0.0-20200901171416-461d35e58618 h1:ZLkxMxSr/YrYENjUJ8g6uB4rUQpZKBR3Olqf2DvuMFk=
github.com/vmware-tanzu/service-apis v0.0.0-20200901171416-461d35e58618/go.mod h1:afqpDk9He9v+/qWix0RRotm3RNyni4Lmc1y9geDCPuo=
github.com/vmware/alb-sdk v0.0.0-20240422063246-6f25c71c5791 h1:Qze7h4JtBPhRTRM+s2PxOF44grtjUMnfv4ELaWCmWsM=
github.com/vmware/alb-sdk v0.0.0-20240422063246-6f25c71c5791/go.mod h1:fuRb4saDY/xy/UMeMvyKYmcplNknEL9ysaqYSw7reNE=
github.com/vmware/load-balancer-and-ingress-services-for-kubernetes v0.0.0-20240426075151-cf4b379fde72 h1:IjR5q7PdWnMnUh7pHBnjoWbtAYWGFWb1D0HneIXj0Ho=
github.com/vmware/load-balancer-and-ingress-services-for-kubernetes v0.0.0-20240426075151-cf4b379fde72/go.mod h1:QMSAftQsMjzYomBl2+90VedAzZdyj6hzQAJKlJBwhyA=
github.com/vmware/alb-sdk v0.0.0-20240619053936-3103500da639 h1:UaTxFP5LeiSOlLEyHC9kmYuzdZWmvYpPB7jevg+ASNw=
github.com/vmware/alb-sdk v0.0.0-20240619053936-3103500da639/go.mod h1:fuRb4saDY/xy/UMeMvyKYmcplNknEL9ysaqYSw7reNE=
github.com/vmware/load-balancer-and-ingress-services-for-kubernetes v0.0.0-20240621074433-756d124a591e h1:9BXPQ9VrftHpLruVgL/MInIJ2JgOOmn3pDul20386bQ=
github.com/vmware/load-balancer-and-ingress-services-for-kubernetes v0.0.0-20240621074433-756d124a591e/go.mod h1:OYFWfOG6sHCDgYgMafq/Rcm3haP0q+9ZCUh0GGyVhCA=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand Down
12 changes: 7 additions & 5 deletions gslb/cache/avi_ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var aviClientInstance *utils.AviRestClientPool
var clientOnce sync.Once

// SharedAviClients initializes a pool of connections to the avi controller
func SharedAviClients() *utils.AviRestClientPool {
func SharedAviClients(tenant string) *utils.AviRestClientPool {
clientOnce.Do(func() {
var err error

Expand All @@ -44,7 +44,9 @@ func SharedAviClients() *utils.AviRestClientPool {
utils.AviLog.Fatal("AVI Controller information is missing, update them in kubernetes secret or via environment variable.")
}
os.Setenv("CTRL_VERSION", ctrlCfg.Version)
aviClientInstance, _, err = utils.NewAviRestClientPool(gslbutils.NumRestWorkers, ctrlCfg.IPAddr, ctrlCfg.Username, ctrlCfg.Password, "", ctrlCfg.Version, "")
userHeaders := utils.SharedCtrlProp().GetCtrlUserHeader()
apiScheme := utils.SharedCtrlProp().GetCtrlAPIScheme()
aviClientInstance, _, err = utils.NewAviRestClientPool(gslbutils.NumRestWorkers, ctrlCfg.IPAddr, ctrlCfg.Username, ctrlCfg.Password, "", ctrlCfg.Version, "", tenant, apiScheme, userHeaders)
if err != nil {
utils.AviLog.Errorf("AVI Controller Initialization failed, %s", err)
}
Expand All @@ -53,7 +55,7 @@ func SharedAviClients() *utils.AviRestClientPool {
}

func IsAviSiteLeader() (bool, error) {
aviRestClientPool := SharedAviClients()
aviRestClientPool := SharedAviClients("admin")
if len(aviRestClientPool.AviClient) < 1 {
gslbutils.Errf("no avi clients initialized, returning")
return false, errors.New("no avi clients initialized")
Expand Down Expand Up @@ -181,8 +183,8 @@ func GetGslbLeaderUuid(client *clients.AviClient) (string, error) {
return leaderUUID, nil
}

func GetHMFromName(name string, gdp bool) (*models.HealthMonitor, error) {
aviClient := SharedAviClients().AviClient[0]
func GetHMFromName(name string, gdp bool, tenant string) (*models.HealthMonitor, error) {
aviClient := SharedAviClients(tenant).AviClient[0]
uri := "api/healthmonitor?name=" + name

result, err := gslbutils.GetUriFromAvi(uri, aviClient, gdp)
Expand Down
135 changes: 77 additions & 58 deletions gslb/cache/controller_obj_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -161,7 +162,7 @@ func (h *AviHmCache) AviHmCachePopulate(client *clients.AviClient,

func (h *AviHmCache) AviHmObjCachePopulate(client *clients.AviClient, hmname ...string) error {
var nextPageURI string
uri := "/api/healthmonitor?page_size=100"
uri := "/api/healthmonitor?include_name&page_size=100"

matchCreatedBy := gslbutils.AMKOControlConfig().CreatedByField()

Expand Down Expand Up @@ -205,7 +206,7 @@ func (h *AviHmCache) AviHmObjCachePopulate(client *clients.AviClient, hmname ...
continue
}

k := TenantName{Tenant: gslbutils.GetTenant(), Name: *hm.Name}
k := TenantName{Tenant: getTenantFromTenantRef(*hm.TenantRef), Name: *hm.Name}
var monitorPort int32
if hm.MonitorPort != nil {
monitorPort = *hm.MonitorPort
Expand Down Expand Up @@ -234,7 +235,7 @@ func (h *AviHmCache) AviHmObjCachePopulate(client *clients.AviClient, hmname ...
cksum := gslbutils.GetGSLBHmChecksum(*hm.Type, monitorPort, []string{description}, createdBy)
hmCacheObj := AviHmObj{
Name: *hm.Name,
Tenant: gslbutils.GetTenant(),
Tenant: getTenantFromTenantRef(*hm.TenantRef),
UUID: *hm.UUID,
Port: monitorPort,
CloudConfigCksum: cksum,
Expand Down Expand Up @@ -308,7 +309,7 @@ func (s *AviPkiCache) AviPkiCacheGetByUUID(uuid string) (interface{}, bool) {
func (s *AviPkiCache) AviPkiCachePopulate(client *clients.AviClient) error {
var nextPageURI string
baseURI := "/api/pkiprofile"
uri := baseURI + "?page_size=100"
uri := baseURI + "?include_name&page_size=100"

// parse all pages with PKI Profile till we hit the last page
for {
Expand Down Expand Up @@ -342,7 +343,7 @@ func (s *AviPkiCache) AviPkiCachePopulate(client *clients.AviClient) error {
continue
}

k := TenantName{Tenant: gslbutils.GetTenant(), Name: *sp.Name}
k := TenantName{Tenant: getTenantFromTenantRef(*sp.TenantRef), Name: *sp.Name}
s.AviPkiCacheAdd(k, &sp)
s.AviPkiCacheAddByUUID(*sp.UUID, &sp)
gslbutils.Debugf("processed pki profile %s, UUID: %s", *sp.Name, *sp.UUID)
Expand Down Expand Up @@ -409,7 +410,7 @@ func (s *AviSpCache) AviSpCacheGetByUUID(uuid string) (interface{}, bool) {
func (s *AviSpCache) AviSitePersistenceCachePopulate(client *clients.AviClient) error {
var nextPageURI string
baseURI := "/api/applicationpersistenceprofile"
uri := baseURI + "?page_size=100"
uri := baseURI + "?include_name&page_size=100"

// parse all pages with Health monitors till we hit the last page
for {
Expand Down Expand Up @@ -443,7 +444,7 @@ func (s *AviSpCache) AviSitePersistenceCachePopulate(client *clients.AviClient)
continue
}

k := TenantName{Tenant: gslbutils.GetTenant(), Name: *sp.Name}
k := TenantName{Tenant: getTenantFromTenantRef(*sp.TenantRef), Name: *sp.Name}
s.AviSpCacheAdd(k, &sp)
s.AviSpCacheAddByUUID(*sp.UUID, &sp)
gslbutils.Debugf("processed site persistence %s, UUID: %s", *sp.Name, *sp.UUID)
Expand Down Expand Up @@ -547,7 +548,7 @@ func (c *AviCache) AviCacheDelete(k interface{}) {

func (c *AviCache) AviObjGSCachePopulate(client *clients.AviClient, gsname ...string) {
var nextPageURI string
uri := "/api/gslbservice?page_size=100"
uri := "/api/gslbservice?include_name&page_size=100"
createdBy := gslbutils.AmkoUser
createdByChanged := false

Expand All @@ -558,7 +559,7 @@ func (c *AviCache) AviObjGSCachePopulate(client *clients.AviClient, gsname ...st
// existing GSs with the new created_by field.
for {
if len(gsname) == 1 {
uri = "/api/gslbservice?name=" + gsname[0]
uri = "/api/gslbservice?include_name&name=" + gsname[0]
} else if nextPageURI != "" {
uri = nextPageURI
}
Expand Down Expand Up @@ -636,10 +637,10 @@ func parseGSObject(c *AviCache, gsObj models.GslbService, gsname []string) {
return
}
}
k := TenantName{Tenant: gslbutils.GetTenant(), Name: name}
k := TenantName{Tenant: getTenantFromTenantRef(*gsObj.TenantRef), Name: name}
gsCacheObj := AviGSCache{
Name: name,
Tenant: gslbutils.GetTenant(),
Tenant: getTenantFromTenantRef(*gsObj.TenantRef),
Uuid: uuid,
Members: gsMembers,
K8sObjects: memberObjs,
Expand Down Expand Up @@ -822,11 +823,7 @@ func GetDetailsFromAviGSLBFormatted(gsObj models.GslbService) (uint32, []GSMembe

hmRefs := gsObj.HealthMonitorRefs
for _, hmRef := range hmRefs {
hmRefSplit := strings.Split(hmRef, "/api/healthmonitor/")
if len(hmRefSplit) != 2 {
return 0, nil, memberObjs, nil, gsDownResponse, createdBy, errors.New("health monitor name is absent in health monitor ref: " + hmRefs[0])
}
hmUUID := hmRefSplit[1]
hmUUID := ExtractUuid(hmRef, "healthmonitor-.*.#")
hmCache := GetAviHmCache()
hmObjIntf, found := hmCache.AviHmCacheGetByUUID(hmUUID)
if !found {
Expand All @@ -845,50 +842,41 @@ func GetDetailsFromAviGSLBFormatted(gsObj models.GslbService) (uint32, []GSMembe
sitePersistenceRequired = *gsObj.SitePersistenceEnabled
if sitePersistenceRequired && gsObj.ApplicationPersistenceProfileRef != nil {
// find out the name of the profile
refSplit := strings.Split(*gsObj.ApplicationPersistenceProfileRef, "/applicationpersistenceprofile/")
if len(refSplit) == 2 {
spCache := GetAviSpCache()
sp, present := spCache.AviSpCacheGetByUUID(refSplit[1])
if present {
spObj, ok := sp.(*models.ApplicationPersistenceProfile)
if ok {
persistenceProfileRef = *spObj.Name
persistenceProfileRefPtr = &persistenceProfileRef
} else {
gslbutils.Warnf("gsName: %s, fetchedRef: %s, msg: stored site persistence not in right format",
*gsObj.Name, *gsObj.ApplicationPersistenceProfileRef)
}
refUUID := ExtractUuid(*gsObj.ApplicationPersistenceProfileRef, "applicationpersistenceprofile-.*.#")
spCache := GetAviSpCache()
sp, present := spCache.AviSpCacheGetByUUID(refUUID)
if present {
spObj, ok := sp.(*models.ApplicationPersistenceProfile)
if ok {
persistenceProfileRef = *spObj.Name
persistenceProfileRefPtr = &persistenceProfileRef
} else {
gslbutils.Warnf("gsName: %s, fetchedRef: %s, uuid: %s, msg: site persistence not present in cache by UUID",
*gsObj.Name, *gsObj.ApplicationPersistenceProfileRef, refSplit[1])
gslbutils.Warnf("gsName: %s, fetchedRef: %s, msg: stored site persistence not in right format",
*gsObj.Name, *gsObj.ApplicationPersistenceProfileRef)
}
} else {
gslbutils.Warnf("gsName: %s, fetchedRef: %s, msg: wrong format for site persistence ref", *gsObj.Name,
*gsObj.ApplicationPersistenceProfileRef)
gslbutils.Warnf("gsName: %s, fetchedRef: %s, uuid: %s, msg: site persistence not present in cache by UUID",
*gsObj.Name, *gsObj.ApplicationPersistenceProfileRef, refUUID)
}

}

if sitePersistenceRequired && gsObj.PkiProfileRef != nil {
// find out the name of the profile
refSplit := strings.Split(*gsObj.PkiProfileRef, "/pkiprofile/")
if len(refSplit) == 2 {
spCache := GetAviPkiCache()
sp, present := spCache.AviPkiCacheGetByUUID(refSplit[1])
if present {
spObj, ok := sp.(*models.PKIprofile)
if ok {
pkiProfileRef = spObj.Name
} else {
gslbutils.Warnf("gsName: %s, fetchedRef: %s, msg: stored pki profile not in right format",
*gsObj.Name, *gsObj.PkiProfileRef)
}
refUUID := ExtractUuid(*gsObj.PkiProfileRef, "pkiprofile-.*.#")
spCache := GetAviPkiCache()
sp, present := spCache.AviPkiCacheGetByUUID(refUUID)
if present {
spObj, ok := sp.(*models.PKIprofile)
if ok {
pkiProfileRef = spObj.Name
} else {
gslbutils.Warnf("gsName: %s, fetchedRef: %s, uuid: %s, msg: pki profile not present in cache by UUID",
*gsObj.Name, *gsObj.PkiProfileRef, refSplit[1])
gslbutils.Warnf("gsName: %s, fetchedRef: %s, msg: stored pki profile not in right format",
*gsObj.Name, *gsObj.PkiProfileRef)
}
} else {
gslbutils.Warnf("gsName: %s, fetchedRef: %s, msg: wrong format for pki profile ref", *gsObj.Name,
*gsObj.PkiProfileRef)
gslbutils.Warnf("gsName: %s, fetchedRef: %s, uuid: %s, msg: pki profile not present in cache by UUID",
*gsObj.Name, *gsObj.PkiProfileRef, refUUID)
}
}
ttl = gsObj.TTL
Expand Down Expand Up @@ -955,8 +943,8 @@ func GetDetailsFromAviGSLBFormatted(gsObj models.GslbService) (uint32, []GSMembe

// As name is encoded, retreiving information about the Hm becomes difficult
// Thats why we fetch Hm description for further processing
func GetHmDescriptionFromName(hmName string) string {
aviRestClientPool := SharedAviClients()
func GetHmDescriptionFromName(hmName, tenant string) string {
aviRestClientPool := SharedAviClients(tenant)
if len(aviRestClientPool.AviClient) < 1 {
return ""
}
Expand Down Expand Up @@ -1010,8 +998,8 @@ func GetGSNameFromHmName(hmName string) (string, error) {
// gen = 1 if the HM name is encoded and the gsname is fetched from its description
// gen = 2 if the HM name follows the old non encoded naming convention and gsname is fetched from hmname
// gen = 0 for error
func GetGSFromHmName(hmName string) (string, int8, error) {
hmDesc := GetHmDescriptionFromName(hmName)
func GetGSFromHmName(hmName, tenant string) (string, int8, error) {
hmDesc := GetHmDescriptionFromName(hmName, tenant)
hmDescriptionSplit := strings.Split(hmDesc, "gsname: ")
if len(hmDescriptionSplit) == 2 {
gsNameField := strings.Split(hmDescriptionSplit[1], ",")
Expand Down Expand Up @@ -1210,7 +1198,7 @@ type TenantName struct {
}

func PopulateGSCache(createSharedCache bool) *AviCache {
aviRestClientPool := SharedAviClients()
aviRestClientPool := SharedAviClients("*")
var aviObjCache *AviCache
if createSharedCache {
aviObjCache = GetAviCache()
Expand All @@ -1228,7 +1216,7 @@ func PopulateGSCache(createSharedCache bool) *AviCache {
}

func PopulateHMCache(createSharedCache bool) *AviHmCache {
aviRestClientPool := SharedAviClients()
aviRestClientPool := SharedAviClients("*")
var aviHmCache *AviHmCache
if createSharedCache {
aviHmCache = GetAviHmCache()
Expand All @@ -1245,7 +1233,7 @@ func PopulateHMCache(createSharedCache bool) *AviHmCache {
}

func PopulateSPCache() *AviSpCache {
aviRestClientPool := SharedAviClients()
aviRestClientPool := SharedAviClients("*")
aviSpCache := GetAviSpCache()
if len(aviRestClientPool.AviClient) > 0 {
aviSpCache.AviSitePersistenceCachePopulate(aviRestClientPool.AviClient[0])
Expand All @@ -1254,7 +1242,7 @@ func PopulateSPCache() *AviSpCache {
}

func PopulatePkiCache() *AviPkiCache {
aviRestClientPool := SharedAviClients()
aviRestClientPool := SharedAviClients("*")
aviSpCache := GetAviPkiCache()
if len(aviRestClientPool.AviClient) > 0 {
aviSpCache.AviPkiCachePopulate(aviRestClientPool.AviClient[0])
Expand All @@ -1266,7 +1254,7 @@ func VerifyVersion() error {
gslbutils.Logf("verifying the controller version")
version := gslbutils.GetAviConfig().Version

aviRestClientPool := SharedAviClients()
aviRestClientPool := SharedAviClients("*")
if len(aviRestClientPool.AviClient) < 1 {
gslbutils.Errf("no avi clients initialized, returning")
gslbutils.AMKOControlConfig().PodEventf(corev1.EventTypeWarning, gslbutils.AMKOShutdown, "No Avi clients initialized.")
Expand Down Expand Up @@ -1303,3 +1291,34 @@ func VerifyVersion() error {

return nil
}

func ExtractUuid(word, pattern string) string {
r, _ := regexp.Compile(pattern)
result := r.FindAllString(word, -1)
if len(result) == 1 {
return result[0][:len(result[0])-1]
}
utils.AviLog.Debugf("Uid extraction not successful from: %s, will retry without hash pattern", word)
return ExtractUuidWithoutHash(word, pattern[:len(pattern)-1])
}

func ExtractUuidWithoutHash(word, pattern string) string {
r, _ := regexp.Compile(pattern)
result := r.FindAllString(word, -1)
if len(result) == 1 {
return result[0][:len(result[0])]
}
return ""
}

func getTenantFromTenantRef(tenantRef string) string {
arr := strings.Split(tenantRef, "#")
if len(arr) == 2 {
return arr[1]
}
if len(arr) == 1 {
arr = strings.Split(tenantRef, "/")
return arr[len(arr)-1]
}
return tenantRef
}
6 changes: 3 additions & 3 deletions gslb/gslbutils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ const (
RejectedStore = "Rejected"

// Multi-cluster key lengths
IngMultiClusterKeyLen = 6
MultiClusterKeyLen = 5
IngMultiClusterKeyLen = 7
MultiClusterKeyLen = 6
GSFQDNKeyLen = 3
HostRuleKeyLen = 7
HostRuleKeyLen = 8

// Default values for Retry Operations
SlowSyncTime = 120
Expand Down
Loading

0 comments on commit 57a95ff

Please sign in to comment.