Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle VS hosts nil #265

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions admiral/pkg/clusters/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its better to check the length of the slice, because if an empty []string is assigned to virtualService.Hosts then it will not be nil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, Right!! I've updated

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 <service_name>.<ns>. 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 <service_name>.<ns>. 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)
Expand Down