Skip to content

Commit

Permalink
Support annotation service-tags: $POD_NAME (#931)
Browse files Browse the repository at this point in the history
Support the annotation

```
consul.hashicorp.com/service-tags: $POD_NAME
```

Where $POD_NAME will be replaced with the Pod's name.

This mimics support we had before the endpoints controller
where environment variable were interpolated for tags.

This PR only supports $POD_NAME for now because it's the only
environment variable we've been asked to support.
  • Loading branch information
lkysow authored Dec 14, 2021
1 parent ff83358 commit e3ad99e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
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
}
}

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 {
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"
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

0 comments on commit e3ad99e

Please sign in to comment.