mirror of https://github.com/status-im/consul.git
agent: ExternalSources instead of Meta
This commit is contained in:
parent
e9ea190df0
commit
b95348c4b1
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue