-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support annotation service-tags: $POD_NAME #931
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -416,15 +416,7 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service | |
meta[strings.TrimPrefix(k, annotationMeta)] = v | ||
} | ||
} | ||
|
||
var tags []string | ||
if raw, ok := pod.Annotations[annotationTags]; ok && raw != "" { | ||
tags = strings.Split(raw, ",") | ||
} | ||
// Get the tags from the deprecated tags annotation and combine. | ||
if raw, ok := pod.Annotations[annotationConnectTags]; ok && raw != "" { | ||
tags = append(tags, strings.Split(raw, ",")...) | ||
} | ||
tags := consulTags(pod) | ||
|
||
service := &api.AgentServiceRegistration{ | ||
ID: serviceID, | ||
|
@@ -433,9 +425,7 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service | |
Address: pod.Status.PodIP, | ||
Meta: meta, | ||
Namespace: r.consulNamespace(pod.Namespace), | ||
} | ||
if len(tags) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't see why we were doing this |
||
service.Tags = tags | ||
Tags: tags, | ||
} | ||
|
||
proxyServiceName := getProxyServiceName(pod, serviceEndpoints) | ||
|
@@ -496,9 +486,7 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service | |
AliasService: serviceID, | ||
}, | ||
}, | ||
} | ||
if len(tags) > 0 { | ||
proxyService.Tags = tags | ||
Tags: tags, | ||
} | ||
|
||
// A user can enable/disable tproxy for an entire namespace. | ||
|
@@ -1034,3 +1022,29 @@ func isLabeledIgnore(labels map[string]string) bool { | |
|
||
return shouldIgnore && labelExists && err == nil | ||
} | ||
|
||
// consulTags returns tags that should be added to the Consul service and proxy registrations. | ||
func consulTags(pod corev1.Pod) []string { | ||
var tags []string | ||
if raw, ok := pod.Annotations[annotationTags]; ok && raw != "" { | ||
tags = strings.Split(raw, ",") | ||
} | ||
// Get the tags from the deprecated tags annotation and combine. | ||
if raw, ok := pod.Annotations[annotationConnectTags]; ok && raw != "" { | ||
tags = append(tags, strings.Split(raw, ",")...) | ||
} | ||
|
||
var interpolatedTags []string | ||
for _, t := range tags { | ||
// Support light interpolation to preserve backwards compatibility where tags could | ||
// be environment variables. | ||
// Right now the only string we interpolate is $POD_NAME since that's all | ||
// users have asked for as of now. More can be added here in the future. | ||
if t == "$POD_NAME" { | ||
t = pod.Name | ||
} | ||
interpolatedTags = append(interpolatedTags, t) | ||
} | ||
|
||
return interpolatedTags | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -886,8 +886,8 @@ func TestReconcileCreateEndpoint(t *testing.T) { | |
pod1.Annotations[annotationService] = "different-consul-svc-name" | ||
pod1.Annotations[fmt.Sprintf("%sname", annotationMeta)] = "abc" | ||
pod1.Annotations[fmt.Sprintf("%sversion", annotationMeta)] = "2" | ||
pod1.Annotations[annotationTags] = "abc,123" | ||
pod1.Annotations[annotationConnectTags] = "def,456" | ||
pod1.Annotations[annotationTags] = "abc,123,$POD_NAME" | ||
pod1.Annotations[annotationConnectTags] = "def,456,$POD_NAME" | ||
Comment on lines
+889
to
+890
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. testing both the supported and deprecated tags just for completeness |
||
pod1.Annotations[annotationUpstreams] = "upstream1:1234" | ||
pod1.Annotations[annotationEnableMetrics] = "true" | ||
pod1.Annotations[annotationPrometheusScrapePort] = "12345" | ||
|
@@ -930,7 +930,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { | |
MetaKeyKubeNS: "default", | ||
MetaKeyManagedBy: managedByValue, | ||
}, | ||
ServiceTags: []string{"abc", "123", "def", "456"}, | ||
ServiceTags: []string{"abc", "123", "pod1", "def", "456", "pod1"}, | ||
}, | ||
}, | ||
expectedProxySvcInstances: []*api.CatalogService{ | ||
|
@@ -963,7 +963,7 @@ func TestReconcileCreateEndpoint(t *testing.T) { | |
MetaKeyKubeNS: "default", | ||
MetaKeyManagedBy: managedByValue, | ||
}, | ||
ServiceTags: []string{"abc", "123", "def", "456"}, | ||
ServiceTags: []string{"abc", "123", "pod1", "def", "456", "pod1"}, | ||
}, | ||
}, | ||
expectedAgentHealthChecks: []*api.AgentCheck{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored this into a
consulTags
function