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" "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 // ServiceSummary is used to summarize a service
type ServiceSummary struct { type ServiceSummary struct {
Kind structs.ServiceKind `json:",omitempty"` Kind structs.ServiceKind `json:",omitempty"`
Name string Name string
Tags []string Tags []string
Meta map[string]string Nodes []string
Nodes []string ChecksPassing int
ChecksPassing int ChecksWarning int
ChecksWarning int ChecksCritical 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 // 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.Nodes = append(sum.Nodes, node.Node)
sum.Kind = service.Kind sum.Kind = service.Kind
// The service meta is per instance, but we aggregate it // If there is an external source, add it to the list of external
// here for the UI to know it exists for _some_ instance. // sources. We only want to add unique sources so there is extra
if len(service.Meta) > 0 { // accounting here with an unexported field to maintain the set
if len(sum.Meta) == 0 { // of sources.
sum.Meta = make(map[string]string) 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 { if _, ok := sum.externalSourceSet[source]; !ok {
sum.Meta[k] = v sum.externalSourceSet[source] = struct{}{}
sum.ExternalSources = append(sum.ExternalSources, source)
} }
} }

View File

@ -168,7 +168,7 @@ func TestSummarizeServices(t *testing.T) {
Kind: structs.ServiceKindConnectProxy, Kind: structs.ServiceKindConnectProxy,
Service: "web", Service: "web",
Tags: []string{}, Tags: []string{},
Meta: map[string]string{"bar": "baz"}, Meta: map[string]string{metaExternalSource: "k8s"},
}, },
}, },
Checks: []*structs.HealthCheck{ Checks: []*structs.HealthCheck{
@ -194,7 +194,7 @@ func TestSummarizeServices(t *testing.T) {
Kind: structs.ServiceKindConnectProxy, Kind: structs.ServiceKindConnectProxy,
Service: "web", Service: "web",
Tags: []string{}, Tags: []string{},
Meta: map[string]string{"foo": "bar"}, Meta: map[string]string{metaExternalSource: "k8s"},
}, },
}, },
Checks: []*structs.HealthCheck{ Checks: []*structs.HealthCheck{
@ -248,15 +248,16 @@ func TestSummarizeServices(t *testing.T) {
} }
expectWeb := &ServiceSummary{ expectWeb := &ServiceSummary{
Kind: structs.ServiceKindConnectProxy, Kind: structs.ServiceKindConnectProxy,
Name: "web", Name: "web",
Tags: []string{}, Tags: []string{},
Meta: map[string]string{"foo": "bar", "bar": "baz"}, Nodes: []string{"bar", "foo"},
Nodes: []string{"bar", "foo"}, ChecksPassing: 2,
ChecksPassing: 2, ChecksWarning: 0,
ChecksWarning: 0, ChecksCritical: 1,
ChecksCritical: 1, ExternalSources: []string{"k8s"},
} }
summary[2].externalSourceSet = nil
if !reflect.DeepEqual(summary[2], expectWeb) { if !reflect.DeepEqual(summary[2], expectWeb) {
t.Fatalf("bad: %v", summary[2]) t.Fatalf("bad: %v", summary[2])
} }