From 80dde5465b0d3b002b15703be6e378279fac6a4c Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Wed, 7 Feb 2018 01:54:42 +0100
Subject: [PATCH] Added support for Service Metadata
---
agent/agent_endpoint.go | 5 +++++
agent/structs/service_definition.go | 2 ++
agent/structs/structs.go | 6 ++++++
api/agent.go | 13 +++++++------
api/catalog.go | 1 +
5 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/agent/agent_endpoint.go b/agent/agent_endpoint.go
index 96c312a2a8..67e1af99bd 100644
--- a/agent/agent_endpoint.go
+++ b/agent/agent_endpoint.go
@@ -576,6 +576,11 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
// Get the node service.
ns := args.NodeService()
+ if err := structs.ValidateMetadata(ns.ServiceMeta, false); err != nil {
+ resp.WriteHeader(http.StatusBadRequest)
+ fmt.Fprint(resp, fmt.Errorf("Invalid Meta: %v", err))
+ return nil, nil
+ }
// Verify the check type.
chkTypes, err := args.CheckTypes()
diff --git a/agent/structs/service_definition.go b/agent/structs/service_definition.go
index 71a7ea0df2..aa1cda535e 100644
--- a/agent/structs/service_definition.go
+++ b/agent/structs/service_definition.go
@@ -6,6 +6,7 @@ type ServiceDefinition struct {
Name string
Tags []string
Address string
+ ServiceMeta map[string]string
Port int
Check CheckType
Checks CheckTypes
@@ -19,6 +20,7 @@ func (s *ServiceDefinition) NodeService() *NodeService {
Service: s.Name,
Tags: s.Tags,
Address: s.Address,
+ ServiceMeta: s.ServiceMeta,
Port: s.Port,
EnableTagOverride: s.EnableTagOverride,
}
diff --git a/agent/structs/structs.go b/agent/structs/structs.go
index b50f912f86..c67a7255da 100644
--- a/agent/structs/structs.go
+++ b/agent/structs/structs.go
@@ -368,6 +368,7 @@ type ServiceNode struct {
ServiceName string
ServiceTags []string
ServiceAddress string
+ ServiceMeta map[string]string
ServicePort int
ServiceEnableTagOverride bool
@@ -390,6 +391,7 @@ func (s *ServiceNode) PartialClone() *ServiceNode {
ServiceTags: tags,
ServiceAddress: s.ServiceAddress,
ServicePort: s.ServicePort,
+ ServiceMeta: s.ServiceMeta,
ServiceEnableTagOverride: s.ServiceEnableTagOverride,
RaftIndex: RaftIndex{
CreateIndex: s.CreateIndex,
@@ -406,6 +408,7 @@ func (s *ServiceNode) ToNodeService() *NodeService {
Tags: s.ServiceTags,
Address: s.ServiceAddress,
Port: s.ServicePort,
+ ServiceMeta: s.ServiceMeta,
EnableTagOverride: s.ServiceEnableTagOverride,
RaftIndex: RaftIndex{
CreateIndex: s.CreateIndex,
@@ -422,6 +425,7 @@ type NodeService struct {
Service string
Tags []string
Address string
+ ServiceMeta map[string]string
Port int
EnableTagOverride bool
@@ -438,6 +442,7 @@ func (s *NodeService) IsSame(other *NodeService) bool {
!reflect.DeepEqual(s.Tags, other.Tags) ||
s.Address != other.Address ||
s.Port != other.Port ||
+ !reflect.DeepEqual(s.ServiceMeta, other.ServiceMeta) ||
s.EnableTagOverride != other.EnableTagOverride {
return false
}
@@ -457,6 +462,7 @@ func (s *NodeService) ToServiceNode(node string) *ServiceNode {
ServiceTags: s.Tags,
ServiceAddress: s.Address,
ServicePort: s.Port,
+ ServiceMeta: s.ServiceMeta,
ServiceEnableTagOverride: s.EnableTagOverride,
RaftIndex: RaftIndex{
CreateIndex: s.CreateIndex,
diff --git a/api/agent.go b/api/agent.go
index 2c826660cd..3abde8ff10 100644
--- a/api/agent.go
+++ b/api/agent.go
@@ -60,12 +60,13 @@ type MembersOpts struct {
// AgentServiceRegistration is used to register a new service
type AgentServiceRegistration struct {
- ID string `json:",omitempty"`
- Name string `json:",omitempty"`
- Tags []string `json:",omitempty"`
- Port int `json:",omitempty"`
- Address string `json:",omitempty"`
- EnableTagOverride bool `json:",omitempty"`
+ ID string `json:",omitempty"`
+ Name string `json:",omitempty"`
+ Tags []string `json:",omitempty"`
+ Port int `json:",omitempty"`
+ Address string `json:",omitempty"`
+ EnableTagOverride bool `json:",omitempty"`
+ ServiceMeta map[string]string `json:",omitempty"`
Check *AgentServiceCheck
Checks AgentServiceChecks
}
diff --git a/api/catalog.go b/api/catalog.go
index 08da6e16ea..80ce1bc815 100644
--- a/api/catalog.go
+++ b/api/catalog.go
@@ -22,6 +22,7 @@ type CatalogService struct {
ServiceName string
ServiceAddress string
ServiceTags []string
+ ServiceMeta map[string]string
ServicePort int
ServiceEnableTagOverride bool
CreateIndex uint64