Skip to content
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

Merged
merged 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ FEATURES:

IMPROVEMENTS:
* Control Plane
* Bump `consul-k8s-control-plane` UBI images for OpenShift to use base image `ubi-minimal:8.5`: [[GH-922](https://github.com/hashicorp/consul-k8s/pull/922)]
* Bump `consul-k8s-control-plane` UBI images for OpenShift to use base image `ubi-minimal:8.5`. [[GH-922](https://github.com/hashicorp/consul-k8s/pull/922)]
* Support the value `$POD_NAME` for the annotation `consul.hashicorp.com/service-tags` that will now be interpolated and set to the pod name. [[GH-931](https://github.com/hashicorp/consul-k8s/pull/931)]


## 0.38.0 (December 08, 2021)
Expand Down
44 changes: 29 additions & 15 deletions control-plane/connect-inject/endpoints_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,7 @@ func (r *EndpointsController) createServiceRegistrations(pod corev1.Pod, service
meta[strings.TrimPrefix(k, annotationMeta)] = v
}
}

Copy link
Member Author

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

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,
Expand All @@ -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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see why we were doing this if check rather than just assigning to the struct field.

service.Tags = tags
Tags: tags,
}

proxyServiceName := getProxyServiceName(pod, serviceEndpoints)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions control-plane/connect-inject/endpoints_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The 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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down