From 5c34cb69808a15b85df30f14a8a902fa360a693c Mon Sep 17 00:00:00 2001 From: nbn01 Date: Mon, 7 Nov 2022 21:39:56 +0530 Subject: [PATCH] fix: Handle VS hosts nil --- admiral/pkg/clusters/handler.go | 66 +++++++++++++++++---------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/admiral/pkg/clusters/handler.go b/admiral/pkg/clusters/handler.go index a2bf4e70..6bafd72c 100644 --- a/admiral/pkg/clusters/handler.go +++ b/admiral/pkg/clusters/handler.go @@ -418,49 +418,51 @@ func handleVirtualServiceEvent( } } - dependentClusters := r.AdmiralCache.CnameDependentClusterCache.Get(virtualService.Hosts[0]).Copy() - if len(dependentClusters) > 0 { - for _, dependentCluster := range dependentClusters { - rc := r.GetRemoteController(dependentCluster) - if clusterId != dependentCluster { - log.Infof(LogFormat, "Event", "VirtualService", obj.Name, clusterId, "Processing") - if event == common.Delete { - err := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(syncNamespace).Delete(ctx, obj.Name, v12.DeleteOptions{}) - if err != nil { - if k8sErrors.IsNotFound(err) { - log.Infof(LogFormat, "Delete", "VirtualService", obj.Name, clusterId, "Either VirtualService was already deleted, or it never existed") + if virtualService.Hosts != nil { + dependentClusters := r.AdmiralCache.CnameDependentClusterCache.Get(virtualService.Hosts[0]).Copy() + if len(dependentClusters) > 0 { + for _, dependentCluster := range dependentClusters { + rc := r.GetRemoteController(dependentCluster) + if clusterId != dependentCluster { + log.Infof(LogFormat, "Event", "VirtualService", obj.Name, clusterId, "Processing") + if event == common.Delete { + err := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(syncNamespace).Delete(ctx, obj.Name, v12.DeleteOptions{}) + if err != nil { + if k8sErrors.IsNotFound(err) { + log.Infof(LogFormat, "Delete", "VirtualService", obj.Name, clusterId, "Either VirtualService was already deleted, or it never existed") + } else { + log.Errorf(LogErrFormat, "Delete", "VirtualService", obj.Name, clusterId, err) + } } else { - log.Errorf(LogErrFormat, "Delete", "VirtualService", obj.Name, clusterId, err) + log.Infof(LogFormat, "Delete", "VirtualService", obj.Name, clusterId, "Success") } } else { - log.Infof(LogFormat, "Delete", "VirtualService", obj.Name, clusterId, "Success") - } - } else { - exist, _ := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(syncNamespace).Get(ctx, obj.Name, v12.GetOptions{}) - //change destination host for all http routes .. to same as host on the virtual service - for _, httpRoute := range virtualService.Http { - for _, destination := range httpRoute.Route { - //get at index 0, we do not support wildcards or multiple hosts currently - if strings.HasSuffix(destination.Destination.Host, common.DotLocalDomainSuffix) { - destination.Destination.Host = virtualService.Hosts[0] + exist, _ := rc.VirtualServiceController.IstioClient.NetworkingV1alpha3().VirtualServices(syncNamespace).Get(ctx, obj.Name, v12.GetOptions{}) + //change destination host for all http routes .. to same as host on the virtual service + for _, httpRoute := range virtualService.Http { + for _, destination := range httpRoute.Route { + //get at index 0, we do not support wildcards or multiple hosts currently + if strings.HasSuffix(destination.Destination.Host, common.DotLocalDomainSuffix) { + destination.Destination.Host = virtualService.Hosts[0] + } } } - } - for _, tlsRoute := range virtualService.Tls { - for _, destination := range tlsRoute.Route { - //get at index 0, we do not support wildcards or multiple hosts currently - if strings.HasSuffix(destination.Destination.Host, common.DotLocalDomainSuffix) { - destination.Destination.Host = virtualService.Hosts[0] + for _, tlsRoute := range virtualService.Tls { + for _, destination := range tlsRoute.Route { + //get at index 0, we do not support wildcards or multiple hosts currently + if strings.HasSuffix(destination.Destination.Host, common.DotLocalDomainSuffix) { + destination.Destination.Host = virtualService.Hosts[0] + } } } + addUpdateVirtualService(ctx, obj, exist, syncNamespace, rc) } - addUpdateVirtualService(ctx, obj, exist, syncNamespace, rc) } } + return nil + } else { + log.Infof(LogFormat, "Event", "VirtualService", obj.Name, clusterId, "No dependent clusters found") } - return nil - } else { - log.Infof(LogFormat, "Event", "VirtualService", obj.Name, clusterId, "No dependent clusters found") } // copy the VirtualService `as is` if they are not generated by Admiral (not in CnameDependentClusterCache)