Skip to content

Commit bd5f3d6

Browse files
refactored DR pinning code (#431)
1 parent d665125 commit bd5f3d6

13 files changed

+1589
-676
lines changed

admiral/pkg/clusters/destinationrule_handler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/admiral"
2121
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
2222
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/util"
23-
"github.com/sirupsen/logrus"
2423
log "github.com/sirupsen/logrus"
2524
"istio.io/client-go/pkg/apis/networking/v1alpha3"
2625
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
@@ -44,7 +43,7 @@ func getDestinationRule(se *networkingV1Alpha3.ServiceEntry,
4443
clientConnectionSettings *v1.ClientConnectionConfig,
4544
currentDR *v1alpha3.DestinationRule,
4645
eventResourceType string,
47-
ctxLogger *logrus.Entry,
46+
ctxLogger *log.Entry,
4847
event admiral.EventType,
4948
doDRUpdateForInClusterRouting bool) *networkingV1Alpha3.DestinationRule {
5049

admiral/pkg/clusters/destinationrule_handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
"github.com/golang/protobuf/ptypes/duration"
1818
"github.com/golang/protobuf/ptypes/wrappers"
19-
cmp "github.com/google/go-cmp/cmp"
19+
"github.com/google/go-cmp/cmp"
2020
"github.com/istio-ecosystem/admiral/admiral/pkg/apis/admiral/model"
2121
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
2222
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/istio"

admiral/pkg/clusters/serviceentry.go

Lines changed: 87 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,8 @@ func modifyServiceEntryForNewServiceOrPod(
925925
remoteRegistry,
926926
sourceClusterToDRHosts,
927927
sourceIdentity,
928-
cname)
928+
cname,
929+
env)
929930
if err != nil {
930931
ctxLogger.Errorf(common.CtxLogFormat, "addUpdateInClusterDestinationRule",
931932
deploymentOrRolloutName, namespace, "", err)
@@ -1104,6 +1105,12 @@ func getAllVirtualServices(
11041105
rc *RemoteController,
11051106
namespace string,
11061107
listOptions v12.ListOptions) (*v1alpha3.VirtualServiceList, error) {
1108+
if rc == nil {
1109+
return nil, fmt.Errorf("remote controller is not initialized")
1110+
}
1111+
if rc.VirtualServiceController == nil || rc.VirtualServiceController.IstioClient == nil {
1112+
return nil, fmt.Errorf("virtualservice controller is not initialized")
1113+
}
11071114
virtualServicesList, err := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(namespace).List(ctx, listOptions)
11081115
if err != nil && k8sErrors.IsNotFound(err) {
11091116
ctxLogger.Debugf(LogFormat, "list", common.VirtualServiceResourceType, "", rc.ClusterID, "virtualservices not found")
@@ -1564,74 +1571,13 @@ func AddServiceEntriesWithDrWorker(
15641571
currentDR := getCurrentDRForLocalityLbSetting(rr, isServiceEntryModifyCalledForSourceCluster, cluster, se, partitionedIdentity)
15651572
ctxLogger.Infof("currentDR set for dr=%v cluster=%v", getIstioResourceName(se.Hosts[0], "-default-dr"), cluster)
15661573

1567-
doDRUpdateForInClusterVSRouting := common.DoDRUpdateForInClusterVSRouting(
1568-
cluster, identityId, isServiceEntryModifyCalledForSourceCluster)
1569-
1570-
// The below code checks if the in-cluster VS's exportTo has valid namespaces
1571-
// This is needed so that we don't prematurely pin the region until the
1572-
// in-cluster vs is enabled or updated with valid exportTo namespaces.
1573-
if doDRUpdateForInClusterVSRouting {
1574-
doDRUpdateForInClusterVSRouting, err = hasInClusterVSWithValidExportToNS(se, rc)
1575-
if err != nil {
1576-
ctxLogger.Errorf(
1577-
common.CtxLogFormat, "AddServiceEntriesWithDrWorker", "", "",
1578-
cluster,
1579-
fmt.Sprintf(
1580-
"hasInClusterVSWithValidExportToNS: failed with error for Identity: %s err: %v",
1581-
identityId, err.Error()))
1582-
doDRUpdateForInClusterVSRouting = false
1583-
}
1584-
if !doDRUpdateForInClusterVSRouting {
1585-
ctxLogger.Infof(
1586-
common.CtxLogFormat, "AddServiceEntriesWithDrWorker", "", "",
1587-
cluster,
1588-
fmt.Sprintf(
1589-
"VSRoutingInClusterEnabled: %v for cluster: %s and Identity: %s as corresponding in-cluster vs still contains sync ns",
1590-
doDRUpdateForInClusterVSRouting, cluster, identityId))
1591-
}
1592-
}
1574+
doDRUpdateForInClusterVSRouting := DoDRUpdateForInClusterVSRouting(
1575+
ctx, ctxLogger, env, cluster, identityId, isServiceEntryModifyCalledForSourceCluster, rr, se)
15931576

1594-
// The below code checks if there is custom VS in the identity's namespace
1595-
// Any Argo VS will be ignored as they are not added to the IdentityNamespaceVirtualServiceCache
1596-
if doDRUpdateForInClusterVSRouting {
1597-
if eventNamespace != "" {
1598-
virtualServicesInIdentityNamespace := rc.
1599-
VirtualServiceController.IdentityNamespaceVirtualServiceCache.Get(eventNamespace)
1600-
if virtualServicesInIdentityNamespace != nil && len(virtualServicesInIdentityNamespace) > 0 {
1601-
doDRUpdateForInClusterVSRouting = false
1602-
ctxLogger.Infof(
1603-
common.CtxLogFormat, "AddServiceEntriesWithDrWorker", "", "",
1604-
cluster,
1605-
fmt.Sprintf(
1606-
"VSRoutingInClusterEnabled: %v for cluster: %s and Identity: %s as there are custom virtualServices in the namespace %v",
1607-
doDRUpdateForInClusterVSRouting, cluster, identityId, eventNamespace))
1608-
}
1609-
}
1610-
}
1611-
1612-
// This code has been added for custom VS to in-cluster VS migration
1613-
// We are preventing pinning to remote cluster until the custom VS's
1614-
// exportTo is set to dot.
1615-
if doDRUpdateForInClusterVSRouting {
1616-
doDRUpdateForInClusterVSRouting, err = DoDRPinning(
1617-
ctx, ctxLogger, rc, env, identityId, getCustomVirtualService)
1618-
if err != nil {
1619-
doDRUpdateForInClusterVSRouting = false
1620-
ctxLogger.Errorf(common.CtxLogFormat, "DoDRPinning", identityId, syncNamespace, cluster,
1621-
fmt.Sprintf("failed for identity %s and env %s due to error=%v", identityId, env, err))
1622-
} else {
1623-
if !doDRUpdateForInClusterVSRouting {
1624-
ctxLogger.Infof(
1625-
common.CtxLogFormat, "AddServiceEntriesWithDrWorker", "", "",
1626-
cluster,
1627-
fmt.Sprintf("VSRoutingInClusterEnabled: %v for cluster: %s and Identity: %s as custom VS exportTo is not dot",
1628-
doDRUpdateForInClusterVSRouting, cluster, identityId))
1629-
}
1630-
}
1631-
1632-
}
1633-
1634-
ctxLogger.Infof(common.CtxLogFormat, "AddServiceEntriesWithDrWorker", "", "", cluster, fmt.Sprintf("VSRoutingInClusterEnabled: %v for cluster: %s and Identity: %s", doDRUpdateForInClusterVSRouting, cluster, identityId))
1577+
ctxLogger.Infof(
1578+
common.CtxLogFormat, "AddServiceEntriesWithDrWorker", "", "", cluster,
1579+
fmt.Sprintf("VSRoutingInClusterEnabled: %v for cluster: %s and Identity: %s",
1580+
doDRUpdateForInClusterVSRouting, cluster, identityId))
16351581
var seDrSet, clientNamespaces = createSeAndDrSetFromGtp(ctxLogger, ctx, env, region, cluster, se,
16361582
globalTrafficPolicy, outlierDetection, clientConnectionSettings, cache, currentDR, doDRUpdateForInClusterVSRouting)
16371583
util.LogElapsedTimeSinceTask(ctxLogger, "AdmiralCacheCreateSeAndDrSetFromGtp", "", "", cluster, "", start)
@@ -1890,10 +1836,73 @@ func AddServiceEntriesWithDrWorker(
18901836
}
18911837
}
18921838

1893-
// DoDRPinning has been added for custom VS to in-cluster VS migration
1894-
// We are preventing pinning to remote cluster until the custom VS's
1895-
// exportTo is set to dot.
1896-
func DoDRPinning(
1839+
// DoesIdentityHaveVS checks if the identity has any virtual services in its namespace
1840+
// It iterates through all the clusters for the identity
1841+
// and check in the IdentityVirtualServiceCache if there is a corresponding custom VS
1842+
// If it finds any, it returns true, otherwise false
1843+
func DoesIdentityHaveVS(
1844+
remoteRegistry *RemoteRegistry,
1845+
identityId string) (bool, error) {
1846+
1847+
if remoteRegistry == nil {
1848+
return false, fmt.Errorf("remoteRegistry is nil")
1849+
}
1850+
if remoteRegistry.AdmiralCache == nil {
1851+
return false, fmt.Errorf("AdmiralCache is nil in remoteRegistry")
1852+
}
1853+
if remoteRegistry.AdmiralCache.IdentityClusterCache == nil {
1854+
return false, fmt.Errorf("IdentityClusterCache is nil in AdmiralCache")
1855+
}
1856+
1857+
identityClustersMap := remoteRegistry.AdmiralCache.IdentityClusterCache.Get(identityId)
1858+
if identityClustersMap == nil {
1859+
return false, fmt.Errorf("identityClustersMap is nil for identity %s", identityId)
1860+
}
1861+
identityClusters := identityClustersMap.GetValues()
1862+
if len(identityClusters) == 0 {
1863+
return false, fmt.Errorf("no clusters found for identity %s", identityId)
1864+
}
1865+
for _, identityCluster := range identityClusters {
1866+
remoteController := remoteRegistry.GetRemoteController(identityCluster)
1867+
if remoteController == nil {
1868+
return false, fmt.Errorf("remoteController is nil for cluster %s", identityCluster)
1869+
}
1870+
if remoteController.VirtualServiceController == nil {
1871+
return false, fmt.Errorf("VirtualServiceController is nil for cluster %s", identityCluster)
1872+
}
1873+
if remoteController.VirtualServiceController.IdentityVirtualServiceCache == nil {
1874+
return false, fmt.Errorf("IdentityVirtualServiceCache is nil for cluster %s", identityCluster)
1875+
}
1876+
virtualServicesInIdentityNamespace := remoteController.
1877+
VirtualServiceController.IdentityVirtualServiceCache.Get(identityId)
1878+
if virtualServicesInIdentityNamespace == nil || len(virtualServicesInIdentityNamespace) == 0 {
1879+
continue
1880+
}
1881+
// Check if the VS in the namespace is an Argo VS
1882+
if common.GetArgoRolloutsEnabled() &&
1883+
remoteController.RolloutController != nil &&
1884+
remoteController.RolloutController.IdentityArgoVSCache != nil {
1885+
argoVSInIdentityNamespace := remoteController.RolloutController.IdentityArgoVSCache.Get(identityId)
1886+
if argoVSInIdentityNamespace == nil || len(argoVSInIdentityNamespace) == 0 {
1887+
return true, nil
1888+
}
1889+
for vsName := range virtualServicesInIdentityNamespace {
1890+
if _, ok := argoVSInIdentityNamespace[vsName]; !ok {
1891+
return true, nil
1892+
}
1893+
}
1894+
} else {
1895+
// If Argo Rollouts is not enabled, then all VS in the namespace are considered custom VS
1896+
return true, nil
1897+
}
1898+
}
1899+
return false, nil
1900+
}
1901+
1902+
// IsCartographerVSDisabled has been added for cartographer VS to in-cluster VS migration
1903+
// It checks if the cartographer virtual service for the given identity and environment
1904+
// has exportTo set to dot. If it does, it returns true, otherwise false.
1905+
func IsCartographerVSDisabled(
18971906
ctx context.Context,
18981907
ctxLogger *logrus.Entry,
18991908
rc *RemoteController,
@@ -1950,6 +1959,12 @@ func hasInClusterVSWithValidExportToNS(se *networking.ServiceEntry, rc *RemoteCo
19501959
if len(se.Hosts) != 1 {
19511960
return false, fmt.Errorf("serviceEntry has more than one host")
19521961
}
1962+
if rc.VirtualServiceController == nil {
1963+
return false, fmt.Errorf("VirtualServiceController is nil in remoteController")
1964+
}
1965+
if rc.VirtualServiceController.VirtualServiceCache == nil {
1966+
return false, fmt.Errorf("VirtualServiceCache is nil in VirtualServiceController")
1967+
}
19531968
vsName := fmt.Sprintf("%s-%s", se.Hosts[0], common.InclusterVSNameSuffix)
19541969
cachedVS := rc.VirtualServiceController.VirtualServiceCache.Get(vsName)
19551970
if cachedVS == nil {

0 commit comments

Comments
 (0)