agent: ExternalSources instead of Meta

This commit is contained in:
Mitchell Hashimoto 2018-09-07 10:06:55 -07:00
parent e9ea190df0
commit b95348c4b1
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 36 additions and 25 deletions

View File

@ -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
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)
}
}

View File

@ -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{
@ -251,12 +251,13 @@ func TestSummarizeServices(t *testing.T) {
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,
ExternalSources: []string{"k8s"},
}
summary[2].externalSourceSet = nil
if !reflect.DeepEqual(summary[2], expectWeb) {
t.Fatalf("bad: %v", summary[2])
}