Merge pull request #4642 from hashicorp/f-ui-meta

agent: aggregate service instance meta for UI purposes
This commit is contained in:
Mitchell Hashimoto 2018-09-07 17:36:23 -07:00 committed by GitHub
commit 49b165965d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 14 deletions

View File

@ -10,15 +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
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
@ -152,6 +159,22 @@ func summarizeServices(dump structs.NodeDump) []*ServiceSummary {
sum.Tags = service.Tags
sum.Nodes = append(sum.Nodes, node.Node)
sum.Kind = service.Kind
// 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{})
}
if _, ok := sum.externalSourceSet[source]; !ok {
sum.externalSourceSet[source] = struct{}{}
sum.ExternalSources = append(sum.ExternalSources, source)
}
}
nodeServices[idx] = sum
}
for _, check := range node.Checks {

View File

@ -168,6 +168,7 @@ func TestSummarizeServices(t *testing.T) {
Kind: structs.ServiceKindConnectProxy,
Service: "web",
Tags: []string{},
Meta: map[string]string{metaExternalSource: "k8s"},
},
},
Checks: []*structs.HealthCheck{
@ -193,6 +194,7 @@ func TestSummarizeServices(t *testing.T) {
Kind: structs.ServiceKindConnectProxy,
Service: "web",
Tags: []string{},
Meta: map[string]string{metaExternalSource: "k8s"},
},
},
Checks: []*structs.HealthCheck{
@ -246,14 +248,16 @@ func TestSummarizeServices(t *testing.T) {
}
expectWeb := &ServiceSummary{
Kind: structs.ServiceKindConnectProxy,
Name: "web",
Tags: []string{},
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])
}