diff --git a/agent/ui_endpoint.go b/agent/ui_endpoint.go index cfe82941ec..d550711cfc 100644 --- a/agent/ui_endpoint.go +++ b/agent/ui_endpoint.go @@ -10,16 +10,22 @@ import ( "github.com/hashicorp/consul/api" ) +// metaExternalSource is the key name for the service instance meta that +// defines the external syncing source. This is used by the UI APIs below +// to extract this. +const metaExternalSource = "external-source" + // ServiceSummary is used to summarize a service type ServiceSummary struct { - Kind structs.ServiceKind `json:",omitempty"` - Name string - Tags []string - Meta map[string]string - Nodes []string - ChecksPassing int - ChecksWarning int - ChecksCritical int + Kind structs.ServiceKind `json:",omitempty"` + Name string + Tags []string + Nodes []string + ChecksPassing int + ChecksWarning int + ChecksCritical int + ExternalSources []string + externalSourceSet map[string]struct{} // internal to track uniqueness } // UINodes is used to list the nodes in a given datacenter. We return a @@ -154,14 +160,18 @@ func summarizeServices(dump structs.NodeDump) []*ServiceSummary { sum.Nodes = append(sum.Nodes, node.Node) sum.Kind = service.Kind - // The service meta is per instance, but we aggregate it - // here for the UI to know it exists for _some_ instance. - if len(service.Meta) > 0 { - if len(sum.Meta) == 0 { - sum.Meta = make(map[string]string) + // If there is an external source, add it to the list of external + // sources. We only want to add unique sources so there is extra + // accounting here with an unexported field to maintain the set + // of sources. + if len(service.Meta) > 0 && service.Meta[metaExternalSource] != "" { + source := service.Meta[metaExternalSource] + if sum.externalSourceSet == nil { + sum.externalSourceSet = make(map[string]struct{}) } - for k, v := range service.Meta { - sum.Meta[k] = v + if _, ok := sum.externalSourceSet[source]; !ok { + sum.externalSourceSet[source] = struct{}{} + sum.ExternalSources = append(sum.ExternalSources, source) } } diff --git a/agent/ui_endpoint_test.go b/agent/ui_endpoint_test.go index 596e1999b7..fdb4f65e7b 100644 --- a/agent/ui_endpoint_test.go +++ b/agent/ui_endpoint_test.go @@ -168,7 +168,7 @@ func TestSummarizeServices(t *testing.T) { Kind: structs.ServiceKindConnectProxy, Service: "web", Tags: []string{}, - Meta: map[string]string{"bar": "baz"}, + Meta: map[string]string{metaExternalSource: "k8s"}, }, }, Checks: []*structs.HealthCheck{ @@ -194,7 +194,7 @@ func TestSummarizeServices(t *testing.T) { Kind: structs.ServiceKindConnectProxy, Service: "web", Tags: []string{}, - Meta: map[string]string{"foo": "bar"}, + Meta: map[string]string{metaExternalSource: "k8s"}, }, }, Checks: []*structs.HealthCheck{ @@ -248,15 +248,16 @@ func TestSummarizeServices(t *testing.T) { } expectWeb := &ServiceSummary{ - Kind: structs.ServiceKindConnectProxy, - Name: "web", - Tags: []string{}, - Meta: map[string]string{"foo": "bar", "bar": "baz"}, - Nodes: []string{"bar", "foo"}, - ChecksPassing: 2, - ChecksWarning: 0, - ChecksCritical: 1, + Kind: structs.ServiceKindConnectProxy, + Name: "web", + Tags: []string{}, + Nodes: []string{"bar", "foo"}, + ChecksPassing: 2, + ChecksWarning: 0, + ChecksCritical: 1, + ExternalSources: []string{"k8s"}, } + summary[2].externalSourceSet = nil if !reflect.DeepEqual(summary[2], expectWeb) { t.Fatalf("bad: %v", summary[2]) }