Skip to content

Commit 161dfd3

Browse files
updating the sidecar config with local clusters (#437)
updating the sidecar config with local clusters
1 parent 9bdc74f commit 161dfd3

File tree

10 files changed

+1101
-28
lines changed

10 files changed

+1101
-28
lines changed

admiral/cmd/admiral/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ func GetRootCmd(args []string) *cobra.Command {
286286
rootCmd.PersistentFlags().StringToStringVarP(&params.VSRoutingInClusterDisabledResources, "vs_routing_in_cluster_disabled_resources", "d", map[string]string{}, "The source clusters and corresponding source identities to disable VS based routing in-cluster on")
287287
rootCmd.PersistentFlags().BoolVar(&params.EnableCustomVSMerge, "enable_custom_vs_merge", false, "Enable/Disable custom VS merge with in cluster VS")
288288
rootCmd.PersistentFlags().StringVar(&params.ProcessVSCreatedBy, "process_vs_created_by", "", "process the VS that was createdBy. Add createdBy label and value provided here for admiral to process this VS")
289+
rootCmd.PersistentFlags().BoolVar(&params.EnableSidecarCaching, "enable_sidecar_caching", false, "Enable/Disable sidecar caching")
290+
rootCmd.PersistentFlags().IntVar(&params.MaxSidecarEgressHostsLimitToCache, "max_sidecar_egress_hosts_limit_to_cache", 100, "This is the max sidecar egress hosts limit to cache. If the number of egress hosts exceeds this limit, then the sidecar will not be cached. This is to limit the memory consumption of the sidecar cache")
289291

290292
rootCmd.PersistentFlags().BoolVar(&params.EnableClientDiscovery, "enable_client_discovery", true, "Enable/Disable Client (mesh egress) Discovery")
291293
rootCmd.PersistentFlags().StringSliceVar(&params.ClientDiscoveryClustersForJobs, "client_discovery_clusters_for_jobs", []string{}, "List of clusters for client discovery for k8s jobs")

admiral/pkg/clusters/serviceentry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ func modifyServiceEntryForNewServiceOrPod(
913913
// Writing phase: We update the base in-cluster virtualservices with the RouteDestinations
914914
// gathered during the discovery phase and write them to the source cluster
915915
err = addUpdateInClusterVirtualServices(
916-
ctx, ctxLogger, remoteRegistry, sourceClusterToInClusterDestinations, cname, sourceIdentity, env)
916+
ctx, ctxLogger, remoteRegistry, sourceClusterToInClusterDestinations, cname, sourceIdentity, env, sourceClusterToEventNsCache)
917917
if err != nil {
918918
ctxLogger.Errorf(common.CtxLogFormat, "addUpdateInClusterVirtualServices",
919919
deploymentOrRolloutName, namespace, "", err)

admiral/pkg/clusters/virtualservice_routing.go

Lines changed: 142 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ import (
2828
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
)
3030

31+
type addUpdateSidecarFunc func(
32+
ctxLogger *log.Entry,
33+
ctx context.Context,
34+
newSidecarConfig *v1alpha3.Sidecar,
35+
cachedSidecar *v1alpha3.Sidecar,
36+
clientNamespace string,
37+
rc *RemoteController)
38+
3139
type envCustomVSTuple struct {
3240
env string
3341
customVS *v1alpha3.VirtualService
@@ -712,7 +720,8 @@ func addUpdateInClusterVirtualServices(
712720
sourceClusterToDestinations map[string]map[string][]*vsrouting.RouteDestination,
713721
cname string,
714722
sourceIdentity string,
715-
env string) error {
723+
env string,
724+
sourceClusterToEventNsCache map[string]string) error {
716725

717726
if sourceIdentity == "" {
718727
return fmt.Errorf("identity is empty")
@@ -748,6 +757,17 @@ func addUpdateInClusterVirtualServices(
748757
return err
749758
}
750759

760+
// Update the client's default sidecar with cluster local services
761+
// This is to make sure that the client proxies have the necessary .local clusters
762+
// available in its config
763+
err = updateClientSidecarWithClusterLocalServices(ctx,
764+
ctxLogger, rc, virtualService, sourceCluster, sourceClusterToEventNsCache, addUpdateSidecar)
765+
if err != nil {
766+
ctxLogger.Errorf(common.CtxLogFormat, "addUpdateInClusterVirtualServices",
767+
virtualService.Name, virtualService.Namespace, sourceCluster,
768+
fmt.Sprintf("updateClientSidecarWithClusterLocalServices failed due to %v", err.Error()))
769+
}
770+
751771
virtualServicesToBeProcessed := []*v1alpha3.VirtualService{virtualService}
752772

753773
// Merge the incluster vs with custom virtualservice, if enabled
@@ -838,6 +858,108 @@ func addUpdateInClusterVirtualServices(
838858
return nil
839859
}
840860

861+
func updateClientSidecarWithClusterLocalServices(
862+
ctx context.Context,
863+
ctxLogger *log.Entry,
864+
rc *RemoteController,
865+
vs *v1alpha3.VirtualService,
866+
sourceCluster string,
867+
sourceClusterToEventNsCache map[string]string,
868+
addUpdateSidecar addUpdateSidecarFunc) error {
869+
870+
if !common.IsSidecarCachingEnabled() {
871+
ctxLogger.Infof(common.CtxLogFormat, "updateClientSidecarWithClusterLocalServices",
872+
vs.Name, vs.Namespace, sourceCluster,
873+
"sidecar caching is disabled, skipping")
874+
return nil
875+
}
876+
877+
if rc == nil {
878+
return fmt.Errorf("remoteController is nil")
879+
}
880+
if rc.SidecarController == nil {
881+
return fmt.Errorf("sidecarController is nil")
882+
}
883+
if rc.SidecarController.SidecarCache == nil {
884+
return fmt.Errorf("sidecarCache is nil")
885+
}
886+
if vs == nil {
887+
return fmt.Errorf("virtualService is nil")
888+
}
889+
if sourceClusterToEventNsCache == nil {
890+
return fmt.Errorf("sourceClusterToEventNsCache is nil")
891+
}
892+
identityNamespace := sourceClusterToEventNsCache[sourceCluster]
893+
if identityNamespace == "" {
894+
return fmt.Errorf("identityNamespace is empty for sourceCluster %s", sourceCluster)
895+
}
896+
newHostToAddToSidecarEgress := fmt.Sprintf("%s/*.svc.cluster.local", identityNamespace)
897+
898+
exportToNamespaces := vs.Spec.ExportTo
899+
if exportToNamespaces == nil || len(exportToNamespaces) == 0 {
900+
return fmt.Errorf("exportToNamespaces is nil or empty for virtualService %s", vs.Name)
901+
}
902+
clientNamepaces := make([]string, 0)
903+
for _, namespace := range exportToNamespaces {
904+
if namespace == common.GetSyncNamespace() {
905+
ctxLogger.Infof(common.CtxLogFormat, "updateClientSidecarWithClusterLocalServices",
906+
vs.Name, vs.Namespace, sourceCluster, "virtualservice contains sync namespace, skipping update")
907+
return nil
908+
}
909+
// We skip the self namespace as the sidecar in the identity's namespace
910+
// already has ./* in its egress host
911+
if namespace == identityNamespace {
912+
continue
913+
}
914+
clientNamepaces = append(clientNamepaces, namespace)
915+
}
916+
if len(clientNamepaces) == 0 {
917+
ctxLogger.Infof(common.CtxLogFormat, "updateClientSidecarWithClusterLocalServices",
918+
vs.Name, vs.Namespace, sourceCluster,
919+
"no client namespaces found to update sidecar with cluster local services")
920+
return nil
921+
}
922+
923+
// For each client namespace, we will update the sidecar with the cluster local services
924+
for _, clientNamespace := range clientNamepaces {
925+
cachedSidecar := rc.SidecarController.SidecarCache.Get(common.GetWorkloadSidecarName(), clientNamespace)
926+
if cachedSidecar == nil {
927+
ctxLogger.Infof(common.CtxLogFormat, "updateClientSidecarWithClusterLocalServices",
928+
vs.Name, vs.Namespace, sourceCluster,
929+
fmt.Sprintf("skipped updating sidecar in namespace %s as it is missing in the cache", clientNamespace))
930+
continue
931+
}
932+
if cachedSidecar.Spec.Egress == nil || len(cachedSidecar.Spec.Egress) == 0 {
933+
ctxLogger.Infof(common.CtxLogFormat, "updateClientSidecarWithClusterLocalServices",
934+
vs.Name, vs.Namespace, sourceCluster,
935+
fmt.Sprintf("skipped updating sidecar in namespace %s as no egress found", clientNamespace))
936+
continue
937+
}
938+
cachedSidecarEgressHosts := cachedSidecar.Spec.Egress[0].Hosts
939+
if cachedSidecarEgressHosts == nil || len(cachedSidecarEgressHosts) == 0 {
940+
ctxLogger.Infof(common.CtxLogFormat, "updateClientSidecarWithClusterLocalServices",
941+
vs.Name, vs.Namespace, sourceCluster,
942+
fmt.Sprintf("skipped updating sidecar in namespace %s as no egress hosts found", clientNamespace))
943+
continue
944+
}
945+
lookup := make(map[string]bool)
946+
for _, egressHost := range cachedSidecarEgressHosts {
947+
lookup[egressHost] = true
948+
}
949+
if lookup[newHostToAddToSidecarEgress] {
950+
continue
951+
}
952+
newSidecar := copySidecar(cachedSidecar)
953+
newSidecar.Spec.Egress[0].Hosts = append(newSidecar.Spec.Egress[0].Hosts, newHostToAddToSidecarEgress)
954+
newSidecarConfig := createSidecarSkeleton(newSidecar.Spec, common.GetWorkloadSidecarName(), clientNamespace)
955+
956+
addUpdateSidecar(ctxLogger, ctx, newSidecarConfig, cachedSidecar, clientNamespace, rc)
957+
}
958+
959+
return nil
960+
961+
}
962+
841963
// shouldPerformDRPinning checks if the DR pinning to remote region is required
842964
// It checks if the identity is multi-region and if there is a GTP for the identity
843965
// If the identity is multi-region and has GTP in the NS, it returns true, else false
@@ -1961,7 +2083,8 @@ func addUpdateRoutingDestinationRule(
19612083
drName, util.IstioSystemNamespace, sourceCluster, "destinationrule created successfully")
19622084

19632085
rc.DestinationRuleController.Cache.Put(newDR)
1964-
2086+
log.Infof("op=%s type=%v cluster=%s length=%d",
2087+
"cacheLength", "DestinationRule", sourceCluster, rc.DestinationRuleController.Cache.Len())
19652088
}
19662089

19672090
return nil
@@ -2471,21 +2594,23 @@ func DoDRUpdateForInClusterVSRouting(
24712594
"", "", cluster, "remoteRegistry is nil")
24722595
return false
24732596
}
2474-
// Check if the incluster VS has valid exportTo namespaces (not sync namespace)
2475-
hasValidInClusterVS, err := hasInClusterVSWithValidExportToNS(se, remoteRegistry.GetRemoteController(cluster))
2476-
if err != nil {
2477-
ctxLogger.Warnf(common.CtxLogFormat, "DoDRUpdateForInClusterVSRouting",
2478-
identity, "", cluster, fmt.Sprintf("error checking for valid in-cluster VS %v", err))
2479-
return false
2480-
}
2481-
if !hasValidInClusterVS {
2482-
ctxLogger.Infof(common.CtxLogFormat, "DoDRUpdateForInClusterVSRouting",
2483-
identity, "", cluster, "skipping DR update as incluter VS does not have valid exportTo namespaces")
2484-
return false
2485-
}
2486-
if isSourceCluster &&
2487-
DoVSRoutingInClusterForClusterAndIdentity(ctx, ctxLogger, env, cluster, identity, remoteRegistry, performCartographerVSCheck) {
2488-
return true
2597+
if isSourceCluster {
2598+
// Check if the incluster VS has valid exportTo namespaces (not sync namespace)
2599+
hasValidInClusterVS, err := hasInClusterVSWithValidExportToNS(se, remoteRegistry.GetRemoteController(cluster))
2600+
if err != nil {
2601+
ctxLogger.Warnf(common.CtxLogFormat, "DoDRUpdateForInClusterVSRouting",
2602+
identity, "", cluster, fmt.Sprintf("error checking for valid in-cluster VS %v", err))
2603+
return false
2604+
}
2605+
if !hasValidInClusterVS {
2606+
ctxLogger.Infof(common.CtxLogFormat, "DoDRUpdateForInClusterVSRouting",
2607+
identity, "", cluster, "skipping DR update as incluter VS does not have valid exportTo namespaces")
2608+
return false
2609+
}
2610+
if DoVSRoutingInClusterForClusterAndIdentity(
2611+
ctx, ctxLogger, env, cluster, identity, remoteRegistry, performCartographerVSCheck) {
2612+
return true
2613+
}
24892614
}
24902615
return false
24912616
}

0 commit comments

Comments
 (0)