Skip to content

Commit 1654c13

Browse files
committed
fix: add DestinationRules handling to ListServiceMeshHosts
1 parent a3cc1b0 commit 1654c13

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

pkg/istio/istio.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,6 @@ func (i *Istio) ListServiceMeshHosts(ctx context.Context, namespace string) (str
713713
}
714714

715715
for _, svc := range servicesList.Items {
716-
// // Skip system services that are not mesh-relevant
717-
// if svc.Namespace == "kube-system" || svc.Namespace == "kube-public" || svc.Namespace == "kube-node-lease" {
718-
// continue
719-
// }
720716
hostName := svc.Name
721717
k8sServiceHosts = append(k8sServiceHosts, hostName)
722718
k8sServiceDetails[hostName] = svc.Namespace
@@ -745,11 +741,29 @@ func (i *Istio) ListServiceMeshHosts(ctx context.Context, namespace string) (str
745741
}
746742
}
747743

744+
// Collect DestinationRule hosts
745+
var destinationRuleHosts []string
746+
var destinationRuleDetails = make(map[string]string)
747+
748+
// Get DestinationRules from the specified namespace
749+
drList, err := i.istioClient.NetworkingV1alpha3().DestinationRules(namespace).List(ctx, metav1.ListOptions{})
750+
if err != nil {
751+
return "", fmt.Errorf("failed to list destination rules: %w", err)
752+
}
753+
754+
for _, dr := range drList.Items {
755+
if dr.Spec.Host != "" {
756+
destinationRuleHosts = append(destinationRuleHosts, dr.Spec.Host)
757+
destinationRuleDetails[dr.Spec.Host] = dr.Name
758+
}
759+
}
760+
748761
// Sort all slices for consistent output
749762
sort.Strings(serviceEntryHosts)
750763
sort.Strings(k8sServiceHosts)
751764
sort.Strings(virtualServiceHosts)
752765
sort.Strings(wildcardHosts)
766+
sort.Strings(destinationRuleHosts)
753767

754768
// Build ServiceEntry section
755769
result += "ServiceEntries (External to Mesh Services):\n"
@@ -790,10 +804,22 @@ func (i *Istio) ListServiceMeshHosts(ctx context.Context, namespace string) (str
790804
}
791805
result += "\n"
792806

807+
// Build DestinationRule section
808+
result += "DestinationRule Hosts:\n"
809+
if len(destinationRuleHosts) == 0 {
810+
result += " (none found)\n"
811+
} else {
812+
for _, host := range destinationRuleHosts {
813+
result += fmt.Sprintf(" %-30s (DestinationRule: %s)\n", host, destinationRuleDetails[host])
814+
}
815+
}
816+
result += "\n"
817+
793818
// Build summary
794819
result += "Summary:\n"
795820
result += fmt.Sprintf("- %d ServiceEntries\n", len(serviceEntryHosts))
796821
result += fmt.Sprintf("- %d Kubernetes Services\n", len(k8sServiceHosts))
797822
result += fmt.Sprintf("- %d VirtualService Configurations\n", len(virtualServiceHosts))
823+
result += fmt.Sprintf("- %d DestinationRule Configurations\n", len(destinationRuleHosts))
798824
return result, nil
799825
}

pkg/istio/service_mesh_hosts_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ func TestListServiceMeshHosts(t *testing.T) {
132132
}
133133
})
134134

135+
t.Run("ListServiceMeshHosts shows DestinationRule hosts", func(t *testing.T) {
136+
result, err := istio.ListServiceMeshHosts(ctx, "bookinfo")
137+
if err != nil {
138+
t.Fatalf("Failed to list service mesh hosts: %v", err)
139+
}
140+
141+
// Should contain DestinationRule section
142+
if !strings.Contains(result, "DestinationRule Hosts:") {
143+
t.Errorf("Expected result to contain 'DestinationRule Hosts:' section")
144+
}
145+
146+
// Should contain DestinationRule host from bookinfo namespace
147+
if !strings.Contains(result, "productpage") {
148+
t.Errorf("Expected result to contain DestinationRule host 'productpage'")
149+
}
150+
151+
// Should contain DestinationRule name
152+
if !strings.Contains(result, "productpage-dr") {
153+
t.Errorf("Expected result to contain DestinationRule name 'productpage-dr'")
154+
}
155+
})
156+
135157
t.Run("ListServiceMeshHosts provides accurate summary", func(t *testing.T) {
136158
result, err := istio.ListServiceMeshHosts(ctx, "production")
137159
if err != nil {
@@ -143,6 +165,7 @@ func TestListServiceMeshHosts(t *testing.T) {
143165
"ServiceEntries",
144166
"Kubernetes Services",
145167
"VirtualService Configurations",
168+
"DestinationRule Configurations",
146169
}
147170

148171
for _, count := range expectedCounts {

0 commit comments

Comments
 (0)